blob: d035eb586aad1c09f2cb9a86910cce7809a13432 [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;
satok7f35c8c2010-10-07 21:13:11 +090086import java.util.HashSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087import java.util.List;
satok913a8922010-08-26 21:53:41 +090088import java.util.Map;
89import java.util.TreeMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090
91/**
92 * This class provides a system service that manages input methods.
93 */
94public class InputMethodManagerService extends IInputMethodManager.Stub
95 implements ServiceConnection, Handler.Callback {
96 static final boolean DEBUG = false;
97 static final String TAG = "InputManagerService";
98
99 static final int MSG_SHOW_IM_PICKER = 1;
satokab751aa2010-09-14 19:17:36 +0900100 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 2;
satok47a44912010-10-06 16:03:58 +0900101 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 3;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 static final int MSG_UNBIND_INPUT = 1000;
104 static final int MSG_BIND_INPUT = 1010;
105 static final int MSG_SHOW_SOFT_INPUT = 1020;
106 static final int MSG_HIDE_SOFT_INPUT = 1030;
107 static final int MSG_ATTACH_TOKEN = 1040;
108 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800109
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110 static final int MSG_START_INPUT = 2000;
111 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 static final int MSG_UNBIND_METHOD = 3000;
114 static final int MSG_BIND_METHOD = 3010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 static final long TIME_TO_RECONNECT = 10*1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800117
satokab751aa2010-09-14 19:17:36 +0900118 private static final int NOT_A_SUBTYPE_ID = -1;
satok8fbb1e82010-11-02 23:15:58 +0900119 // If IME doesn't support the system locale, the default subtype will be the first defined one.
120 private static final int DEFAULT_SUBTYPE_ID = 0;
satokab751aa2010-09-14 19:17:36 +0900121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122 final Context mContext;
123 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900124 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 final SettingsObserver mSettingsObserver;
Joe Onorato089de882010-04-12 08:18:45 -0700126 final StatusBarManagerService mStatusBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 final IWindowManager mIWindowManager;
128 final HandlerCaller mCaller;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 final InputBindResult mNoBinding = new InputBindResult(null, null, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 // All known input methods. mMethodMap also serves as the global
133 // lock for this class.
satokd87c2592010-09-29 11:52:06 +0900134 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<InputMethodInfo>();
135 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<String, InputMethodInfo>();
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 }
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -0800369 Settings.Secure.putString(mContext.getContentResolver(),
370 Settings.Secure.DEFAULT_INPUT_METHOD, "");
371 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");
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -0800428 Settings.Secure.putString(mContext.getContentResolver(),
429 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
satokd87c2592010-09-29 11:52:06 +0900486 // mSettings should be created before buildInputMethodListLocked
487 mSettings = new InputMethodSettings(context.getContentResolver(), mMethodMap, mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 buildInputMethodListLocked(mMethodList, mMethodMap);
satokd87c2592010-09-29 11:52:06 +0900489 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490
satokd87c2592010-09-29 11:52:06 +0900491 if (TextUtils.isEmpty(Settings.Secure.getString(
492 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 InputMethodInfo defIm = null;
satokd87c2592010-09-29 11:52:06 +0900494 for (InputMethodInfo imi: mMethodList) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 if (defIm == null && imi.getIsDefaultResourceId() != 0) {
496 try {
satokd87c2592010-09-29 11:52:06 +0900497 Resources res = context.createPackageContext(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 imi.getPackageName(), 0).getResources();
499 if (res.getBoolean(imi.getIsDefaultResourceId())) {
500 defIm = imi;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800501 Slog.i(TAG, "Selected default: " + imi.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502 }
503 } catch (PackageManager.NameNotFoundException ex) {
504 } catch (Resources.NotFoundException ex) {
505 }
506 }
507 }
satokd87c2592010-09-29 11:52:06 +0900508 if (defIm == null && mMethodList.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 defIm = mMethodList.get(0);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800510 Slog.i(TAG, "No default found, using " + defIm.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512 if (defIm != null) {
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -0800513 Settings.Secure.putString(mContext.getContentResolver(),
514 Settings.Secure.DEFAULT_INPUT_METHOD, defIm.getId());
515 putSelectedInputMethodSubtype(defIm, NOT_A_SUBTYPE_ID);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 }
517 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800518
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519 mSettingsObserver = new SettingsObserver(mHandler);
520 updateFromSettingsLocked();
521 }
522
523 @Override
524 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
525 throws RemoteException {
526 try {
527 return super.onTransact(code, data, reply, flags);
528 } catch (RuntimeException e) {
529 // The input method manager only throws security exceptions, so let's
530 // log all others.
531 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800532 Slog.e(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 }
534 throw e;
535 }
536 }
537
538 public void systemReady() {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700539 synchronized (mMethodMap) {
540 if (!mSystemReady) {
541 mSystemReady = true;
Dianne Hackborncc278702009-09-02 23:07:23 -0700542 try {
543 startInputInnerLocked();
544 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800545 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700546 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700547 }
548 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800550
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551 public List<InputMethodInfo> getInputMethodList() {
552 synchronized (mMethodMap) {
553 return new ArrayList<InputMethodInfo>(mMethodList);
554 }
555 }
556
557 public List<InputMethodInfo> getEnabledInputMethodList() {
558 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900559 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 }
561 }
562
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 public void addClient(IInputMethodClient client,
564 IInputContext inputContext, int uid, int pid) {
565 synchronized (mMethodMap) {
566 mClients.put(client.asBinder(), new ClientState(client,
567 inputContext, uid, pid));
568 }
569 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 public void removeClient(IInputMethodClient client) {
572 synchronized (mMethodMap) {
573 mClients.remove(client.asBinder());
574 }
575 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800576
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577 void executeOrSendMessage(IInterface target, Message msg) {
578 if (target.asBinder() instanceof Binder) {
579 mCaller.sendMessage(msg);
580 } else {
581 handleMessage(msg);
582 msg.recycle();
583 }
584 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800585
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700586 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800588 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800589 + mCurClient.client.asBinder());
590 if (mBoundToMethod) {
591 mBoundToMethod = false;
592 if (mCurMethod != null) {
593 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
594 MSG_UNBIND_INPUT, mCurMethod));
595 }
596 }
597 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
598 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
599 mCurClient.sessionRequested = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800600
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 // Call setActive(false) on the old client
602 try {
603 mCurClient.client.setActive(false);
604 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800605 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606 + mCurClient.pid + " uid " + mCurClient.uid);
607 }
608 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800609
The Android Open Source Project10592532009-03-18 17:39:46 -0700610 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611 }
612 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800613
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 private int getImeShowFlags() {
615 int flags = 0;
616 if (mShowForced) {
617 flags |= InputMethod.SHOW_FORCED
618 | InputMethod.SHOW_EXPLICIT;
619 } else if (mShowExplicitlyRequested) {
620 flags |= InputMethod.SHOW_EXPLICIT;
621 }
622 return flags;
623 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800624
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800625 private int getAppShowFlags() {
626 int flags = 0;
627 if (mShowForced) {
628 flags |= InputMethodManager.SHOW_FORCED;
629 } else if (!mShowExplicitlyRequested) {
630 flags |= InputMethodManager.SHOW_IMPLICIT;
631 }
632 return flags;
633 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800634
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635 InputBindResult attachNewInputLocked(boolean initial, boolean needResult) {
636 if (!mBoundToMethod) {
637 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
638 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
639 mBoundToMethod = true;
640 }
641 final SessionState session = mCurClient.curSession;
642 if (initial) {
643 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
644 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
645 } else {
646 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
647 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
648 }
649 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800650 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -0800651 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800652 }
653 return needResult
654 ? new InputBindResult(session.session, mCurId, mCurSeq)
655 : null;
656 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800657
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800658 InputBindResult startInputLocked(IInputMethodClient client,
659 IInputContext inputContext, EditorInfo attribute,
660 boolean initial, boolean needResult) {
661 // If no method is currently selected, do nothing.
662 if (mCurMethodId == null) {
663 return mNoBinding;
664 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800665
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800666 ClientState cs = mClients.get(client.asBinder());
667 if (cs == null) {
668 throw new IllegalArgumentException("unknown client "
669 + client.asBinder());
670 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800671
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672 try {
673 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
674 // Check with the window manager to make sure this client actually
675 // has a window with focus. If not, reject. This is thread safe
676 // because if the focus changes some time before or after, the
677 // next client receiving focus that has any interest in input will
678 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800679 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800680 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
681 return null;
682 }
683 } catch (RemoteException e) {
684 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800685
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800686 if (mCurClient != cs) {
687 // If the client is changing, we need to switch over to the new
688 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700689 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800690 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800691 + cs.client.asBinder());
692
693 // If the screen is on, inform the new client it is active
694 if (mScreenOn) {
695 try {
696 cs.client.setActive(mScreenOn);
697 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800698 Slog.w(TAG, "Got RemoteException sending setActive notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800699 + cs.pid + " uid " + cs.uid);
700 }
701 }
702 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800703
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704 // Bump up the sequence for this client and attach it.
705 mCurSeq++;
706 if (mCurSeq <= 0) mCurSeq = 1;
707 mCurClient = cs;
708 mCurInputContext = inputContext;
709 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800710
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 // Check if the input method is changing.
712 if (mCurId != null && mCurId.equals(mCurMethodId)) {
713 if (cs.curSession != null) {
714 // Fast case: if we are already connected to the input method,
715 // then just return it.
716 return attachNewInputLocked(initial, needResult);
717 }
718 if (mHaveConnection) {
719 if (mCurMethod != null) {
720 if (!cs.sessionRequested) {
721 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800722 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800723 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
724 MSG_CREATE_SESSION, mCurMethod,
725 new MethodCallback(mCurMethod)));
726 }
727 // Return to client, and we will get back with it when
728 // we have had a session made for it.
729 return new InputBindResult(null, mCurId, mCurSeq);
730 } else if (SystemClock.uptimeMillis()
731 < (mLastBindTime+TIME_TO_RECONNECT)) {
732 // In this case we have connected to the service, but
733 // don't yet have its interface. If it hasn't been too
734 // long since we did the connection, we'll return to
735 // the client and wait to get the service interface so
736 // we can report back. If it has been too long, we want
737 // to fall through so we can try a disconnect/reconnect
738 // to see if we can get back in touch with the service.
739 return new InputBindResult(null, mCurId, mCurSeq);
740 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800741 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
742 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800743 }
744 }
745 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800746
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700747 return startInputInnerLocked();
748 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800749
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700750 InputBindResult startInputInnerLocked() {
751 if (mCurMethodId == null) {
752 return mNoBinding;
753 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800754
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700755 if (!mSystemReady) {
756 // If the system is not yet ready, we shouldn't be running third
757 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -0700758 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700759 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800760
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800761 InputMethodInfo info = mMethodMap.get(mCurMethodId);
762 if (info == null) {
763 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
764 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800765
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700766 unbindCurrentMethodLocked(false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800767
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800768 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
769 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -0700770 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
771 com.android.internal.R.string.input_method_binding_label);
772 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
773 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800774 if (mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE)) {
775 mLastBindTime = SystemClock.uptimeMillis();
776 mHaveConnection = true;
777 mCurId = info.getId();
778 mCurToken = new Binder();
779 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800780 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800781 mIWindowManager.addWindowToken(mCurToken,
782 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
783 } catch (RemoteException e) {
784 }
785 return new InputBindResult(null, mCurId, mCurSeq);
786 } else {
787 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800788 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800789 + mCurIntent);
790 }
791 return null;
792 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800793
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800794 public InputBindResult startInput(IInputMethodClient client,
795 IInputContext inputContext, EditorInfo attribute,
796 boolean initial, boolean needResult) {
797 synchronized (mMethodMap) {
798 final long ident = Binder.clearCallingIdentity();
799 try {
800 return startInputLocked(client, inputContext, attribute,
801 initial, needResult);
802 } finally {
803 Binder.restoreCallingIdentity(ident);
804 }
805 }
806 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800807
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800808 public void finishInput(IInputMethodClient client) {
809 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800810
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800811 public void onServiceConnected(ComponentName name, IBinder service) {
812 synchronized (mMethodMap) {
813 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
814 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -0700815 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800816 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborncc278702009-09-02 23:07:23 -0700817 unbindCurrentMethodLocked(false);
818 return;
819 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800820 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -0700821 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
822 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800823 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800824 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -0700825 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800826 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -0700827 MSG_CREATE_SESSION, mCurMethod,
828 new MethodCallback(mCurMethod)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800829 }
830 }
831 }
832 }
833
834 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
835 synchronized (mMethodMap) {
836 if (mCurMethod != null && method != null
837 && mCurMethod.asBinder() == method.asBinder()) {
838 if (mCurClient != null) {
839 mCurClient.curSession = new SessionState(mCurClient,
840 method, session);
841 mCurClient.sessionRequested = false;
842 InputBindResult res = attachNewInputLocked(true, true);
843 if (res.method != null) {
844 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
845 MSG_BIND_METHOD, mCurClient.client, res));
846 }
847 }
848 }
849 }
850 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800851
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700852 void unbindCurrentMethodLocked(boolean reportToClient) {
853 if (mHaveConnection) {
854 mContext.unbindService(this);
855 mHaveConnection = false;
856 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800857
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700858 if (mCurToken != null) {
859 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800860 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700861 mIWindowManager.removeWindowToken(mCurToken);
862 } catch (RemoteException e) {
863 }
864 mCurToken = null;
865 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800866
The Android Open Source Project10592532009-03-18 17:39:46 -0700867 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700868 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800869
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700870 if (reportToClient && mCurClient != null) {
871 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
872 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
873 }
874 }
875
Devin Taylor0c33ed22010-02-23 13:26:46 -0600876 private void finishSession(SessionState sessionState) {
877 if (sessionState != null && sessionState.session != null) {
878 try {
879 sessionState.session.finishSession();
880 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -0700881 Slog.w(TAG, "Session failed to close due to remote exception", e);
Devin Taylor0c33ed22010-02-23 13:26:46 -0600882 }
883 }
884 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800885
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700886 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800887 if (mCurMethod != null) {
888 for (ClientState cs : mClients.values()) {
889 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -0600890 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800891 cs.curSession = null;
892 }
Devin Taylor0c33ed22010-02-23 13:26:46 -0600893
894 finishSession(mEnabledSession);
895 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800896 mCurMethod = null;
897 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700898 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800899 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800900
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800901 public void onServiceDisconnected(ComponentName name) {
902 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800903 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800904 + " mCurIntent=" + mCurIntent);
905 if (mCurMethod != null && mCurIntent != null
906 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700907 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800908 // We consider this to be a new bind attempt, since the system
909 // should now try to restart the service for us.
910 mLastBindTime = SystemClock.uptimeMillis();
911 mShowRequested = mInputShown;
912 mInputShown = false;
913 if (mCurClient != null) {
914 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
915 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
916 }
917 }
918 }
919 }
920
921 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700922 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923 long ident = Binder.clearCallingIdentity();
924 try {
925 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700926 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800927 return;
928 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800929
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800930 synchronized (mMethodMap) {
931 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800932 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -0700933 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800934 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800935 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -0700936 mStatusBar.setIcon("ime", packageName, iconId, 0);
937 mStatusBar.setIconVisibility("ime", true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800938 }
939 }
940 } finally {
941 Binder.restoreCallingIdentity(ident);
942 }
943 }
944
satok06487a52010-10-29 11:37:18 +0900945 public void setIMEButtonVisible(IBinder token, boolean visible) {
946 int uid = Binder.getCallingUid();
947 long ident = Binder.clearCallingIdentity();
948 try {
949 if (token == null || mCurToken != token) {
950 Slog.w(TAG, "Ignoring setIMEButtonVisible of uid " + uid + " token: " + token);
951 return;
952 }
953
954 synchronized (mMethodMap) {
955 mStatusBar.setIMEButtonVisible(visible);
956 }
957 } finally {
958 Binder.restoreCallingIdentity(ident);
959 }
960 }
961
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800962 void updateFromSettingsLocked() {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700963 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
964 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
965 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
966 // enabled.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800967 String id = Settings.Secure.getString(mContext.getContentResolver(),
satokab751aa2010-09-14 19:17:36 +0900968 Settings.Secure.DEFAULT_INPUT_METHOD);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700969 if (id != null && id.length() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800970 try {
satokab751aa2010-09-14 19:17:36 +0900971 setInputMethodLocked(id, getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800972 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800973 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -0700974 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700975 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800976 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700977 } else {
978 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -0700979 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700980 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800981 }
982 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800983
satokab751aa2010-09-14 19:17:36 +0900984 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800985 InputMethodInfo info = mMethodMap.get(id);
986 if (info == null) {
satok913a8922010-08-26 21:53:41 +0900987 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800989
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990 if (id.equals(mCurMethodId)) {
satokab751aa2010-09-14 19:17:36 +0900991 if (subtypeId != NOT_A_SUBTYPE_ID) {
992 InputMethodSubtype subtype = info.getSubtypes().get(subtypeId);
993 if (subtype != mCurrentSubtype) {
994 synchronized (mMethodMap) {
995 if (mCurMethod != null) {
996 try {
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -0800997 putSelectedInputMethodSubtype(info, subtypeId);
satok06e07442010-11-02 19:46:55 +0900998 if (mInputShown) {
999 // If mInputShown is false, there is no IME button on the
1000 // system bar.
1001 // Thus there is no need to make it invisible explicitly.
1002 mStatusBar.setIMEButtonVisible(true);
1003 }
satokab751aa2010-09-14 19:17:36 +09001004 mCurMethod.changeInputMethodSubtype(subtype);
1005 } catch (RemoteException e) {
1006 return;
1007 }
1008 }
1009 }
1010 }
1011 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012 return;
1013 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001014
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015 final long ident = Binder.clearCallingIdentity();
1016 try {
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08001017 mCurMethodId = id;
satokab751aa2010-09-14 19:17:36 +09001018 // Set a subtype to this input method.
1019 // subtypeId the name of a subtype which will be set.
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08001020 putSelectedInputMethodSubtype(info, subtypeId);
1021
1022 Settings.Secure.putString(mContext.getContentResolver(),
1023 Settings.Secure.DEFAULT_INPUT_METHOD, id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024
1025 if (ActivityManagerNative.isSystemReady()) {
1026 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001027 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001028 intent.putExtra("input_method_id", id);
1029 mContext.sendBroadcast(intent);
1030 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001031 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 } finally {
1033 Binder.restoreCallingIdentity(ident);
1034 }
1035 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001036
The Android Open Source Project4df24232009-03-05 14:34:35 -08001037 public boolean showSoftInput(IInputMethodClient client, int flags,
1038 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001039 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040 long ident = Binder.clearCallingIdentity();
1041 try {
1042 synchronized (mMethodMap) {
1043 if (mCurClient == null || client == null
1044 || mCurClient.client.asBinder() != client.asBinder()) {
1045 try {
1046 // We need to check if this is the current client with
1047 // focus in the window manager, to allow this call to
1048 // be made before input is started in it.
1049 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001050 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001051 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052 }
1053 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001054 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055 }
1056 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001057
Joe Onorato8a9b2202010-02-26 18:56:32 -08001058 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001059 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 }
1061 } finally {
1062 Binder.restoreCallingIdentity(ident);
1063 }
1064 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001065
The Android Open Source Project4df24232009-03-05 14:34:35 -08001066 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001067 mShowRequested = true;
1068 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1069 mShowExplicitlyRequested = true;
1070 }
1071 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1072 mShowExplicitlyRequested = true;
1073 mShowForced = true;
1074 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001075
Dianne Hackborncc278702009-09-02 23:07:23 -07001076 if (!mSystemReady) {
1077 return false;
1078 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001079
The Android Open Source Project4df24232009-03-05 14:34:35 -08001080 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001081 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001082 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1083 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1084 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001085 mInputShown = true;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001086 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001087 } else if (mHaveConnection && SystemClock.uptimeMillis()
1088 < (mLastBindTime+TIME_TO_RECONNECT)) {
1089 // The client has asked to have the input method shown, but
1090 // we have been sitting here too long with a connection to the
1091 // service and no interface received, so let's disconnect/connect
1092 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001093 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094 SystemClock.uptimeMillis()-mLastBindTime,1);
1095 mContext.unbindService(this);
1096 mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE);
1097 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001098
The Android Open Source Project4df24232009-03-05 14:34:35 -08001099 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001100 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001101
The Android Open Source Project4df24232009-03-05 14:34:35 -08001102 public boolean hideSoftInput(IInputMethodClient client, int flags,
1103 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001104 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 long ident = Binder.clearCallingIdentity();
1106 try {
1107 synchronized (mMethodMap) {
1108 if (mCurClient == null || client == null
1109 || mCurClient.client.asBinder() != client.asBinder()) {
1110 try {
1111 // We need to check if this is the current client with
1112 // focus in the window manager, to allow this call to
1113 // be made before input is started in it.
1114 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001115 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1116 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001117 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001118 }
1119 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001120 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001121 }
1122 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001123
Joe Onorato8a9b2202010-02-26 18:56:32 -08001124 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001125 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001126 }
1127 } finally {
1128 Binder.restoreCallingIdentity(ident);
1129 }
1130 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001131
The Android Open Source Project4df24232009-03-05 14:34:35 -08001132 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001133 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1134 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001135 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001136 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001137 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001138 }
1139 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001140 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001142 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001144 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001145 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001146 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1147 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1148 res = true;
1149 } else {
1150 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 }
1152 mInputShown = false;
1153 mShowRequested = false;
1154 mShowExplicitlyRequested = false;
1155 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001156 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001157 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001158
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001159 public void windowGainedFocus(IInputMethodClient client, IBinder windowToken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160 boolean viewHasFocus, boolean isTextEditor, int softInputMode,
1161 boolean first, int windowFlags) {
1162 long ident = Binder.clearCallingIdentity();
1163 try {
1164 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001165 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001166 + " viewHasFocus=" + viewHasFocus
1167 + " isTextEditor=" + isTextEditor
1168 + " softInputMode=#" + Integer.toHexString(softInputMode)
1169 + " first=" + first + " flags=#"
1170 + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001172 if (mCurClient == null || client == null
1173 || mCurClient.client.asBinder() != client.asBinder()) {
1174 try {
1175 // We need to check if this is the current client with
1176 // focus in the window manager, to allow this call to
1177 // be made before input is started in it.
1178 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001179 Slog.w(TAG, "Client not active, ignoring focus gain of: " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 return;
1181 }
1182 } catch (RemoteException e) {
1183 }
1184 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001185
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001186 if (mCurFocusedWindow == windowToken) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001187 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001188 return;
1189 }
1190 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1193 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
1194 if (!isTextEditor || (softInputMode &
1195 WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1196 != WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE) {
1197 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1198 // There is no focus view, and this window will
1199 // be behind any soft input window, so hide the
1200 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001201 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001202 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 }
1204 } else if (isTextEditor && (softInputMode &
1205 WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1206 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1207 && (softInputMode &
1208 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
1209 // There is a focus view, and we are navigating forward
1210 // into the window, so show the input window for the user.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001211 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001212 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 }
1214 break;
1215 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1216 // Do nothing.
1217 break;
1218 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1219 if ((softInputMode &
1220 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001221 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001222 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 }
1224 break;
1225 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001226 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001227 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 break;
1229 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1230 if ((softInputMode &
1231 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001232 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001233 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001234 }
1235 break;
1236 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001237 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001238 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001239 break;
1240 }
1241 }
1242 } finally {
1243 Binder.restoreCallingIdentity(ident);
1244 }
1245 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001247 public void showInputMethodPickerFromClient(IInputMethodClient client) {
1248 synchronized (mMethodMap) {
1249 if (mCurClient == null || client == null
1250 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001251 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001252 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001253 }
1254
1255 mHandler.sendEmptyMessage(MSG_SHOW_IM_PICKER);
1256 }
1257 }
1258
satokab751aa2010-09-14 19:17:36 +09001259 public void showInputMethodSubtypePickerFromClient(IInputMethodClient client) {
1260 synchronized (mMethodMap) {
1261 if (mCurClient == null || client == null
1262 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001263 Slog.w(TAG, "Ignoring showInputMethodSubtypePickerFromClient of: " + client);
satokab751aa2010-09-14 19:17:36 +09001264 }
1265
1266 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1267 }
1268 }
1269
satok47a44912010-10-06 16:03:58 +09001270 public void showInputMethodAndSubtypeEnablerFromClient(
1271 IInputMethodClient client, String topId) {
1272 // TODO: Handle topId for setting the top position of the list activity
1273 synchronized (mMethodMap) {
1274 if (mCurClient == null || client == null
1275 || mCurClient.client.asBinder() != client.asBinder()) {
1276 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
1277 }
1278
1279 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_ENABLER);
1280 }
1281 }
1282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001283 public void setInputMethod(IBinder token, String id) {
satokab751aa2010-09-14 19:17:36 +09001284 setInputMethodWithSubtype(token, id, NOT_A_SUBTYPE_ID);
1285 }
1286
1287 private void setInputMethodWithSubtype(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288 synchronized (mMethodMap) {
1289 if (token == null) {
1290 if (mContext.checkCallingOrSelfPermission(
1291 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1292 != PackageManager.PERMISSION_GRANTED) {
1293 throw new SecurityException(
1294 "Using null token requires permission "
1295 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1296 }
1297 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001298 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
1299 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300 return;
1301 }
1302
1303 long ident = Binder.clearCallingIdentity();
1304 try {
satokab751aa2010-09-14 19:17:36 +09001305 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001306 } finally {
1307 Binder.restoreCallingIdentity(ident);
1308 }
1309 }
1310 }
1311
1312 public void hideMySoftInput(IBinder token, int flags) {
1313 synchronized (mMethodMap) {
1314 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001315 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
1316 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001317 return;
1318 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001319 long ident = Binder.clearCallingIdentity();
1320 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001321 hideCurrentInputLocked(flags, null);
1322 } finally {
1323 Binder.restoreCallingIdentity(ident);
1324 }
1325 }
1326 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001327
The Android Open Source Project4df24232009-03-05 14:34:35 -08001328 public void showMySoftInput(IBinder token, int flags) {
1329 synchronized (mMethodMap) {
1330 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001331 Slog.w(TAG, "Ignoring showMySoftInput of uid "
1332 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001333 return;
1334 }
1335 long ident = Binder.clearCallingIdentity();
1336 try {
1337 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001338 } finally {
1339 Binder.restoreCallingIdentity(ident);
1340 }
1341 }
1342 }
1343
1344 void setEnabledSessionInMainThread(SessionState session) {
1345 if (mEnabledSession != session) {
1346 if (mEnabledSession != null) {
1347 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001348 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001349 mEnabledSession.method.setSessionEnabled(
1350 mEnabledSession.session, false);
1351 } catch (RemoteException e) {
1352 }
1353 }
1354 mEnabledSession = session;
1355 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001356 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001357 session.method.setSessionEnabled(
1358 session.session, true);
1359 } catch (RemoteException e) {
1360 }
1361 }
1362 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001364 public boolean handleMessage(Message msg) {
1365 HandlerCaller.SomeArgs args;
1366 switch (msg.what) {
1367 case MSG_SHOW_IM_PICKER:
1368 showInputMethodMenu();
1369 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001370
satokab751aa2010-09-14 19:17:36 +09001371 case MSG_SHOW_IM_SUBTYPE_PICKER:
1372 showInputMethodSubtypeMenu();
1373 return true;
1374
satok47a44912010-10-06 16:03:58 +09001375 case MSG_SHOW_IM_SUBTYPE_ENABLER:
1376 showInputMethodAndSubtypeEnabler();
1377 return true;
1378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001379 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001380
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001381 case MSG_UNBIND_INPUT:
1382 try {
1383 ((IInputMethod)msg.obj).unbindInput();
1384 } catch (RemoteException e) {
1385 // There is nothing interesting about the method dying.
1386 }
1387 return true;
1388 case MSG_BIND_INPUT:
1389 args = (HandlerCaller.SomeArgs)msg.obj;
1390 try {
1391 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
1392 } catch (RemoteException e) {
1393 }
1394 return true;
1395 case MSG_SHOW_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001396 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001397 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001398 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
1399 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001400 } catch (RemoteException e) {
1401 }
1402 return true;
1403 case MSG_HIDE_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001404 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001405 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001406 ((IInputMethod)args.arg1).hideSoftInput(0,
1407 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001408 } catch (RemoteException e) {
1409 }
1410 return true;
1411 case MSG_ATTACH_TOKEN:
1412 args = (HandlerCaller.SomeArgs)msg.obj;
1413 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001414 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001415 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
1416 } catch (RemoteException e) {
1417 }
1418 return true;
1419 case MSG_CREATE_SESSION:
1420 args = (HandlerCaller.SomeArgs)msg.obj;
1421 try {
1422 ((IInputMethod)args.arg1).createSession(
1423 (IInputMethodCallback)args.arg2);
1424 } catch (RemoteException e) {
1425 }
1426 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001427 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001429 case MSG_START_INPUT:
1430 args = (HandlerCaller.SomeArgs)msg.obj;
1431 try {
1432 SessionState session = (SessionState)args.arg1;
1433 setEnabledSessionInMainThread(session);
1434 session.method.startInput((IInputContext)args.arg2,
1435 (EditorInfo)args.arg3);
1436 } catch (RemoteException e) {
1437 }
1438 return true;
1439 case MSG_RESTART_INPUT:
1440 args = (HandlerCaller.SomeArgs)msg.obj;
1441 try {
1442 SessionState session = (SessionState)args.arg1;
1443 setEnabledSessionInMainThread(session);
1444 session.method.restartInput((IInputContext)args.arg2,
1445 (EditorInfo)args.arg3);
1446 } catch (RemoteException e) {
1447 }
1448 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001450 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001452 case MSG_UNBIND_METHOD:
1453 try {
1454 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
1455 } catch (RemoteException e) {
1456 // There is nothing interesting about the last client dying.
1457 }
1458 return true;
1459 case MSG_BIND_METHOD:
1460 args = (HandlerCaller.SomeArgs)msg.obj;
1461 try {
1462 ((IInputMethodClient)args.arg1).onBindMethod(
1463 (InputBindResult)args.arg2);
1464 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001465 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001466 }
1467 return true;
1468 }
1469 return false;
1470 }
1471
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001472 private boolean isSystemIme(InputMethodInfo inputMethod) {
1473 return (inputMethod.getServiceInfo().applicationInfo.flags
1474 & ApplicationInfo.FLAG_SYSTEM) != 0;
1475 }
1476
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001477 private boolean chooseNewDefaultIMELocked() {
satokd87c2592010-09-29 11:52:06 +09001478 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001479 if (enabled != null && enabled.size() > 0) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07001480 // We'd prefer to fall back on a system IME, since that is safer.
1481 int i=enabled.size();
1482 while (i > 0) {
1483 i--;
1484 if ((enabled.get(i).getServiceInfo().applicationInfo.flags
1485 & ApplicationInfo.FLAG_SYSTEM) != 0) {
1486 break;
1487 }
1488 }
satokab751aa2010-09-14 19:17:36 +09001489 InputMethodInfo imi = enabled.get(i);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08001490 Settings.Secure.putString(mContext.getContentResolver(),
1491 Settings.Secure.DEFAULT_INPUT_METHOD, imi.getId());
1492 putSelectedInputMethodSubtype(imi, NOT_A_SUBTYPE_ID);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001493 return true;
1494 }
1495
1496 return false;
1497 }
1498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001499 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
1500 HashMap<String, InputMethodInfo> map) {
1501 list.clear();
1502 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001504 PackageManager pm = mContext.getPackageManager();
Amith Yamasanie861ec12010-03-24 21:39:27 -07001505 final Configuration config = mContext.getResources().getConfiguration();
1506 final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY;
1507 String disabledSysImes = Settings.Secure.getString(mContext.getContentResolver(),
1508 Secure.DISABLED_SYSTEM_INPUT_METHODS);
1509 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001510
1511 List<ResolveInfo> services = pm.queryIntentServices(
1512 new Intent(InputMethod.SERVICE_INTERFACE),
1513 PackageManager.GET_META_DATA);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001514
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001515 for (int i = 0; i < services.size(); ++i) {
1516 ResolveInfo ri = services.get(i);
1517 ServiceInfo si = ri.serviceInfo;
1518 ComponentName compName = new ComponentName(si.packageName, si.name);
1519 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
1520 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001521 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001522 + ": it does not require the permission "
1523 + android.Manifest.permission.BIND_INPUT_METHOD);
1524 continue;
1525 }
1526
Joe Onorato8a9b2202010-02-26 18:56:32 -08001527 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528
1529 try {
1530 InputMethodInfo p = new InputMethodInfo(mContext, ri);
1531 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07001532 final String id = p.getId();
1533 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001534
Amith Yamasanie861ec12010-03-24 21:39:27 -07001535 // System IMEs are enabled by default, unless there's a hard keyboard
1536 // and the system IME was explicitly disabled
1537 if (isSystemIme(p) && (!haveHardKeyboard || disabledSysImes.indexOf(id) < 0)) {
1538 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001539 }
1540
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001541 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001542 Slog.d(TAG, "Found a third-party input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001543 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001544
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001545 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001546 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001547 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001548 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001549 }
1550 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001551
1552 String defaultIme = Settings.Secure.getString(mContext
1553 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok913a8922010-08-26 21:53:41 +09001554 if (!TextUtils.isEmpty(defaultIme) && !map.containsKey(defaultIme)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001555 if (chooseNewDefaultIMELocked()) {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001556 updateFromSettingsLocked();
1557 }
1558 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001559 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001560
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001561 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001562
satokab751aa2010-09-14 19:17:36 +09001563 private void showInputMethodMenu() {
1564 showInputMethodMenuInternal(false);
1565 }
1566
1567 private void showInputMethodSubtypeMenu() {
1568 showInputMethodMenuInternal(true);
1569 }
1570
satok47a44912010-10-06 16:03:58 +09001571 private void showInputMethodAndSubtypeEnabler() {
satok86417ea2010-10-27 14:11:03 +09001572 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_AND_SUBTYPE_ENABLER);
satok47a44912010-10-06 16:03:58 +09001573 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09001574 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
1575 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok47a44912010-10-06 16:03:58 +09001576 mContext.startActivity(intent);
1577 }
1578
satokab751aa2010-09-14 19:17:36 +09001579 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001580 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001581
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001582 final Context context = mContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001584 final PackageManager pm = context.getPackageManager();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001585
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001586 String lastInputMethodId = Settings.Secure.getString(context
1587 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satokab751aa2010-09-14 19:17:36 +09001588 int lastInputMethodSubtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08001589 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001590
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001591 synchronized (mMethodMap) {
satok7f35c8c2010-10-07 21:13:11 +09001592 final List<Pair<InputMethodInfo, ArrayList<String>>> immis =
1593 mSettings.getEnabledInputMethodAndSubtypeListLocked();
1594 ArrayList<Integer> subtypeIds = new ArrayList<Integer>();
1595
1596 if (immis == null || immis.size() == 0) {
1597 return;
1598 }
1599
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001600 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001601
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001602 int N = immis.size();
satok913a8922010-08-26 21:53:41 +09001603
satokab751aa2010-09-14 19:17:36 +09001604 final Map<CharSequence, Pair<InputMethodInfo, Integer>> imMap =
1605 new TreeMap<CharSequence, Pair<InputMethodInfo, Integer>>(Collator.getInstance());
satok913a8922010-08-26 21:53:41 +09001606
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001607 for (int i = 0; i < N; ++i) {
satok7f35c8c2010-10-07 21:13:11 +09001608 InputMethodInfo property = immis.get(i).first;
1609 final ArrayList<String> enabledSubtypeIds = immis.get(i).second;
1610 HashSet<String> enabledSubtypeSet = new HashSet<String>();
1611 for (String s : enabledSubtypeIds) {
1612 enabledSubtypeSet.add(s);
1613 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001614 if (property == null) {
1615 continue;
1616 }
satokab751aa2010-09-14 19:17:36 +09001617 ArrayList<InputMethodSubtype> subtypes = property.getSubtypes();
1618 CharSequence label = property.loadLabel(pm);
satok7f35c8c2010-10-07 21:13:11 +09001619 if (showSubtypes && enabledSubtypeSet.size() > 0) {
satokab751aa2010-09-14 19:17:36 +09001620 for (int j = 0; j < subtypes.size(); ++j) {
1621 InputMethodSubtype subtype = subtypes.get(j);
satok7f35c8c2010-10-07 21:13:11 +09001622 if (enabledSubtypeSet.contains(String.valueOf(subtype.hashCode()))) {
1623 CharSequence title;
1624 int nameResId = subtype.getNameResId();
satok9ef02832010-11-04 21:17:48 +09001625 String mode = subtype.getMode();
satok7f35c8c2010-10-07 21:13:11 +09001626 if (nameResId != 0) {
1627 title = pm.getText(property.getPackageName(), nameResId,
1628 property.getServiceInfo().applicationInfo);
1629 } else {
1630 CharSequence language = subtype.getLocale();
satok7f35c8c2010-10-07 21:13:11 +09001631 // TODO: Use more friendly Title and UI
1632 title = label + "," + (mode == null ? "" : mode) + ","
1633 + (language == null ? "" : language);
1634 }
1635 imMap.put(title, new Pair<InputMethodInfo, Integer>(property, j));
satokab751aa2010-09-14 19:17:36 +09001636 }
satokab751aa2010-09-14 19:17:36 +09001637 }
1638 } else {
1639 imMap.put(label,
1640 new Pair<InputMethodInfo, Integer>(property, NOT_A_SUBTYPE_ID));
1641 subtypeIds.add(0);
1642 }
Dianne Hackborn97106ab2010-03-03 00:08:31 -08001643 }
satok913a8922010-08-26 21:53:41 +09001644
1645 N = imMap.size();
1646 mItems = imMap.keySet().toArray(new CharSequence[N]);
satokab751aa2010-09-14 19:17:36 +09001647 mIms = new InputMethodInfo[N];
1648 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001649 int checkedItem = 0;
1650 for (int i = 0; i < N; ++i) {
satokab751aa2010-09-14 19:17:36 +09001651 Pair<InputMethodInfo, Integer> value = imMap.get(mItems[i]);
1652 mIms[i] = value.first;
1653 mSubtypeIds[i] = value.second;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001654 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09001655 int subtypeId = mSubtypeIds[i];
1656 if ((subtypeId == NOT_A_SUBTYPE_ID)
1657 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
1658 || (subtypeId == lastInputMethodSubtypeId)) {
1659 checkedItem = i;
1660 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001661 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001662 }
satokab751aa2010-09-14 19:17:36 +09001663
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001664 AlertDialog.OnClickListener adocl = new AlertDialog.OnClickListener() {
1665 public void onClick(DialogInterface dialog, int which) {
1666 hideInputMethodMenu();
1667 }
1668 };
satokd87c2592010-09-29 11:52:06 +09001669
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001670 TypedArray a = context.obtainStyledAttributes(null,
1671 com.android.internal.R.styleable.DialogPreference,
1672 com.android.internal.R.attr.alertDialogStyle, 0);
1673 mDialogBuilder = new AlertDialog.Builder(context)
1674 .setTitle(com.android.internal.R.string.select_input_method)
1675 .setOnCancelListener(new OnCancelListener() {
1676 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001677 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001678 }
1679 })
1680 .setIcon(a.getDrawable(
1681 com.android.internal.R.styleable.DialogPreference_dialogTitle));
1682 a.recycle();
satokd87c2592010-09-29 11:52:06 +09001683
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001684 mDialogBuilder.setSingleChoiceItems(mItems, checkedItem,
1685 new AlertDialog.OnClickListener() {
1686 public void onClick(DialogInterface dialog, int which) {
1687 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09001688 if (mIms == null || mIms.length <= which
1689 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001690 return;
1691 }
1692 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09001693 int subtypeId = mSubtypeIds[which];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001694 hideInputMethodMenu();
1695 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09001696 if ((subtypeId < 0)
1697 || (subtypeId >= im.getSubtypes().size())) {
1698 subtypeId = NOT_A_SUBTYPE_ID;
1699 }
1700 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001701 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08001702 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001703 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001704 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001705
satok7f35c8c2010-10-07 21:13:11 +09001706 if (showSubtypes) {
1707 mDialogBuilder.setPositiveButton(com.android.internal.R.string.more_item_label,
1708 new DialogInterface.OnClickListener() {
1709 public void onClick(DialogInterface dialog, int whichButton) {
1710 showInputMethodAndSubtypeEnabler();
1711 }
1712 });
1713 }
satok0ff647b2010-10-08 13:49:28 +09001714 mDialogBuilder.setNegativeButton(com.android.internal.R.string.cancel,
1715 new DialogInterface.OnClickListener() {
1716 public void onClick(DialogInterface dialog, int whichButton) {
1717 hideInputMethodMenu();
1718 }
1719 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001720 mSwitchingDialog = mDialogBuilder.create();
1721 mSwitchingDialog.getWindow().setType(
1722 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
1723 mSwitchingDialog.show();
1724 }
1725 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001726
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001727 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07001728 synchronized (mMethodMap) {
1729 hideInputMethodMenuLocked();
1730 }
1731 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001732
The Android Open Source Project10592532009-03-18 17:39:46 -07001733 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001734 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001735
The Android Open Source Project10592532009-03-18 17:39:46 -07001736 if (mSwitchingDialog != null) {
1737 mSwitchingDialog.dismiss();
1738 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001739 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001740
The Android Open Source Project10592532009-03-18 17:39:46 -07001741 mDialogBuilder = null;
1742 mItems = null;
1743 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001744 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001746 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001747
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001748 public boolean setInputMethodEnabled(String id, boolean enabled) {
1749 synchronized (mMethodMap) {
1750 if (mContext.checkCallingOrSelfPermission(
1751 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1752 != PackageManager.PERMISSION_GRANTED) {
1753 throw new SecurityException(
1754 "Requires permission "
1755 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1756 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001757
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001758 long ident = Binder.clearCallingIdentity();
1759 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001760 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001761 } finally {
1762 Binder.restoreCallingIdentity(ident);
1763 }
1764 }
1765 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001766
1767 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
1768 // Make sure this is a valid input method.
1769 InputMethodInfo imm = mMethodMap.get(id);
1770 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09001771 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001772 }
1773
satokd87c2592010-09-29 11:52:06 +09001774 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
1775 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001776
satokd87c2592010-09-29 11:52:06 +09001777 if (enabled) {
1778 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
1779 if (pair.first.equals(id)) {
1780 // We are enabling this input method, but it is already enabled.
1781 // Nothing to do. The previous state was enabled.
1782 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001783 }
1784 }
satokd87c2592010-09-29 11:52:06 +09001785 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
1786 // Previous state was disabled.
1787 return false;
1788 } else {
1789 StringBuilder builder = new StringBuilder();
1790 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
1791 builder, enabledInputMethodsList, id)) {
1792 // Disabled input method is currently selected, switch to another one.
1793 String selId = Settings.Secure.getString(mContext.getContentResolver(),
1794 Settings.Secure.DEFAULT_INPUT_METHOD);
1795 if (id.equals(selId)) {
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08001796 Settings.Secure.putString(
1797 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD,
1798 enabledInputMethodsList.size() > 0
1799 ? enabledInputMethodsList.get(0).first : "");
1800 resetSelectedInputMethodSubtype();
satokd87c2592010-09-29 11:52:06 +09001801 }
1802 // Previous state was enabled.
1803 return true;
1804 } else {
1805 // We are disabling the input method but it is already disabled.
1806 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001807 return false;
1808 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001809 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001810 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001811
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08001812 private void resetSelectedInputMethodSubtype() {
1813 Settings.Secure.putInt(mContext.getContentResolver(),
1814 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, NOT_A_SUBTYPE_ID);
satokab751aa2010-09-14 19:17:36 +09001815 }
1816
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08001817 private void putSelectedInputMethodSubtype(InputMethodInfo imi, int subtypeId) {
1818 final ArrayList<InputMethodSubtype> subtypes = imi.getSubtypes();
1819 if (subtypeId >= 0 && subtypeId < subtypes.size()) {
1820 Settings.Secure.putInt(mContext.getContentResolver(),
1821 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE,
1822 subtypes.get(subtypeId).hashCode());
1823 mCurrentSubtype = subtypes.get(subtypeId);
satok1ab852f2010-11-05 22:56:07 +09001824 } else {
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08001825 resetSelectedInputMethodSubtype();
1826 mCurrentSubtype = null;
satokab751aa2010-09-14 19:17:36 +09001827 }
1828 }
1829
1830 private int getSelectedInputMethodSubtypeId(String id) {
1831 InputMethodInfo imi = mMethodMap.get(id);
1832 if (imi == null) {
1833 return NOT_A_SUBTYPE_ID;
1834 }
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08001835 ArrayList<InputMethodSubtype> subtypes = imi.getSubtypes();
satokab751aa2010-09-14 19:17:36 +09001836 int subtypeId;
1837 try {
1838 subtypeId = Settings.Secure.getInt(mContext.getContentResolver(),
1839 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE);
1840 } catch (SettingNotFoundException e) {
1841 return NOT_A_SUBTYPE_ID;
1842 }
1843 for (int i = 0; i < subtypes.size(); ++i) {
1844 InputMethodSubtype ims = subtypes.get(i);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08001845 if (subtypeId == ims.hashCode()) {
satokab751aa2010-09-14 19:17:36 +09001846 return i;
1847 }
1848 }
1849 return NOT_A_SUBTYPE_ID;
1850 }
1851
satok8fbb1e82010-11-02 23:15:58 +09001852 // If there are no selected subtypes, tries finding the most applicable one according to the
1853 // current system locale
1854 private int findApplicableSubtype(String id) {
1855 InputMethodInfo imi = mMethodMap.get(id);
1856 if (imi == null) {
1857 return NOT_A_SUBTYPE_ID;
1858 }
1859 ArrayList<InputMethodSubtype> subtypes = imi.getSubtypes();
1860 if (subtypes == null || subtypes.size() == 0) {
1861 return NOT_A_SUBTYPE_ID;
1862 }
1863 final String locale = mContext.getResources().getConfiguration().locale.toString();
1864 final String language = locale.substring(0, 2);
1865 boolean partialMatchFound = false;
1866 int applicableSubtypeId = DEFAULT_SUBTYPE_ID;
1867 for (int i = 0; i < subtypes.size(); ++i) {
1868 final String subtypeLocale = subtypes.get(i).getLocale();
satok9ef02832010-11-04 21:17:48 +09001869 // An applicable subtype should be a keyboard subtype
1870 if (subtypes.get(i).getMode().equalsIgnoreCase("keyboard")) {
1871 if (locale.equals(subtypeLocale)) {
1872 // Exact match (e.g. system locale is "en_US" and subtype locale is "en_US")
1873 applicableSubtypeId = i;
1874 break;
1875 } else if (!partialMatchFound && subtypeLocale.startsWith(language)) {
1876 // Partial match (e.g. system locale is "en_US" and subtype locale is "en")
1877 applicableSubtypeId = i;
1878 partialMatchFound = true;
1879 }
satok8fbb1e82010-11-02 23:15:58 +09001880 }
1881 }
1882
1883 // The first subtype applicable to the system locale will be defined as the most applicable
1884 // subtype.
1885 if (DEBUG) {
1886 Slog.d(TAG, "Applicable InputMethodSubtype was found: " + applicableSubtypeId
1887 + subtypes.get(applicableSubtypeId).getLocale());
1888 }
1889 return applicableSubtypeId;
1890 }
1891
satokab751aa2010-09-14 19:17:36 +09001892 /**
1893 * @return Return the current subtype of this input method.
1894 */
1895 public InputMethodSubtype getCurrentInputMethodSubtype() {
satok8fbb1e82010-11-02 23:15:58 +09001896 boolean subtypeIsSelected = false;
1897 try {
1898 subtypeIsSelected = Settings.Secure.getInt(mContext.getContentResolver(),
1899 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE) != NOT_A_SUBTYPE_ID;
1900 } catch (SettingNotFoundException e) {
1901 }
1902 if (!subtypeIsSelected || mCurrentSubtype == null) {
1903 String lastInputMethodId = Settings.Secure.getString(mContext
1904 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
1905 int subtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
1906 if (subtypeId == NOT_A_SUBTYPE_ID) {
1907 subtypeId = findApplicableSubtype(lastInputMethodId);
1908 }
1909 if (subtypeId != NOT_A_SUBTYPE_ID) {
1910 mCurrentSubtype = mMethodMap.get(lastInputMethodId).getSubtypes().get(subtypeId);
1911 }
1912 }
satokab751aa2010-09-14 19:17:36 +09001913 return mCurrentSubtype;
1914 }
1915
satokd87c2592010-09-29 11:52:06 +09001916 /**
1917 * Utility class for putting and getting settings for InputMethod
1918 * TODO: Move all putters and getters of settings to this class.
1919 */
1920 private static class InputMethodSettings {
1921 // The string for enabled input method is saved as follows:
1922 // example: ("ime0;subtype0;subtype1;subtype2:ime1:ime2;subtype0")
1923 private static final char INPUT_METHOD_SEPARATER = ':';
1924 private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';';
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08001925 private final TextUtils.SimpleStringSplitter mStringColonSplitter =
satokd87c2592010-09-29 11:52:06 +09001926 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER);
1927
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08001928 private final TextUtils.SimpleStringSplitter mStringSemiColonSplitter =
satokd87c2592010-09-29 11:52:06 +09001929 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATER);
1930
1931 private final ContentResolver mResolver;
1932 private final HashMap<String, InputMethodInfo> mMethodMap;
1933 private final ArrayList<InputMethodInfo> mMethodList;
1934
1935 private String mEnabledInputMethodsStrCache;
1936
1937 private static void buildEnabledInputMethodsSettingString(
1938 StringBuilder builder, Pair<String, ArrayList<String>> pair) {
1939 String id = pair.first;
1940 ArrayList<String> subtypes = pair.second;
1941 builder.append(id);
satok57c767c2010-11-01 22:34:08 +09001942 // Inputmethod and subtypes are saved in the settings as follows:
1943 // ime0;subtype0;subtype1:ime1;subtype0:ime2:ime3;subtype0;subtype1
1944 for (String subtypeId: subtypes) {
1945 builder.append(INPUT_METHOD_SUBTYPE_SEPARATER).append(subtypeId);
satokd87c2592010-09-29 11:52:06 +09001946 }
1947 }
1948
1949 public InputMethodSettings(
1950 ContentResolver resolver, HashMap<String, InputMethodInfo> methodMap,
1951 ArrayList<InputMethodInfo> methodList) {
1952 mResolver = resolver;
1953 mMethodMap = methodMap;
1954 mMethodList = methodList;
1955 }
1956
1957 public List<InputMethodInfo> getEnabledInputMethodListLocked() {
1958 return createEnabledInputMethodListLocked(
1959 getEnabledInputMethodsAndSubtypeListLocked());
1960 }
1961
satok7f35c8c2010-10-07 21:13:11 +09001962 public List<Pair<InputMethodInfo, ArrayList<String>>>
1963 getEnabledInputMethodAndSubtypeListLocked() {
1964 return createEnabledInputMethodAndSubtypeListLocked(
1965 getEnabledInputMethodsAndSubtypeListLocked());
1966 }
1967
satokd87c2592010-09-29 11:52:06 +09001968 // At the initial boot, the settings for input methods are not set,
1969 // so we need to enable IME in that case.
1970 public void enableAllIMEsIfThereIsNoEnabledIME() {
1971 if (TextUtils.isEmpty(getEnabledInputMethodsStr())) {
1972 StringBuilder sb = new StringBuilder();
1973 final int N = mMethodList.size();
1974 for (int i = 0; i < N; i++) {
1975 InputMethodInfo imi = mMethodList.get(i);
1976 Slog.i(TAG, "Adding: " + imi.getId());
1977 if (i > 0) sb.append(':');
1978 sb.append(imi.getId());
1979 }
1980 putEnabledInputMethodsStr(sb.toString());
1981 }
1982 }
1983
1984 public List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() {
1985 ArrayList<Pair<String, ArrayList<String>>> imsList
1986 = new ArrayList<Pair<String, ArrayList<String>>>();
1987 final String enabledInputMethodsStr = getEnabledInputMethodsStr();
1988 if (TextUtils.isEmpty(enabledInputMethodsStr)) {
1989 return imsList;
1990 }
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08001991 mStringColonSplitter.setString(enabledInputMethodsStr);
1992 while (mStringColonSplitter.hasNext()) {
1993 String nextImsStr = mStringColonSplitter.next();
1994 mStringSemiColonSplitter.setString(nextImsStr);
1995 if (mStringSemiColonSplitter.hasNext()) {
satokd87c2592010-09-29 11:52:06 +09001996 ArrayList<String> subtypeHashes = new ArrayList<String>();
1997 // The first element is ime id.
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08001998 String imeId = mStringSemiColonSplitter.next();
1999 while (mStringSemiColonSplitter.hasNext()) {
2000 subtypeHashes.add(mStringSemiColonSplitter.next());
satokd87c2592010-09-29 11:52:06 +09002001 }
2002 imsList.add(new Pair<String, ArrayList<String>>(imeId, subtypeHashes));
2003 }
2004 }
2005 return imsList;
2006 }
2007
2008 public void appendAndPutEnabledInputMethodLocked(String id, boolean reloadInputMethodStr) {
2009 if (reloadInputMethodStr) {
2010 getEnabledInputMethodsStr();
2011 }
2012 if (TextUtils.isEmpty(mEnabledInputMethodsStrCache)) {
2013 // Add in the newly enabled input method.
2014 putEnabledInputMethodsStr(id);
2015 } else {
2016 putEnabledInputMethodsStr(
2017 mEnabledInputMethodsStrCache + INPUT_METHOD_SEPARATER + id);
2018 }
2019 }
2020
2021 /**
2022 * Build and put a string of EnabledInputMethods with removing specified Id.
2023 * @return the specified id was removed or not.
2024 */
2025 public boolean buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2026 StringBuilder builder, List<Pair<String, ArrayList<String>>> imsList, String id) {
2027 boolean isRemoved = false;
2028 boolean needsAppendSeparator = false;
2029 for (Pair<String, ArrayList<String>> ims: imsList) {
2030 String curId = ims.first;
2031 if (curId.equals(id)) {
2032 // We are disabling this input method, and it is
2033 // currently enabled. Skip it to remove from the
2034 // new list.
2035 isRemoved = true;
2036 } else {
2037 if (needsAppendSeparator) {
2038 builder.append(INPUT_METHOD_SEPARATER);
2039 } else {
2040 needsAppendSeparator = true;
2041 }
2042 buildEnabledInputMethodsSettingString(builder, ims);
2043 }
2044 }
2045 if (isRemoved) {
2046 // Update the setting with the new list of input methods.
2047 putEnabledInputMethodsStr(builder.toString());
2048 }
2049 return isRemoved;
2050 }
2051
2052 private List<InputMethodInfo> createEnabledInputMethodListLocked(
2053 List<Pair<String, ArrayList<String>>> imsList) {
2054 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
2055 for (Pair<String, ArrayList<String>> ims: imsList) {
2056 InputMethodInfo info = mMethodMap.get(ims.first);
2057 if (info != null) {
2058 res.add(info);
2059 }
2060 }
2061 return res;
2062 }
2063
satok7f35c8c2010-10-07 21:13:11 +09002064 private List<Pair<InputMethodInfo, ArrayList<String>>>
2065 createEnabledInputMethodAndSubtypeListLocked(
2066 List<Pair<String, ArrayList<String>>> imsList) {
2067 final ArrayList<Pair<InputMethodInfo, ArrayList<String>>> res
2068 = new ArrayList<Pair<InputMethodInfo, ArrayList<String>>>();
2069 for (Pair<String, ArrayList<String>> ims : imsList) {
2070 InputMethodInfo info = mMethodMap.get(ims.first);
2071 if (info != null) {
2072 res.add(new Pair<InputMethodInfo, ArrayList<String>>(info, ims.second));
2073 }
2074 }
2075 return res;
2076 }
2077
satokd87c2592010-09-29 11:52:06 +09002078 private void putEnabledInputMethodsStr(String str) {
2079 Settings.Secure.putString(mResolver, Settings.Secure.ENABLED_INPUT_METHODS, str);
2080 mEnabledInputMethodsStrCache = str;
2081 }
2082
2083 private String getEnabledInputMethodsStr() {
2084 mEnabledInputMethodsStrCache = Settings.Secure.getString(
2085 mResolver, Settings.Secure.ENABLED_INPUT_METHODS);
2086 return mEnabledInputMethodsStrCache;
2087 }
2088 }
2089
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002090 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002091
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002092 @Override
2093 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2094 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
2095 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002096
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002097 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
2098 + Binder.getCallingPid()
2099 + ", uid=" + Binder.getCallingUid());
2100 return;
2101 }
2102
2103 IInputMethod method;
2104 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002106 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002107
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002108 synchronized (mMethodMap) {
2109 p.println("Current Input Method Manager state:");
2110 int N = mMethodList.size();
2111 p.println(" Input Methods:");
2112 for (int i=0; i<N; i++) {
2113 InputMethodInfo info = mMethodList.get(i);
2114 p.println(" InputMethod #" + i + ":");
2115 info.dump(p, " ");
2116 }
2117 p.println(" Clients:");
2118 for (ClientState ci : mClients.values()) {
2119 p.println(" Client " + ci + ":");
2120 p.println(" client=" + ci.client);
2121 p.println(" inputContext=" + ci.inputContext);
2122 p.println(" sessionRequested=" + ci.sessionRequested);
2123 p.println(" curSession=" + ci.curSession);
2124 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002125 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002126 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002127 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
2128 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002129 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
2130 + " mBoundToMethod=" + mBoundToMethod);
2131 p.println(" mCurToken=" + mCurToken);
2132 p.println(" mCurIntent=" + mCurIntent);
2133 method = mCurMethod;
2134 p.println(" mCurMethod=" + mCurMethod);
2135 p.println(" mEnabledSession=" + mEnabledSession);
2136 p.println(" mShowRequested=" + mShowRequested
2137 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
2138 + " mShowForced=" + mShowForced
2139 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07002140 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002141 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002142
Jeff Brownb88102f2010-09-08 11:49:43 -07002143 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002144 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002145 pw.flush();
2146 try {
2147 client.client.asBinder().dump(fd, args);
2148 } catch (RemoteException e) {
2149 p.println("Input method client dead: " + e);
2150 }
Jeff Brownb88102f2010-09-08 11:49:43 -07002151 } else {
2152 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002153 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002154
Jeff Brownb88102f2010-09-08 11:49:43 -07002155 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002156 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002157 pw.flush();
2158 try {
2159 method.asBinder().dump(fd, args);
2160 } catch (RemoteException e) {
2161 p.println("Input method service dead: " + e);
2162 }
Jeff Brownb88102f2010-09-08 11:49:43 -07002163 } else {
2164 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002165 }
2166 }
2167}