blob: dc942a293427b7cd13018bc38b1b022dce8da053 [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) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800338 String action = intent.getAction();
339 String pkgList[] = null;
340 if (Intent.ACTION_PACKAGE_ADDED.equals(action) ||
341 Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
342 Uri uri = intent.getData();
343 String pkg = uri != null ? uri.getSchemeSpecificPart() : null;
344 if (pkg != null) {
345 pkgList = new String[] { pkg };
346 }
347 } else if (Intent.ACTION_MEDIA_RESOURCES_AVAILABLE.equals(action) ||
348 Intent.ACTION_MEDIA_RESOURCES_UNAVAILABLE.equals(action)) {
349 pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
350 }
351 if (pkgList == null || pkgList.length == 0) {
352 return;
353 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 synchronized (mMethodMap) {
355 buildInputMethodListLocked(mMethodList, mMethodMap);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800356
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 InputMethodInfo curIm = null;
358 String curInputMethodId = Settings.Secure.getString(context
359 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
360 final int N = mMethodList.size();
361 if (curInputMethodId != null) {
362 for (int i=0; i<N; i++) {
363 if (mMethodList.get(i).getId().equals(curInputMethodId)) {
364 curIm = mMethodList.get(i);
365 }
366 }
367 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800368
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800370
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800371 if (curIm != null) {
372 boolean foundPkg = false;
373 for (String pkg : pkgList) {
374 if (curIm.getPackageName().equals(pkg)) {
375 foundPkg = true;
376 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377 }
378 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800379 if (foundPkg) {
380 ServiceInfo si = null;
381 try {
382 si = mContext.getPackageManager().getServiceInfo(
383 curIm.getComponent(), 0);
384 } catch (PackageManager.NameNotFoundException ex) {
385 }
386 if (si == null) {
387 // Uh oh, current input method is no longer around!
388 // Pick another one...
389 Log.i(TAG, "Current input method removed: " + curInputMethodId);
390 if (!chooseNewDefaultIME()) {
391 changed = true;
392 curIm = null;
393 curInputMethodId = "";
394 Log.i(TAG, "Unsetting current input method");
395 Settings.Secure.putString(mContext.getContentResolver(),
396 Settings.Secure.DEFAULT_INPUT_METHOD,
397 curInputMethodId);
398 }
399 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800400
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800401 } else if (curIm == null) {
402 // We currently don't have a default input method... is
403 // one now available?
404 changed = chooseNewDefaultIME();
405 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800406
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800407 if (changed) {
408 updateFromSettingsLocked();
409 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 }
411 }
412 }
413 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800414
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 class MethodCallback extends IInputMethodCallback.Stub {
416 final IInputMethod mMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800417
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418 MethodCallback(IInputMethod method) {
419 mMethod = method;
420 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800421
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 public void finishedEvent(int seq, boolean handled) throws RemoteException {
423 }
424
425 public void sessionCreated(IInputMethodSession session) throws RemoteException {
426 onSessionCreated(mMethod, session);
427 }
428 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 public InputMethodManagerService(Context context, StatusBarService statusBar) {
431 mContext = context;
432 mHandler = new Handler(this);
433 mIWindowManager = IWindowManager.Stub.asInterface(
434 ServiceManager.getService(Context.WINDOW_SERVICE));
435 mCaller = new HandlerCaller(context, new HandlerCaller.Callback() {
436 public void executeMessage(Message msg) {
437 handleMessage(msg);
438 }
439 });
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800440
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800441 PackageReceiver mBroadcastReceiver = new PackageReceiver();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 IntentFilter packageFilt = new IntentFilter();
443 packageFilt.addAction(Intent.ACTION_PACKAGE_ADDED);
444 packageFilt.addAction(Intent.ACTION_PACKAGE_CHANGED);
445 packageFilt.addAction(Intent.ACTION_PACKAGE_REMOVED);
446 packageFilt.addAction(Intent.ACTION_PACKAGE_RESTARTED);
447 packageFilt.addDataScheme("package");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800448 mContext.registerReceiver(mBroadcastReceiver, packageFilt);
449 // Register for events related to sdcard installation.
450 IntentFilter sdFilter = new IntentFilter();
451 sdFilter.addAction(Intent.ACTION_MEDIA_RESOURCES_AVAILABLE);
452 sdFilter.addAction(Intent.ACTION_MEDIA_RESOURCES_UNAVAILABLE);
453 mContext.registerReceiver(mBroadcastReceiver, sdFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 IntentFilter screenOnOffFilt = new IntentFilter();
456 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_ON);
457 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_OFF);
The Android Open Source Project10592532009-03-18 17:39:46 -0700458 screenOnOffFilt.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 mContext.registerReceiver(new ScreenOnOffReceiver(), screenOnOffFilt);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800460
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 buildInputMethodListLocked(mMethodList, mMethodMap);
462
463 final String enabledStr = Settings.Secure.getString(
464 mContext.getContentResolver(),
465 Settings.Secure.ENABLED_INPUT_METHODS);
466 Log.i(TAG, "Enabled input methods: " + enabledStr);
467 if (enabledStr == null) {
468 Log.i(TAG, "Enabled input methods has not been set, enabling all");
469 InputMethodInfo defIm = null;
470 StringBuilder sb = new StringBuilder(256);
471 final int N = mMethodList.size();
472 for (int i=0; i<N; i++) {
473 InputMethodInfo imi = mMethodList.get(i);
474 Log.i(TAG, "Adding: " + imi.getId());
475 if (i > 0) sb.append(':');
476 sb.append(imi.getId());
477 if (defIm == null && imi.getIsDefaultResourceId() != 0) {
478 try {
479 Resources res = mContext.createPackageContext(
480 imi.getPackageName(), 0).getResources();
481 if (res.getBoolean(imi.getIsDefaultResourceId())) {
482 defIm = imi;
483 Log.i(TAG, "Selected default: " + imi.getId());
484 }
485 } catch (PackageManager.NameNotFoundException ex) {
486 } catch (Resources.NotFoundException ex) {
487 }
488 }
489 }
490 if (defIm == null && N > 0) {
491 defIm = mMethodList.get(0);
492 Log.i(TAG, "No default found, using " + defIm.getId());
493 }
494 Settings.Secure.putString(mContext.getContentResolver(),
495 Settings.Secure.ENABLED_INPUT_METHODS, sb.toString());
496 if (defIm != null) {
497 Settings.Secure.putString(mContext.getContentResolver(),
498 Settings.Secure.DEFAULT_INPUT_METHOD, defIm.getId());
499 }
500 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800501
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502 mStatusBar = statusBar;
503 mInputMethodData = IconData.makeIcon("ime", null, 0, 0, 0);
504 mInputMethodIcon = statusBar.addIcon(mInputMethodData, null);
505 statusBar.setIconVisibility(mInputMethodIcon, false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800506
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507 mSettingsObserver = new SettingsObserver(mHandler);
508 updateFromSettingsLocked();
509 }
510
511 @Override
512 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
513 throws RemoteException {
514 try {
515 return super.onTransact(code, data, reply, flags);
516 } catch (RuntimeException e) {
517 // The input method manager only throws security exceptions, so let's
518 // log all others.
519 if (!(e instanceof SecurityException)) {
520 Log.e(TAG, "Input Method Manager Crash", e);
521 }
522 throw e;
523 }
524 }
525
526 public void systemReady() {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700527 synchronized (mMethodMap) {
528 if (!mSystemReady) {
529 mSystemReady = true;
Dianne Hackborncc278702009-09-02 23:07:23 -0700530 try {
531 startInputInnerLocked();
532 } catch (RuntimeException e) {
533 Log.w(TAG, "Unexpected exception", e);
534 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700535 }
536 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800537 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800538
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 public List<InputMethodInfo> getInputMethodList() {
540 synchronized (mMethodMap) {
541 return new ArrayList<InputMethodInfo>(mMethodList);
542 }
543 }
544
545 public List<InputMethodInfo> getEnabledInputMethodList() {
546 synchronized (mMethodMap) {
547 return getEnabledInputMethodListLocked();
548 }
549 }
550
551 List<InputMethodInfo> getEnabledInputMethodListLocked() {
552 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800553
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 final String enabledStr = Settings.Secure.getString(
555 mContext.getContentResolver(),
556 Settings.Secure.ENABLED_INPUT_METHODS);
557 if (enabledStr != null) {
558 final TextUtils.SimpleStringSplitter splitter = mStringColonSplitter;
559 splitter.setString(enabledStr);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800560
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561 while (splitter.hasNext()) {
562 InputMethodInfo info = mMethodMap.get(splitter.next());
563 if (info != null) {
564 res.add(info);
565 }
566 }
567 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800568
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 return res;
570 }
571
572 public void addClient(IInputMethodClient client,
573 IInputContext inputContext, int uid, int pid) {
574 synchronized (mMethodMap) {
575 mClients.put(client.asBinder(), new ClientState(client,
576 inputContext, uid, pid));
577 }
578 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800579
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800580 public void removeClient(IInputMethodClient client) {
581 synchronized (mMethodMap) {
582 mClients.remove(client.asBinder());
583 }
584 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800585
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586 void executeOrSendMessage(IInterface target, Message msg) {
587 if (target.asBinder() instanceof Binder) {
588 mCaller.sendMessage(msg);
589 } else {
590 handleMessage(msg);
591 msg.recycle();
592 }
593 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800594
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700595 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596 if (mCurClient != null) {
597 if (DEBUG) Log.v(TAG, "unbindCurrentInputLocked: client = "
598 + mCurClient.client.asBinder());
599 if (mBoundToMethod) {
600 mBoundToMethod = false;
601 if (mCurMethod != null) {
602 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
603 MSG_UNBIND_INPUT, mCurMethod));
604 }
605 }
606 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
607 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
608 mCurClient.sessionRequested = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800609
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 // Call setActive(false) on the old client
611 try {
612 mCurClient.client.setActive(false);
613 } catch (RemoteException e) {
614 Log.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
615 + mCurClient.pid + " uid " + mCurClient.uid);
616 }
617 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800618
The Android Open Source Project10592532009-03-18 17:39:46 -0700619 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 }
621 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800622
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 private int getImeShowFlags() {
624 int flags = 0;
625 if (mShowForced) {
626 flags |= InputMethod.SHOW_FORCED
627 | InputMethod.SHOW_EXPLICIT;
628 } else if (mShowExplicitlyRequested) {
629 flags |= InputMethod.SHOW_EXPLICIT;
630 }
631 return flags;
632 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800633
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634 private int getAppShowFlags() {
635 int flags = 0;
636 if (mShowForced) {
637 flags |= InputMethodManager.SHOW_FORCED;
638 } else if (!mShowExplicitlyRequested) {
639 flags |= InputMethodManager.SHOW_IMPLICIT;
640 }
641 return flags;
642 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800643
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644 InputBindResult attachNewInputLocked(boolean initial, boolean needResult) {
645 if (!mBoundToMethod) {
646 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
647 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
648 mBoundToMethod = true;
649 }
650 final SessionState session = mCurClient.curSession;
651 if (initial) {
652 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
653 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
654 } else {
655 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
656 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
657 }
658 if (mShowRequested) {
659 if (DEBUG) Log.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -0800660 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800661 }
662 return needResult
663 ? new InputBindResult(session.session, mCurId, mCurSeq)
664 : null;
665 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800666
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 InputBindResult startInputLocked(IInputMethodClient client,
668 IInputContext inputContext, EditorInfo attribute,
669 boolean initial, boolean needResult) {
670 // If no method is currently selected, do nothing.
671 if (mCurMethodId == null) {
672 return mNoBinding;
673 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800674
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 ClientState cs = mClients.get(client.asBinder());
676 if (cs == null) {
677 throw new IllegalArgumentException("unknown client "
678 + client.asBinder());
679 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800680
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 try {
682 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
683 // Check with the window manager to make sure this client actually
684 // has a window with focus. If not, reject. This is thread safe
685 // because if the focus changes some time before or after, the
686 // next client receiving focus that has any interest in input will
687 // be calling through here after that change happens.
688 Log.w(TAG, "Starting input on non-focused client " + cs.client
689 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
690 return null;
691 }
692 } catch (RemoteException e) {
693 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800694
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695 if (mCurClient != cs) {
696 // If the client is changing, we need to switch over to the new
697 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700698 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800699 if (DEBUG) Log.v(TAG, "switching to client: client = "
700 + cs.client.asBinder());
701
702 // If the screen is on, inform the new client it is active
703 if (mScreenOn) {
704 try {
705 cs.client.setActive(mScreenOn);
706 } catch (RemoteException e) {
707 Log.w(TAG, "Got RemoteException sending setActive notification to pid "
708 + cs.pid + " uid " + cs.uid);
709 }
710 }
711 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800712
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 // Bump up the sequence for this client and attach it.
714 mCurSeq++;
715 if (mCurSeq <= 0) mCurSeq = 1;
716 mCurClient = cs;
717 mCurInputContext = inputContext;
718 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800719
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800720 // Check if the input method is changing.
721 if (mCurId != null && mCurId.equals(mCurMethodId)) {
722 if (cs.curSession != null) {
723 // Fast case: if we are already connected to the input method,
724 // then just return it.
725 return attachNewInputLocked(initial, needResult);
726 }
727 if (mHaveConnection) {
728 if (mCurMethod != null) {
729 if (!cs.sessionRequested) {
730 cs.sessionRequested = true;
731 if (DEBUG) Log.v(TAG, "Creating new session for client " + cs);
732 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
733 MSG_CREATE_SESSION, mCurMethod,
734 new MethodCallback(mCurMethod)));
735 }
736 // Return to client, and we will get back with it when
737 // we have had a session made for it.
738 return new InputBindResult(null, mCurId, mCurSeq);
739 } else if (SystemClock.uptimeMillis()
740 < (mLastBindTime+TIME_TO_RECONNECT)) {
741 // In this case we have connected to the service, but
742 // don't yet have its interface. If it hasn't been too
743 // long since we did the connection, we'll return to
744 // the client and wait to get the service interface so
745 // we can report back. If it has been too long, we want
746 // to fall through so we can try a disconnect/reconnect
747 // to see if we can get back in touch with the service.
748 return new InputBindResult(null, mCurId, mCurSeq);
749 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800750 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
751 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800752 }
753 }
754 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800755
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700756 return startInputInnerLocked();
757 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800758
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700759 InputBindResult startInputInnerLocked() {
760 if (mCurMethodId == null) {
761 return mNoBinding;
762 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800763
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700764 if (!mSystemReady) {
765 // If the system is not yet ready, we shouldn't be running third
766 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -0700767 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700768 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800769
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770 InputMethodInfo info = mMethodMap.get(mCurMethodId);
771 if (info == null) {
772 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
773 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800774
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700775 unbindCurrentMethodLocked(false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800776
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800777 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
778 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -0700779 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
780 com.android.internal.R.string.input_method_binding_label);
781 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
782 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800783 if (mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE)) {
784 mLastBindTime = SystemClock.uptimeMillis();
785 mHaveConnection = true;
786 mCurId = info.getId();
787 mCurToken = new Binder();
788 try {
789 if (DEBUG) Log.v(TAG, "Adding window token: " + mCurToken);
790 mIWindowManager.addWindowToken(mCurToken,
791 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
792 } catch (RemoteException e) {
793 }
794 return new InputBindResult(null, mCurId, mCurSeq);
795 } else {
796 mCurIntent = null;
797 Log.w(TAG, "Failure connecting to input method service: "
798 + mCurIntent);
799 }
800 return null;
801 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800802
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800803 public InputBindResult startInput(IInputMethodClient client,
804 IInputContext inputContext, EditorInfo attribute,
805 boolean initial, boolean needResult) {
806 synchronized (mMethodMap) {
807 final long ident = Binder.clearCallingIdentity();
808 try {
809 return startInputLocked(client, inputContext, attribute,
810 initial, needResult);
811 } finally {
812 Binder.restoreCallingIdentity(ident);
813 }
814 }
815 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800816
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800817 public void finishInput(IInputMethodClient client) {
818 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800819
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800820 public void onServiceConnected(ComponentName name, IBinder service) {
821 synchronized (mMethodMap) {
822 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
823 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -0700824 if (mCurToken == null) {
825 Log.w(TAG, "Service connected without a token!");
826 unbindCurrentMethodLocked(false);
827 return;
828 }
829 if (DEBUG) Log.v(TAG, "Initiating attach with token: " + mCurToken);
830 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
831 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800832 if (mCurClient != null) {
Dianne Hackborncc278702009-09-02 23:07:23 -0700833 if (DEBUG) Log.v(TAG, "Creating first session while with client "
834 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800835 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -0700836 MSG_CREATE_SESSION, mCurMethod,
837 new MethodCallback(mCurMethod)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800838 }
839 }
840 }
841 }
842
843 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
844 synchronized (mMethodMap) {
845 if (mCurMethod != null && method != null
846 && mCurMethod.asBinder() == method.asBinder()) {
847 if (mCurClient != null) {
848 mCurClient.curSession = new SessionState(mCurClient,
849 method, session);
850 mCurClient.sessionRequested = false;
851 InputBindResult res = attachNewInputLocked(true, true);
852 if (res.method != null) {
853 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
854 MSG_BIND_METHOD, mCurClient.client, res));
855 }
856 }
857 }
858 }
859 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800860
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700861 void unbindCurrentMethodLocked(boolean reportToClient) {
862 if (mHaveConnection) {
863 mContext.unbindService(this);
864 mHaveConnection = false;
865 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800866
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700867 if (mCurToken != null) {
868 try {
869 if (DEBUG) Log.v(TAG, "Removing window token: " + mCurToken);
870 mIWindowManager.removeWindowToken(mCurToken);
871 } catch (RemoteException e) {
872 }
873 mCurToken = null;
874 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800875
The Android Open Source Project10592532009-03-18 17:39:46 -0700876 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700877 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800878
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700879 if (reportToClient && mCurClient != null) {
880 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
881 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
882 }
883 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800884
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700885 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800886 if (mCurMethod != null) {
887 for (ClientState cs : mClients.values()) {
888 cs.sessionRequested = false;
889 cs.curSession = null;
890 }
891 mCurMethod = null;
892 }
893 mStatusBar.setIconVisibility(mInputMethodIcon, false);
894 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800895
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800896 public void onServiceDisconnected(ComponentName name) {
897 synchronized (mMethodMap) {
898 if (DEBUG) Log.v(TAG, "Service disconnected: " + name
899 + " mCurIntent=" + mCurIntent);
900 if (mCurMethod != null && mCurIntent != null
901 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700902 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800903 // We consider this to be a new bind attempt, since the system
904 // should now try to restart the service for us.
905 mLastBindTime = SystemClock.uptimeMillis();
906 mShowRequested = mInputShown;
907 mInputShown = false;
908 if (mCurClient != null) {
909 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
910 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
911 }
912 }
913 }
914 }
915
916 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
917 long ident = Binder.clearCallingIdentity();
918 try {
919 if (token == null || mCurToken != token) {
920 Log.w(TAG, "Ignoring setInputMethod of token: " + token);
921 return;
922 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800923
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800924 synchronized (mMethodMap) {
925 if (iconId == 0) {
926 if (DEBUG) Log.d(TAG, "hide the small icon for the input method");
927 mStatusBar.setIconVisibility(mInputMethodIcon, false);
928 } else if (packageName != null) {
929 if (DEBUG) Log.d(TAG, "show a small icon for the input method");
930 mInputMethodData.iconId = iconId;
931 mInputMethodData.iconPackage = packageName;
932 mStatusBar.updateIcon(mInputMethodIcon, mInputMethodData, null);
933 mStatusBar.setIconVisibility(mInputMethodIcon, true);
934 }
935 }
936 } finally {
937 Binder.restoreCallingIdentity(ident);
938 }
939 }
940
941 void updateFromSettingsLocked() {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700942 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
943 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
944 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
945 // enabled.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800946 String id = Settings.Secure.getString(mContext.getContentResolver(),
947 Settings.Secure.DEFAULT_INPUT_METHOD);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700948 if (id != null && id.length() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800949 try {
950 setInputMethodLocked(id);
951 } catch (IllegalArgumentException e) {
952 Log.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -0700953 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700954 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800955 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700956 } else {
957 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -0700958 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700959 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800960 }
961 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800962
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800963 void setInputMethodLocked(String id) {
964 InputMethodInfo info = mMethodMap.get(id);
965 if (info == null) {
966 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
967 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800968
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800969 if (id.equals(mCurMethodId)) {
970 return;
971 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800972
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800973 final long ident = Binder.clearCallingIdentity();
974 try {
975 mCurMethodId = id;
976 Settings.Secure.putString(mContext.getContentResolver(),
977 Settings.Secure.DEFAULT_INPUT_METHOD, id);
978
979 if (ActivityManagerNative.isSystemReady()) {
980 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -0800981 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800982 intent.putExtra("input_method_id", id);
983 mContext.sendBroadcast(intent);
984 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700985 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800986 } finally {
987 Binder.restoreCallingIdentity(ident);
988 }
989 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800990
The Android Open Source Project4df24232009-03-05 14:34:35 -0800991 public boolean showSoftInput(IInputMethodClient client, int flags,
992 ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800993 long ident = Binder.clearCallingIdentity();
994 try {
995 synchronized (mMethodMap) {
996 if (mCurClient == null || client == null
997 || mCurClient.client.asBinder() != client.asBinder()) {
998 try {
999 // We need to check if this is the current client with
1000 // focus in the window manager, to allow this call to
1001 // be made before input is started in it.
1002 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
1003 Log.w(TAG, "Ignoring showSoftInput of: " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001004 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001005 }
1006 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001007 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008 }
1009 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001010
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011 if (DEBUG) Log.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001012 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001013 }
1014 } finally {
1015 Binder.restoreCallingIdentity(ident);
1016 }
1017 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001018
The Android Open Source Project4df24232009-03-05 14:34:35 -08001019 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001020 mShowRequested = true;
1021 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1022 mShowExplicitlyRequested = true;
1023 }
1024 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1025 mShowExplicitlyRequested = true;
1026 mShowForced = true;
1027 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001028
Dianne Hackborncc278702009-09-02 23:07:23 -07001029 if (!mSystemReady) {
1030 return false;
1031 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001032
The Android Open Source Project4df24232009-03-05 14:34:35 -08001033 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001034 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001035 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1036 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1037 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001038 mInputShown = true;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001039 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040 } else if (mHaveConnection && SystemClock.uptimeMillis()
1041 < (mLastBindTime+TIME_TO_RECONNECT)) {
1042 // The client has asked to have the input method shown, but
1043 // we have been sitting here too long with a connection to the
1044 // service and no interface received, so let's disconnect/connect
1045 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001046 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001047 SystemClock.uptimeMillis()-mLastBindTime,1);
1048 mContext.unbindService(this);
1049 mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE);
1050 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001051
The Android Open Source Project4df24232009-03-05 14:34:35 -08001052 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001053 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001054
The Android Open Source Project4df24232009-03-05 14:34:35 -08001055 public boolean hideSoftInput(IInputMethodClient client, int flags,
1056 ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001057 long ident = Binder.clearCallingIdentity();
1058 try {
1059 synchronized (mMethodMap) {
1060 if (mCurClient == null || client == null
1061 || mCurClient.client.asBinder() != client.asBinder()) {
1062 try {
1063 // We need to check if this is the current client with
1064 // focus in the window manager, to allow this call to
1065 // be made before input is started in it.
1066 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
1067 Log.w(TAG, "Ignoring hideSoftInput of: " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001068 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069 }
1070 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001071 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001072 }
1073 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001074
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001075 if (DEBUG) Log.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001076 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001077 }
1078 } finally {
1079 Binder.restoreCallingIdentity(ident);
1080 }
1081 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001082
The Android Open Source Project4df24232009-03-05 14:34:35 -08001083 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001084 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1085 && (mShowExplicitlyRequested || mShowForced)) {
1086 if (DEBUG) Log.v(TAG,
1087 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001088 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001089 }
1090 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
1091 if (DEBUG) Log.v(TAG,
1092 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001093 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001095 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001096 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001097 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1098 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1099 res = true;
1100 } else {
1101 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001102 }
1103 mInputShown = false;
1104 mShowRequested = false;
1105 mShowExplicitlyRequested = false;
1106 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001107 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001109
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001110 public void windowGainedFocus(IInputMethodClient client, IBinder windowToken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 boolean viewHasFocus, boolean isTextEditor, int softInputMode,
1112 boolean first, int windowFlags) {
1113 long ident = Binder.clearCallingIdentity();
1114 try {
1115 synchronized (mMethodMap) {
1116 if (DEBUG) Log.v(TAG, "windowGainedFocus: " + client.asBinder()
1117 + " viewHasFocus=" + viewHasFocus
1118 + " isTextEditor=" + isTextEditor
1119 + " softInputMode=#" + Integer.toHexString(softInputMode)
1120 + " first=" + first + " flags=#"
1121 + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001123 if (mCurClient == null || client == null
1124 || mCurClient.client.asBinder() != client.asBinder()) {
1125 try {
1126 // We need to check if this is the current client with
1127 // focus in the window manager, to allow this call to
1128 // be made before input is started in it.
1129 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001130 Log.w(TAG, "Client not active, ignoring focus gain of: " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001131 return;
1132 }
1133 } catch (RemoteException e) {
1134 }
1135 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001136
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001137 if (mCurFocusedWindow == windowToken) {
1138 Log.w(TAG, "Window already focused, ignoring focus gain of: " + client);
1139 return;
1140 }
1141 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1144 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
1145 if (!isTextEditor || (softInputMode &
1146 WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1147 != WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE) {
1148 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1149 // There is no focus view, and this window will
1150 // be behind any soft input window, so hide the
1151 // soft input window if it is shown.
1152 if (DEBUG) Log.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001153 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001154 }
1155 } else if (isTextEditor && (softInputMode &
1156 WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1157 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1158 && (softInputMode &
1159 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
1160 // There is a focus view, and we are navigating forward
1161 // into the window, so show the input window for the user.
1162 if (DEBUG) Log.v(TAG, "Unspecified window will show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001163 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001164 }
1165 break;
1166 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1167 // Do nothing.
1168 break;
1169 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1170 if ((softInputMode &
1171 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
1172 if (DEBUG) Log.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001173 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001174 }
1175 break;
1176 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
1177 if (DEBUG) Log.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001178 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001179 break;
1180 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1181 if ((softInputMode &
1182 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
1183 if (DEBUG) Log.v(TAG, "Window asks to show input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001184 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185 }
1186 break;
1187 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
1188 if (DEBUG) Log.v(TAG, "Window asks to always show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001189 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001190 break;
1191 }
1192 }
1193 } finally {
1194 Binder.restoreCallingIdentity(ident);
1195 }
1196 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 public void showInputMethodPickerFromClient(IInputMethodClient client) {
1199 synchronized (mMethodMap) {
1200 if (mCurClient == null || client == null
1201 || mCurClient.client.asBinder() != client.asBinder()) {
1202 Log.w(TAG, "Ignoring showInputMethodDialogFromClient of: " + client);
1203 }
1204
1205 mHandler.sendEmptyMessage(MSG_SHOW_IM_PICKER);
1206 }
1207 }
1208
1209 public void setInputMethod(IBinder token, String id) {
1210 synchronized (mMethodMap) {
1211 if (token == null) {
1212 if (mContext.checkCallingOrSelfPermission(
1213 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1214 != PackageManager.PERMISSION_GRANTED) {
1215 throw new SecurityException(
1216 "Using null token requires permission "
1217 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1218 }
1219 } else if (mCurToken != token) {
1220 Log.w(TAG, "Ignoring setInputMethod of token: " + token);
1221 return;
1222 }
1223
1224 long ident = Binder.clearCallingIdentity();
1225 try {
1226 setInputMethodLocked(id);
1227 } finally {
1228 Binder.restoreCallingIdentity(ident);
1229 }
1230 }
1231 }
1232
1233 public void hideMySoftInput(IBinder token, int flags) {
1234 synchronized (mMethodMap) {
1235 if (token == null || mCurToken != token) {
1236 Log.w(TAG, "Ignoring hideInputMethod of token: " + token);
1237 return;
1238 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001239 long ident = Binder.clearCallingIdentity();
1240 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001241 hideCurrentInputLocked(flags, null);
1242 } finally {
1243 Binder.restoreCallingIdentity(ident);
1244 }
1245 }
1246 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001247
The Android Open Source Project4df24232009-03-05 14:34:35 -08001248 public void showMySoftInput(IBinder token, int flags) {
1249 synchronized (mMethodMap) {
1250 if (token == null || mCurToken != token) {
1251 Log.w(TAG, "Ignoring hideInputMethod of token: " + token);
1252 return;
1253 }
1254 long ident = Binder.clearCallingIdentity();
1255 try {
1256 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001257 } finally {
1258 Binder.restoreCallingIdentity(ident);
1259 }
1260 }
1261 }
1262
1263 void setEnabledSessionInMainThread(SessionState session) {
1264 if (mEnabledSession != session) {
1265 if (mEnabledSession != null) {
1266 try {
1267 if (DEBUG) Log.v(TAG, "Disabling: " + mEnabledSession);
1268 mEnabledSession.method.setSessionEnabled(
1269 mEnabledSession.session, false);
1270 } catch (RemoteException e) {
1271 }
1272 }
1273 mEnabledSession = session;
1274 try {
1275 if (DEBUG) Log.v(TAG, "Enabling: " + mEnabledSession);
1276 session.method.setSessionEnabled(
1277 session.session, true);
1278 } catch (RemoteException e) {
1279 }
1280 }
1281 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001283 public boolean handleMessage(Message msg) {
1284 HandlerCaller.SomeArgs args;
1285 switch (msg.what) {
1286 case MSG_SHOW_IM_PICKER:
1287 showInputMethodMenu();
1288 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001290 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 case MSG_UNBIND_INPUT:
1293 try {
1294 ((IInputMethod)msg.obj).unbindInput();
1295 } catch (RemoteException e) {
1296 // There is nothing interesting about the method dying.
1297 }
1298 return true;
1299 case MSG_BIND_INPUT:
1300 args = (HandlerCaller.SomeArgs)msg.obj;
1301 try {
1302 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
1303 } catch (RemoteException e) {
1304 }
1305 return true;
1306 case MSG_SHOW_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001307 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001308 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001309 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
1310 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001311 } catch (RemoteException e) {
1312 }
1313 return true;
1314 case MSG_HIDE_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001315 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001316 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001317 ((IInputMethod)args.arg1).hideSoftInput(0,
1318 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001319 } catch (RemoteException e) {
1320 }
1321 return true;
1322 case MSG_ATTACH_TOKEN:
1323 args = (HandlerCaller.SomeArgs)msg.obj;
1324 try {
1325 if (DEBUG) Log.v(TAG, "Sending attach of token: " + args.arg2);
1326 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
1327 } catch (RemoteException e) {
1328 }
1329 return true;
1330 case MSG_CREATE_SESSION:
1331 args = (HandlerCaller.SomeArgs)msg.obj;
1332 try {
1333 ((IInputMethod)args.arg1).createSession(
1334 (IInputMethodCallback)args.arg2);
1335 } catch (RemoteException e) {
1336 }
1337 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001338 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001340 case MSG_START_INPUT:
1341 args = (HandlerCaller.SomeArgs)msg.obj;
1342 try {
1343 SessionState session = (SessionState)args.arg1;
1344 setEnabledSessionInMainThread(session);
1345 session.method.startInput((IInputContext)args.arg2,
1346 (EditorInfo)args.arg3);
1347 } catch (RemoteException e) {
1348 }
1349 return true;
1350 case MSG_RESTART_INPUT:
1351 args = (HandlerCaller.SomeArgs)msg.obj;
1352 try {
1353 SessionState session = (SessionState)args.arg1;
1354 setEnabledSessionInMainThread(session);
1355 session.method.restartInput((IInputContext)args.arg2,
1356 (EditorInfo)args.arg3);
1357 } catch (RemoteException e) {
1358 }
1359 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001361 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001363 case MSG_UNBIND_METHOD:
1364 try {
1365 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
1366 } catch (RemoteException e) {
1367 // There is nothing interesting about the last client dying.
1368 }
1369 return true;
1370 case MSG_BIND_METHOD:
1371 args = (HandlerCaller.SomeArgs)msg.obj;
1372 try {
1373 ((IInputMethodClient)args.arg1).onBindMethod(
1374 (InputBindResult)args.arg2);
1375 } catch (RemoteException e) {
1376 Log.w(TAG, "Client died receiving input method " + args.arg2);
1377 }
1378 return true;
1379 }
1380 return false;
1381 }
1382
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001383 private boolean isSystemIme(InputMethodInfo inputMethod) {
1384 return (inputMethod.getServiceInfo().applicationInfo.flags
1385 & ApplicationInfo.FLAG_SYSTEM) != 0;
1386 }
1387
1388 private boolean chooseNewDefaultIME() {
1389 List<InputMethodInfo> enabled = getEnabledInputMethodListLocked();
1390 if (enabled != null && enabled.size() > 0) {
1391 Settings.Secure.putString(mContext.getContentResolver(),
1392 Settings.Secure.DEFAULT_INPUT_METHOD,
1393 enabled.get(0).getId());
1394 return true;
1395 }
1396
1397 return false;
1398 }
1399
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001400 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
1401 HashMap<String, InputMethodInfo> map) {
1402 list.clear();
1403 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001404
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001405 PackageManager pm = mContext.getPackageManager();
1406
1407 List<ResolveInfo> services = pm.queryIntentServices(
1408 new Intent(InputMethod.SERVICE_INTERFACE),
1409 PackageManager.GET_META_DATA);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001411 for (int i = 0; i < services.size(); ++i) {
1412 ResolveInfo ri = services.get(i);
1413 ServiceInfo si = ri.serviceInfo;
1414 ComponentName compName = new ComponentName(si.packageName, si.name);
1415 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
1416 si.permission)) {
1417 Log.w(TAG, "Skipping input method " + compName
1418 + ": it does not require the permission "
1419 + android.Manifest.permission.BIND_INPUT_METHOD);
1420 continue;
1421 }
1422
1423 if (DEBUG) Log.d(TAG, "Checking " + compName);
1424
1425 try {
1426 InputMethodInfo p = new InputMethodInfo(mContext, ri);
1427 list.add(p);
1428 map.put(p.getId(), p);
1429
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001430 // System IMEs are enabled by default
1431 if (isSystemIme(p)) {
1432 setInputMethodEnabled(p.getId(), true);
1433 }
1434
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001435 if (DEBUG) {
1436 Log.d(TAG, "Found a third-party input method " + p);
1437 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001439 } catch (XmlPullParserException e) {
1440 Log.w(TAG, "Unable to load input method " + compName, e);
1441 } catch (IOException e) {
1442 Log.w(TAG, "Unable to load input method " + compName, e);
1443 }
1444 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001445
1446 String defaultIme = Settings.Secure.getString(mContext
1447 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
1448 if (!map.containsKey(defaultIme)) {
1449 if (chooseNewDefaultIME()) {
1450 updateFromSettingsLocked();
1451 }
1452 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001453 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001455 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001456
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001457 void showInputMethodMenu() {
1458 if (DEBUG) Log.v(TAG, "Show switching menu");
1459
1460 hideInputMethodMenu();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001461
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001462 final Context context = mContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001463
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001464 final PackageManager pm = context.getPackageManager();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001466 String lastInputMethodId = Settings.Secure.getString(context
1467 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
1468 if (DEBUG) Log.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001470 final List<InputMethodInfo> immis = getEnabledInputMethodList();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001472 int N = (immis == null ? 0 : immis.size());
1473
1474 mItems = new CharSequence[N];
1475 mIms = new InputMethodInfo[N];
1476
1477 for (int i = 0; i < N; ++i) {
1478 InputMethodInfo property = immis.get(i);
1479 mItems[i] = property.loadLabel(pm);
1480 mIms[i] = property;
1481 }
1482
1483 int checkedItem = 0;
1484 for (int i = 0; i < N; ++i) {
1485 if (mIms[i].getId().equals(lastInputMethodId)) {
1486 checkedItem = i;
1487 break;
1488 }
1489 }
1490
1491 AlertDialog.OnClickListener adocl = new AlertDialog.OnClickListener() {
1492 public void onClick(DialogInterface dialog, int which) {
1493 hideInputMethodMenu();
1494 }
1495 };
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001496
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001497 TypedArray a = context.obtainStyledAttributes(null,
1498 com.android.internal.R.styleable.DialogPreference,
1499 com.android.internal.R.attr.alertDialogStyle, 0);
1500 mDialogBuilder = new AlertDialog.Builder(context)
1501 .setTitle(com.android.internal.R.string.select_input_method)
1502 .setOnCancelListener(new OnCancelListener() {
1503 public void onCancel(DialogInterface dialog) {
1504 hideInputMethodMenu();
1505 }
1506 })
1507 .setIcon(a.getDrawable(
1508 com.android.internal.R.styleable.DialogPreference_dialogTitle));
1509 a.recycle();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001510
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001511 mDialogBuilder.setSingleChoiceItems(mItems, checkedItem,
1512 new AlertDialog.OnClickListener() {
1513 public void onClick(DialogInterface dialog, int which) {
1514 synchronized (mMethodMap) {
1515 InputMethodInfo im = mIms[which];
1516 hideInputMethodMenu();
1517 setInputMethodLocked(im.getId());
1518 }
1519 }
1520 });
1521
1522 synchronized (mMethodMap) {
1523 mSwitchingDialog = mDialogBuilder.create();
1524 mSwitchingDialog.getWindow().setType(
1525 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
1526 mSwitchingDialog.show();
1527 }
1528 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001529
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001530 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07001531 synchronized (mMethodMap) {
1532 hideInputMethodMenuLocked();
1533 }
1534 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001535
The Android Open Source Project10592532009-03-18 17:39:46 -07001536 void hideInputMethodMenuLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001537 if (DEBUG) Log.v(TAG, "Hide switching menu");
1538
The Android Open Source Project10592532009-03-18 17:39:46 -07001539 if (mSwitchingDialog != null) {
1540 mSwitchingDialog.dismiss();
1541 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001542 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001543
The Android Open Source Project10592532009-03-18 17:39:46 -07001544 mDialogBuilder = null;
1545 mItems = null;
1546 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001547 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001548
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001549 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001550
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001551 public boolean setInputMethodEnabled(String id, boolean enabled) {
1552 synchronized (mMethodMap) {
1553 if (mContext.checkCallingOrSelfPermission(
1554 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1555 != PackageManager.PERMISSION_GRANTED) {
1556 throw new SecurityException(
1557 "Requires permission "
1558 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1559 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001560
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001561 long ident = Binder.clearCallingIdentity();
1562 try {
1563 // Make sure this is a valid input method.
1564 InputMethodInfo imm = mMethodMap.get(id);
1565 if (imm == null) {
1566 if (imm == null) {
1567 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1568 }
1569 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001571 StringBuilder builder = new StringBuilder(256);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001572
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001573 boolean removed = false;
1574 String firstId = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001575
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001576 // Look through the currently enabled input methods.
1577 String enabledStr = Settings.Secure.getString(mContext.getContentResolver(),
1578 Settings.Secure.ENABLED_INPUT_METHODS);
1579 if (enabledStr != null) {
1580 final TextUtils.SimpleStringSplitter splitter = mStringColonSplitter;
1581 splitter.setString(enabledStr);
1582 while (splitter.hasNext()) {
1583 String curId = splitter.next();
1584 if (curId.equals(id)) {
1585 if (enabled) {
1586 // We are enabling this input method, but it is
1587 // already enabled. Nothing to do. The previous
1588 // state was enabled.
1589 return true;
1590 }
1591 // We are disabling this input method, and it is
1592 // currently enabled. Skip it to remove from the
1593 // new list.
1594 removed = true;
1595 } else if (!enabled) {
1596 // We are building a new list of input methods that
1597 // doesn't contain the given one.
1598 if (firstId == null) firstId = curId;
1599 if (builder.length() > 0) builder.append(':');
1600 builder.append(curId);
1601 }
1602 }
1603 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001604
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001605 if (!enabled) {
1606 if (!removed) {
1607 // We are disabling the input method but it is already
1608 // disabled. Nothing to do. The previous state was
1609 // disabled.
1610 return false;
1611 }
1612 // Update the setting with the new list of input methods.
1613 Settings.Secure.putString(mContext.getContentResolver(),
1614 Settings.Secure.ENABLED_INPUT_METHODS, builder.toString());
1615 // We the disabled input method is currently selected, switch
1616 // to another one.
1617 String selId = Settings.Secure.getString(mContext.getContentResolver(),
1618 Settings.Secure.DEFAULT_INPUT_METHOD);
1619 if (id.equals(selId)) {
1620 Settings.Secure.putString(mContext.getContentResolver(),
1621 Settings.Secure.DEFAULT_INPUT_METHOD,
1622 firstId != null ? firstId : "");
1623 }
1624 // Previous state was enabled.
1625 return true;
1626 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001627
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001628 // Add in the newly enabled input method.
1629 if (enabledStr == null || enabledStr.length() == 0) {
1630 enabledStr = id;
1631 } else {
1632 enabledStr = enabledStr + ':' + id;
1633 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001634
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001635 Settings.Secure.putString(mContext.getContentResolver(),
1636 Settings.Secure.ENABLED_INPUT_METHODS, enabledStr);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001637
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001638 // Previous state was disabled.
1639 return false;
1640 } finally {
1641 Binder.restoreCallingIdentity(ident);
1642 }
1643 }
1644 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001645
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001646 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001647
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001648 @Override
1649 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1650 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1651 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001652
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001653 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
1654 + Binder.getCallingPid()
1655 + ", uid=" + Binder.getCallingUid());
1656 return;
1657 }
1658
1659 IInputMethod method;
1660 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001661
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001662 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001663
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001664 synchronized (mMethodMap) {
1665 p.println("Current Input Method Manager state:");
1666 int N = mMethodList.size();
1667 p.println(" Input Methods:");
1668 for (int i=0; i<N; i++) {
1669 InputMethodInfo info = mMethodList.get(i);
1670 p.println(" InputMethod #" + i + ":");
1671 info.dump(p, " ");
1672 }
1673 p.println(" Clients:");
1674 for (ClientState ci : mClients.values()) {
1675 p.println(" Client " + ci + ":");
1676 p.println(" client=" + ci.client);
1677 p.println(" inputContext=" + ci.inputContext);
1678 p.println(" sessionRequested=" + ci.sessionRequested);
1679 p.println(" curSession=" + ci.curSession);
1680 }
1681 p.println(" mInputMethodIcon=" + mInputMethodIcon);
1682 p.println(" mInputMethodData=" + mInputMethodData);
The Android Open Source Project10592532009-03-18 17:39:46 -07001683 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001684 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001685 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
1686 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001687 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
1688 + " mBoundToMethod=" + mBoundToMethod);
1689 p.println(" mCurToken=" + mCurToken);
1690 p.println(" mCurIntent=" + mCurIntent);
1691 method = mCurMethod;
1692 p.println(" mCurMethod=" + mCurMethod);
1693 p.println(" mEnabledSession=" + mEnabledSession);
1694 p.println(" mShowRequested=" + mShowRequested
1695 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
1696 + " mShowForced=" + mShowForced
1697 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07001698 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001699 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001700
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001701 if (client != null) {
1702 p.println(" ");
1703 pw.flush();
1704 try {
1705 client.client.asBinder().dump(fd, args);
1706 } catch (RemoteException e) {
1707 p.println("Input method client dead: " + e);
1708 }
1709 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001710
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001711 if (method != null) {
1712 p.println(" ");
1713 pw.flush();
1714 try {
1715 method.asBinder().dump(fd, args);
1716 } catch (RemoteException e) {
1717 p.println("Input method service dead: " + e);
1718 }
1719 }
1720 }
1721}