blob: 675760f46ced3ddbc970c14784803daed053ca66 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006-2008 The Android Open Source Project
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * the License at
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008 * http://www.apache.org/licenses/LICENSE-2.0
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17package com.android.server;
18
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080019import com.android.internal.content.PackageMonitor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import com.android.internal.os.HandlerCaller;
21import com.android.internal.view.IInputContext;
22import com.android.internal.view.IInputMethod;
23import com.android.internal.view.IInputMethodCallback;
24import com.android.internal.view.IInputMethodClient;
25import com.android.internal.view.IInputMethodManager;
26import com.android.internal.view.IInputMethodSession;
27import com.android.internal.view.InputBindResult;
28
Joe Onorato7a0f36b2010-06-07 10:24:36 -070029import com.android.server.StatusBarManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030
31import org.xmlpull.v1.XmlPullParserException;
32
33import android.app.ActivityManagerNative;
34import android.app.AlertDialog;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070035import android.app.PendingIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.content.ComponentName;
37import android.content.ContentResolver;
38import android.content.Context;
39import android.content.DialogInterface;
40import android.content.IntentFilter;
41import android.content.DialogInterface.OnCancelListener;
42import android.content.Intent;
43import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070044import android.content.pm.ApplicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.content.pm.PackageManager;
46import android.content.pm.ResolveInfo;
47import android.content.pm.ServiceInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070048import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.content.res.Resources;
50import android.content.res.TypedArray;
51import android.database.ContentObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.os.Binder;
53import android.os.Handler;
54import android.os.IBinder;
55import android.os.IInterface;
56import android.os.Message;
57import android.os.Parcel;
58import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080059import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.os.ServiceManager;
61import android.os.SystemClock;
62import android.provider.Settings;
Amith Yamasanie861ec12010-03-24 21:39:27 -070063import android.provider.Settings.Secure;
satokab751aa2010-09-14 19:17:36 +090064import android.provider.Settings.SettingNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.text.TextUtils;
66import android.util.EventLog;
satokab751aa2010-09-14 19:17:36 +090067import android.util.Pair;
Joe Onorato8a9b2202010-02-26 18:56:32 -080068import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.util.PrintWriterPrinter;
70import android.util.Printer;
71import android.view.IWindowManager;
72import android.view.WindowManager;
satokab751aa2010-09-14 19:17:36 +090073import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074import android.view.inputmethod.InputBinding;
75import android.view.inputmethod.InputMethod;
76import android.view.inputmethod.InputMethodInfo;
77import android.view.inputmethod.InputMethodManager;
satokab751aa2010-09-14 19:17:36 +090078import android.view.inputmethod.InputMethodSubtype;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079
80import java.io.FileDescriptor;
81import java.io.IOException;
82import java.io.PrintWriter;
satok913a8922010-08-26 21:53:41 +090083import java.text.Collator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084import java.util.ArrayList;
85import java.util.HashMap;
86import java.util.List;
satok913a8922010-08-26 21:53:41 +090087import java.util.Map;
88import java.util.TreeMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089
90/**
91 * This class provides a system service that manages input methods.
92 */
93public class InputMethodManagerService extends IInputMethodManager.Stub
94 implements ServiceConnection, Handler.Callback {
95 static final boolean DEBUG = false;
96 static final String TAG = "InputManagerService";
97
98 static final int MSG_SHOW_IM_PICKER = 1;
satokab751aa2010-09-14 19:17:36 +090099 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 2;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 static final int MSG_UNBIND_INPUT = 1000;
102 static final int MSG_BIND_INPUT = 1010;
103 static final int MSG_SHOW_SOFT_INPUT = 1020;
104 static final int MSG_HIDE_SOFT_INPUT = 1030;
105 static final int MSG_ATTACH_TOKEN = 1040;
106 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800107
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 static final int MSG_START_INPUT = 2000;
109 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 static final int MSG_UNBIND_METHOD = 3000;
112 static final int MSG_BIND_METHOD = 3010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 static final long TIME_TO_RECONNECT = 10*1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800115
satokab751aa2010-09-14 19:17:36 +0900116 private static final int NOT_A_SUBTYPE_ID = -1;
117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 final Context mContext;
119 final Handler mHandler;
120 final SettingsObserver mSettingsObserver;
Joe Onorato089de882010-04-12 08:18:45 -0700121 final StatusBarManagerService mStatusBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122 final IWindowManager mIWindowManager;
123 final HandlerCaller mCaller;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800124
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 final InputBindResult mNoBinding = new InputBindResult(null, null, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800126
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 // All known input methods. mMethodMap also serves as the global
128 // lock for this class.
129 final ArrayList<InputMethodInfo> mMethodList
130 = new ArrayList<InputMethodInfo>();
131 final HashMap<String, InputMethodInfo> mMethodMap
132 = new HashMap<String, InputMethodInfo>();
133
134 final TextUtils.SimpleStringSplitter mStringColonSplitter
135 = new TextUtils.SimpleStringSplitter(':');
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800136
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 class SessionState {
138 final ClientState client;
139 final IInputMethod method;
140 final IInputMethodSession session;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 @Override
143 public String toString() {
144 return "SessionState{uid " + client.uid + " pid " + client.pid
145 + " method " + Integer.toHexString(
146 System.identityHashCode(method))
147 + " session " + Integer.toHexString(
148 System.identityHashCode(session))
149 + "}";
150 }
151
152 SessionState(ClientState _client, IInputMethod _method,
153 IInputMethodSession _session) {
154 client = _client;
155 method = _method;
156 session = _session;
157 }
158 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 class ClientState {
161 final IInputMethodClient client;
162 final IInputContext inputContext;
163 final int uid;
164 final int pid;
165 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 boolean sessionRequested;
168 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 @Override
171 public String toString() {
172 return "ClientState{" + Integer.toHexString(
173 System.identityHashCode(this)) + " uid " + uid
174 + " pid " + pid + "}";
175 }
176
177 ClientState(IInputMethodClient _client, IInputContext _inputContext,
178 int _uid, int _pid) {
179 client = _client;
180 inputContext = _inputContext;
181 uid = _uid;
182 pid = _pid;
183 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
184 }
185 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800186
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 final HashMap<IBinder, ClientState> mClients
188 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700191 * Set once the system is ready to run third party code.
192 */
193 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800194
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700195 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 * Id of the currently selected input method.
197 */
198 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 /**
201 * The current binding sequence number, incremented every time there is
202 * a new bind performed.
203 */
204 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 /**
207 * The client that is currently bound to an input method.
208 */
209 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700212 * The last window token that gained focus.
213 */
214 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800215
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700216 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 * The input context last provided by the current client.
218 */
219 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 /**
222 * The attributes last provided by the current client.
223 */
224 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 /**
227 * The input method ID of the input method service that we are currently
228 * connected to or in the process of connecting to.
229 */
230 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800231
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 /**
satokab751aa2010-09-14 19:17:36 +0900233 * The current subtype of the current input method.
234 */
235 private InputMethodSubtype mCurrentSubtype;
236
237
238 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 * Set to true if our ServiceConnection is currently actively bound to
240 * a service (whether or not we have gotten its IBinder back yet).
241 */
242 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800243
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244 /**
245 * Set if the client has asked for the input method to be shown.
246 */
247 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800248
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 /**
250 * Set if we were explicitly told to show the input method.
251 */
252 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 /**
255 * Set if we were forced to be shown.
256 */
257 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 /**
260 * Set if we last told the input method to show itself.
261 */
262 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 /**
265 * The Intent used to connect to the current input method.
266 */
267 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800268
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 /**
270 * The token we have made for the currently active input method, to
271 * identify it in the future.
272 */
273 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800274
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 /**
276 * If non-null, this is the input method service we are currently connected
277 * to.
278 */
279 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 /**
282 * Time that we last initiated a bind to the input method, to determine
283 * if we should try to disconnect and reconnect to it.
284 */
285 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287 /**
288 * Have we called mCurMethod.bindInput()?
289 */
290 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 /**
293 * Currently enabled session. Only touched by service thread, not
294 * protected by a lock.
295 */
296 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800297
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298 /**
299 * True if the screen is on. The value is true initially.
300 */
301 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 AlertDialog.Builder mDialogBuilder;
304 AlertDialog mSwitchingDialog;
305 InputMethodInfo[] mIms;
306 CharSequence[] mItems;
satokab751aa2010-09-14 19:17:36 +0900307 int[] mSubtypeIds;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309 class SettingsObserver extends ContentObserver {
310 SettingsObserver(Handler handler) {
311 super(handler);
312 ContentResolver resolver = mContext.getContentResolver();
313 resolver.registerContentObserver(Settings.Secure.getUriFor(
314 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
satokab751aa2010-09-14 19:17:36 +0900315 resolver.registerContentObserver(Settings.Secure.getUriFor(
316 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 @Override public void onChange(boolean selfChange) {
320 synchronized (mMethodMap) {
321 updateFromSettingsLocked();
322 }
323 }
324 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 class ScreenOnOffReceiver extends android.content.BroadcastReceiver {
327 @Override
328 public void onReceive(Context context, Intent intent) {
329 if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
330 mScreenOn = true;
331 } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
332 mScreenOn = false;
The Android Open Source Project10592532009-03-18 17:39:46 -0700333 } else if (intent.getAction().equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
334 hideInputMethodMenu();
335 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800337 Slog.w(TAG, "Unexpected intent " + intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 }
339
340 // Inform the current client of the change in active status
341 try {
342 if (mCurClient != null && mCurClient.client != null) {
343 mCurClient.client.setActive(mScreenOn);
344 }
345 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800346 Slog.w(TAG, "Got RemoteException sending 'screen on/off' notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 + mCurClient.pid + " uid " + mCurClient.uid);
348 }
349 }
350 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800351
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800352 class MyPackageMonitor extends PackageMonitor {
353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800355 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 synchronized (mMethodMap) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800357 String curInputMethodId = Settings.Secure.getString(mContext
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
359 final int N = mMethodList.size();
360 if (curInputMethodId != null) {
361 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800362 InputMethodInfo imi = mMethodList.get(i);
363 if (imi.getId().equals(curInputMethodId)) {
364 for (String pkg : packages) {
365 if (imi.getPackageName().equals(pkg)) {
366 if (!doit) {
367 return true;
368 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800369 Settings.Secure.putString(mContext.getContentResolver(),
370 Settings.Secure.DEFAULT_INPUT_METHOD, "");
satokab751aa2010-09-14 19:17:36 +0900371 resetSelectedInputMethodSubtype();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800372 chooseNewDefaultIMELocked();
373 return true;
374 }
375 }
376 }
377 }
378 }
379 }
380 return false;
381 }
382
383 @Override
384 public void onSomePackagesChanged() {
385 synchronized (mMethodMap) {
386 InputMethodInfo curIm = null;
387 String curInputMethodId = Settings.Secure.getString(mContext
388 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
389 final int N = mMethodList.size();
390 if (curInputMethodId != null) {
391 for (int i=0; i<N; i++) {
392 InputMethodInfo imi = mMethodList.get(i);
393 if (imi.getId().equals(curInputMethodId)) {
394 curIm = imi;
395 }
396 int change = isPackageDisappearing(imi.getPackageName());
397 if (change == PACKAGE_TEMPORARY_CHANGE
398 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800399 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800400 + imi.getComponent());
401 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 }
403 }
404 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800405
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800406 buildInputMethodListLocked(mMethodList, mMethodMap);
407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800409
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800410 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800411 int change = isPackageDisappearing(curIm.getPackageName());
412 if (change == PACKAGE_TEMPORARY_CHANGE
413 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800414 ServiceInfo si = null;
415 try {
416 si = mContext.getPackageManager().getServiceInfo(
417 curIm.getComponent(), 0);
418 } catch (PackageManager.NameNotFoundException ex) {
419 }
420 if (si == null) {
421 // Uh oh, current input method is no longer around!
422 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800423 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800424 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800425 changed = true;
426 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800427 Slog.i(TAG, "Unsetting current input method");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800428 Settings.Secure.putString(mContext.getContentResolver(),
satokab751aa2010-09-14 19:17:36 +0900429 Settings.Secure.DEFAULT_INPUT_METHOD, "");
430 resetSelectedInputMethodSubtype();
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800431 }
432 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800433 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800434 }
satokab751aa2010-09-14 19:17:36 +0900435
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800436 if (curIm == null) {
437 // We currently don't have a default input method... is
438 // one now available?
439 changed = chooseNewDefaultIMELocked();
440 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800441
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800442 if (changed) {
443 updateFromSettingsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 }
445 }
446 }
447 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800448
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 class MethodCallback extends IInputMethodCallback.Stub {
450 final IInputMethod mMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800452 MethodCallback(IInputMethod method) {
453 mMethod = method;
454 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 public void finishedEvent(int seq, boolean handled) throws RemoteException {
457 }
458
459 public void sessionCreated(IInputMethodSession session) throws RemoteException {
460 onSessionCreated(mMethod, session);
461 }
462 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800463
Joe Onorato089de882010-04-12 08:18:45 -0700464 public InputMethodManagerService(Context context, StatusBarManagerService statusBar) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 mContext = context;
466 mHandler = new Handler(this);
467 mIWindowManager = IWindowManager.Stub.asInterface(
468 ServiceManager.getService(Context.WINDOW_SERVICE));
469 mCaller = new HandlerCaller(context, new HandlerCaller.Callback() {
470 public void executeMessage(Message msg) {
471 handleMessage(msg);
472 }
473 });
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800474
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800475 (new MyPackageMonitor()).register(mContext, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800476
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477 IntentFilter screenOnOffFilt = new IntentFilter();
478 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_ON);
479 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_OFF);
The Android Open Source Project10592532009-03-18 17:39:46 -0700480 screenOnOffFilt.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481 mContext.registerReceiver(new ScreenOnOffReceiver(), screenOnOffFilt);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800482
satok913a8922010-08-26 21:53:41 +0900483 mStatusBar = statusBar;
484 statusBar.setIconVisibility("ime", false);
485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486 buildInputMethodListLocked(mMethodList, mMethodMap);
487
488 final String enabledStr = Settings.Secure.getString(
489 mContext.getContentResolver(),
490 Settings.Secure.ENABLED_INPUT_METHODS);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800491 Slog.i(TAG, "Enabled input methods: " + enabledStr);
satok913a8922010-08-26 21:53:41 +0900492 final String defaultIme = Settings.Secure.getString(mContext
493 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
494 if (enabledStr == null || TextUtils.isEmpty(defaultIme)) {
495 Slog.i(TAG, "Enabled input methods or default IME has not been set, enabling all");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496 InputMethodInfo defIm = null;
497 StringBuilder sb = new StringBuilder(256);
498 final int N = mMethodList.size();
499 for (int i=0; i<N; i++) {
500 InputMethodInfo imi = mMethodList.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800501 Slog.i(TAG, "Adding: " + imi.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502 if (i > 0) sb.append(':');
503 sb.append(imi.getId());
504 if (defIm == null && imi.getIsDefaultResourceId() != 0) {
505 try {
506 Resources res = mContext.createPackageContext(
507 imi.getPackageName(), 0).getResources();
508 if (res.getBoolean(imi.getIsDefaultResourceId())) {
509 defIm = imi;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800510 Slog.i(TAG, "Selected default: " + imi.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511 }
512 } catch (PackageManager.NameNotFoundException ex) {
513 } catch (Resources.NotFoundException ex) {
514 }
515 }
516 }
517 if (defIm == null && N > 0) {
518 defIm = mMethodList.get(0);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800519 Slog.i(TAG, "No default found, using " + defIm.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520 }
521 Settings.Secure.putString(mContext.getContentResolver(),
522 Settings.Secure.ENABLED_INPUT_METHODS, sb.toString());
523 if (defIm != null) {
524 Settings.Secure.putString(mContext.getContentResolver(),
525 Settings.Secure.DEFAULT_INPUT_METHOD, defIm.getId());
satokab751aa2010-09-14 19:17:36 +0900526 putSelectedInputMethodSubtype(defIm, NOT_A_SUBTYPE_ID);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 }
528 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800529
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530 mSettingsObserver = new SettingsObserver(mHandler);
531 updateFromSettingsLocked();
532 }
533
534 @Override
535 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
536 throws RemoteException {
537 try {
538 return super.onTransact(code, data, reply, flags);
539 } catch (RuntimeException e) {
540 // The input method manager only throws security exceptions, so let's
541 // log all others.
542 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800543 Slog.e(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544 }
545 throw e;
546 }
547 }
548
549 public void systemReady() {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700550 synchronized (mMethodMap) {
551 if (!mSystemReady) {
552 mSystemReady = true;
Dianne Hackborncc278702009-09-02 23:07:23 -0700553 try {
554 startInputInnerLocked();
555 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800556 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700557 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700558 }
559 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800561
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 public List<InputMethodInfo> getInputMethodList() {
563 synchronized (mMethodMap) {
564 return new ArrayList<InputMethodInfo>(mMethodList);
565 }
566 }
567
568 public List<InputMethodInfo> getEnabledInputMethodList() {
569 synchronized (mMethodMap) {
570 return getEnabledInputMethodListLocked();
571 }
572 }
573
574 List<InputMethodInfo> getEnabledInputMethodListLocked() {
575 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800576
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577 final String enabledStr = Settings.Secure.getString(
578 mContext.getContentResolver(),
579 Settings.Secure.ENABLED_INPUT_METHODS);
580 if (enabledStr != null) {
581 final TextUtils.SimpleStringSplitter splitter = mStringColonSplitter;
582 splitter.setString(enabledStr);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 while (splitter.hasNext()) {
585 InputMethodInfo info = mMethodMap.get(splitter.next());
586 if (info != null) {
587 res.add(info);
588 }
589 }
590 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800591
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800592 return res;
593 }
594
595 public void addClient(IInputMethodClient client,
596 IInputContext inputContext, int uid, int pid) {
597 synchronized (mMethodMap) {
598 mClients.put(client.asBinder(), new ClientState(client,
599 inputContext, uid, pid));
600 }
601 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800602
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603 public void removeClient(IInputMethodClient client) {
604 synchronized (mMethodMap) {
605 mClients.remove(client.asBinder());
606 }
607 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800608
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609 void executeOrSendMessage(IInterface target, Message msg) {
610 if (target.asBinder() instanceof Binder) {
611 mCaller.sendMessage(msg);
612 } else {
613 handleMessage(msg);
614 msg.recycle();
615 }
616 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800617
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700618 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800620 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 + mCurClient.client.asBinder());
622 if (mBoundToMethod) {
623 mBoundToMethod = false;
624 if (mCurMethod != null) {
625 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
626 MSG_UNBIND_INPUT, mCurMethod));
627 }
628 }
629 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
630 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
631 mCurClient.sessionRequested = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800632
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633 // Call setActive(false) on the old client
634 try {
635 mCurClient.client.setActive(false);
636 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800637 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 + mCurClient.pid + " uid " + mCurClient.uid);
639 }
640 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800641
The Android Open Source Project10592532009-03-18 17:39:46 -0700642 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643 }
644 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800645
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 private int getImeShowFlags() {
647 int flags = 0;
648 if (mShowForced) {
649 flags |= InputMethod.SHOW_FORCED
650 | InputMethod.SHOW_EXPLICIT;
651 } else if (mShowExplicitlyRequested) {
652 flags |= InputMethod.SHOW_EXPLICIT;
653 }
654 return flags;
655 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800656
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800657 private int getAppShowFlags() {
658 int flags = 0;
659 if (mShowForced) {
660 flags |= InputMethodManager.SHOW_FORCED;
661 } else if (!mShowExplicitlyRequested) {
662 flags |= InputMethodManager.SHOW_IMPLICIT;
663 }
664 return flags;
665 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800666
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 InputBindResult attachNewInputLocked(boolean initial, boolean needResult) {
668 if (!mBoundToMethod) {
669 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
670 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
671 mBoundToMethod = true;
672 }
673 final SessionState session = mCurClient.curSession;
674 if (initial) {
675 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
676 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
677 } else {
678 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
679 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
680 }
681 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800682 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -0800683 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684 }
685 return needResult
686 ? new InputBindResult(session.session, mCurId, mCurSeq)
687 : null;
688 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800689
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800690 InputBindResult startInputLocked(IInputMethodClient client,
691 IInputContext inputContext, EditorInfo attribute,
692 boolean initial, boolean needResult) {
693 // If no method is currently selected, do nothing.
694 if (mCurMethodId == null) {
695 return mNoBinding;
696 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800697
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698 ClientState cs = mClients.get(client.asBinder());
699 if (cs == null) {
700 throw new IllegalArgumentException("unknown client "
701 + client.asBinder());
702 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800703
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704 try {
705 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
706 // Check with the window manager to make sure this client actually
707 // has a window with focus. If not, reject. This is thread safe
708 // because if the focus changes some time before or after, the
709 // next client receiving focus that has any interest in input will
710 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800711 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
713 return null;
714 }
715 } catch (RemoteException e) {
716 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800717
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800718 if (mCurClient != cs) {
719 // If the client is changing, we need to switch over to the new
720 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700721 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800722 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800723 + cs.client.asBinder());
724
725 // If the screen is on, inform the new client it is active
726 if (mScreenOn) {
727 try {
728 cs.client.setActive(mScreenOn);
729 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800730 Slog.w(TAG, "Got RemoteException sending setActive notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800731 + cs.pid + " uid " + cs.uid);
732 }
733 }
734 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800735
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800736 // Bump up the sequence for this client and attach it.
737 mCurSeq++;
738 if (mCurSeq <= 0) mCurSeq = 1;
739 mCurClient = cs;
740 mCurInputContext = inputContext;
741 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800742
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800743 // Check if the input method is changing.
744 if (mCurId != null && mCurId.equals(mCurMethodId)) {
745 if (cs.curSession != null) {
746 // Fast case: if we are already connected to the input method,
747 // then just return it.
748 return attachNewInputLocked(initial, needResult);
749 }
750 if (mHaveConnection) {
751 if (mCurMethod != null) {
752 if (!cs.sessionRequested) {
753 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800754 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800755 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
756 MSG_CREATE_SESSION, mCurMethod,
757 new MethodCallback(mCurMethod)));
758 }
759 // Return to client, and we will get back with it when
760 // we have had a session made for it.
761 return new InputBindResult(null, mCurId, mCurSeq);
762 } else if (SystemClock.uptimeMillis()
763 < (mLastBindTime+TIME_TO_RECONNECT)) {
764 // In this case we have connected to the service, but
765 // don't yet have its interface. If it hasn't been too
766 // long since we did the connection, we'll return to
767 // the client and wait to get the service interface so
768 // we can report back. If it has been too long, we want
769 // to fall through so we can try a disconnect/reconnect
770 // to see if we can get back in touch with the service.
771 return new InputBindResult(null, mCurId, mCurSeq);
772 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800773 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
774 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800775 }
776 }
777 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800778
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700779 return startInputInnerLocked();
780 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800781
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700782 InputBindResult startInputInnerLocked() {
783 if (mCurMethodId == null) {
784 return mNoBinding;
785 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800786
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700787 if (!mSystemReady) {
788 // If the system is not yet ready, we shouldn't be running third
789 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -0700790 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700791 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800792
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800793 InputMethodInfo info = mMethodMap.get(mCurMethodId);
794 if (info == null) {
795 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
796 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800797
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700798 unbindCurrentMethodLocked(false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800799
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800800 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
801 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -0700802 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
803 com.android.internal.R.string.input_method_binding_label);
804 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
805 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800806 if (mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE)) {
807 mLastBindTime = SystemClock.uptimeMillis();
808 mHaveConnection = true;
809 mCurId = info.getId();
810 mCurToken = new Binder();
811 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800812 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800813 mIWindowManager.addWindowToken(mCurToken,
814 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
815 } catch (RemoteException e) {
816 }
817 return new InputBindResult(null, mCurId, mCurSeq);
818 } else {
819 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800820 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800821 + mCurIntent);
822 }
823 return null;
824 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800825
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800826 public InputBindResult startInput(IInputMethodClient client,
827 IInputContext inputContext, EditorInfo attribute,
828 boolean initial, boolean needResult) {
829 synchronized (mMethodMap) {
830 final long ident = Binder.clearCallingIdentity();
831 try {
832 return startInputLocked(client, inputContext, attribute,
833 initial, needResult);
834 } finally {
835 Binder.restoreCallingIdentity(ident);
836 }
837 }
838 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800839
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800840 public void finishInput(IInputMethodClient client) {
841 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800842
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800843 public void onServiceConnected(ComponentName name, IBinder service) {
844 synchronized (mMethodMap) {
845 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
846 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -0700847 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800848 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborncc278702009-09-02 23:07:23 -0700849 unbindCurrentMethodLocked(false);
850 return;
851 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800852 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -0700853 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
854 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800855 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800856 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -0700857 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800858 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -0700859 MSG_CREATE_SESSION, mCurMethod,
860 new MethodCallback(mCurMethod)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800861 }
862 }
863 }
864 }
865
866 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
867 synchronized (mMethodMap) {
868 if (mCurMethod != null && method != null
869 && mCurMethod.asBinder() == method.asBinder()) {
870 if (mCurClient != null) {
871 mCurClient.curSession = new SessionState(mCurClient,
872 method, session);
873 mCurClient.sessionRequested = false;
874 InputBindResult res = attachNewInputLocked(true, true);
875 if (res.method != null) {
876 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
877 MSG_BIND_METHOD, mCurClient.client, res));
878 }
879 }
880 }
881 }
882 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800883
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700884 void unbindCurrentMethodLocked(boolean reportToClient) {
885 if (mHaveConnection) {
886 mContext.unbindService(this);
887 mHaveConnection = false;
888 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800889
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700890 if (mCurToken != null) {
891 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800892 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700893 mIWindowManager.removeWindowToken(mCurToken);
894 } catch (RemoteException e) {
895 }
896 mCurToken = null;
897 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800898
The Android Open Source Project10592532009-03-18 17:39:46 -0700899 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700900 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800901
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700902 if (reportToClient && mCurClient != null) {
903 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
904 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
905 }
906 }
907
Devin Taylor0c33ed22010-02-23 13:26:46 -0600908 private void finishSession(SessionState sessionState) {
909 if (sessionState != null && sessionState.session != null) {
910 try {
911 sessionState.session.finishSession();
912 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -0700913 Slog.w(TAG, "Session failed to close due to remote exception", e);
Devin Taylor0c33ed22010-02-23 13:26:46 -0600914 }
915 }
916 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800917
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700918 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919 if (mCurMethod != null) {
920 for (ClientState cs : mClients.values()) {
921 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -0600922 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923 cs.curSession = null;
924 }
Devin Taylor0c33ed22010-02-23 13:26:46 -0600925
926 finishSession(mEnabledSession);
927 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800928 mCurMethod = null;
929 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700930 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800931 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800932
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800933 public void onServiceDisconnected(ComponentName name) {
934 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800935 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800936 + " mCurIntent=" + mCurIntent);
937 if (mCurMethod != null && mCurIntent != null
938 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700939 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800940 // We consider this to be a new bind attempt, since the system
941 // should now try to restart the service for us.
942 mLastBindTime = SystemClock.uptimeMillis();
943 mShowRequested = mInputShown;
944 mInputShown = false;
945 if (mCurClient != null) {
946 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
947 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
948 }
949 }
950 }
951 }
952
953 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
954 long ident = Binder.clearCallingIdentity();
955 try {
956 if (token == null || mCurToken != token) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800957 Slog.w(TAG, "Ignoring setInputMethod of token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800958 return;
959 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800960
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800961 synchronized (mMethodMap) {
962 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800963 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -0700964 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800965 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800966 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -0700967 mStatusBar.setIcon("ime", packageName, iconId, 0);
968 mStatusBar.setIconVisibility("ime", true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800969 }
970 }
971 } finally {
972 Binder.restoreCallingIdentity(ident);
973 }
974 }
975
976 void updateFromSettingsLocked() {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700977 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
978 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
979 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
980 // enabled.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800981 String id = Settings.Secure.getString(mContext.getContentResolver(),
satokab751aa2010-09-14 19:17:36 +0900982 Settings.Secure.DEFAULT_INPUT_METHOD);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700983 if (id != null && id.length() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800984 try {
satokab751aa2010-09-14 19:17:36 +0900985 setInputMethodLocked(id, getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800986 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800987 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -0700988 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700989 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700991 } else {
992 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -0700993 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700994 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800995 }
996 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800997
satokab751aa2010-09-14 19:17:36 +0900998 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800999 InputMethodInfo info = mMethodMap.get(id);
1000 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001001 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001002 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001003
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001004 if (id.equals(mCurMethodId)) {
satokab751aa2010-09-14 19:17:36 +09001005 if (subtypeId != NOT_A_SUBTYPE_ID) {
1006 InputMethodSubtype subtype = info.getSubtypes().get(subtypeId);
1007 if (subtype != mCurrentSubtype) {
1008 synchronized (mMethodMap) {
1009 if (mCurMethod != null) {
1010 try {
1011 putSelectedInputMethodSubtype(info, subtypeId);
1012 mCurMethod.changeInputMethodSubtype(subtype);
1013 } catch (RemoteException e) {
1014 return;
1015 }
1016 }
1017 }
1018 }
1019 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001020 return;
1021 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001023 final long ident = Binder.clearCallingIdentity();
1024 try {
1025 mCurMethodId = id;
satokab751aa2010-09-14 19:17:36 +09001026 // Set a subtype to this input method.
1027 // subtypeId the name of a subtype which will be set.
1028 if (putSelectedInputMethodSubtype(info, subtypeId)) {
1029 mCurrentSubtype = info.getSubtypes().get(subtypeId);
1030 } else {
1031 mCurrentSubtype = null;
1032 }
1033
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001034 Settings.Secure.putString(mContext.getContentResolver(),
satokab751aa2010-09-14 19:17:36 +09001035 Settings.Secure.DEFAULT_INPUT_METHOD, id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001036
1037 if (ActivityManagerNative.isSystemReady()) {
1038 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001039 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040 intent.putExtra("input_method_id", id);
1041 mContext.sendBroadcast(intent);
1042 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001043 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001044 } finally {
1045 Binder.restoreCallingIdentity(ident);
1046 }
1047 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001048
The Android Open Source Project4df24232009-03-05 14:34:35 -08001049 public boolean showSoftInput(IInputMethodClient client, int flags,
1050 ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001051 long ident = Binder.clearCallingIdentity();
1052 try {
1053 synchronized (mMethodMap) {
1054 if (mCurClient == null || client == null
1055 || mCurClient.client.asBinder() != client.asBinder()) {
1056 try {
1057 // We need to check if this is the current client with
1058 // focus in the window manager, to allow this call to
1059 // be made before input is started in it.
1060 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001061 Slog.w(TAG, "Ignoring showSoftInput of: " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001062 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001063 }
1064 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001065 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001066 }
1067 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001068
Joe Onorato8a9b2202010-02-26 18:56:32 -08001069 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001070 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001071 }
1072 } finally {
1073 Binder.restoreCallingIdentity(ident);
1074 }
1075 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001076
The Android Open Source Project4df24232009-03-05 14:34:35 -08001077 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001078 mShowRequested = true;
1079 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1080 mShowExplicitlyRequested = true;
1081 }
1082 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1083 mShowExplicitlyRequested = true;
1084 mShowForced = true;
1085 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001086
Dianne Hackborncc278702009-09-02 23:07:23 -07001087 if (!mSystemReady) {
1088 return false;
1089 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001090
The Android Open Source Project4df24232009-03-05 14:34:35 -08001091 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001093 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1094 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1095 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001096 mInputShown = true;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001097 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001098 } else if (mHaveConnection && SystemClock.uptimeMillis()
1099 < (mLastBindTime+TIME_TO_RECONNECT)) {
1100 // The client has asked to have the input method shown, but
1101 // we have been sitting here too long with a connection to the
1102 // service and no interface received, so let's disconnect/connect
1103 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001104 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 SystemClock.uptimeMillis()-mLastBindTime,1);
1106 mContext.unbindService(this);
1107 mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE);
1108 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001109
The Android Open Source Project4df24232009-03-05 14:34:35 -08001110 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001112
The Android Open Source Project4df24232009-03-05 14:34:35 -08001113 public boolean hideSoftInput(IInputMethodClient client, int flags,
1114 ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001115 long ident = Binder.clearCallingIdentity();
1116 try {
1117 synchronized (mMethodMap) {
1118 if (mCurClient == null || client == null
1119 || mCurClient.client.asBinder() != client.asBinder()) {
1120 try {
1121 // We need to check if this is the current client with
1122 // focus in the window manager, to allow this call to
1123 // be made before input is started in it.
1124 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001125 Slog.w(TAG, "Ignoring hideSoftInput of: " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001126 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001127 }
1128 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001129 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001130 }
1131 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001132
Joe Onorato8a9b2202010-02-26 18:56:32 -08001133 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001134 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135 }
1136 } finally {
1137 Binder.restoreCallingIdentity(ident);
1138 }
1139 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001140
The Android Open Source Project4df24232009-03-05 14:34:35 -08001141 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001142 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1143 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001144 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001145 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001146 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001147 }
1148 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001149 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001150 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001151 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001152 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001153 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001154 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001155 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1156 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1157 res = true;
1158 } else {
1159 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160 }
1161 mInputShown = false;
1162 mShowRequested = false;
1163 mShowExplicitlyRequested = false;
1164 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001165 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001166 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001167
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001168 public void windowGainedFocus(IInputMethodClient client, IBinder windowToken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 boolean viewHasFocus, boolean isTextEditor, int softInputMode,
1170 boolean first, int windowFlags) {
1171 long ident = Binder.clearCallingIdentity();
1172 try {
1173 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001174 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001175 + " viewHasFocus=" + viewHasFocus
1176 + " isTextEditor=" + isTextEditor
1177 + " softInputMode=#" + Integer.toHexString(softInputMode)
1178 + " first=" + first + " flags=#"
1179 + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 if (mCurClient == null || client == null
1182 || mCurClient.client.asBinder() != client.asBinder()) {
1183 try {
1184 // We need to check if this is the current client with
1185 // focus in the window manager, to allow this call to
1186 // be made before input is started in it.
1187 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001188 Slog.w(TAG, "Client not active, ignoring focus gain of: " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 return;
1190 }
1191 } catch (RemoteException e) {
1192 }
1193 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001194
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001195 if (mCurFocusedWindow == windowToken) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001196 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001197 return;
1198 }
1199 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001201 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1202 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
1203 if (!isTextEditor || (softInputMode &
1204 WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1205 != WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE) {
1206 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1207 // There is no focus view, and this window will
1208 // be behind any soft input window, so hide the
1209 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001210 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001211 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 }
1213 } else if (isTextEditor && (softInputMode &
1214 WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1215 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1216 && (softInputMode &
1217 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
1218 // There is a focus view, and we are navigating forward
1219 // into the window, so show the input window for the user.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001220 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001221 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222 }
1223 break;
1224 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1225 // Do nothing.
1226 break;
1227 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1228 if ((softInputMode &
1229 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001230 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001231 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001232 }
1233 break;
1234 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001235 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001236 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237 break;
1238 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1239 if ((softInputMode &
1240 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001241 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001242 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 }
1244 break;
1245 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001246 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001247 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001248 break;
1249 }
1250 }
1251 } finally {
1252 Binder.restoreCallingIdentity(ident);
1253 }
1254 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001256 public void showInputMethodPickerFromClient(IInputMethodClient client) {
1257 synchronized (mMethodMap) {
1258 if (mCurClient == null || client == null
1259 || mCurClient.client.asBinder() != client.asBinder()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001260 Slog.w(TAG, "Ignoring showInputMethodDialogFromClient of: " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001261 }
1262
1263 mHandler.sendEmptyMessage(MSG_SHOW_IM_PICKER);
1264 }
1265 }
1266
satokab751aa2010-09-14 19:17:36 +09001267 public void showInputMethodSubtypePickerFromClient(IInputMethodClient client) {
1268 synchronized (mMethodMap) {
1269 if (mCurClient == null || client == null
1270 || mCurClient.client.asBinder() != client.asBinder()) {
1271 Slog.w(TAG, "Ignoring showInputSubtypeMethodDialogFromClient of: " + client);
1272 }
1273
1274 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1275 }
1276 }
1277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001278 public void setInputMethod(IBinder token, String id) {
satokab751aa2010-09-14 19:17:36 +09001279 setInputMethodWithSubtype(token, id, NOT_A_SUBTYPE_ID);
1280 }
1281
1282 private void setInputMethodWithSubtype(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001283 synchronized (mMethodMap) {
1284 if (token == null) {
1285 if (mContext.checkCallingOrSelfPermission(
1286 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1287 != PackageManager.PERMISSION_GRANTED) {
1288 throw new SecurityException(
1289 "Using null token requires permission "
1290 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1291 }
1292 } else if (mCurToken != token) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001293 Slog.w(TAG, "Ignoring setInputMethod of token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294 return;
1295 }
1296
1297 long ident = Binder.clearCallingIdentity();
1298 try {
satokab751aa2010-09-14 19:17:36 +09001299 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300 } finally {
1301 Binder.restoreCallingIdentity(ident);
1302 }
1303 }
1304 }
1305
1306 public void hideMySoftInput(IBinder token, int flags) {
1307 synchronized (mMethodMap) {
1308 if (token == null || mCurToken != token) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001309 Slog.w(TAG, "Ignoring hideInputMethod of token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310 return;
1311 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001312 long ident = Binder.clearCallingIdentity();
1313 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001314 hideCurrentInputLocked(flags, null);
1315 } finally {
1316 Binder.restoreCallingIdentity(ident);
1317 }
1318 }
1319 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001320
The Android Open Source Project4df24232009-03-05 14:34:35 -08001321 public void showMySoftInput(IBinder token, int flags) {
1322 synchronized (mMethodMap) {
1323 if (token == null || mCurToken != token) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001324 Slog.w(TAG, "Ignoring hideInputMethod of token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001325 return;
1326 }
1327 long ident = Binder.clearCallingIdentity();
1328 try {
1329 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001330 } finally {
1331 Binder.restoreCallingIdentity(ident);
1332 }
1333 }
1334 }
1335
1336 void setEnabledSessionInMainThread(SessionState session) {
1337 if (mEnabledSession != session) {
1338 if (mEnabledSession != null) {
1339 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001340 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001341 mEnabledSession.method.setSessionEnabled(
1342 mEnabledSession.session, false);
1343 } catch (RemoteException e) {
1344 }
1345 }
1346 mEnabledSession = session;
1347 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001348 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001349 session.method.setSessionEnabled(
1350 session.session, true);
1351 } catch (RemoteException e) {
1352 }
1353 }
1354 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001356 public boolean handleMessage(Message msg) {
1357 HandlerCaller.SomeArgs args;
1358 switch (msg.what) {
1359 case MSG_SHOW_IM_PICKER:
1360 showInputMethodMenu();
1361 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001362
satokab751aa2010-09-14 19:17:36 +09001363 case MSG_SHOW_IM_SUBTYPE_PICKER:
1364 showInputMethodSubtypeMenu();
1365 return true;
1366
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001367 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001368
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001369 case MSG_UNBIND_INPUT:
1370 try {
1371 ((IInputMethod)msg.obj).unbindInput();
1372 } catch (RemoteException e) {
1373 // There is nothing interesting about the method dying.
1374 }
1375 return true;
1376 case MSG_BIND_INPUT:
1377 args = (HandlerCaller.SomeArgs)msg.obj;
1378 try {
1379 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
1380 } catch (RemoteException e) {
1381 }
1382 return true;
1383 case MSG_SHOW_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001384 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001385 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001386 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
1387 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001388 } catch (RemoteException e) {
1389 }
1390 return true;
1391 case MSG_HIDE_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001392 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001393 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001394 ((IInputMethod)args.arg1).hideSoftInput(0,
1395 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001396 } catch (RemoteException e) {
1397 }
1398 return true;
1399 case MSG_ATTACH_TOKEN:
1400 args = (HandlerCaller.SomeArgs)msg.obj;
1401 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001402 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001403 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
1404 } catch (RemoteException e) {
1405 }
1406 return true;
1407 case MSG_CREATE_SESSION:
1408 args = (HandlerCaller.SomeArgs)msg.obj;
1409 try {
1410 ((IInputMethod)args.arg1).createSession(
1411 (IInputMethodCallback)args.arg2);
1412 } catch (RemoteException e) {
1413 }
1414 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001415 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001416
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001417 case MSG_START_INPUT:
1418 args = (HandlerCaller.SomeArgs)msg.obj;
1419 try {
1420 SessionState session = (SessionState)args.arg1;
1421 setEnabledSessionInMainThread(session);
1422 session.method.startInput((IInputContext)args.arg2,
1423 (EditorInfo)args.arg3);
1424 } catch (RemoteException e) {
1425 }
1426 return true;
1427 case MSG_RESTART_INPUT:
1428 args = (HandlerCaller.SomeArgs)msg.obj;
1429 try {
1430 SessionState session = (SessionState)args.arg1;
1431 setEnabledSessionInMainThread(session);
1432 session.method.restartInput((IInputContext)args.arg2,
1433 (EditorInfo)args.arg3);
1434 } catch (RemoteException e) {
1435 }
1436 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001437
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001438 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001440 case MSG_UNBIND_METHOD:
1441 try {
1442 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
1443 } catch (RemoteException e) {
1444 // There is nothing interesting about the last client dying.
1445 }
1446 return true;
1447 case MSG_BIND_METHOD:
1448 args = (HandlerCaller.SomeArgs)msg.obj;
1449 try {
1450 ((IInputMethodClient)args.arg1).onBindMethod(
1451 (InputBindResult)args.arg2);
1452 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001453 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001454 }
1455 return true;
1456 }
1457 return false;
1458 }
1459
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001460 private boolean isSystemIme(InputMethodInfo inputMethod) {
1461 return (inputMethod.getServiceInfo().applicationInfo.flags
1462 & ApplicationInfo.FLAG_SYSTEM) != 0;
1463 }
1464
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001465 private boolean chooseNewDefaultIMELocked() {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001466 List<InputMethodInfo> enabled = getEnabledInputMethodListLocked();
1467 if (enabled != null && enabled.size() > 0) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07001468 // We'd prefer to fall back on a system IME, since that is safer.
1469 int i=enabled.size();
1470 while (i > 0) {
1471 i--;
1472 if ((enabled.get(i).getServiceInfo().applicationInfo.flags
1473 & ApplicationInfo.FLAG_SYSTEM) != 0) {
1474 break;
1475 }
1476 }
satokab751aa2010-09-14 19:17:36 +09001477 InputMethodInfo imi = enabled.get(i);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001478 Settings.Secure.putString(mContext.getContentResolver(),
satokab751aa2010-09-14 19:17:36 +09001479 Settings.Secure.DEFAULT_INPUT_METHOD, imi.getId());
1480 putSelectedInputMethodSubtype(imi, NOT_A_SUBTYPE_ID);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001481 return true;
1482 }
1483
1484 return false;
1485 }
1486
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001487 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
1488 HashMap<String, InputMethodInfo> map) {
1489 list.clear();
1490 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001492 PackageManager pm = mContext.getPackageManager();
Amith Yamasanie861ec12010-03-24 21:39:27 -07001493 final Configuration config = mContext.getResources().getConfiguration();
1494 final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY;
1495 String disabledSysImes = Settings.Secure.getString(mContext.getContentResolver(),
1496 Secure.DISABLED_SYSTEM_INPUT_METHODS);
1497 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001498
1499 List<ResolveInfo> services = pm.queryIntentServices(
1500 new Intent(InputMethod.SERVICE_INTERFACE),
1501 PackageManager.GET_META_DATA);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001503 for (int i = 0; i < services.size(); ++i) {
1504 ResolveInfo ri = services.get(i);
1505 ServiceInfo si = ri.serviceInfo;
1506 ComponentName compName = new ComponentName(si.packageName, si.name);
1507 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
1508 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001509 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001510 + ": it does not require the permission "
1511 + android.Manifest.permission.BIND_INPUT_METHOD);
1512 continue;
1513 }
1514
Joe Onorato8a9b2202010-02-26 18:56:32 -08001515 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001516
1517 try {
1518 InputMethodInfo p = new InputMethodInfo(mContext, ri);
1519 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07001520 final String id = p.getId();
1521 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001522
Amith Yamasanie861ec12010-03-24 21:39:27 -07001523 // System IMEs are enabled by default, unless there's a hard keyboard
1524 // and the system IME was explicitly disabled
1525 if (isSystemIme(p) && (!haveHardKeyboard || disabledSysImes.indexOf(id) < 0)) {
1526 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001527 }
1528
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001529 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001530 Slog.d(TAG, "Found a third-party input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001531 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001532
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001533 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001534 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001535 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001536 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001537 }
1538 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001539
1540 String defaultIme = Settings.Secure.getString(mContext
1541 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok913a8922010-08-26 21:53:41 +09001542 if (!TextUtils.isEmpty(defaultIme) && !map.containsKey(defaultIme)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001543 if (chooseNewDefaultIMELocked()) {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001544 updateFromSettingsLocked();
1545 }
1546 }
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
satokab751aa2010-09-14 19:17:36 +09001551 private void showInputMethodMenu() {
1552 showInputMethodMenuInternal(false);
1553 }
1554
1555 private void showInputMethodSubtypeMenu() {
1556 showInputMethodMenuInternal(true);
1557 }
1558
1559 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001560 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001561
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001562 final Context context = mContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001563
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001564 final PackageManager pm = context.getPackageManager();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001565
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001566 String lastInputMethodId = Settings.Secure.getString(context
1567 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satokab751aa2010-09-14 19:17:36 +09001568 int lastInputMethodSubtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08001569 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001571 final List<InputMethodInfo> immis = getEnabledInputMethodList();
satokab751aa2010-09-14 19:17:36 +09001572 ArrayList<Integer> subtypeIds = new ArrayList<Integer>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001573
Dianne Hackborn97106ab2010-03-03 00:08:31 -08001574 if (immis == null) {
1575 return;
1576 }
satokab751aa2010-09-14 19:17:36 +09001577
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001578 synchronized (mMethodMap) {
1579 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001580
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001581 int N = immis.size();
satok913a8922010-08-26 21:53:41 +09001582
satokab751aa2010-09-14 19:17:36 +09001583 final Map<CharSequence, Pair<InputMethodInfo, Integer>> imMap =
1584 new TreeMap<CharSequence, Pair<InputMethodInfo, Integer>>(Collator.getInstance());
satok913a8922010-08-26 21:53:41 +09001585
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001586 for (int i = 0; i < N; ++i) {
1587 InputMethodInfo property = immis.get(i);
1588 if (property == null) {
1589 continue;
1590 }
satokab751aa2010-09-14 19:17:36 +09001591 // TODO: Show only enabled subtypes
1592 ArrayList<InputMethodSubtype> subtypes = property.getSubtypes();
1593 CharSequence label = property.loadLabel(pm);
1594 if (showSubtypes && subtypes.size() > 0) {
1595 for (int j = 0; j < subtypes.size(); ++j) {
1596 InputMethodSubtype subtype = subtypes.get(j);
1597 CharSequence title;
1598 int nameResId = subtype.getNameResId();
1599 int modeResId = subtype.getModeResId();
1600 if (nameResId != 0) {
1601 title = pm.getText(property.getPackageName(), nameResId,
1602 property.getServiceInfo().applicationInfo);
1603 } else {
1604 CharSequence language = subtype.getLocale();
1605 CharSequence mode = modeResId == 0 ? null
1606 : pm.getText(property.getPackageName(), modeResId,
1607 property.getServiceInfo().applicationInfo);
1608 // TODO: Use more friendly Title and UI
1609 title = label + "," + (mode == null ? "" : mode) + ","
1610 + (language == null ? "" : language);
1611 }
1612 imMap.put(title, new Pair<InputMethodInfo, Integer>(property, j));
1613 }
1614 } else {
1615 imMap.put(label,
1616 new Pair<InputMethodInfo, Integer>(property, NOT_A_SUBTYPE_ID));
1617 subtypeIds.add(0);
1618 }
Dianne Hackborn97106ab2010-03-03 00:08:31 -08001619 }
satok913a8922010-08-26 21:53:41 +09001620
1621 N = imMap.size();
1622 mItems = imMap.keySet().toArray(new CharSequence[N]);
satokab751aa2010-09-14 19:17:36 +09001623 mIms = new InputMethodInfo[N];
1624 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001625 int checkedItem = 0;
1626 for (int i = 0; i < N; ++i) {
satokab751aa2010-09-14 19:17:36 +09001627 Pair<InputMethodInfo, Integer> value = imMap.get(mItems[i]);
1628 mIms[i] = value.first;
1629 mSubtypeIds[i] = value.second;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001630 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09001631 int subtypeId = mSubtypeIds[i];
1632 if ((subtypeId == NOT_A_SUBTYPE_ID)
1633 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
1634 || (subtypeId == lastInputMethodSubtypeId)) {
1635 checkedItem = i;
1636 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001637 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001638 }
satokab751aa2010-09-14 19:17:36 +09001639
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001640 AlertDialog.OnClickListener adocl = new AlertDialog.OnClickListener() {
1641 public void onClick(DialogInterface dialog, int which) {
1642 hideInputMethodMenu();
1643 }
1644 };
1645
1646 TypedArray a = context.obtainStyledAttributes(null,
1647 com.android.internal.R.styleable.DialogPreference,
1648 com.android.internal.R.attr.alertDialogStyle, 0);
1649 mDialogBuilder = new AlertDialog.Builder(context)
1650 .setTitle(com.android.internal.R.string.select_input_method)
1651 .setOnCancelListener(new OnCancelListener() {
1652 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001653 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001654 }
1655 })
1656 .setIcon(a.getDrawable(
1657 com.android.internal.R.styleable.DialogPreference_dialogTitle));
1658 a.recycle();
1659
1660 mDialogBuilder.setSingleChoiceItems(mItems, checkedItem,
1661 new AlertDialog.OnClickListener() {
1662 public void onClick(DialogInterface dialog, int which) {
1663 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09001664 if (mIms == null || mIms.length <= which
1665 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001666 return;
1667 }
1668 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09001669 int subtypeId = mSubtypeIds[which];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001670 hideInputMethodMenu();
1671 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09001672 if ((subtypeId < 0)
1673 || (subtypeId >= im.getSubtypes().size())) {
1674 subtypeId = NOT_A_SUBTYPE_ID;
1675 }
1676 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001677 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08001678 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001679 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001680 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682 mSwitchingDialog = mDialogBuilder.create();
1683 mSwitchingDialog.getWindow().setType(
1684 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
1685 mSwitchingDialog.show();
1686 }
1687 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001688
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001689 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07001690 synchronized (mMethodMap) {
1691 hideInputMethodMenuLocked();
1692 }
1693 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001694
The Android Open Source Project10592532009-03-18 17:39:46 -07001695 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001696 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001697
The Android Open Source Project10592532009-03-18 17:39:46 -07001698 if (mSwitchingDialog != null) {
1699 mSwitchingDialog.dismiss();
1700 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001701 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001702
The Android Open Source Project10592532009-03-18 17:39:46 -07001703 mDialogBuilder = null;
1704 mItems = null;
1705 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001706 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001707
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001708 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001709
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001710 public boolean setInputMethodEnabled(String id, boolean enabled) {
1711 synchronized (mMethodMap) {
1712 if (mContext.checkCallingOrSelfPermission(
1713 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1714 != PackageManager.PERMISSION_GRANTED) {
1715 throw new SecurityException(
1716 "Requires permission "
1717 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1718 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001719
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001720 long ident = Binder.clearCallingIdentity();
1721 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001722 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001723 } finally {
1724 Binder.restoreCallingIdentity(ident);
1725 }
1726 }
1727 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001728
1729 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
1730 // Make sure this is a valid input method.
1731 InputMethodInfo imm = mMethodMap.get(id);
1732 if (imm == null) {
1733 if (imm == null) {
1734 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1735 }
1736 }
1737
1738 StringBuilder builder = new StringBuilder(256);
1739
1740 boolean removed = false;
1741 String firstId = null;
1742
1743 // Look through the currently enabled input methods.
1744 String enabledStr = Settings.Secure.getString(mContext.getContentResolver(),
1745 Settings.Secure.ENABLED_INPUT_METHODS);
1746 if (enabledStr != null) {
1747 final TextUtils.SimpleStringSplitter splitter = mStringColonSplitter;
1748 splitter.setString(enabledStr);
1749 while (splitter.hasNext()) {
1750 String curId = splitter.next();
1751 if (curId.equals(id)) {
1752 if (enabled) {
1753 // We are enabling this input method, but it is
1754 // already enabled. Nothing to do. The previous
1755 // state was enabled.
1756 return true;
1757 }
1758 // We are disabling this input method, and it is
1759 // currently enabled. Skip it to remove from the
1760 // new list.
1761 removed = true;
1762 } else if (!enabled) {
1763 // We are building a new list of input methods that
1764 // doesn't contain the given one.
1765 if (firstId == null) firstId = curId;
1766 if (builder.length() > 0) builder.append(':');
1767 builder.append(curId);
1768 }
1769 }
1770 }
1771
1772 if (!enabled) {
1773 if (!removed) {
1774 // We are disabling the input method but it is already
1775 // disabled. Nothing to do. The previous state was
1776 // disabled.
1777 return false;
1778 }
1779 // Update the setting with the new list of input methods.
1780 Settings.Secure.putString(mContext.getContentResolver(),
1781 Settings.Secure.ENABLED_INPUT_METHODS, builder.toString());
1782 // We the disabled input method is currently selected, switch
1783 // to another one.
1784 String selId = Settings.Secure.getString(mContext.getContentResolver(),
1785 Settings.Secure.DEFAULT_INPUT_METHOD);
1786 if (id.equals(selId)) {
1787 Settings.Secure.putString(mContext.getContentResolver(),
1788 Settings.Secure.DEFAULT_INPUT_METHOD,
1789 firstId != null ? firstId : "");
satokab751aa2010-09-14 19:17:36 +09001790 resetSelectedInputMethodSubtype();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001791 }
1792 // Previous state was enabled.
1793 return true;
1794 }
1795
1796 // Add in the newly enabled input method.
1797 if (enabledStr == null || enabledStr.length() == 0) {
1798 enabledStr = id;
1799 } else {
1800 enabledStr = enabledStr + ':' + id;
1801 }
1802
1803 Settings.Secure.putString(mContext.getContentResolver(),
1804 Settings.Secure.ENABLED_INPUT_METHODS, enabledStr);
1805
1806 // Previous state was disabled.
1807 return false;
1808 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001809
satokab751aa2010-09-14 19:17:36 +09001810 private void resetSelectedInputMethodSubtype() {
1811 Settings.Secure.putInt(mContext.getContentResolver(),
1812 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, NOT_A_SUBTYPE_ID);
1813 }
1814
1815 private boolean putSelectedInputMethodSubtype(InputMethodInfo imi, int subtypeId) {
1816 ArrayList<InputMethodSubtype> subtypes = imi.getSubtypes();
1817 if (subtypeId >= 0 && subtypeId < subtypes.size()) {
1818 Settings.Secure.putInt(mContext.getContentResolver(),
1819 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE,
1820 subtypes.get(subtypeId).hashCode());
1821 return true;
1822 } else {
1823 resetSelectedInputMethodSubtype();
1824 return false;
1825 }
1826 }
1827
1828 private int getSelectedInputMethodSubtypeId(String id) {
1829 InputMethodInfo imi = mMethodMap.get(id);
1830 if (imi == null) {
1831 return NOT_A_SUBTYPE_ID;
1832 }
1833 ArrayList<InputMethodSubtype> subtypes = imi.getSubtypes();
1834 int subtypeId;
1835 try {
1836 subtypeId = Settings.Secure.getInt(mContext.getContentResolver(),
1837 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE);
1838 } catch (SettingNotFoundException e) {
1839 return NOT_A_SUBTYPE_ID;
1840 }
1841 for (int i = 0; i < subtypes.size(); ++i) {
1842 InputMethodSubtype ims = subtypes.get(i);
1843 if (subtypeId == ims.hashCode()) {
1844 return i;
1845 }
1846 }
1847 return NOT_A_SUBTYPE_ID;
1848 }
1849
1850 /**
1851 * @return Return the current subtype of this input method.
1852 */
1853 public InputMethodSubtype getCurrentInputMethodSubtype() {
1854 return mCurrentSubtype;
1855 }
1856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001857 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001858
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001859 @Override
1860 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1861 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1862 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001863
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001864 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
1865 + Binder.getCallingPid()
1866 + ", uid=" + Binder.getCallingUid());
1867 return;
1868 }
1869
1870 IInputMethod method;
1871 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001872
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001873 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001874
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001875 synchronized (mMethodMap) {
1876 p.println("Current Input Method Manager state:");
1877 int N = mMethodList.size();
1878 p.println(" Input Methods:");
1879 for (int i=0; i<N; i++) {
1880 InputMethodInfo info = mMethodList.get(i);
1881 p.println(" InputMethod #" + i + ":");
1882 info.dump(p, " ");
1883 }
1884 p.println(" Clients:");
1885 for (ClientState ci : mClients.values()) {
1886 p.println(" Client " + ci + ":");
1887 p.println(" client=" + ci.client);
1888 p.println(" inputContext=" + ci.inputContext);
1889 p.println(" sessionRequested=" + ci.sessionRequested);
1890 p.println(" curSession=" + ci.curSession);
1891 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001892 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001893 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001894 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
1895 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001896 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
1897 + " mBoundToMethod=" + mBoundToMethod);
1898 p.println(" mCurToken=" + mCurToken);
1899 p.println(" mCurIntent=" + mCurIntent);
1900 method = mCurMethod;
1901 p.println(" mCurMethod=" + mCurMethod);
1902 p.println(" mEnabledSession=" + mEnabledSession);
1903 p.println(" mShowRequested=" + mShowRequested
1904 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
1905 + " mShowForced=" + mShowForced
1906 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07001907 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001908 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001909
Jeff Brownb88102f2010-09-08 11:49:43 -07001910 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001911 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001912 pw.flush();
1913 try {
1914 client.client.asBinder().dump(fd, args);
1915 } catch (RemoteException e) {
1916 p.println("Input method client dead: " + e);
1917 }
Jeff Brownb88102f2010-09-08 11:49:43 -07001918 } else {
1919 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001920 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001921
Jeff Brownb88102f2010-09-08 11:49:43 -07001922 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001923 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001924 pw.flush();
1925 try {
1926 method.asBinder().dump(fd, args);
1927 } catch (RemoteException e) {
1928 p.println("Input method service dead: " + e);
1929 }
Jeff Brownb88102f2010-09-08 11:49:43 -07001930 } else {
1931 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001932 }
1933 }
1934}