blob: a64cb1acd1a2be855d20b36986372b1543dc4448 [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);
952 intent.putExtra("input_method_id", id);
953 mContext.sendBroadcast(intent);
954 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700955 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800956 } finally {
957 Binder.restoreCallingIdentity(ident);
958 }
959 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800960
The Android Open Source Project4df24232009-03-05 14:34:35 -0800961 public boolean showSoftInput(IInputMethodClient client, int flags,
962 ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800963 long ident = Binder.clearCallingIdentity();
964 try {
965 synchronized (mMethodMap) {
966 if (mCurClient == null || client == null
967 || mCurClient.client.asBinder() != client.asBinder()) {
968 try {
969 // We need to check if this is the current client with
970 // focus in the window manager, to allow this call to
971 // be made before input is started in it.
972 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
973 Log.w(TAG, "Ignoring showSoftInput of: " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -0800974 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800975 }
976 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -0800977 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800978 }
979 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800980
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800981 if (DEBUG) Log.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -0800982 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800983 }
984 } finally {
985 Binder.restoreCallingIdentity(ident);
986 }
987 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800988
The Android Open Source Project4df24232009-03-05 14:34:35 -0800989 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990 mShowRequested = true;
991 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
992 mShowExplicitlyRequested = true;
993 }
994 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
995 mShowExplicitlyRequested = true;
996 mShowForced = true;
997 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800998
Dianne Hackborncc278702009-09-02 23:07:23 -0700999 if (!mSystemReady) {
1000 return false;
1001 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001002
The Android Open Source Project4df24232009-03-05 14:34:35 -08001003 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001004 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001005 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1006 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1007 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008 mInputShown = true;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001009 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001010 } else if (mHaveConnection && SystemClock.uptimeMillis()
1011 < (mLastBindTime+TIME_TO_RECONNECT)) {
1012 // The client has asked to have the input method shown, but
1013 // we have been sitting here too long with a connection to the
1014 // service and no interface received, so let's disconnect/connect
1015 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001016 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001017 SystemClock.uptimeMillis()-mLastBindTime,1);
1018 mContext.unbindService(this);
1019 mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE);
1020 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001021
The Android Open Source Project4df24232009-03-05 14:34:35 -08001022 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001023 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001024
The Android Open Source Project4df24232009-03-05 14:34:35 -08001025 public boolean hideSoftInput(IInputMethodClient client, int flags,
1026 ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027 long ident = Binder.clearCallingIdentity();
1028 try {
1029 synchronized (mMethodMap) {
1030 if (mCurClient == null || client == null
1031 || mCurClient.client.asBinder() != client.asBinder()) {
1032 try {
1033 // We need to check if this is the current client with
1034 // focus in the window manager, to allow this call to
1035 // be made before input is started in it.
1036 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
1037 Log.w(TAG, "Ignoring hideSoftInput of: " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001038 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001039 }
1040 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001041 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 }
1043 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001044
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 if (DEBUG) Log.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001046 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001047 }
1048 } finally {
1049 Binder.restoreCallingIdentity(ident);
1050 }
1051 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001052
The Android Open Source Project4df24232009-03-05 14:34:35 -08001053 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001054 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1055 && (mShowExplicitlyRequested || mShowForced)) {
1056 if (DEBUG) Log.v(TAG,
1057 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001058 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001059 }
1060 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
1061 if (DEBUG) Log.v(TAG,
1062 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001063 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001065 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001066 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001067 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1068 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1069 res = true;
1070 } else {
1071 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001072 }
1073 mInputShown = false;
1074 mShowRequested = false;
1075 mShowExplicitlyRequested = false;
1076 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001077 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001078 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001079
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001080 public void windowGainedFocus(IInputMethodClient client, IBinder windowToken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001081 boolean viewHasFocus, boolean isTextEditor, int softInputMode,
1082 boolean first, int windowFlags) {
1083 long ident = Binder.clearCallingIdentity();
1084 try {
1085 synchronized (mMethodMap) {
1086 if (DEBUG) Log.v(TAG, "windowGainedFocus: " + client.asBinder()
1087 + " viewHasFocus=" + viewHasFocus
1088 + " isTextEditor=" + isTextEditor
1089 + " softInputMode=#" + Integer.toHexString(softInputMode)
1090 + " first=" + first + " flags=#"
1091 + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 if (mCurClient == null || client == null
1094 || mCurClient.client.asBinder() != client.asBinder()) {
1095 try {
1096 // We need to check if this is the current client with
1097 // focus in the window manager, to allow this call to
1098 // be made before input is started in it.
1099 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001100 Log.w(TAG, "Client not active, ignoring focus gain of: " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001101 return;
1102 }
1103 } catch (RemoteException e) {
1104 }
1105 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001106
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001107 if (mCurFocusedWindow == windowToken) {
1108 Log.w(TAG, "Window already focused, ignoring focus gain of: " + client);
1109 return;
1110 }
1111 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001113 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1114 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
1115 if (!isTextEditor || (softInputMode &
1116 WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1117 != WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE) {
1118 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1119 // There is no focus view, and this window will
1120 // be behind any soft input window, so hide the
1121 // soft input window if it is shown.
1122 if (DEBUG) Log.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001123 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001124 }
1125 } else if (isTextEditor && (softInputMode &
1126 WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1127 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1128 && (softInputMode &
1129 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
1130 // There is a focus view, and we are navigating forward
1131 // into the window, so show the input window for the user.
1132 if (DEBUG) Log.v(TAG, "Unspecified window will show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001133 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001134 }
1135 break;
1136 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1137 // Do nothing.
1138 break;
1139 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1140 if ((softInputMode &
1141 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
1142 if (DEBUG) Log.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001143 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 }
1145 break;
1146 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
1147 if (DEBUG) Log.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001148 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001149 break;
1150 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1151 if ((softInputMode &
1152 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
1153 if (DEBUG) Log.v(TAG, "Window asks to show input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001154 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001155 }
1156 break;
1157 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
1158 if (DEBUG) Log.v(TAG, "Window asks to always show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001159 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160 break;
1161 }
1162 }
1163 } finally {
1164 Binder.restoreCallingIdentity(ident);
1165 }
1166 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001168 public void showInputMethodPickerFromClient(IInputMethodClient client) {
1169 synchronized (mMethodMap) {
1170 if (mCurClient == null || client == null
1171 || mCurClient.client.asBinder() != client.asBinder()) {
1172 Log.w(TAG, "Ignoring showInputMethodDialogFromClient of: " + client);
1173 }
1174
1175 mHandler.sendEmptyMessage(MSG_SHOW_IM_PICKER);
1176 }
1177 }
1178
1179 public void setInputMethod(IBinder token, String id) {
1180 synchronized (mMethodMap) {
1181 if (token == null) {
1182 if (mContext.checkCallingOrSelfPermission(
1183 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1184 != PackageManager.PERMISSION_GRANTED) {
1185 throw new SecurityException(
1186 "Using null token requires permission "
1187 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1188 }
1189 } else if (mCurToken != token) {
1190 Log.w(TAG, "Ignoring setInputMethod of token: " + token);
1191 return;
1192 }
1193
1194 long ident = Binder.clearCallingIdentity();
1195 try {
1196 setInputMethodLocked(id);
1197 } finally {
1198 Binder.restoreCallingIdentity(ident);
1199 }
1200 }
1201 }
1202
1203 public void hideMySoftInput(IBinder token, int flags) {
1204 synchronized (mMethodMap) {
1205 if (token == null || mCurToken != token) {
1206 Log.w(TAG, "Ignoring hideInputMethod of token: " + token);
1207 return;
1208 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209 long ident = Binder.clearCallingIdentity();
1210 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001211 hideCurrentInputLocked(flags, null);
1212 } finally {
1213 Binder.restoreCallingIdentity(ident);
1214 }
1215 }
1216 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001217
The Android Open Source Project4df24232009-03-05 14:34:35 -08001218 public void showMySoftInput(IBinder token, int flags) {
1219 synchronized (mMethodMap) {
1220 if (token == null || mCurToken != token) {
1221 Log.w(TAG, "Ignoring hideInputMethod of token: " + token);
1222 return;
1223 }
1224 long ident = Binder.clearCallingIdentity();
1225 try {
1226 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 } finally {
1228 Binder.restoreCallingIdentity(ident);
1229 }
1230 }
1231 }
1232
1233 void setEnabledSessionInMainThread(SessionState session) {
1234 if (mEnabledSession != session) {
1235 if (mEnabledSession != null) {
1236 try {
1237 if (DEBUG) Log.v(TAG, "Disabling: " + mEnabledSession);
1238 mEnabledSession.method.setSessionEnabled(
1239 mEnabledSession.session, false);
1240 } catch (RemoteException e) {
1241 }
1242 }
1243 mEnabledSession = session;
1244 try {
1245 if (DEBUG) Log.v(TAG, "Enabling: " + mEnabledSession);
1246 session.method.setSessionEnabled(
1247 session.session, true);
1248 } catch (RemoteException e) {
1249 }
1250 }
1251 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001252
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001253 public boolean handleMessage(Message msg) {
1254 HandlerCaller.SomeArgs args;
1255 switch (msg.what) {
1256 case MSG_SHOW_IM_PICKER:
1257 showInputMethodMenu();
1258 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001260 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 case MSG_UNBIND_INPUT:
1263 try {
1264 ((IInputMethod)msg.obj).unbindInput();
1265 } catch (RemoteException e) {
1266 // There is nothing interesting about the method dying.
1267 }
1268 return true;
1269 case MSG_BIND_INPUT:
1270 args = (HandlerCaller.SomeArgs)msg.obj;
1271 try {
1272 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
1273 } catch (RemoteException e) {
1274 }
1275 return true;
1276 case MSG_SHOW_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001277 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001278 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001279 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
1280 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001281 } catch (RemoteException e) {
1282 }
1283 return true;
1284 case MSG_HIDE_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001285 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001286 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001287 ((IInputMethod)args.arg1).hideSoftInput(0,
1288 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001289 } catch (RemoteException e) {
1290 }
1291 return true;
1292 case MSG_ATTACH_TOKEN:
1293 args = (HandlerCaller.SomeArgs)msg.obj;
1294 try {
1295 if (DEBUG) Log.v(TAG, "Sending attach of token: " + args.arg2);
1296 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
1297 } catch (RemoteException e) {
1298 }
1299 return true;
1300 case MSG_CREATE_SESSION:
1301 args = (HandlerCaller.SomeArgs)msg.obj;
1302 try {
1303 ((IInputMethod)args.arg1).createSession(
1304 (IInputMethodCallback)args.arg2);
1305 } catch (RemoteException e) {
1306 }
1307 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001308 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310 case MSG_START_INPUT:
1311 args = (HandlerCaller.SomeArgs)msg.obj;
1312 try {
1313 SessionState session = (SessionState)args.arg1;
1314 setEnabledSessionInMainThread(session);
1315 session.method.startInput((IInputContext)args.arg2,
1316 (EditorInfo)args.arg3);
1317 } catch (RemoteException e) {
1318 }
1319 return true;
1320 case MSG_RESTART_INPUT:
1321 args = (HandlerCaller.SomeArgs)msg.obj;
1322 try {
1323 SessionState session = (SessionState)args.arg1;
1324 setEnabledSessionInMainThread(session);
1325 session.method.restartInput((IInputContext)args.arg2,
1326 (EditorInfo)args.arg3);
1327 } catch (RemoteException e) {
1328 }
1329 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001331 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001333 case MSG_UNBIND_METHOD:
1334 try {
1335 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
1336 } catch (RemoteException e) {
1337 // There is nothing interesting about the last client dying.
1338 }
1339 return true;
1340 case MSG_BIND_METHOD:
1341 args = (HandlerCaller.SomeArgs)msg.obj;
1342 try {
1343 ((IInputMethodClient)args.arg1).onBindMethod(
1344 (InputBindResult)args.arg2);
1345 } catch (RemoteException e) {
1346 Log.w(TAG, "Client died receiving input method " + args.arg2);
1347 }
1348 return true;
1349 }
1350 return false;
1351 }
1352
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001353 private boolean isSystemIme(InputMethodInfo inputMethod) {
1354 return (inputMethod.getServiceInfo().applicationInfo.flags
1355 & ApplicationInfo.FLAG_SYSTEM) != 0;
1356 }
1357
1358 private boolean chooseNewDefaultIME() {
1359 List<InputMethodInfo> enabled = getEnabledInputMethodListLocked();
1360 if (enabled != null && enabled.size() > 0) {
1361 Settings.Secure.putString(mContext.getContentResolver(),
1362 Settings.Secure.DEFAULT_INPUT_METHOD,
1363 enabled.get(0).getId());
1364 return true;
1365 }
1366
1367 return false;
1368 }
1369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001370 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
1371 HashMap<String, InputMethodInfo> map) {
1372 list.clear();
1373 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001375 PackageManager pm = mContext.getPackageManager();
1376
1377 List<ResolveInfo> services = pm.queryIntentServices(
1378 new Intent(InputMethod.SERVICE_INTERFACE),
1379 PackageManager.GET_META_DATA);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001380
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001381 for (int i = 0; i < services.size(); ++i) {
1382 ResolveInfo ri = services.get(i);
1383 ServiceInfo si = ri.serviceInfo;
1384 ComponentName compName = new ComponentName(si.packageName, si.name);
1385 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
1386 si.permission)) {
1387 Log.w(TAG, "Skipping input method " + compName
1388 + ": it does not require the permission "
1389 + android.Manifest.permission.BIND_INPUT_METHOD);
1390 continue;
1391 }
1392
1393 if (DEBUG) Log.d(TAG, "Checking " + compName);
1394
1395 try {
1396 InputMethodInfo p = new InputMethodInfo(mContext, ri);
1397 list.add(p);
1398 map.put(p.getId(), p);
1399
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001400 // System IMEs are enabled by default
1401 if (isSystemIme(p)) {
1402 setInputMethodEnabled(p.getId(), true);
1403 }
1404
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001405 if (DEBUG) {
1406 Log.d(TAG, "Found a third-party input method " + p);
1407 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001408
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001409 } catch (XmlPullParserException e) {
1410 Log.w(TAG, "Unable to load input method " + compName, e);
1411 } catch (IOException e) {
1412 Log.w(TAG, "Unable to load input method " + compName, e);
1413 }
1414 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001415
1416 String defaultIme = Settings.Secure.getString(mContext
1417 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
1418 if (!map.containsKey(defaultIme)) {
1419 if (chooseNewDefaultIME()) {
1420 updateFromSettingsLocked();
1421 }
1422 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001423 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001425 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001426
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001427 void showInputMethodMenu() {
1428 if (DEBUG) Log.v(TAG, "Show switching menu");
1429
1430 hideInputMethodMenu();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001431
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001432 final Context context = mContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001434 final PackageManager pm = context.getPackageManager();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001435
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001436 String lastInputMethodId = Settings.Secure.getString(context
1437 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
1438 if (DEBUG) Log.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001440 final List<InputMethodInfo> immis = getEnabledInputMethodList();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001442 int N = (immis == null ? 0 : immis.size());
1443
1444 mItems = new CharSequence[N];
1445 mIms = new InputMethodInfo[N];
1446
1447 for (int i = 0; i < N; ++i) {
1448 InputMethodInfo property = immis.get(i);
1449 mItems[i] = property.loadLabel(pm);
1450 mIms[i] = property;
1451 }
1452
1453 int checkedItem = 0;
1454 for (int i = 0; i < N; ++i) {
1455 if (mIms[i].getId().equals(lastInputMethodId)) {
1456 checkedItem = i;
1457 break;
1458 }
1459 }
1460
1461 AlertDialog.OnClickListener adocl = new AlertDialog.OnClickListener() {
1462 public void onClick(DialogInterface dialog, int which) {
1463 hideInputMethodMenu();
1464 }
1465 };
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001467 TypedArray a = context.obtainStyledAttributes(null,
1468 com.android.internal.R.styleable.DialogPreference,
1469 com.android.internal.R.attr.alertDialogStyle, 0);
1470 mDialogBuilder = new AlertDialog.Builder(context)
1471 .setTitle(com.android.internal.R.string.select_input_method)
1472 .setOnCancelListener(new OnCancelListener() {
1473 public void onCancel(DialogInterface dialog) {
1474 hideInputMethodMenu();
1475 }
1476 })
1477 .setIcon(a.getDrawable(
1478 com.android.internal.R.styleable.DialogPreference_dialogTitle));
1479 a.recycle();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001480
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001481 mDialogBuilder.setSingleChoiceItems(mItems, checkedItem,
1482 new AlertDialog.OnClickListener() {
1483 public void onClick(DialogInterface dialog, int which) {
1484 synchronized (mMethodMap) {
1485 InputMethodInfo im = mIms[which];
1486 hideInputMethodMenu();
1487 setInputMethodLocked(im.getId());
1488 }
1489 }
1490 });
1491
1492 synchronized (mMethodMap) {
1493 mSwitchingDialog = mDialogBuilder.create();
1494 mSwitchingDialog.getWindow().setType(
1495 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
1496 mSwitchingDialog.show();
1497 }
1498 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001499
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001500 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07001501 synchronized (mMethodMap) {
1502 hideInputMethodMenuLocked();
1503 }
1504 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001505
The Android Open Source Project10592532009-03-18 17:39:46 -07001506 void hideInputMethodMenuLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001507 if (DEBUG) Log.v(TAG, "Hide switching menu");
1508
The Android Open Source Project10592532009-03-18 17:39:46 -07001509 if (mSwitchingDialog != null) {
1510 mSwitchingDialog.dismiss();
1511 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001512 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001513
The Android Open Source Project10592532009-03-18 17:39:46 -07001514 mDialogBuilder = null;
1515 mItems = null;
1516 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001517 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001518
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001519 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001520
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001521 public boolean setInputMethodEnabled(String id, boolean enabled) {
1522 synchronized (mMethodMap) {
1523 if (mContext.checkCallingOrSelfPermission(
1524 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1525 != PackageManager.PERMISSION_GRANTED) {
1526 throw new SecurityException(
1527 "Requires permission "
1528 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1529 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001530
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001531 long ident = Binder.clearCallingIdentity();
1532 try {
1533 // Make sure this is a valid input method.
1534 InputMethodInfo imm = mMethodMap.get(id);
1535 if (imm == null) {
1536 if (imm == null) {
1537 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1538 }
1539 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001540
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001541 StringBuilder builder = new StringBuilder(256);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001542
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001543 boolean removed = false;
1544 String firstId = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001545
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001546 // Look through the currently enabled input methods.
1547 String enabledStr = Settings.Secure.getString(mContext.getContentResolver(),
1548 Settings.Secure.ENABLED_INPUT_METHODS);
1549 if (enabledStr != null) {
1550 final TextUtils.SimpleStringSplitter splitter = mStringColonSplitter;
1551 splitter.setString(enabledStr);
1552 while (splitter.hasNext()) {
1553 String curId = splitter.next();
1554 if (curId.equals(id)) {
1555 if (enabled) {
1556 // We are enabling this input method, but it is
1557 // already enabled. Nothing to do. The previous
1558 // state was enabled.
1559 return true;
1560 }
1561 // We are disabling this input method, and it is
1562 // currently enabled. Skip it to remove from the
1563 // new list.
1564 removed = true;
1565 } else if (!enabled) {
1566 // We are building a new list of input methods that
1567 // doesn't contain the given one.
1568 if (firstId == null) firstId = curId;
1569 if (builder.length() > 0) builder.append(':');
1570 builder.append(curId);
1571 }
1572 }
1573 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001574
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001575 if (!enabled) {
1576 if (!removed) {
1577 // We are disabling the input method but it is already
1578 // disabled. Nothing to do. The previous state was
1579 // disabled.
1580 return false;
1581 }
1582 // Update the setting with the new list of input methods.
1583 Settings.Secure.putString(mContext.getContentResolver(),
1584 Settings.Secure.ENABLED_INPUT_METHODS, builder.toString());
1585 // We the disabled input method is currently selected, switch
1586 // to another one.
1587 String selId = Settings.Secure.getString(mContext.getContentResolver(),
1588 Settings.Secure.DEFAULT_INPUT_METHOD);
1589 if (id.equals(selId)) {
1590 Settings.Secure.putString(mContext.getContentResolver(),
1591 Settings.Secure.DEFAULT_INPUT_METHOD,
1592 firstId != null ? firstId : "");
1593 }
1594 // Previous state was enabled.
1595 return true;
1596 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001597
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001598 // Add in the newly enabled input method.
1599 if (enabledStr == null || enabledStr.length() == 0) {
1600 enabledStr = id;
1601 } else {
1602 enabledStr = enabledStr + ':' + id;
1603 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001604
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001605 Settings.Secure.putString(mContext.getContentResolver(),
1606 Settings.Secure.ENABLED_INPUT_METHODS, enabledStr);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001607
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001608 // Previous state was disabled.
1609 return false;
1610 } finally {
1611 Binder.restoreCallingIdentity(ident);
1612 }
1613 }
1614 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001615
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001616 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001617
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001618 @Override
1619 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1620 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1621 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001622
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001623 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
1624 + Binder.getCallingPid()
1625 + ", uid=" + Binder.getCallingUid());
1626 return;
1627 }
1628
1629 IInputMethod method;
1630 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001631
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001632 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001633
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001634 synchronized (mMethodMap) {
1635 p.println("Current Input Method Manager state:");
1636 int N = mMethodList.size();
1637 p.println(" Input Methods:");
1638 for (int i=0; i<N; i++) {
1639 InputMethodInfo info = mMethodList.get(i);
1640 p.println(" InputMethod #" + i + ":");
1641 info.dump(p, " ");
1642 }
1643 p.println(" Clients:");
1644 for (ClientState ci : mClients.values()) {
1645 p.println(" Client " + ci + ":");
1646 p.println(" client=" + ci.client);
1647 p.println(" inputContext=" + ci.inputContext);
1648 p.println(" sessionRequested=" + ci.sessionRequested);
1649 p.println(" curSession=" + ci.curSession);
1650 }
1651 p.println(" mInputMethodIcon=" + mInputMethodIcon);
1652 p.println(" mInputMethodData=" + mInputMethodData);
The Android Open Source Project10592532009-03-18 17:39:46 -07001653 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001654 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001655 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
1656 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001657 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
1658 + " mBoundToMethod=" + mBoundToMethod);
1659 p.println(" mCurToken=" + mCurToken);
1660 p.println(" mCurIntent=" + mCurIntent);
1661 method = mCurMethod;
1662 p.println(" mCurMethod=" + mCurMethod);
1663 p.println(" mEnabledSession=" + mEnabledSession);
1664 p.println(" mShowRequested=" + mShowRequested
1665 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
1666 + " mShowForced=" + mShowForced
1667 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07001668 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001669 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001670
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001671 if (client != null) {
1672 p.println(" ");
1673 pw.flush();
1674 try {
1675 client.client.asBinder().dump(fd, args);
1676 } catch (RemoteException e) {
1677 p.println("Input method client dead: " + e);
1678 }
1679 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001680
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001681 if (method != null) {
1682 p.println(" ");
1683 pw.flush();
1684 try {
1685 method.asBinder().dump(fd, args);
1686 } catch (RemoteException e) {
1687 p.println("Input method service dead: " + e);
1688 }
1689 }
1690 }
1691}