blob: 0d3cfdeca0d7ff44eeb2adf48ec617e1069b137d [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;
satok723a27e2010-11-11 14:58:11 +0900119 private static final String NOT_A_SUBTYPE_ID_STR = String.valueOf(NOT_A_SUBTYPE_ID);
satok8fbb1e82010-11-02 23:15:58 +0900120 // If IME doesn't support the system locale, the default subtype will be the first defined one.
121 private static final int DEFAULT_SUBTYPE_ID = 0;
satokab751aa2010-09-14 19:17:36 +0900122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 final Context mContext;
124 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900125 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126 final SettingsObserver mSettingsObserver;
Joe Onorato089de882010-04-12 08:18:45 -0700127 final StatusBarManagerService mStatusBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 final IWindowManager mIWindowManager;
129 final HandlerCaller mCaller;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 final InputBindResult mNoBinding = new InputBindResult(null, null, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 // All known input methods. mMethodMap also serves as the global
134 // lock for this class.
satokd87c2592010-09-29 11:52:06 +0900135 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<InputMethodInfo>();
136 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<String, InputMethodInfo>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 class SessionState {
139 final ClientState client;
140 final IInputMethod method;
141 final IInputMethodSession session;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 @Override
144 public String toString() {
145 return "SessionState{uid " + client.uid + " pid " + client.pid
146 + " method " + Integer.toHexString(
147 System.identityHashCode(method))
148 + " session " + Integer.toHexString(
149 System.identityHashCode(session))
150 + "}";
151 }
152
153 SessionState(ClientState _client, IInputMethod _method,
154 IInputMethodSession _session) {
155 client = _client;
156 method = _method;
157 session = _session;
158 }
159 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 class ClientState {
162 final IInputMethodClient client;
163 final IInputContext inputContext;
164 final int uid;
165 final int pid;
166 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 boolean sessionRequested;
169 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800170
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 @Override
172 public String toString() {
173 return "ClientState{" + Integer.toHexString(
174 System.identityHashCode(this)) + " uid " + uid
175 + " pid " + pid + "}";
176 }
177
178 ClientState(IInputMethodClient _client, IInputContext _inputContext,
179 int _uid, int _pid) {
180 client = _client;
181 inputContext = _inputContext;
182 uid = _uid;
183 pid = _pid;
184 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
185 }
186 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 final HashMap<IBinder, ClientState> mClients
189 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700192 * Set once the system is ready to run third party code.
193 */
194 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800195
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700196 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 * Id of the currently selected input method.
198 */
199 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 /**
202 * The current binding sequence number, incremented every time there is
203 * a new bind performed.
204 */
205 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 /**
208 * The client that is currently bound to an input method.
209 */
210 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700213 * The last window token that gained focus.
214 */
215 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800216
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700217 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 * The input context last provided by the current client.
219 */
220 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800221
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 /**
223 * The attributes last provided by the current client.
224 */
225 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800226
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 /**
228 * The input method ID of the input method service that we are currently
229 * connected to or in the process of connecting to.
230 */
231 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 /**
satokab751aa2010-09-14 19:17:36 +0900234 * The current subtype of the current input method.
235 */
236 private InputMethodSubtype mCurrentSubtype;
237
238
239 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 * Set to true if our ServiceConnection is currently actively bound to
241 * a service (whether or not we have gotten its IBinder back yet).
242 */
243 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 /**
246 * Set if the client has asked for the input method to be shown.
247 */
248 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 /**
251 * Set if we were explicitly told to show the input method.
252 */
253 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 /**
256 * Set if we were forced to be shown.
257 */
258 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 /**
261 * Set if we last told the input method to show itself.
262 */
263 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800264
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 /**
266 * The Intent used to connect to the current input method.
267 */
268 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 /**
271 * The token we have made for the currently active input method, to
272 * identify it in the future.
273 */
274 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800275
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 /**
277 * If non-null, this is the input method service we are currently connected
278 * to.
279 */
280 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 /**
283 * Time that we last initiated a bind to the input method, to determine
284 * if we should try to disconnect and reconnect to it.
285 */
286 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 /**
289 * Have we called mCurMethod.bindInput()?
290 */
291 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 /**
294 * Currently enabled session. Only touched by service thread, not
295 * protected by a lock.
296 */
297 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 /**
300 * True if the screen is on. The value is true initially.
301 */
302 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800303
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 AlertDialog.Builder mDialogBuilder;
305 AlertDialog mSwitchingDialog;
306 InputMethodInfo[] mIms;
307 CharSequence[] mItems;
satokab751aa2010-09-14 19:17:36 +0900308 int[] mSubtypeIds;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310 class SettingsObserver extends ContentObserver {
311 SettingsObserver(Handler handler) {
312 super(handler);
313 ContentResolver resolver = mContext.getContentResolver();
314 resolver.registerContentObserver(Settings.Secure.getUriFor(
315 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
satokab751aa2010-09-14 19:17:36 +0900316 resolver.registerContentObserver(Settings.Secure.getUriFor(
317 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 @Override public void onChange(boolean selfChange) {
321 synchronized (mMethodMap) {
322 updateFromSettingsLocked();
323 }
324 }
325 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 class ScreenOnOffReceiver extends android.content.BroadcastReceiver {
328 @Override
329 public void onReceive(Context context, Intent intent) {
330 if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
331 mScreenOn = true;
332 } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
333 mScreenOn = false;
The Android Open Source Project10592532009-03-18 17:39:46 -0700334 } else if (intent.getAction().equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
335 hideInputMethodMenu();
336 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800338 Slog.w(TAG, "Unexpected intent " + intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 }
340
341 // Inform the current client of the change in active status
342 try {
343 if (mCurClient != null && mCurClient.client != null) {
344 mCurClient.client.setActive(mScreenOn);
345 }
346 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800347 Slog.w(TAG, "Got RemoteException sending 'screen on/off' notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 + mCurClient.pid + " uid " + mCurClient.uid);
349 }
350 }
351 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800352
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800353 class MyPackageMonitor extends PackageMonitor {
354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800356 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 synchronized (mMethodMap) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800358 String curInputMethodId = Settings.Secure.getString(mContext
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
360 final int N = mMethodList.size();
361 if (curInputMethodId != null) {
362 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800363 InputMethodInfo imi = mMethodList.get(i);
364 if (imi.getId().equals(curInputMethodId)) {
365 for (String pkg : packages) {
366 if (imi.getPackageName().equals(pkg)) {
367 if (!doit) {
368 return true;
369 }
satok723a27e2010-11-11 14:58:11 +0900370 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800371 chooseNewDefaultIMELocked();
372 return true;
373 }
374 }
375 }
376 }
377 }
378 }
379 return false;
380 }
381
382 @Override
383 public void onSomePackagesChanged() {
384 synchronized (mMethodMap) {
385 InputMethodInfo curIm = null;
386 String curInputMethodId = Settings.Secure.getString(mContext
387 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
388 final int N = mMethodList.size();
389 if (curInputMethodId != null) {
390 for (int i=0; i<N; i++) {
391 InputMethodInfo imi = mMethodList.get(i);
392 if (imi.getId().equals(curInputMethodId)) {
393 curIm = imi;
394 }
395 int change = isPackageDisappearing(imi.getPackageName());
396 if (change == PACKAGE_TEMPORARY_CHANGE
397 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800398 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800399 + imi.getComponent());
400 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 }
402 }
403 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800404
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800405 buildInputMethodListLocked(mMethodList, mMethodMap);
406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800408
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800409 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800410 int change = isPackageDisappearing(curIm.getPackageName());
411 if (change == PACKAGE_TEMPORARY_CHANGE
412 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800413 ServiceInfo si = null;
414 try {
415 si = mContext.getPackageManager().getServiceInfo(
416 curIm.getComponent(), 0);
417 } catch (PackageManager.NameNotFoundException ex) {
418 }
419 if (si == null) {
420 // Uh oh, current input method is no longer around!
421 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800422 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800423 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800424 changed = true;
425 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800426 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900427 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800428 }
429 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800430 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800431 }
satokab751aa2010-09-14 19:17:36 +0900432
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800433 if (curIm == null) {
434 // We currently don't have a default input method... is
435 // one now available?
436 changed = chooseNewDefaultIMELocked();
437 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800438
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800439 if (changed) {
440 updateFromSettingsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441 }
442 }
443 }
444 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800445
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446 class MethodCallback extends IInputMethodCallback.Stub {
447 final IInputMethod mMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800448
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 MethodCallback(IInputMethod method) {
450 mMethod = method;
451 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800452
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 public void finishedEvent(int seq, boolean handled) throws RemoteException {
454 }
455
456 public void sessionCreated(IInputMethodSession session) throws RemoteException {
457 onSessionCreated(mMethod, session);
458 }
459 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800460
Joe Onorato089de882010-04-12 08:18:45 -0700461 public InputMethodManagerService(Context context, StatusBarManagerService statusBar) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 mContext = context;
463 mHandler = new Handler(this);
464 mIWindowManager = IWindowManager.Stub.asInterface(
465 ServiceManager.getService(Context.WINDOW_SERVICE));
466 mCaller = new HandlerCaller(context, new HandlerCaller.Callback() {
467 public void executeMessage(Message msg) {
468 handleMessage(msg);
469 }
470 });
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800471
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800472 (new MyPackageMonitor()).register(mContext, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 IntentFilter screenOnOffFilt = new IntentFilter();
475 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_ON);
476 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_OFF);
The Android Open Source Project10592532009-03-18 17:39:46 -0700477 screenOnOffFilt.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 mContext.registerReceiver(new ScreenOnOffReceiver(), screenOnOffFilt);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800479
satok913a8922010-08-26 21:53:41 +0900480 mStatusBar = statusBar;
481 statusBar.setIconVisibility("ime", false);
482
satokd87c2592010-09-29 11:52:06 +0900483 // mSettings should be created before buildInputMethodListLocked
484 mSettings = new InputMethodSettings(context.getContentResolver(), mMethodMap, mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485 buildInputMethodListLocked(mMethodList, mMethodMap);
satokd87c2592010-09-29 11:52:06 +0900486 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487
satokd87c2592010-09-29 11:52:06 +0900488 if (TextUtils.isEmpty(Settings.Secure.getString(
489 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 InputMethodInfo defIm = null;
satokd87c2592010-09-29 11:52:06 +0900491 for (InputMethodInfo imi: mMethodList) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 if (defIm == null && imi.getIsDefaultResourceId() != 0) {
493 try {
satokd87c2592010-09-29 11:52:06 +0900494 Resources res = context.createPackageContext(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 imi.getPackageName(), 0).getResources();
496 if (res.getBoolean(imi.getIsDefaultResourceId())) {
497 defIm = imi;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800498 Slog.i(TAG, "Selected default: " + imi.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 }
500 } catch (PackageManager.NameNotFoundException ex) {
501 } catch (Resources.NotFoundException ex) {
502 }
503 }
504 }
satokd87c2592010-09-29 11:52:06 +0900505 if (defIm == null && mMethodList.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506 defIm = mMethodList.get(0);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800507 Slog.i(TAG, "No default found, using " + defIm.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 if (defIm != null) {
satok723a27e2010-11-11 14:58:11 +0900510 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511 }
512 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 mSettingsObserver = new SettingsObserver(mHandler);
515 updateFromSettingsLocked();
516 }
517
518 @Override
519 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
520 throws RemoteException {
521 try {
522 return super.onTransact(code, data, reply, flags);
523 } catch (RuntimeException e) {
524 // The input method manager only throws security exceptions, so let's
525 // log all others.
526 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800527 Slog.e(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528 }
529 throw e;
530 }
531 }
532
533 public void systemReady() {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700534 synchronized (mMethodMap) {
535 if (!mSystemReady) {
536 mSystemReady = true;
Dianne Hackborncc278702009-09-02 23:07:23 -0700537 try {
538 startInputInnerLocked();
539 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800540 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700541 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700542 }
543 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800545
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800546 public List<InputMethodInfo> getInputMethodList() {
547 synchronized (mMethodMap) {
548 return new ArrayList<InputMethodInfo>(mMethodList);
549 }
550 }
551
552 public List<InputMethodInfo> getEnabledInputMethodList() {
553 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900554 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 }
556 }
557
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 public void addClient(IInputMethodClient client,
559 IInputContext inputContext, int uid, int pid) {
560 synchronized (mMethodMap) {
561 mClients.put(client.asBinder(), new ClientState(client,
562 inputContext, uid, pid));
563 }
564 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800565
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566 public void removeClient(IInputMethodClient client) {
567 synchronized (mMethodMap) {
568 mClients.remove(client.asBinder());
569 }
570 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800572 void executeOrSendMessage(IInterface target, Message msg) {
573 if (target.asBinder() instanceof Binder) {
574 mCaller.sendMessage(msg);
575 } else {
576 handleMessage(msg);
577 msg.recycle();
578 }
579 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800580
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700581 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800583 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 + mCurClient.client.asBinder());
585 if (mBoundToMethod) {
586 mBoundToMethod = false;
587 if (mCurMethod != null) {
588 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
589 MSG_UNBIND_INPUT, mCurMethod));
590 }
591 }
592 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
593 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
594 mCurClient.sessionRequested = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800595
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596 // Call setActive(false) on the old client
597 try {
598 mCurClient.client.setActive(false);
599 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800600 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 + mCurClient.pid + " uid " + mCurClient.uid);
602 }
603 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800604
The Android Open Source Project10592532009-03-18 17:39:46 -0700605 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606 }
607 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800608
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609 private int getImeShowFlags() {
610 int flags = 0;
611 if (mShowForced) {
612 flags |= InputMethod.SHOW_FORCED
613 | InputMethod.SHOW_EXPLICIT;
614 } else if (mShowExplicitlyRequested) {
615 flags |= InputMethod.SHOW_EXPLICIT;
616 }
617 return flags;
618 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800619
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 private int getAppShowFlags() {
621 int flags = 0;
622 if (mShowForced) {
623 flags |= InputMethodManager.SHOW_FORCED;
624 } else if (!mShowExplicitlyRequested) {
625 flags |= InputMethodManager.SHOW_IMPLICIT;
626 }
627 return flags;
628 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800629
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630 InputBindResult attachNewInputLocked(boolean initial, boolean needResult) {
631 if (!mBoundToMethod) {
632 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
633 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
634 mBoundToMethod = true;
635 }
636 final SessionState session = mCurClient.curSession;
637 if (initial) {
638 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
639 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
640 } else {
641 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
642 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
643 }
644 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800645 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -0800646 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800647 }
648 return needResult
649 ? new InputBindResult(session.session, mCurId, mCurSeq)
650 : null;
651 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800652
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800653 InputBindResult startInputLocked(IInputMethodClient client,
654 IInputContext inputContext, EditorInfo attribute,
655 boolean initial, boolean needResult) {
656 // If no method is currently selected, do nothing.
657 if (mCurMethodId == null) {
658 return mNoBinding;
659 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800660
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800661 ClientState cs = mClients.get(client.asBinder());
662 if (cs == null) {
663 throw new IllegalArgumentException("unknown client "
664 + client.asBinder());
665 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800666
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 try {
668 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
669 // Check with the window manager to make sure this client actually
670 // has a window with focus. If not, reject. This is thread safe
671 // because if the focus changes some time before or after, the
672 // next client receiving focus that has any interest in input will
673 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800674 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
676 return null;
677 }
678 } catch (RemoteException e) {
679 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800680
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 if (mCurClient != cs) {
682 // If the client is changing, we need to switch over to the new
683 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700684 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800685 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800686 + cs.client.asBinder());
687
688 // If the screen is on, inform the new client it is active
689 if (mScreenOn) {
690 try {
691 cs.client.setActive(mScreenOn);
692 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800693 Slog.w(TAG, "Got RemoteException sending setActive notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800694 + cs.pid + " uid " + cs.uid);
695 }
696 }
697 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800698
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800699 // Bump up the sequence for this client and attach it.
700 mCurSeq++;
701 if (mCurSeq <= 0) mCurSeq = 1;
702 mCurClient = cs;
703 mCurInputContext = inputContext;
704 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 // Check if the input method is changing.
707 if (mCurId != null && mCurId.equals(mCurMethodId)) {
708 if (cs.curSession != null) {
709 // Fast case: if we are already connected to the input method,
710 // then just return it.
711 return attachNewInputLocked(initial, needResult);
712 }
713 if (mHaveConnection) {
714 if (mCurMethod != null) {
715 if (!cs.sessionRequested) {
716 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800717 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800718 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
719 MSG_CREATE_SESSION, mCurMethod,
720 new MethodCallback(mCurMethod)));
721 }
722 // Return to client, and we will get back with it when
723 // we have had a session made for it.
724 return new InputBindResult(null, mCurId, mCurSeq);
725 } else if (SystemClock.uptimeMillis()
726 < (mLastBindTime+TIME_TO_RECONNECT)) {
727 // In this case we have connected to the service, but
728 // don't yet have its interface. If it hasn't been too
729 // long since we did the connection, we'll return to
730 // the client and wait to get the service interface so
731 // we can report back. If it has been too long, we want
732 // to fall through so we can try a disconnect/reconnect
733 // to see if we can get back in touch with the service.
734 return new InputBindResult(null, mCurId, mCurSeq);
735 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800736 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
737 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800738 }
739 }
740 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800741
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700742 return startInputInnerLocked();
743 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800744
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700745 InputBindResult startInputInnerLocked() {
746 if (mCurMethodId == null) {
747 return mNoBinding;
748 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800749
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700750 if (!mSystemReady) {
751 // If the system is not yet ready, we shouldn't be running third
752 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -0700753 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700754 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800755
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800756 InputMethodInfo info = mMethodMap.get(mCurMethodId);
757 if (info == null) {
758 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
759 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800760
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700761 unbindCurrentMethodLocked(false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800762
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800763 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
764 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -0700765 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
766 com.android.internal.R.string.input_method_binding_label);
767 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
768 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800769 if (mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE)) {
770 mLastBindTime = SystemClock.uptimeMillis();
771 mHaveConnection = true;
772 mCurId = info.getId();
773 mCurToken = new Binder();
774 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800775 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800776 mIWindowManager.addWindowToken(mCurToken,
777 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
778 } catch (RemoteException e) {
779 }
780 return new InputBindResult(null, mCurId, mCurSeq);
781 } else {
782 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800783 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800784 + mCurIntent);
785 }
786 return null;
787 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800788
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800789 public InputBindResult startInput(IInputMethodClient client,
790 IInputContext inputContext, EditorInfo attribute,
791 boolean initial, boolean needResult) {
792 synchronized (mMethodMap) {
793 final long ident = Binder.clearCallingIdentity();
794 try {
795 return startInputLocked(client, inputContext, attribute,
796 initial, needResult);
797 } finally {
798 Binder.restoreCallingIdentity(ident);
799 }
800 }
801 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800802
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800803 public void finishInput(IInputMethodClient client) {
804 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800805
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800806 public void onServiceConnected(ComponentName name, IBinder service) {
807 synchronized (mMethodMap) {
808 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
809 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -0700810 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800811 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborncc278702009-09-02 23:07:23 -0700812 unbindCurrentMethodLocked(false);
813 return;
814 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800815 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -0700816 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
817 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800818 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800819 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -0700820 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800821 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -0700822 MSG_CREATE_SESSION, mCurMethod,
823 new MethodCallback(mCurMethod)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800824 }
825 }
826 }
827 }
828
829 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
830 synchronized (mMethodMap) {
831 if (mCurMethod != null && method != null
832 && mCurMethod.asBinder() == method.asBinder()) {
833 if (mCurClient != null) {
834 mCurClient.curSession = new SessionState(mCurClient,
835 method, session);
836 mCurClient.sessionRequested = false;
837 InputBindResult res = attachNewInputLocked(true, true);
838 if (res.method != null) {
839 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
840 MSG_BIND_METHOD, mCurClient.client, res));
841 }
842 }
843 }
844 }
845 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800846
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700847 void unbindCurrentMethodLocked(boolean reportToClient) {
848 if (mHaveConnection) {
849 mContext.unbindService(this);
850 mHaveConnection = false;
851 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800852
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700853 if (mCurToken != null) {
854 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800855 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700856 mIWindowManager.removeWindowToken(mCurToken);
857 } catch (RemoteException e) {
858 }
859 mCurToken = null;
860 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800861
The Android Open Source Project10592532009-03-18 17:39:46 -0700862 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700863 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800864
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700865 if (reportToClient && mCurClient != null) {
866 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
867 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
868 }
869 }
870
Devin Taylor0c33ed22010-02-23 13:26:46 -0600871 private void finishSession(SessionState sessionState) {
872 if (sessionState != null && sessionState.session != null) {
873 try {
874 sessionState.session.finishSession();
875 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -0700876 Slog.w(TAG, "Session failed to close due to remote exception", e);
Devin Taylor0c33ed22010-02-23 13:26:46 -0600877 }
878 }
879 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800880
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700881 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800882 if (mCurMethod != null) {
883 for (ClientState cs : mClients.values()) {
884 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -0600885 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800886 cs.curSession = null;
887 }
Devin Taylor0c33ed22010-02-23 13:26:46 -0600888
889 finishSession(mEnabledSession);
890 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800891 mCurMethod = null;
892 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700893 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800894 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800895
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800896 public void onServiceDisconnected(ComponentName name) {
897 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800898 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800899 + " mCurIntent=" + mCurIntent);
900 if (mCurMethod != null && mCurIntent != null
901 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700902 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800903 // We consider this to be a new bind attempt, since the system
904 // should now try to restart the service for us.
905 mLastBindTime = SystemClock.uptimeMillis();
906 mShowRequested = mInputShown;
907 mInputShown = false;
908 if (mCurClient != null) {
909 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
910 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
911 }
912 }
913 }
914 }
915
916 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700917 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800918 long ident = Binder.clearCallingIdentity();
919 try {
920 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700921 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800922 return;
923 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800925 synchronized (mMethodMap) {
926 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800927 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -0700928 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800929 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800930 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -0700931 mStatusBar.setIcon("ime", packageName, iconId, 0);
932 mStatusBar.setIconVisibility("ime", true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800933 }
934 }
935 } finally {
936 Binder.restoreCallingIdentity(ident);
937 }
938 }
939
satok06487a52010-10-29 11:37:18 +0900940 public void setIMEButtonVisible(IBinder token, boolean visible) {
941 int uid = Binder.getCallingUid();
942 long ident = Binder.clearCallingIdentity();
943 try {
944 if (token == null || mCurToken != token) {
945 Slog.w(TAG, "Ignoring setIMEButtonVisible of uid " + uid + " token: " + token);
946 return;
947 }
948
949 synchronized (mMethodMap) {
950 mStatusBar.setIMEButtonVisible(visible);
951 }
952 } finally {
953 Binder.restoreCallingIdentity(ident);
954 }
955 }
956
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800957 void updateFromSettingsLocked() {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700958 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
959 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
960 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
961 // enabled.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800962 String id = Settings.Secure.getString(mContext.getContentResolver(),
satokab751aa2010-09-14 19:17:36 +0900963 Settings.Secure.DEFAULT_INPUT_METHOD);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700964 if (id != null && id.length() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800965 try {
satokab751aa2010-09-14 19:17:36 +0900966 setInputMethodLocked(id, getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800967 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800968 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -0700969 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700970 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800971 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700972 } else {
973 // There is no longer an input method set, so stop any current one.
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 }
977 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800978
satokab751aa2010-09-14 19:17:36 +0900979 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800980 InputMethodInfo info = mMethodMap.get(id);
981 if (info == null) {
satok913a8922010-08-26 21:53:41 +0900982 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800983 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800984
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800985 if (id.equals(mCurMethodId)) {
satokab751aa2010-09-14 19:17:36 +0900986 if (subtypeId != NOT_A_SUBTYPE_ID) {
987 InputMethodSubtype subtype = info.getSubtypes().get(subtypeId);
988 if (subtype != mCurrentSubtype) {
989 synchronized (mMethodMap) {
990 if (mCurMethod != null) {
991 try {
satok723a27e2010-11-11 14:58:11 +0900992 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
satok06e07442010-11-02 19:46:55 +0900993 if (mInputShown) {
994 // If mInputShown is false, there is no IME button on the
995 // system bar.
996 // Thus there is no need to make it invisible explicitly.
997 mStatusBar.setIMEButtonVisible(true);
998 }
satokab751aa2010-09-14 19:17:36 +0900999 mCurMethod.changeInputMethodSubtype(subtype);
1000 } catch (RemoteException e) {
1001 return;
1002 }
1003 }
1004 }
1005 }
1006 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007 return;
1008 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001009
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001010 final long ident = Binder.clearCallingIdentity();
1011 try {
satokab751aa2010-09-14 19:17:36 +09001012 // Set a subtype to this input method.
1013 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001014 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1015 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1016 // because mCurMethodId is stored as a history in
1017 // setSelectedInputMethodAndSubtypeLocked().
1018 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019
1020 if (ActivityManagerNative.isSystemReady()) {
1021 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001022 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001023 intent.putExtra("input_method_id", id);
1024 mContext.sendBroadcast(intent);
1025 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001026 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027 } finally {
1028 Binder.restoreCallingIdentity(ident);
1029 }
1030 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001031
The Android Open Source Project4df24232009-03-05 14:34:35 -08001032 public boolean showSoftInput(IInputMethodClient client, int flags,
1033 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001034 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 long ident = Binder.clearCallingIdentity();
1036 try {
1037 synchronized (mMethodMap) {
1038 if (mCurClient == null || client == null
1039 || mCurClient.client.asBinder() != client.asBinder()) {
1040 try {
1041 // We need to check if this is the current client with
1042 // focus in the window manager, to allow this call to
1043 // be made before input is started in it.
1044 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001045 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001046 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001047 }
1048 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001049 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001050 }
1051 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001052
Joe Onorato8a9b2202010-02-26 18:56:32 -08001053 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001054 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055 }
1056 } finally {
1057 Binder.restoreCallingIdentity(ident);
1058 }
1059 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001060
The Android Open Source Project4df24232009-03-05 14:34:35 -08001061 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062 mShowRequested = true;
1063 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1064 mShowExplicitlyRequested = true;
1065 }
1066 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1067 mShowExplicitlyRequested = true;
1068 mShowForced = true;
1069 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001070
Dianne Hackborncc278702009-09-02 23:07:23 -07001071 if (!mSystemReady) {
1072 return false;
1073 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001074
The Android Open Source Project4df24232009-03-05 14:34:35 -08001075 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001076 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001077 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1078 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1079 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001080 mInputShown = true;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001081 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001082 } else if (mHaveConnection && SystemClock.uptimeMillis()
1083 < (mLastBindTime+TIME_TO_RECONNECT)) {
1084 // The client has asked to have the input method shown, but
1085 // we have been sitting here too long with a connection to the
1086 // service and no interface received, so let's disconnect/connect
1087 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001088 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001089 SystemClock.uptimeMillis()-mLastBindTime,1);
1090 mContext.unbindService(this);
1091 mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE);
1092 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001093
The Android Open Source Project4df24232009-03-05 14:34:35 -08001094 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001096
The Android Open Source Project4df24232009-03-05 14:34:35 -08001097 public boolean hideSoftInput(IInputMethodClient client, int flags,
1098 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001099 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001100 long ident = Binder.clearCallingIdentity();
1101 try {
1102 synchronized (mMethodMap) {
1103 if (mCurClient == null || client == null
1104 || mCurClient.client.asBinder() != client.asBinder()) {
1105 try {
1106 // We need to check if this is the current client with
1107 // focus in the window manager, to allow this call to
1108 // be made before input is started in it.
1109 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001110 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1111 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001112 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001113 }
1114 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001115 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001116 }
1117 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001118
Joe Onorato8a9b2202010-02-26 18:56:32 -08001119 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001120 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001121 }
1122 } finally {
1123 Binder.restoreCallingIdentity(ident);
1124 }
1125 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001126
The Android Open Source Project4df24232009-03-05 14:34:35 -08001127 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001128 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1129 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001130 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001131 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001132 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001133 }
1134 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
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: forced show not cancelled by not-always 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 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001139 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001140 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001141 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1142 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1143 res = true;
1144 } else {
1145 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001146 }
1147 mInputShown = false;
1148 mShowRequested = false;
1149 mShowExplicitlyRequested = false;
1150 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001151 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001152 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001153
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001154 public void windowGainedFocus(IInputMethodClient client, IBinder windowToken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001155 boolean viewHasFocus, boolean isTextEditor, int softInputMode,
1156 boolean first, int windowFlags) {
1157 long ident = Binder.clearCallingIdentity();
1158 try {
1159 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001160 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001161 + " viewHasFocus=" + viewHasFocus
1162 + " isTextEditor=" + isTextEditor
1163 + " softInputMode=#" + Integer.toHexString(softInputMode)
1164 + " first=" + first + " flags=#"
1165 + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001167 if (mCurClient == null || client == null
1168 || mCurClient.client.asBinder() != client.asBinder()) {
1169 try {
1170 // We need to check if this is the current client with
1171 // focus in the window manager, to allow this call to
1172 // be made before input is started in it.
1173 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001174 Slog.w(TAG, "Client not active, ignoring focus gain of: " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001175 return;
1176 }
1177 } catch (RemoteException e) {
1178 }
1179 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001180
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001181 if (mCurFocusedWindow == windowToken) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001182 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001183 return;
1184 }
1185 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001186
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001187 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1188 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
1189 if (!isTextEditor || (softInputMode &
1190 WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1191 != WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE) {
1192 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1193 // There is no focus view, and this window will
1194 // be behind any soft input window, so hide the
1195 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001196 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001197 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 }
1199 } else if (isTextEditor && (softInputMode &
1200 WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1201 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1202 && (softInputMode &
1203 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
1204 // There is a focus view, and we are navigating forward
1205 // into the window, so show the input window for the user.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001206 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001207 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001208 }
1209 break;
1210 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1211 // Do nothing.
1212 break;
1213 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1214 if ((softInputMode &
1215 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001216 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001217 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001218 }
1219 break;
1220 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001221 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
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 break;
1224 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1225 if ((softInputMode &
1226 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001227 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001228 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 }
1230 break;
1231 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001232 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
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 break;
1235 }
1236 }
1237 } finally {
1238 Binder.restoreCallingIdentity(ident);
1239 }
1240 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001242 public void showInputMethodPickerFromClient(IInputMethodClient client) {
1243 synchronized (mMethodMap) {
1244 if (mCurClient == null || client == null
1245 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001246 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001247 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001248 }
1249
1250 mHandler.sendEmptyMessage(MSG_SHOW_IM_PICKER);
1251 }
1252 }
1253
satokab751aa2010-09-14 19:17:36 +09001254 public void showInputMethodSubtypePickerFromClient(IInputMethodClient client) {
1255 synchronized (mMethodMap) {
1256 if (mCurClient == null || client == null
1257 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001258 Slog.w(TAG, "Ignoring showInputMethodSubtypePickerFromClient of: " + client);
satokab751aa2010-09-14 19:17:36 +09001259 }
1260
1261 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1262 }
1263 }
1264
satok47a44912010-10-06 16:03:58 +09001265 public void showInputMethodAndSubtypeEnablerFromClient(
1266 IInputMethodClient client, String topId) {
1267 // TODO: Handle topId for setting the top position of the list activity
1268 synchronized (mMethodMap) {
1269 if (mCurClient == null || client == null
1270 || mCurClient.client.asBinder() != client.asBinder()) {
1271 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
1272 }
1273
1274 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_ENABLER);
1275 }
1276 }
1277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001278 public void setInputMethod(IBinder token, String id) {
satokab751aa2010-09-14 19:17:36 +09001279 setInputMethodWithSubtype(token, id, NOT_A_SUBTYPE_ID);
1280 }
1281
1282 private void setInputMethodWithSubtype(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001283 synchronized (mMethodMap) {
1284 if (token == null) {
1285 if (mContext.checkCallingOrSelfPermission(
1286 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1287 != PackageManager.PERMISSION_GRANTED) {
1288 throw new SecurityException(
1289 "Using null token requires permission "
1290 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1291 }
1292 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001293 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
1294 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001295 return;
1296 }
1297
1298 long ident = Binder.clearCallingIdentity();
1299 try {
satokab751aa2010-09-14 19:17:36 +09001300 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001301 } finally {
1302 Binder.restoreCallingIdentity(ident);
1303 }
1304 }
1305 }
1306
1307 public void hideMySoftInput(IBinder token, int flags) {
1308 synchronized (mMethodMap) {
1309 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001310 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
1311 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001312 return;
1313 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001314 long ident = Binder.clearCallingIdentity();
1315 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001316 hideCurrentInputLocked(flags, null);
1317 } finally {
1318 Binder.restoreCallingIdentity(ident);
1319 }
1320 }
1321 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001322
The Android Open Source Project4df24232009-03-05 14:34:35 -08001323 public void showMySoftInput(IBinder token, int flags) {
1324 synchronized (mMethodMap) {
1325 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001326 Slog.w(TAG, "Ignoring showMySoftInput of uid "
1327 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001328 return;
1329 }
1330 long ident = Binder.clearCallingIdentity();
1331 try {
1332 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001333 } finally {
1334 Binder.restoreCallingIdentity(ident);
1335 }
1336 }
1337 }
1338
1339 void setEnabledSessionInMainThread(SessionState session) {
1340 if (mEnabledSession != session) {
1341 if (mEnabledSession != null) {
1342 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001343 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001344 mEnabledSession.method.setSessionEnabled(
1345 mEnabledSession.session, false);
1346 } catch (RemoteException e) {
1347 }
1348 }
1349 mEnabledSession = session;
1350 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001351 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001352 session.method.setSessionEnabled(
1353 session.session, true);
1354 } catch (RemoteException e) {
1355 }
1356 }
1357 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001359 public boolean handleMessage(Message msg) {
1360 HandlerCaller.SomeArgs args;
1361 switch (msg.what) {
1362 case MSG_SHOW_IM_PICKER:
1363 showInputMethodMenu();
1364 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001365
satokab751aa2010-09-14 19:17:36 +09001366 case MSG_SHOW_IM_SUBTYPE_PICKER:
1367 showInputMethodSubtypeMenu();
1368 return true;
1369
satok47a44912010-10-06 16:03:58 +09001370 case MSG_SHOW_IM_SUBTYPE_ENABLER:
1371 showInputMethodAndSubtypeEnabler();
1372 return true;
1373
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001374 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001376 case MSG_UNBIND_INPUT:
1377 try {
1378 ((IInputMethod)msg.obj).unbindInput();
1379 } catch (RemoteException e) {
1380 // There is nothing interesting about the method dying.
1381 }
1382 return true;
1383 case MSG_BIND_INPUT:
1384 args = (HandlerCaller.SomeArgs)msg.obj;
1385 try {
1386 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
1387 } catch (RemoteException e) {
1388 }
1389 return true;
1390 case MSG_SHOW_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001391 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001392 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001393 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
1394 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001395 } catch (RemoteException e) {
1396 }
1397 return true;
1398 case MSG_HIDE_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001399 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001400 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001401 ((IInputMethod)args.arg1).hideSoftInput(0,
1402 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001403 } catch (RemoteException e) {
1404 }
1405 return true;
1406 case MSG_ATTACH_TOKEN:
1407 args = (HandlerCaller.SomeArgs)msg.obj;
1408 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001409 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001410 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
1411 } catch (RemoteException e) {
1412 }
1413 return true;
1414 case MSG_CREATE_SESSION:
1415 args = (HandlerCaller.SomeArgs)msg.obj;
1416 try {
1417 ((IInputMethod)args.arg1).createSession(
1418 (IInputMethodCallback)args.arg2);
1419 } catch (RemoteException e) {
1420 }
1421 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001422 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001423
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424 case MSG_START_INPUT:
1425 args = (HandlerCaller.SomeArgs)msg.obj;
1426 try {
1427 SessionState session = (SessionState)args.arg1;
1428 setEnabledSessionInMainThread(session);
1429 session.method.startInput((IInputContext)args.arg2,
1430 (EditorInfo)args.arg3);
1431 } catch (RemoteException e) {
1432 }
1433 return true;
1434 case MSG_RESTART_INPUT:
1435 args = (HandlerCaller.SomeArgs)msg.obj;
1436 try {
1437 SessionState session = (SessionState)args.arg1;
1438 setEnabledSessionInMainThread(session);
1439 session.method.restartInput((IInputContext)args.arg2,
1440 (EditorInfo)args.arg3);
1441 } catch (RemoteException e) {
1442 }
1443 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001444
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001445 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001446
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001447 case MSG_UNBIND_METHOD:
1448 try {
1449 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
1450 } catch (RemoteException e) {
1451 // There is nothing interesting about the last client dying.
1452 }
1453 return true;
1454 case MSG_BIND_METHOD:
1455 args = (HandlerCaller.SomeArgs)msg.obj;
1456 try {
1457 ((IInputMethodClient)args.arg1).onBindMethod(
1458 (InputBindResult)args.arg2);
1459 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001460 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001461 }
1462 return true;
1463 }
1464 return false;
1465 }
1466
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001467 private boolean isSystemIme(InputMethodInfo inputMethod) {
1468 return (inputMethod.getServiceInfo().applicationInfo.flags
1469 & ApplicationInfo.FLAG_SYSTEM) != 0;
1470 }
1471
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001472 private boolean chooseNewDefaultIMELocked() {
satokd87c2592010-09-29 11:52:06 +09001473 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001474 if (enabled != null && enabled.size() > 0) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07001475 // We'd prefer to fall back on a system IME, since that is safer.
1476 int i=enabled.size();
1477 while (i > 0) {
1478 i--;
1479 if ((enabled.get(i).getServiceInfo().applicationInfo.flags
1480 & ApplicationInfo.FLAG_SYSTEM) != 0) {
1481 break;
1482 }
1483 }
satokab751aa2010-09-14 19:17:36 +09001484 InputMethodInfo imi = enabled.get(i);
satok723a27e2010-11-11 14:58:11 +09001485 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001486 return true;
1487 }
1488
1489 return false;
1490 }
1491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001492 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
1493 HashMap<String, InputMethodInfo> map) {
1494 list.clear();
1495 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001496
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001497 PackageManager pm = mContext.getPackageManager();
Amith Yamasanie861ec12010-03-24 21:39:27 -07001498 final Configuration config = mContext.getResources().getConfiguration();
1499 final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY;
1500 String disabledSysImes = Settings.Secure.getString(mContext.getContentResolver(),
1501 Secure.DISABLED_SYSTEM_INPUT_METHODS);
1502 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001503
1504 List<ResolveInfo> services = pm.queryIntentServices(
1505 new Intent(InputMethod.SERVICE_INTERFACE),
1506 PackageManager.GET_META_DATA);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001507
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001508 for (int i = 0; i < services.size(); ++i) {
1509 ResolveInfo ri = services.get(i);
1510 ServiceInfo si = ri.serviceInfo;
1511 ComponentName compName = new ComponentName(si.packageName, si.name);
1512 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
1513 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001514 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001515 + ": it does not require the permission "
1516 + android.Manifest.permission.BIND_INPUT_METHOD);
1517 continue;
1518 }
1519
Joe Onorato8a9b2202010-02-26 18:56:32 -08001520 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001521
1522 try {
1523 InputMethodInfo p = new InputMethodInfo(mContext, ri);
1524 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07001525 final String id = p.getId();
1526 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001527
Amith Yamasanie861ec12010-03-24 21:39:27 -07001528 // System IMEs are enabled by default, unless there's a hard keyboard
1529 // and the system IME was explicitly disabled
1530 if (isSystemIme(p) && (!haveHardKeyboard || disabledSysImes.indexOf(id) < 0)) {
1531 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001532 }
1533
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001534 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001535 Slog.d(TAG, "Found a third-party input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001536 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001537
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001538 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001539 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001540 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001541 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001542 }
1543 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001544
1545 String defaultIme = Settings.Secure.getString(mContext
1546 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok913a8922010-08-26 21:53:41 +09001547 if (!TextUtils.isEmpty(defaultIme) && !map.containsKey(defaultIme)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001548 if (chooseNewDefaultIMELocked()) {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001549 updateFromSettingsLocked();
1550 }
1551 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001552 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001553
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001554 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001555
satokab751aa2010-09-14 19:17:36 +09001556 private void showInputMethodMenu() {
1557 showInputMethodMenuInternal(false);
1558 }
1559
1560 private void showInputMethodSubtypeMenu() {
1561 showInputMethodMenuInternal(true);
1562 }
1563
satok47a44912010-10-06 16:03:58 +09001564 private void showInputMethodAndSubtypeEnabler() {
satok86417ea2010-10-27 14:11:03 +09001565 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_AND_SUBTYPE_ENABLER);
satok47a44912010-10-06 16:03:58 +09001566 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09001567 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
1568 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok47a44912010-10-06 16:03:58 +09001569 mContext.startActivity(intent);
1570 }
1571
satokab751aa2010-09-14 19:17:36 +09001572 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001573 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001574
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001575 final Context context = mContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001576
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001577 final PackageManager pm = context.getPackageManager();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001578
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001579 String lastInputMethodId = Settings.Secure.getString(context
1580 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satokab751aa2010-09-14 19:17:36 +09001581 int lastInputMethodSubtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08001582 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001583
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001584 synchronized (mMethodMap) {
satok7f35c8c2010-10-07 21:13:11 +09001585 final List<Pair<InputMethodInfo, ArrayList<String>>> immis =
1586 mSettings.getEnabledInputMethodAndSubtypeListLocked();
1587 ArrayList<Integer> subtypeIds = new ArrayList<Integer>();
1588
1589 if (immis == null || immis.size() == 0) {
1590 return;
1591 }
1592
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001593 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001594
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001595 int N = immis.size();
satok913a8922010-08-26 21:53:41 +09001596
satokab751aa2010-09-14 19:17:36 +09001597 final Map<CharSequence, Pair<InputMethodInfo, Integer>> imMap =
1598 new TreeMap<CharSequence, Pair<InputMethodInfo, Integer>>(Collator.getInstance());
satok913a8922010-08-26 21:53:41 +09001599
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001600 for (int i = 0; i < N; ++i) {
satok7f35c8c2010-10-07 21:13:11 +09001601 InputMethodInfo property = immis.get(i).first;
1602 final ArrayList<String> enabledSubtypeIds = immis.get(i).second;
1603 HashSet<String> enabledSubtypeSet = new HashSet<String>();
1604 for (String s : enabledSubtypeIds) {
1605 enabledSubtypeSet.add(s);
1606 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001607 if (property == null) {
1608 continue;
1609 }
satokab751aa2010-09-14 19:17:36 +09001610 ArrayList<InputMethodSubtype> subtypes = property.getSubtypes();
1611 CharSequence label = property.loadLabel(pm);
satok7f35c8c2010-10-07 21:13:11 +09001612 if (showSubtypes && enabledSubtypeSet.size() > 0) {
satokab751aa2010-09-14 19:17:36 +09001613 for (int j = 0; j < subtypes.size(); ++j) {
1614 InputMethodSubtype subtype = subtypes.get(j);
satok7f35c8c2010-10-07 21:13:11 +09001615 if (enabledSubtypeSet.contains(String.valueOf(subtype.hashCode()))) {
1616 CharSequence title;
1617 int nameResId = subtype.getNameResId();
satok9ef02832010-11-04 21:17:48 +09001618 String mode = subtype.getMode();
satok7f35c8c2010-10-07 21:13:11 +09001619 if (nameResId != 0) {
1620 title = pm.getText(property.getPackageName(), nameResId,
1621 property.getServiceInfo().applicationInfo);
1622 } else {
1623 CharSequence language = subtype.getLocale();
satok7f35c8c2010-10-07 21:13:11 +09001624 // TODO: Use more friendly Title and UI
1625 title = label + "," + (mode == null ? "" : mode) + ","
1626 + (language == null ? "" : language);
1627 }
1628 imMap.put(title, new Pair<InputMethodInfo, Integer>(property, j));
satokab751aa2010-09-14 19:17:36 +09001629 }
satokab751aa2010-09-14 19:17:36 +09001630 }
1631 } else {
1632 imMap.put(label,
1633 new Pair<InputMethodInfo, Integer>(property, NOT_A_SUBTYPE_ID));
1634 subtypeIds.add(0);
1635 }
Dianne Hackborn97106ab2010-03-03 00:08:31 -08001636 }
satok913a8922010-08-26 21:53:41 +09001637
1638 N = imMap.size();
1639 mItems = imMap.keySet().toArray(new CharSequence[N]);
satokab751aa2010-09-14 19:17:36 +09001640 mIms = new InputMethodInfo[N];
1641 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001642 int checkedItem = 0;
1643 for (int i = 0; i < N; ++i) {
satokab751aa2010-09-14 19:17:36 +09001644 Pair<InputMethodInfo, Integer> value = imMap.get(mItems[i]);
1645 mIms[i] = value.first;
1646 mSubtypeIds[i] = value.second;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001647 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09001648 int subtypeId = mSubtypeIds[i];
1649 if ((subtypeId == NOT_A_SUBTYPE_ID)
1650 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
1651 || (subtypeId == lastInputMethodSubtypeId)) {
1652 checkedItem = i;
1653 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001654 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001655 }
satokab751aa2010-09-14 19:17:36 +09001656
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001657 AlertDialog.OnClickListener adocl = new AlertDialog.OnClickListener() {
1658 public void onClick(DialogInterface dialog, int which) {
1659 hideInputMethodMenu();
1660 }
1661 };
satokd87c2592010-09-29 11:52:06 +09001662
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001663 TypedArray a = context.obtainStyledAttributes(null,
1664 com.android.internal.R.styleable.DialogPreference,
1665 com.android.internal.R.attr.alertDialogStyle, 0);
1666 mDialogBuilder = new AlertDialog.Builder(context)
1667 .setTitle(com.android.internal.R.string.select_input_method)
1668 .setOnCancelListener(new OnCancelListener() {
1669 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001670 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001671 }
1672 })
1673 .setIcon(a.getDrawable(
1674 com.android.internal.R.styleable.DialogPreference_dialogTitle));
1675 a.recycle();
satokd87c2592010-09-29 11:52:06 +09001676
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001677 mDialogBuilder.setSingleChoiceItems(mItems, checkedItem,
1678 new AlertDialog.OnClickListener() {
1679 public void onClick(DialogInterface dialog, int which) {
1680 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09001681 if (mIms == null || mIms.length <= which
1682 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001683 return;
1684 }
1685 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09001686 int subtypeId = mSubtypeIds[which];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001687 hideInputMethodMenu();
1688 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09001689 if ((subtypeId < 0)
1690 || (subtypeId >= im.getSubtypes().size())) {
1691 subtypeId = NOT_A_SUBTYPE_ID;
1692 }
1693 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001694 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08001695 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001696 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001697 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001698
satok7f35c8c2010-10-07 21:13:11 +09001699 if (showSubtypes) {
1700 mDialogBuilder.setPositiveButton(com.android.internal.R.string.more_item_label,
1701 new DialogInterface.OnClickListener() {
1702 public void onClick(DialogInterface dialog, int whichButton) {
1703 showInputMethodAndSubtypeEnabler();
1704 }
1705 });
1706 }
satok0ff647b2010-10-08 13:49:28 +09001707 mDialogBuilder.setNegativeButton(com.android.internal.R.string.cancel,
1708 new DialogInterface.OnClickListener() {
1709 public void onClick(DialogInterface dialog, int whichButton) {
1710 hideInputMethodMenu();
1711 }
1712 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001713 mSwitchingDialog = mDialogBuilder.create();
1714 mSwitchingDialog.getWindow().setType(
1715 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
1716 mSwitchingDialog.show();
1717 }
1718 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001719
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001720 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07001721 synchronized (mMethodMap) {
1722 hideInputMethodMenuLocked();
1723 }
1724 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001725
The Android Open Source Project10592532009-03-18 17:39:46 -07001726 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001727 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001728
The Android Open Source Project10592532009-03-18 17:39:46 -07001729 if (mSwitchingDialog != null) {
1730 mSwitchingDialog.dismiss();
1731 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001732 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001733
The Android Open Source Project10592532009-03-18 17:39:46 -07001734 mDialogBuilder = null;
1735 mItems = null;
1736 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001737 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001738
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001739 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001740
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001741 public boolean setInputMethodEnabled(String id, boolean enabled) {
1742 synchronized (mMethodMap) {
1743 if (mContext.checkCallingOrSelfPermission(
1744 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1745 != PackageManager.PERMISSION_GRANTED) {
1746 throw new SecurityException(
1747 "Requires permission "
1748 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1749 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001750
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001751 long ident = Binder.clearCallingIdentity();
1752 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001753 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001754 } finally {
1755 Binder.restoreCallingIdentity(ident);
1756 }
1757 }
1758 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001759
1760 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
1761 // Make sure this is a valid input method.
1762 InputMethodInfo imm = mMethodMap.get(id);
1763 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09001764 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001765 }
1766
satokd87c2592010-09-29 11:52:06 +09001767 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
1768 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001769
satokd87c2592010-09-29 11:52:06 +09001770 if (enabled) {
1771 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
1772 if (pair.first.equals(id)) {
1773 // We are enabling this input method, but it is already enabled.
1774 // Nothing to do. The previous state was enabled.
1775 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001776 }
1777 }
satokd87c2592010-09-29 11:52:06 +09001778 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
1779 // Previous state was disabled.
1780 return false;
1781 } else {
1782 StringBuilder builder = new StringBuilder();
1783 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
1784 builder, enabledInputMethodsList, id)) {
1785 // Disabled input method is currently selected, switch to another one.
1786 String selId = Settings.Secure.getString(mContext.getContentResolver(),
1787 Settings.Secure.DEFAULT_INPUT_METHOD);
1788 if (id.equals(selId)) {
satok723a27e2010-11-11 14:58:11 +09001789 resetSelectedInputMethodAndSubtypeLocked(enabledInputMethodsList.size() > 0
1790 ? enabledInputMethodsList.get(0).first : "");
satokd87c2592010-09-29 11:52:06 +09001791 }
1792 // Previous state was enabled.
1793 return true;
1794 } else {
1795 // We are disabling the input method but it is already disabled.
1796 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001797 return false;
1798 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001799 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001800 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001801
satok723a27e2010-11-11 14:58:11 +09001802 private void saveCurrentInputMethodAndSubtypeToHistory() {
1803 String subtypeId = NOT_A_SUBTYPE_ID_STR;
1804 if (mCurrentSubtype != null) {
1805 subtypeId = String.valueOf(mCurrentSubtype.hashCode());
1806 }
1807 mSettings.addSubtypeToHistory(mCurMethodId, subtypeId);
satokab751aa2010-09-14 19:17:36 +09001808 }
1809
satok723a27e2010-11-11 14:58:11 +09001810 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
1811 boolean setSubtypeOnly) {
1812 // Update the history of InputMethod and Subtype
1813 saveCurrentInputMethodAndSubtypeToHistory();
1814
1815 // Set Subtype here
1816 if (imi == null || subtypeId < 0) {
1817 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08001818 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09001819 } else {
1820 final ArrayList<InputMethodSubtype> subtypes = imi.getSubtypes();
1821 if (subtypeId < subtypes.size()) {
1822 mSettings.putSelectedSubtype(subtypes.get(subtypeId).hashCode());
1823 mCurrentSubtype = subtypes.get(subtypeId);
1824 } else {
1825 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
1826 mCurrentSubtype = null;
1827 }
satokab751aa2010-09-14 19:17:36 +09001828 }
satok723a27e2010-11-11 14:58:11 +09001829
1830 if (!setSubtypeOnly) {
1831 // Set InputMethod here
1832 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
1833 }
1834 }
1835
1836 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
1837 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
1838 int lastSubtypeId = NOT_A_SUBTYPE_ID;
1839 // newDefaultIme is empty when there is no candidate for the selected IME.
1840 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
1841 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
1842 if (subtypeHashCode != null) {
1843 try {
1844 lastSubtypeId = getSubtypeIdFromHashCode(
1845 imi, Integer.valueOf(subtypeHashCode));
1846 } catch (NumberFormatException e) {
1847 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
1848 }
1849 }
1850 }
1851 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09001852 }
1853
1854 private int getSelectedInputMethodSubtypeId(String id) {
1855 InputMethodInfo imi = mMethodMap.get(id);
1856 if (imi == null) {
1857 return NOT_A_SUBTYPE_ID;
1858 }
satokab751aa2010-09-14 19:17:36 +09001859 int subtypeId;
1860 try {
1861 subtypeId = Settings.Secure.getInt(mContext.getContentResolver(),
1862 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE);
1863 } catch (SettingNotFoundException e) {
1864 return NOT_A_SUBTYPE_ID;
1865 }
satok723a27e2010-11-11 14:58:11 +09001866 return getSubtypeIdFromHashCode(imi, subtypeId);
1867 }
1868
1869 private int getSubtypeIdFromHashCode(InputMethodInfo imi, int subtypeHashCode) {
1870 ArrayList<InputMethodSubtype> subtypes = imi.getSubtypes();
satokab751aa2010-09-14 19:17:36 +09001871 for (int i = 0; i < subtypes.size(); ++i) {
1872 InputMethodSubtype ims = subtypes.get(i);
satok723a27e2010-11-11 14:58:11 +09001873 if (subtypeHashCode == ims.hashCode()) {
satokab751aa2010-09-14 19:17:36 +09001874 return i;
1875 }
1876 }
1877 return NOT_A_SUBTYPE_ID;
1878 }
1879
satok8fbb1e82010-11-02 23:15:58 +09001880 // If there are no selected subtypes, tries finding the most applicable one according to the
1881 // current system locale
1882 private int findApplicableSubtype(String id) {
1883 InputMethodInfo imi = mMethodMap.get(id);
1884 if (imi == null) {
1885 return NOT_A_SUBTYPE_ID;
1886 }
1887 ArrayList<InputMethodSubtype> subtypes = imi.getSubtypes();
1888 if (subtypes == null || subtypes.size() == 0) {
1889 return NOT_A_SUBTYPE_ID;
1890 }
1891 final String locale = mContext.getResources().getConfiguration().locale.toString();
1892 final String language = locale.substring(0, 2);
1893 boolean partialMatchFound = false;
1894 int applicableSubtypeId = DEFAULT_SUBTYPE_ID;
1895 for (int i = 0; i < subtypes.size(); ++i) {
1896 final String subtypeLocale = subtypes.get(i).getLocale();
satok9ef02832010-11-04 21:17:48 +09001897 // An applicable subtype should be a keyboard subtype
1898 if (subtypes.get(i).getMode().equalsIgnoreCase("keyboard")) {
1899 if (locale.equals(subtypeLocale)) {
1900 // Exact match (e.g. system locale is "en_US" and subtype locale is "en_US")
1901 applicableSubtypeId = i;
1902 break;
1903 } else if (!partialMatchFound && subtypeLocale.startsWith(language)) {
1904 // Partial match (e.g. system locale is "en_US" and subtype locale is "en")
1905 applicableSubtypeId = i;
1906 partialMatchFound = true;
1907 }
satok8fbb1e82010-11-02 23:15:58 +09001908 }
1909 }
1910
1911 // The first subtype applicable to the system locale will be defined as the most applicable
1912 // subtype.
1913 if (DEBUG) {
1914 Slog.d(TAG, "Applicable InputMethodSubtype was found: " + applicableSubtypeId
1915 + subtypes.get(applicableSubtypeId).getLocale());
1916 }
1917 return applicableSubtypeId;
1918 }
1919
satokab751aa2010-09-14 19:17:36 +09001920 /**
1921 * @return Return the current subtype of this input method.
1922 */
1923 public InputMethodSubtype getCurrentInputMethodSubtype() {
satok8fbb1e82010-11-02 23:15:58 +09001924 boolean subtypeIsSelected = false;
1925 try {
1926 subtypeIsSelected = Settings.Secure.getInt(mContext.getContentResolver(),
1927 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE) != NOT_A_SUBTYPE_ID;
1928 } catch (SettingNotFoundException e) {
1929 }
1930 if (!subtypeIsSelected || mCurrentSubtype == null) {
1931 String lastInputMethodId = Settings.Secure.getString(mContext
1932 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
1933 int subtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
1934 if (subtypeId == NOT_A_SUBTYPE_ID) {
1935 subtypeId = findApplicableSubtype(lastInputMethodId);
1936 }
1937 if (subtypeId != NOT_A_SUBTYPE_ID) {
1938 mCurrentSubtype = mMethodMap.get(lastInputMethodId).getSubtypes().get(subtypeId);
1939 }
1940 }
satokab751aa2010-09-14 19:17:36 +09001941 return mCurrentSubtype;
1942 }
1943
satokd87c2592010-09-29 11:52:06 +09001944 /**
1945 * Utility class for putting and getting settings for InputMethod
1946 * TODO: Move all putters and getters of settings to this class.
1947 */
1948 private static class InputMethodSettings {
1949 // The string for enabled input method is saved as follows:
1950 // example: ("ime0;subtype0;subtype1;subtype2:ime1:ime2;subtype0")
1951 private static final char INPUT_METHOD_SEPARATER = ':';
1952 private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';';
satok723a27e2010-11-11 14:58:11 +09001953 private final TextUtils.SimpleStringSplitter mInputMethodSplitter =
satokd87c2592010-09-29 11:52:06 +09001954 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER);
1955
satok723a27e2010-11-11 14:58:11 +09001956 private final TextUtils.SimpleStringSplitter mSubtypeSplitter =
satokd87c2592010-09-29 11:52:06 +09001957 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATER);
1958
1959 private final ContentResolver mResolver;
1960 private final HashMap<String, InputMethodInfo> mMethodMap;
1961 private final ArrayList<InputMethodInfo> mMethodList;
1962
1963 private String mEnabledInputMethodsStrCache;
1964
1965 private static void buildEnabledInputMethodsSettingString(
1966 StringBuilder builder, Pair<String, ArrayList<String>> pair) {
1967 String id = pair.first;
1968 ArrayList<String> subtypes = pair.second;
1969 builder.append(id);
satok57c767c2010-11-01 22:34:08 +09001970 // Inputmethod and subtypes are saved in the settings as follows:
1971 // ime0;subtype0;subtype1:ime1;subtype0:ime2:ime3;subtype0;subtype1
1972 for (String subtypeId: subtypes) {
1973 builder.append(INPUT_METHOD_SUBTYPE_SEPARATER).append(subtypeId);
satokd87c2592010-09-29 11:52:06 +09001974 }
1975 }
1976
1977 public InputMethodSettings(
1978 ContentResolver resolver, HashMap<String, InputMethodInfo> methodMap,
1979 ArrayList<InputMethodInfo> methodList) {
1980 mResolver = resolver;
1981 mMethodMap = methodMap;
1982 mMethodList = methodList;
1983 }
1984
1985 public List<InputMethodInfo> getEnabledInputMethodListLocked() {
1986 return createEnabledInputMethodListLocked(
1987 getEnabledInputMethodsAndSubtypeListLocked());
1988 }
1989
satok7f35c8c2010-10-07 21:13:11 +09001990 public List<Pair<InputMethodInfo, ArrayList<String>>>
1991 getEnabledInputMethodAndSubtypeListLocked() {
1992 return createEnabledInputMethodAndSubtypeListLocked(
1993 getEnabledInputMethodsAndSubtypeListLocked());
1994 }
1995
satokd87c2592010-09-29 11:52:06 +09001996 // At the initial boot, the settings for input methods are not set,
1997 // so we need to enable IME in that case.
1998 public void enableAllIMEsIfThereIsNoEnabledIME() {
1999 if (TextUtils.isEmpty(getEnabledInputMethodsStr())) {
2000 StringBuilder sb = new StringBuilder();
2001 final int N = mMethodList.size();
2002 for (int i = 0; i < N; i++) {
2003 InputMethodInfo imi = mMethodList.get(i);
2004 Slog.i(TAG, "Adding: " + imi.getId());
2005 if (i > 0) sb.append(':');
2006 sb.append(imi.getId());
2007 }
2008 putEnabledInputMethodsStr(sb.toString());
2009 }
2010 }
2011
2012 public List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() {
2013 ArrayList<Pair<String, ArrayList<String>>> imsList
2014 = new ArrayList<Pair<String, ArrayList<String>>>();
2015 final String enabledInputMethodsStr = getEnabledInputMethodsStr();
2016 if (TextUtils.isEmpty(enabledInputMethodsStr)) {
2017 return imsList;
2018 }
satok723a27e2010-11-11 14:58:11 +09002019 mInputMethodSplitter.setString(enabledInputMethodsStr);
2020 while (mInputMethodSplitter.hasNext()) {
2021 String nextImsStr = mInputMethodSplitter.next();
2022 mSubtypeSplitter.setString(nextImsStr);
2023 if (mSubtypeSplitter.hasNext()) {
satokd87c2592010-09-29 11:52:06 +09002024 ArrayList<String> subtypeHashes = new ArrayList<String>();
2025 // The first element is ime id.
satok723a27e2010-11-11 14:58:11 +09002026 String imeId = mSubtypeSplitter.next();
2027 while (mSubtypeSplitter.hasNext()) {
2028 subtypeHashes.add(mSubtypeSplitter.next());
satokd87c2592010-09-29 11:52:06 +09002029 }
2030 imsList.add(new Pair<String, ArrayList<String>>(imeId, subtypeHashes));
2031 }
2032 }
2033 return imsList;
2034 }
2035
2036 public void appendAndPutEnabledInputMethodLocked(String id, boolean reloadInputMethodStr) {
2037 if (reloadInputMethodStr) {
2038 getEnabledInputMethodsStr();
2039 }
2040 if (TextUtils.isEmpty(mEnabledInputMethodsStrCache)) {
2041 // Add in the newly enabled input method.
2042 putEnabledInputMethodsStr(id);
2043 } else {
2044 putEnabledInputMethodsStr(
2045 mEnabledInputMethodsStrCache + INPUT_METHOD_SEPARATER + id);
2046 }
2047 }
2048
2049 /**
2050 * Build and put a string of EnabledInputMethods with removing specified Id.
2051 * @return the specified id was removed or not.
2052 */
2053 public boolean buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2054 StringBuilder builder, List<Pair<String, ArrayList<String>>> imsList, String id) {
2055 boolean isRemoved = false;
2056 boolean needsAppendSeparator = false;
2057 for (Pair<String, ArrayList<String>> ims: imsList) {
2058 String curId = ims.first;
2059 if (curId.equals(id)) {
2060 // We are disabling this input method, and it is
2061 // currently enabled. Skip it to remove from the
2062 // new list.
2063 isRemoved = true;
2064 } else {
2065 if (needsAppendSeparator) {
2066 builder.append(INPUT_METHOD_SEPARATER);
2067 } else {
2068 needsAppendSeparator = true;
2069 }
2070 buildEnabledInputMethodsSettingString(builder, ims);
2071 }
2072 }
2073 if (isRemoved) {
2074 // Update the setting with the new list of input methods.
2075 putEnabledInputMethodsStr(builder.toString());
2076 }
2077 return isRemoved;
2078 }
2079
2080 private List<InputMethodInfo> createEnabledInputMethodListLocked(
2081 List<Pair<String, ArrayList<String>>> imsList) {
2082 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
2083 for (Pair<String, ArrayList<String>> ims: imsList) {
2084 InputMethodInfo info = mMethodMap.get(ims.first);
2085 if (info != null) {
2086 res.add(info);
2087 }
2088 }
2089 return res;
2090 }
2091
satok7f35c8c2010-10-07 21:13:11 +09002092 private List<Pair<InputMethodInfo, ArrayList<String>>>
2093 createEnabledInputMethodAndSubtypeListLocked(
2094 List<Pair<String, ArrayList<String>>> imsList) {
2095 final ArrayList<Pair<InputMethodInfo, ArrayList<String>>> res
2096 = new ArrayList<Pair<InputMethodInfo, ArrayList<String>>>();
2097 for (Pair<String, ArrayList<String>> ims : imsList) {
2098 InputMethodInfo info = mMethodMap.get(ims.first);
2099 if (info != null) {
2100 res.add(new Pair<InputMethodInfo, ArrayList<String>>(info, ims.second));
2101 }
2102 }
2103 return res;
2104 }
2105
satokd87c2592010-09-29 11:52:06 +09002106 private void putEnabledInputMethodsStr(String str) {
2107 Settings.Secure.putString(mResolver, Settings.Secure.ENABLED_INPUT_METHODS, str);
2108 mEnabledInputMethodsStrCache = str;
2109 }
2110
2111 private String getEnabledInputMethodsStr() {
2112 mEnabledInputMethodsStrCache = Settings.Secure.getString(
2113 mResolver, Settings.Secure.ENABLED_INPUT_METHODS);
satok723a27e2010-11-11 14:58:11 +09002114 if (DEBUG) {
2115 Slog.d(TAG, "getEnabledInputMethodsStr: " + mEnabledInputMethodsStrCache);
2116 }
satokd87c2592010-09-29 11:52:06 +09002117 return mEnabledInputMethodsStrCache;
2118 }
satok723a27e2010-11-11 14:58:11 +09002119
2120 private void saveSubtypeHistory(
2121 List<Pair<String, String>> savedImes, String newImeId, String newSubtypeId) {
2122 StringBuilder builder = new StringBuilder();
2123 boolean isImeAdded = false;
2124 if (!TextUtils.isEmpty(newImeId) && !TextUtils.isEmpty(newSubtypeId)) {
2125 builder.append(newImeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2126 newSubtypeId);
2127 isImeAdded = true;
2128 }
2129 for (Pair<String, String> ime: savedImes) {
2130 String imeId = ime.first;
2131 String subtypeId = ime.second;
2132 if (TextUtils.isEmpty(subtypeId)) {
2133 subtypeId = NOT_A_SUBTYPE_ID_STR;
2134 }
2135 if (isImeAdded) {
2136 builder.append(INPUT_METHOD_SEPARATER);
2137 } else {
2138 isImeAdded = true;
2139 }
2140 builder.append(imeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2141 subtypeId);
2142 }
2143 // Remove the last INPUT_METHOD_SEPARATER
2144 putSubtypeHistoryStr(builder.toString());
2145 }
2146
2147 public void addSubtypeToHistory(String imeId, String subtypeId) {
2148 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
2149 for (Pair<String, String> ime: subtypeHistory) {
2150 if (ime.first.equals(imeId)) {
2151 if (DEBUG) {
2152 Slog.v(TAG, "Subtype found in the history: " + imeId
2153 + ime.second);
2154 }
2155 // We should break here
2156 subtypeHistory.remove(ime);
2157 break;
2158 }
2159 }
2160 saveSubtypeHistory(subtypeHistory, imeId, subtypeId);
2161 }
2162
2163 private void putSubtypeHistoryStr(String str) {
2164 if (DEBUG) {
2165 Slog.d(TAG, "putSubtypeHistoryStr: " + str);
2166 }
2167 Settings.Secure.putString(
2168 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, str);
2169 }
2170
2171 public Pair<String, String> getLastInputMethodAndSubtypeLocked() {
2172 // Gets the first one from the history
2173 return getLastSubtypeForInputMethodLockedInternal(null);
2174 }
2175
2176 public String getLastSubtypeForInputMethodLocked(String imeId) {
2177 Pair<String, String> ime = getLastSubtypeForInputMethodLockedInternal(imeId);
2178 if (ime != null) {
2179 return ime.second;
2180 } else {
2181 return null;
2182 }
2183 }
2184
2185 private Pair<String, String> getLastSubtypeForInputMethodLockedInternal(String imeId) {
2186 List<Pair<String, ArrayList<String>>> enabledImes =
2187 getEnabledInputMethodsAndSubtypeListLocked();
2188 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
2189 for (Pair<String, String> imeAndSubtype: subtypeHistory) {
2190 final String imeInTheHistory = imeAndSubtype.first;
2191 // If imeId is empty, returns the first IME and subtype in the history
2192 if (TextUtils.isEmpty(imeId) || imeInTheHistory.equals(imeId)) {
2193 final String subtypeInTheHistory = imeAndSubtype.second;
2194 final String subtypeHashCode = getEnabledSubtypeForInputMethodAndSubtypeLocked(
2195 enabledImes, imeInTheHistory, subtypeInTheHistory);
2196 if (!TextUtils.isEmpty(subtypeHashCode)) {
2197 if (DEBUG) {
2198 Slog.d(TAG, "Enabled subtype found in the history:" + subtypeHashCode);
2199 }
2200 return new Pair<String, String>(imeInTheHistory, subtypeHashCode);
2201 }
2202 }
2203 }
2204 if (DEBUG) {
2205 Slog.d(TAG, "No enabled IME found in the history");
2206 }
2207 return null;
2208 }
2209
2210 private String getEnabledSubtypeForInputMethodAndSubtypeLocked(List<Pair<String,
2211 ArrayList<String>>> enabledImes, String imeId, String subtypeHashCode) {
2212 for (Pair<String, ArrayList<String>> enabledIme: enabledImes) {
2213 if (enabledIme.first.equals(imeId)) {
2214 for (String s: enabledIme.second) {
2215 if (s.equals(subtypeHashCode)) {
2216 // If both imeId and subtypeId are enabled, return subtypeId.
2217 return s;
2218 }
2219 }
2220 // If imeId was enabled but subtypeId was disabled.
2221 return NOT_A_SUBTYPE_ID_STR;
2222 }
2223 }
2224 // If both imeId and subtypeId are disabled, return null
2225 return null;
2226 }
2227
2228 private List<Pair<String, String>> loadInputMethodAndSubtypeHistoryLocked() {
2229 ArrayList<Pair<String, String>> imsList = new ArrayList<Pair<String, String>>();
2230 final String subtypeHistoryStr = getSubtypeHistoryStr();
2231 if (TextUtils.isEmpty(subtypeHistoryStr)) {
2232 return imsList;
2233 }
2234 mInputMethodSplitter.setString(subtypeHistoryStr);
2235 while (mInputMethodSplitter.hasNext()) {
2236 String nextImsStr = mInputMethodSplitter.next();
2237 mSubtypeSplitter.setString(nextImsStr);
2238 if (mSubtypeSplitter.hasNext()) {
2239 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2240 // The first element is ime id.
2241 String imeId = mSubtypeSplitter.next();
2242 while (mSubtypeSplitter.hasNext()) {
2243 subtypeId = mSubtypeSplitter.next();
2244 break;
2245 }
2246 imsList.add(new Pair<String, String>(imeId, subtypeId));
2247 }
2248 }
2249 return imsList;
2250 }
2251
2252 private String getSubtypeHistoryStr() {
2253 if (DEBUG) {
2254 Slog.d(TAG, "getSubtypeHistoryStr: " + Settings.Secure.getString(
2255 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY));
2256 }
2257 return Settings.Secure.getString(
2258 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY);
2259 }
2260
2261 public void putSelectedInputMethod(String imeId) {
2262 Settings.Secure.putString(mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, imeId);
2263 }
2264
2265 public void putSelectedSubtype(int subtypeId) {
2266 Settings.Secure.putInt(
2267 mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, subtypeId);
2268 }
satokd87c2592010-09-29 11:52:06 +09002269 }
2270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002271 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002273 @Override
2274 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2275 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
2276 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002278 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
2279 + Binder.getCallingPid()
2280 + ", uid=" + Binder.getCallingUid());
2281 return;
2282 }
2283
2284 IInputMethod method;
2285 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002287 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002289 synchronized (mMethodMap) {
2290 p.println("Current Input Method Manager state:");
2291 int N = mMethodList.size();
2292 p.println(" Input Methods:");
2293 for (int i=0; i<N; i++) {
2294 InputMethodInfo info = mMethodList.get(i);
2295 p.println(" InputMethod #" + i + ":");
2296 info.dump(p, " ");
2297 }
2298 p.println(" Clients:");
2299 for (ClientState ci : mClients.values()) {
2300 p.println(" Client " + ci + ":");
2301 p.println(" client=" + ci.client);
2302 p.println(" inputContext=" + ci.inputContext);
2303 p.println(" sessionRequested=" + ci.sessionRequested);
2304 p.println(" curSession=" + ci.curSession);
2305 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002306 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002307 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002308 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
2309 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002310 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
2311 + " mBoundToMethod=" + mBoundToMethod);
2312 p.println(" mCurToken=" + mCurToken);
2313 p.println(" mCurIntent=" + mCurIntent);
2314 method = mCurMethod;
2315 p.println(" mCurMethod=" + mCurMethod);
2316 p.println(" mEnabledSession=" + mEnabledSession);
2317 p.println(" mShowRequested=" + mShowRequested
2318 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
2319 + " mShowForced=" + mShowForced
2320 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07002321 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002322 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002323
Jeff Brownb88102f2010-09-08 11:49:43 -07002324 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002325 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002326 pw.flush();
2327 try {
2328 client.client.asBinder().dump(fd, args);
2329 } catch (RemoteException e) {
2330 p.println("Input method client dead: " + e);
2331 }
Jeff Brownb88102f2010-09-08 11:49:43 -07002332 } else {
2333 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002334 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002335
Jeff Brownb88102f2010-09-08 11:49:43 -07002336 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002337 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002338 pw.flush();
2339 try {
2340 method.asBinder().dump(fd, args);
2341 } catch (RemoteException e) {
2342 p.println("Input method service dead: " + e);
2343 }
Jeff Brownb88102f2010-09-08 11:49:43 -07002344 } else {
2345 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002346 }
2347 }
2348}