blob: e14345b9ea7cafc38c11e9ba0e75c6cb5f8c3ed2 [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
satok67ddf9c2010-11-17 09:45:54 +0900558 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi) {
559 synchronized (mMethodMap) {
satok884ef9a2010-11-18 10:39:46 +0900560 if (imi == null && mCurMethodId != null) {
561 imi = mMethodMap.get(mCurMethodId);
562 }
satok67ddf9c2010-11-17 09:45:54 +0900563 return mSettings.getEnabledInputMethodSubtypeListLocked(imi);
564 }
565 }
566
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 public void addClient(IInputMethodClient client,
568 IInputContext inputContext, int uid, int pid) {
569 synchronized (mMethodMap) {
570 mClients.put(client.asBinder(), new ClientState(client,
571 inputContext, uid, pid));
572 }
573 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800574
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800575 public void removeClient(IInputMethodClient client) {
576 synchronized (mMethodMap) {
577 mClients.remove(client.asBinder());
578 }
579 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800580
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800581 void executeOrSendMessage(IInterface target, Message msg) {
582 if (target.asBinder() instanceof Binder) {
583 mCaller.sendMessage(msg);
584 } else {
585 handleMessage(msg);
586 msg.recycle();
587 }
588 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800589
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700590 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800592 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593 + mCurClient.client.asBinder());
594 if (mBoundToMethod) {
595 mBoundToMethod = false;
596 if (mCurMethod != null) {
597 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
598 MSG_UNBIND_INPUT, mCurMethod));
599 }
600 }
601 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
602 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
603 mCurClient.sessionRequested = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800604
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 // Call setActive(false) on the old client
606 try {
607 mCurClient.client.setActive(false);
608 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800609 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 + mCurClient.pid + " uid " + mCurClient.uid);
611 }
612 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800613
The Android Open Source Project10592532009-03-18 17:39:46 -0700614 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615 }
616 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800617
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 private int getImeShowFlags() {
619 int flags = 0;
620 if (mShowForced) {
621 flags |= InputMethod.SHOW_FORCED
622 | InputMethod.SHOW_EXPLICIT;
623 } else if (mShowExplicitlyRequested) {
624 flags |= InputMethod.SHOW_EXPLICIT;
625 }
626 return flags;
627 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800628
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800629 private int getAppShowFlags() {
630 int flags = 0;
631 if (mShowForced) {
632 flags |= InputMethodManager.SHOW_FORCED;
633 } else if (!mShowExplicitlyRequested) {
634 flags |= InputMethodManager.SHOW_IMPLICIT;
635 }
636 return flags;
637 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800638
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639 InputBindResult attachNewInputLocked(boolean initial, boolean needResult) {
640 if (!mBoundToMethod) {
641 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
642 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
643 mBoundToMethod = true;
644 }
645 final SessionState session = mCurClient.curSession;
646 if (initial) {
647 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
648 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
649 } else {
650 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
651 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
652 }
653 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800654 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -0800655 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656 }
657 return needResult
658 ? new InputBindResult(session.session, mCurId, mCurSeq)
659 : null;
660 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800661
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800662 InputBindResult startInputLocked(IInputMethodClient client,
663 IInputContext inputContext, EditorInfo attribute,
664 boolean initial, boolean needResult) {
665 // If no method is currently selected, do nothing.
666 if (mCurMethodId == null) {
667 return mNoBinding;
668 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800669
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670 ClientState cs = mClients.get(client.asBinder());
671 if (cs == null) {
672 throw new IllegalArgumentException("unknown client "
673 + client.asBinder());
674 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800675
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 try {
677 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
678 // Check with the window manager to make sure this client actually
679 // has a window with focus. If not, reject. This is thread safe
680 // because if the focus changes some time before or after, the
681 // next client receiving focus that has any interest in input will
682 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800683 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
685 return null;
686 }
687 } catch (RemoteException e) {
688 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800689
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800690 if (mCurClient != cs) {
691 // If the client is changing, we need to switch over to the new
692 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700693 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800694 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695 + cs.client.asBinder());
696
697 // If the screen is on, inform the new client it is active
698 if (mScreenOn) {
699 try {
700 cs.client.setActive(mScreenOn);
701 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800702 Slog.w(TAG, "Got RemoteException sending setActive notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800703 + cs.pid + " uid " + cs.uid);
704 }
705 }
706 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800707
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800708 // Bump up the sequence for this client and attach it.
709 mCurSeq++;
710 if (mCurSeq <= 0) mCurSeq = 1;
711 mCurClient = cs;
712 mCurInputContext = inputContext;
713 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800714
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800715 // Check if the input method is changing.
716 if (mCurId != null && mCurId.equals(mCurMethodId)) {
717 if (cs.curSession != null) {
718 // Fast case: if we are already connected to the input method,
719 // then just return it.
720 return attachNewInputLocked(initial, needResult);
721 }
722 if (mHaveConnection) {
723 if (mCurMethod != null) {
724 if (!cs.sessionRequested) {
725 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800726 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800727 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
728 MSG_CREATE_SESSION, mCurMethod,
729 new MethodCallback(mCurMethod)));
730 }
731 // Return to client, and we will get back with it when
732 // we have had a session made for it.
733 return new InputBindResult(null, mCurId, mCurSeq);
734 } else if (SystemClock.uptimeMillis()
735 < (mLastBindTime+TIME_TO_RECONNECT)) {
736 // In this case we have connected to the service, but
737 // don't yet have its interface. If it hasn't been too
738 // long since we did the connection, we'll return to
739 // the client and wait to get the service interface so
740 // we can report back. If it has been too long, we want
741 // to fall through so we can try a disconnect/reconnect
742 // to see if we can get back in touch with the service.
743 return new InputBindResult(null, mCurId, mCurSeq);
744 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800745 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
746 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800747 }
748 }
749 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800750
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700751 return startInputInnerLocked();
752 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800753
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700754 InputBindResult startInputInnerLocked() {
755 if (mCurMethodId == null) {
756 return mNoBinding;
757 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800758
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700759 if (!mSystemReady) {
760 // If the system is not yet ready, we shouldn't be running third
761 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -0700762 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700763 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800764
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800765 InputMethodInfo info = mMethodMap.get(mCurMethodId);
766 if (info == null) {
767 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
768 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800769
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700770 unbindCurrentMethodLocked(false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800771
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800772 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
773 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -0700774 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
775 com.android.internal.R.string.input_method_binding_label);
776 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
777 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800778 if (mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE)) {
779 mLastBindTime = SystemClock.uptimeMillis();
780 mHaveConnection = true;
781 mCurId = info.getId();
782 mCurToken = new Binder();
783 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800784 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800785 mIWindowManager.addWindowToken(mCurToken,
786 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
787 } catch (RemoteException e) {
788 }
789 return new InputBindResult(null, mCurId, mCurSeq);
790 } else {
791 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800792 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800793 + mCurIntent);
794 }
795 return null;
796 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800797
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800798 public InputBindResult startInput(IInputMethodClient client,
799 IInputContext inputContext, EditorInfo attribute,
800 boolean initial, boolean needResult) {
801 synchronized (mMethodMap) {
802 final long ident = Binder.clearCallingIdentity();
803 try {
804 return startInputLocked(client, inputContext, attribute,
805 initial, needResult);
806 } finally {
807 Binder.restoreCallingIdentity(ident);
808 }
809 }
810 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800811
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800812 public void finishInput(IInputMethodClient client) {
813 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800814
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800815 public void onServiceConnected(ComponentName name, IBinder service) {
816 synchronized (mMethodMap) {
817 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
818 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -0700819 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800820 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborncc278702009-09-02 23:07:23 -0700821 unbindCurrentMethodLocked(false);
822 return;
823 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800824 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -0700825 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
826 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800827 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800828 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -0700829 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800830 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -0700831 MSG_CREATE_SESSION, mCurMethod,
832 new MethodCallback(mCurMethod)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800833 }
834 }
835 }
836 }
837
838 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
839 synchronized (mMethodMap) {
840 if (mCurMethod != null && method != null
841 && mCurMethod.asBinder() == method.asBinder()) {
842 if (mCurClient != null) {
843 mCurClient.curSession = new SessionState(mCurClient,
844 method, session);
845 mCurClient.sessionRequested = false;
846 InputBindResult res = attachNewInputLocked(true, true);
847 if (res.method != null) {
848 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
849 MSG_BIND_METHOD, mCurClient.client, res));
850 }
851 }
852 }
853 }
854 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800855
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700856 void unbindCurrentMethodLocked(boolean reportToClient) {
857 if (mHaveConnection) {
858 mContext.unbindService(this);
859 mHaveConnection = false;
860 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800861
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700862 if (mCurToken != null) {
863 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800864 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700865 mIWindowManager.removeWindowToken(mCurToken);
866 } catch (RemoteException e) {
867 }
868 mCurToken = null;
869 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800870
The Android Open Source Project10592532009-03-18 17:39:46 -0700871 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700872 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800873
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700874 if (reportToClient && mCurClient != null) {
875 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
876 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
877 }
878 }
879
Devin Taylor0c33ed22010-02-23 13:26:46 -0600880 private void finishSession(SessionState sessionState) {
881 if (sessionState != null && sessionState.session != null) {
882 try {
883 sessionState.session.finishSession();
884 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -0700885 Slog.w(TAG, "Session failed to close due to remote exception", e);
Devin Taylor0c33ed22010-02-23 13:26:46 -0600886 }
887 }
888 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800889
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700890 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800891 if (mCurMethod != null) {
892 for (ClientState cs : mClients.values()) {
893 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -0600894 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800895 cs.curSession = null;
896 }
Devin Taylor0c33ed22010-02-23 13:26:46 -0600897
898 finishSession(mEnabledSession);
899 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800900 mCurMethod = null;
901 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700902 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800903 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800904
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800905 public void onServiceDisconnected(ComponentName name) {
906 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800907 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800908 + " mCurIntent=" + mCurIntent);
909 if (mCurMethod != null && mCurIntent != null
910 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700911 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800912 // We consider this to be a new bind attempt, since the system
913 // should now try to restart the service for us.
914 mLastBindTime = SystemClock.uptimeMillis();
915 mShowRequested = mInputShown;
916 mInputShown = false;
917 if (mCurClient != null) {
918 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
919 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
920 }
921 }
922 }
923 }
924
925 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700926 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800927 long ident = Binder.clearCallingIdentity();
928 try {
929 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700930 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800931 return;
932 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800933
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800934 synchronized (mMethodMap) {
935 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800936 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -0700937 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800938 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800939 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -0700940 mStatusBar.setIcon("ime", packageName, iconId, 0);
941 mStatusBar.setIconVisibility("ime", true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800942 }
943 }
944 } finally {
945 Binder.restoreCallingIdentity(ident);
946 }
947 }
948
satok06487a52010-10-29 11:37:18 +0900949 public void setIMEButtonVisible(IBinder token, boolean visible) {
950 int uid = Binder.getCallingUid();
951 long ident = Binder.clearCallingIdentity();
952 try {
953 if (token == null || mCurToken != token) {
954 Slog.w(TAG, "Ignoring setIMEButtonVisible of uid " + uid + " token: " + token);
955 return;
956 }
957
958 synchronized (mMethodMap) {
959 mStatusBar.setIMEButtonVisible(visible);
960 }
961 } finally {
962 Binder.restoreCallingIdentity(ident);
963 }
964 }
965
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800966 void updateFromSettingsLocked() {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700967 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
968 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
969 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
970 // enabled.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800971 String id = Settings.Secure.getString(mContext.getContentResolver(),
satokab751aa2010-09-14 19:17:36 +0900972 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +0900973 // There is no input method selected, try to choose new applicable input method.
974 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
975 id = Settings.Secure.getString(mContext.getContentResolver(),
976 Settings.Secure.DEFAULT_INPUT_METHOD);
977 }
978 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800979 try {
satokab751aa2010-09-14 19:17:36 +0900980 setInputMethodLocked(id, getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800981 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800982 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -0700983 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700984 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800985 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700986 } else {
987 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -0700988 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700989 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990 }
991 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800992
satokab751aa2010-09-14 19:17:36 +0900993 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800994 InputMethodInfo info = mMethodMap.get(id);
995 if (info == null) {
satok913a8922010-08-26 21:53:41 +0900996 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800997 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800998
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800999 if (id.equals(mCurMethodId)) {
satokb66d2872010-11-10 01:04:04 +09001000 ArrayList<InputMethodSubtype> subtypes = info.getSubtypes();
1001 if (subtypeId >= 0 && subtypeId < subtypes.size()) {
1002 InputMethodSubtype subtype = subtypes.get(subtypeId);
satokab751aa2010-09-14 19:17:36 +09001003 if (subtype != mCurrentSubtype) {
1004 synchronized (mMethodMap) {
1005 if (mCurMethod != null) {
1006 try {
satok723a27e2010-11-11 14:58:11 +09001007 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
satok06e07442010-11-02 19:46:55 +09001008 if (mInputShown) {
1009 // If mInputShown is false, there is no IME button on the
1010 // system bar.
1011 // Thus there is no need to make it invisible explicitly.
1012 mStatusBar.setIMEButtonVisible(true);
1013 }
satokab751aa2010-09-14 19:17:36 +09001014 mCurMethod.changeInputMethodSubtype(subtype);
1015 } catch (RemoteException e) {
1016 return;
1017 }
1018 }
1019 }
1020 }
1021 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001022 return;
1023 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001024
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 final long ident = Binder.clearCallingIdentity();
1026 try {
satokab751aa2010-09-14 19:17:36 +09001027 // Set a subtype to this input method.
1028 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001029 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1030 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1031 // because mCurMethodId is stored as a history in
1032 // setSelectedInputMethodAndSubtypeLocked().
1033 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001034
1035 if (ActivityManagerNative.isSystemReady()) {
1036 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001037 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001038 intent.putExtra("input_method_id", id);
1039 mContext.sendBroadcast(intent);
1040 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001041 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 } finally {
1043 Binder.restoreCallingIdentity(ident);
1044 }
1045 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001046
The Android Open Source Project4df24232009-03-05 14:34:35 -08001047 public boolean showSoftInput(IInputMethodClient client, int flags,
1048 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001049 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001050 long ident = Binder.clearCallingIdentity();
1051 try {
1052 synchronized (mMethodMap) {
1053 if (mCurClient == null || client == null
1054 || mCurClient.client.asBinder() != client.asBinder()) {
1055 try {
1056 // We need to check if this is the current client with
1057 // focus in the window manager, to allow this call to
1058 // be made before input is started in it.
1059 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001060 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001061 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062 }
1063 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001064 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001065 }
1066 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001067
Joe Onorato8a9b2202010-02-26 18:56:32 -08001068 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001069 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001070 }
1071 } finally {
1072 Binder.restoreCallingIdentity(ident);
1073 }
1074 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001075
The Android Open Source Project4df24232009-03-05 14:34:35 -08001076 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001077 mShowRequested = true;
1078 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1079 mShowExplicitlyRequested = true;
1080 }
1081 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1082 mShowExplicitlyRequested = true;
1083 mShowForced = true;
1084 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001085
Dianne Hackborncc278702009-09-02 23:07:23 -07001086 if (!mSystemReady) {
1087 return false;
1088 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001089
The Android Open Source Project4df24232009-03-05 14:34:35 -08001090 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001092 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1093 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1094 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095 mInputShown = true;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001096 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097 } else if (mHaveConnection && SystemClock.uptimeMillis()
1098 < (mLastBindTime+TIME_TO_RECONNECT)) {
1099 // The client has asked to have the input method shown, but
1100 // we have been sitting here too long with a connection to the
1101 // service and no interface received, so let's disconnect/connect
1102 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001103 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001104 SystemClock.uptimeMillis()-mLastBindTime,1);
1105 mContext.unbindService(this);
1106 mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE);
1107 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001108
The Android Open Source Project4df24232009-03-05 14:34:35 -08001109 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001110 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001111
The Android Open Source Project4df24232009-03-05 14:34:35 -08001112 public boolean hideSoftInput(IInputMethodClient client, int flags,
1113 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001114 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001115 long ident = Binder.clearCallingIdentity();
1116 try {
1117 synchronized (mMethodMap) {
1118 if (mCurClient == null || client == null
1119 || mCurClient.client.asBinder() != client.asBinder()) {
1120 try {
1121 // We need to check if this is the current client with
1122 // focus in the window manager, to allow this call to
1123 // be made before input is started in it.
1124 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001125 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1126 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001127 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001128 }
1129 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001130 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001131 }
1132 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001133
Joe Onorato8a9b2202010-02-26 18:56:32 -08001134 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001135 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001136 }
1137 } finally {
1138 Binder.restoreCallingIdentity(ident);
1139 }
1140 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001141
The Android Open Source Project4df24232009-03-05 14:34:35 -08001142 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1144 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001145 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001146 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001147 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001148 }
1149 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001150 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001152 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001153 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001154 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001155 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001156 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1157 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1158 res = true;
1159 } else {
1160 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001161 }
1162 mInputShown = false;
1163 mShowRequested = false;
1164 mShowExplicitlyRequested = false;
1165 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001166 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001167 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001168
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001169 public void windowGainedFocus(IInputMethodClient client, IBinder windowToken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001170 boolean viewHasFocus, boolean isTextEditor, int softInputMode,
1171 boolean first, int windowFlags) {
1172 long ident = Binder.clearCallingIdentity();
1173 try {
1174 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001175 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001176 + " viewHasFocus=" + viewHasFocus
1177 + " isTextEditor=" + isTextEditor
1178 + " softInputMode=#" + Integer.toHexString(softInputMode)
1179 + " first=" + first + " flags=#"
1180 + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001182 if (mCurClient == null || client == null
1183 || mCurClient.client.asBinder() != client.asBinder()) {
1184 try {
1185 // We need to check if this is the current client with
1186 // focus in the window manager, to allow this call to
1187 // be made before input is started in it.
1188 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001189 Slog.w(TAG, "Client not active, ignoring focus gain of: " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001190 return;
1191 }
1192 } catch (RemoteException e) {
1193 }
1194 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001195
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001196 if (mCurFocusedWindow == windowToken) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001197 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001198 return;
1199 }
1200 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001201
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001202 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1203 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
1204 if (!isTextEditor || (softInputMode &
1205 WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1206 != WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE) {
1207 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1208 // There is no focus view, and this window will
1209 // be behind any soft input window, so hide the
1210 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001211 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001212 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 }
1214 } else if (isTextEditor && (softInputMode &
1215 WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1216 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1217 && (softInputMode &
1218 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
1219 // There is a focus view, and we are navigating forward
1220 // into the window, so show the input window for the user.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001221 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001222 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 }
1224 break;
1225 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1226 // Do nothing.
1227 break;
1228 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1229 if ((softInputMode &
1230 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001231 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001232 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001233 }
1234 break;
1235 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001236 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001237 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 break;
1239 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1240 if ((softInputMode &
1241 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001242 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001243 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001244 }
1245 break;
1246 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001247 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001248 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001249 break;
1250 }
1251 }
1252 } finally {
1253 Binder.restoreCallingIdentity(ident);
1254 }
1255 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001257 public void showInputMethodPickerFromClient(IInputMethodClient client) {
1258 synchronized (mMethodMap) {
1259 if (mCurClient == null || client == null
1260 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001261 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001262 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 }
1264
1265 mHandler.sendEmptyMessage(MSG_SHOW_IM_PICKER);
1266 }
1267 }
1268
satokab751aa2010-09-14 19:17:36 +09001269 public void showInputMethodSubtypePickerFromClient(IInputMethodClient client) {
1270 synchronized (mMethodMap) {
1271 if (mCurClient == null || client == null
1272 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001273 Slog.w(TAG, "Ignoring showInputMethodSubtypePickerFromClient of: " + client);
satokab751aa2010-09-14 19:17:36 +09001274 }
1275
1276 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1277 }
1278 }
1279
satok47a44912010-10-06 16:03:58 +09001280 public void showInputMethodAndSubtypeEnablerFromClient(
1281 IInputMethodClient client, String topId) {
1282 // TODO: Handle topId for setting the top position of the list activity
1283 synchronized (mMethodMap) {
1284 if (mCurClient == null || client == null
1285 || mCurClient.client.asBinder() != client.asBinder()) {
1286 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
1287 }
1288
1289 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_ENABLER);
1290 }
1291 }
1292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001293 public void setInputMethod(IBinder token, String id) {
satokab751aa2010-09-14 19:17:36 +09001294 setInputMethodWithSubtype(token, id, NOT_A_SUBTYPE_ID);
1295 }
1296
satok735cf382010-11-11 20:40:09 +09001297 public boolean switchToLastInputMethod(IBinder token) {
1298 synchronized (mMethodMap) {
1299 Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
1300 if (lastIme != null) {
1301 InputMethodInfo imi = mMethodMap.get(lastIme.first);
1302 if (imi != null) {
1303 setInputMethodWithSubtype(token, lastIme.first, getSubtypeIdFromHashCode(
1304 imi, Integer.valueOf(lastIme.second)));
1305 return true;
1306 }
1307 }
1308 return false;
1309 }
1310 }
1311
satokab751aa2010-09-14 19:17:36 +09001312 private void setInputMethodWithSubtype(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001313 synchronized (mMethodMap) {
1314 if (token == null) {
1315 if (mContext.checkCallingOrSelfPermission(
1316 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1317 != PackageManager.PERMISSION_GRANTED) {
1318 throw new SecurityException(
1319 "Using null token requires permission "
1320 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1321 }
1322 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001323 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
1324 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001325 return;
1326 }
1327
1328 long ident = Binder.clearCallingIdentity();
1329 try {
satokab751aa2010-09-14 19:17:36 +09001330 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001331 } finally {
1332 Binder.restoreCallingIdentity(ident);
1333 }
1334 }
1335 }
1336
1337 public void hideMySoftInput(IBinder token, int flags) {
1338 synchronized (mMethodMap) {
1339 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001340 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
1341 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001342 return;
1343 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001344 long ident = Binder.clearCallingIdentity();
1345 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001346 hideCurrentInputLocked(flags, null);
1347 } finally {
1348 Binder.restoreCallingIdentity(ident);
1349 }
1350 }
1351 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001352
The Android Open Source Project4df24232009-03-05 14:34:35 -08001353 public void showMySoftInput(IBinder token, int flags) {
1354 synchronized (mMethodMap) {
1355 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001356 Slog.w(TAG, "Ignoring showMySoftInput of uid "
1357 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001358 return;
1359 }
1360 long ident = Binder.clearCallingIdentity();
1361 try {
1362 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001363 } finally {
1364 Binder.restoreCallingIdentity(ident);
1365 }
1366 }
1367 }
1368
1369 void setEnabledSessionInMainThread(SessionState session) {
1370 if (mEnabledSession != session) {
1371 if (mEnabledSession != null) {
1372 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001373 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001374 mEnabledSession.method.setSessionEnabled(
1375 mEnabledSession.session, false);
1376 } catch (RemoteException e) {
1377 }
1378 }
1379 mEnabledSession = session;
1380 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001381 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001382 session.method.setSessionEnabled(
1383 session.session, true);
1384 } catch (RemoteException e) {
1385 }
1386 }
1387 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001388
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001389 public boolean handleMessage(Message msg) {
1390 HandlerCaller.SomeArgs args;
1391 switch (msg.what) {
1392 case MSG_SHOW_IM_PICKER:
1393 showInputMethodMenu();
1394 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001395
satokab751aa2010-09-14 19:17:36 +09001396 case MSG_SHOW_IM_SUBTYPE_PICKER:
1397 showInputMethodSubtypeMenu();
1398 return true;
1399
satok47a44912010-10-06 16:03:58 +09001400 case MSG_SHOW_IM_SUBTYPE_ENABLER:
1401 showInputMethodAndSubtypeEnabler();
1402 return true;
1403
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001404 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001406 case MSG_UNBIND_INPUT:
1407 try {
1408 ((IInputMethod)msg.obj).unbindInput();
1409 } catch (RemoteException e) {
1410 // There is nothing interesting about the method dying.
1411 }
1412 return true;
1413 case MSG_BIND_INPUT:
1414 args = (HandlerCaller.SomeArgs)msg.obj;
1415 try {
1416 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
1417 } catch (RemoteException e) {
1418 }
1419 return true;
1420 case MSG_SHOW_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001421 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001422 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001423 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
1424 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001425 } catch (RemoteException e) {
1426 }
1427 return true;
1428 case MSG_HIDE_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001429 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001430 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001431 ((IInputMethod)args.arg1).hideSoftInput(0,
1432 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001433 } catch (RemoteException e) {
1434 }
1435 return true;
1436 case MSG_ATTACH_TOKEN:
1437 args = (HandlerCaller.SomeArgs)msg.obj;
1438 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001439 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001440 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
1441 } catch (RemoteException e) {
1442 }
1443 return true;
1444 case MSG_CREATE_SESSION:
1445 args = (HandlerCaller.SomeArgs)msg.obj;
1446 try {
1447 ((IInputMethod)args.arg1).createSession(
1448 (IInputMethodCallback)args.arg2);
1449 } catch (RemoteException e) {
1450 }
1451 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001452 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001454 case MSG_START_INPUT:
1455 args = (HandlerCaller.SomeArgs)msg.obj;
1456 try {
1457 SessionState session = (SessionState)args.arg1;
1458 setEnabledSessionInMainThread(session);
1459 session.method.startInput((IInputContext)args.arg2,
1460 (EditorInfo)args.arg3);
1461 } catch (RemoteException e) {
1462 }
1463 return true;
1464 case MSG_RESTART_INPUT:
1465 args = (HandlerCaller.SomeArgs)msg.obj;
1466 try {
1467 SessionState session = (SessionState)args.arg1;
1468 setEnabledSessionInMainThread(session);
1469 session.method.restartInput((IInputContext)args.arg2,
1470 (EditorInfo)args.arg3);
1471 } catch (RemoteException e) {
1472 }
1473 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001474
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001475 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001476
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001477 case MSG_UNBIND_METHOD:
1478 try {
1479 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
1480 } catch (RemoteException e) {
1481 // There is nothing interesting about the last client dying.
1482 }
1483 return true;
1484 case MSG_BIND_METHOD:
1485 args = (HandlerCaller.SomeArgs)msg.obj;
1486 try {
1487 ((IInputMethodClient)args.arg1).onBindMethod(
1488 (InputBindResult)args.arg2);
1489 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001490 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001491 }
1492 return true;
1493 }
1494 return false;
1495 }
1496
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001497 private boolean isSystemIme(InputMethodInfo inputMethod) {
1498 return (inputMethod.getServiceInfo().applicationInfo.flags
1499 & ApplicationInfo.FLAG_SYSTEM) != 0;
1500 }
1501
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001502 private boolean chooseNewDefaultIMELocked() {
satokd87c2592010-09-29 11:52:06 +09001503 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001504 if (enabled != null && enabled.size() > 0) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07001505 // We'd prefer to fall back on a system IME, since that is safer.
1506 int i=enabled.size();
1507 while (i > 0) {
1508 i--;
1509 if ((enabled.get(i).getServiceInfo().applicationInfo.flags
1510 & ApplicationInfo.FLAG_SYSTEM) != 0) {
1511 break;
1512 }
1513 }
satokab751aa2010-09-14 19:17:36 +09001514 InputMethodInfo imi = enabled.get(i);
satok03eb319a2010-11-11 18:17:42 +09001515 if (DEBUG) {
1516 Slog.d(TAG, "New default IME was selected: " + imi.getId());
1517 }
satok723a27e2010-11-11 14:58:11 +09001518 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001519 return true;
1520 }
1521
1522 return false;
1523 }
1524
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001525 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
1526 HashMap<String, InputMethodInfo> map) {
1527 list.clear();
1528 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001529
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001530 PackageManager pm = mContext.getPackageManager();
Amith Yamasanie861ec12010-03-24 21:39:27 -07001531 final Configuration config = mContext.getResources().getConfiguration();
1532 final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY;
1533 String disabledSysImes = Settings.Secure.getString(mContext.getContentResolver(),
1534 Secure.DISABLED_SYSTEM_INPUT_METHODS);
1535 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001536
1537 List<ResolveInfo> services = pm.queryIntentServices(
1538 new Intent(InputMethod.SERVICE_INTERFACE),
1539 PackageManager.GET_META_DATA);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001540
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001541 for (int i = 0; i < services.size(); ++i) {
1542 ResolveInfo ri = services.get(i);
1543 ServiceInfo si = ri.serviceInfo;
1544 ComponentName compName = new ComponentName(si.packageName, si.name);
1545 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
1546 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001547 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001548 + ": it does not require the permission "
1549 + android.Manifest.permission.BIND_INPUT_METHOD);
1550 continue;
1551 }
1552
Joe Onorato8a9b2202010-02-26 18:56:32 -08001553 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001554
1555 try {
1556 InputMethodInfo p = new InputMethodInfo(mContext, ri);
1557 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07001558 final String id = p.getId();
1559 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001560
Amith Yamasanie861ec12010-03-24 21:39:27 -07001561 // System IMEs are enabled by default, unless there's a hard keyboard
1562 // and the system IME was explicitly disabled
1563 if (isSystemIme(p) && (!haveHardKeyboard || disabledSysImes.indexOf(id) < 0)) {
1564 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001565 }
1566
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001567 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001568 Slog.d(TAG, "Found a third-party input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001569 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001571 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001572 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001573 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001574 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001575 }
1576 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001577
1578 String defaultIme = Settings.Secure.getString(mContext
1579 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok913a8922010-08-26 21:53:41 +09001580 if (!TextUtils.isEmpty(defaultIme) && !map.containsKey(defaultIme)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001581 if (chooseNewDefaultIMELocked()) {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001582 updateFromSettingsLocked();
1583 }
1584 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001585 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001586
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001587 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001588
satokab751aa2010-09-14 19:17:36 +09001589 private void showInputMethodMenu() {
1590 showInputMethodMenuInternal(false);
1591 }
1592
1593 private void showInputMethodSubtypeMenu() {
1594 showInputMethodMenuInternal(true);
1595 }
1596
satok47a44912010-10-06 16:03:58 +09001597 private void showInputMethodAndSubtypeEnabler() {
satok86417ea2010-10-27 14:11:03 +09001598 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_AND_SUBTYPE_ENABLER);
satok47a44912010-10-06 16:03:58 +09001599 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09001600 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
1601 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok47a44912010-10-06 16:03:58 +09001602 mContext.startActivity(intent);
1603 }
1604
satokab751aa2010-09-14 19:17:36 +09001605 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001606 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001607
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001608 final Context context = mContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001609
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001610 final PackageManager pm = context.getPackageManager();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001611
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001612 String lastInputMethodId = Settings.Secure.getString(context
1613 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satokab751aa2010-09-14 19:17:36 +09001614 int lastInputMethodSubtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08001615 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001616
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001617 synchronized (mMethodMap) {
satok7f35c8c2010-10-07 21:13:11 +09001618 final List<Pair<InputMethodInfo, ArrayList<String>>> immis =
satok67ddf9c2010-11-17 09:45:54 +09001619 mSettings.getEnabledInputMethodAndSubtypeHashCodeListLocked();
satok7f35c8c2010-10-07 21:13:11 +09001620 ArrayList<Integer> subtypeIds = new ArrayList<Integer>();
1621
1622 if (immis == null || immis.size() == 0) {
1623 return;
1624 }
1625
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001626 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001627
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001628 int N = immis.size();
satok913a8922010-08-26 21:53:41 +09001629
satokab751aa2010-09-14 19:17:36 +09001630 final Map<CharSequence, Pair<InputMethodInfo, Integer>> imMap =
1631 new TreeMap<CharSequence, Pair<InputMethodInfo, Integer>>(Collator.getInstance());
satok913a8922010-08-26 21:53:41 +09001632
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001633 for (int i = 0; i < N; ++i) {
satok7f35c8c2010-10-07 21:13:11 +09001634 InputMethodInfo property = immis.get(i).first;
1635 final ArrayList<String> enabledSubtypeIds = immis.get(i).second;
1636 HashSet<String> enabledSubtypeSet = new HashSet<String>();
1637 for (String s : enabledSubtypeIds) {
1638 enabledSubtypeSet.add(s);
1639 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001640 if (property == null) {
1641 continue;
1642 }
satokab751aa2010-09-14 19:17:36 +09001643 ArrayList<InputMethodSubtype> subtypes = property.getSubtypes();
1644 CharSequence label = property.loadLabel(pm);
satok7f35c8c2010-10-07 21:13:11 +09001645 if (showSubtypes && enabledSubtypeSet.size() > 0) {
satokab751aa2010-09-14 19:17:36 +09001646 for (int j = 0; j < subtypes.size(); ++j) {
1647 InputMethodSubtype subtype = subtypes.get(j);
satok7f35c8c2010-10-07 21:13:11 +09001648 if (enabledSubtypeSet.contains(String.valueOf(subtype.hashCode()))) {
1649 CharSequence title;
1650 int nameResId = subtype.getNameResId();
satok9ef02832010-11-04 21:17:48 +09001651 String mode = subtype.getMode();
satok7f35c8c2010-10-07 21:13:11 +09001652 if (nameResId != 0) {
1653 title = pm.getText(property.getPackageName(), nameResId,
1654 property.getServiceInfo().applicationInfo);
1655 } else {
1656 CharSequence language = subtype.getLocale();
satok7f35c8c2010-10-07 21:13:11 +09001657 // TODO: Use more friendly Title and UI
1658 title = label + "," + (mode == null ? "" : mode) + ","
1659 + (language == null ? "" : language);
1660 }
1661 imMap.put(title, new Pair<InputMethodInfo, Integer>(property, j));
satokab751aa2010-09-14 19:17:36 +09001662 }
satokab751aa2010-09-14 19:17:36 +09001663 }
1664 } else {
1665 imMap.put(label,
1666 new Pair<InputMethodInfo, Integer>(property, NOT_A_SUBTYPE_ID));
1667 subtypeIds.add(0);
1668 }
Dianne Hackborn97106ab2010-03-03 00:08:31 -08001669 }
satok913a8922010-08-26 21:53:41 +09001670
1671 N = imMap.size();
1672 mItems = imMap.keySet().toArray(new CharSequence[N]);
satokab751aa2010-09-14 19:17:36 +09001673 mIms = new InputMethodInfo[N];
1674 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001675 int checkedItem = 0;
1676 for (int i = 0; i < N; ++i) {
satokab751aa2010-09-14 19:17:36 +09001677 Pair<InputMethodInfo, Integer> value = imMap.get(mItems[i]);
1678 mIms[i] = value.first;
1679 mSubtypeIds[i] = value.second;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001680 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09001681 int subtypeId = mSubtypeIds[i];
1682 if ((subtypeId == NOT_A_SUBTYPE_ID)
1683 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
1684 || (subtypeId == lastInputMethodSubtypeId)) {
1685 checkedItem = i;
1686 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001687 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001688 }
satokab751aa2010-09-14 19:17:36 +09001689
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001690 AlertDialog.OnClickListener adocl = new AlertDialog.OnClickListener() {
1691 public void onClick(DialogInterface dialog, int which) {
1692 hideInputMethodMenu();
1693 }
1694 };
satokd87c2592010-09-29 11:52:06 +09001695
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001696 TypedArray a = context.obtainStyledAttributes(null,
1697 com.android.internal.R.styleable.DialogPreference,
1698 com.android.internal.R.attr.alertDialogStyle, 0);
1699 mDialogBuilder = new AlertDialog.Builder(context)
1700 .setTitle(com.android.internal.R.string.select_input_method)
1701 .setOnCancelListener(new OnCancelListener() {
1702 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001703 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001704 }
1705 })
1706 .setIcon(a.getDrawable(
1707 com.android.internal.R.styleable.DialogPreference_dialogTitle));
1708 a.recycle();
satokd87c2592010-09-29 11:52:06 +09001709
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001710 mDialogBuilder.setSingleChoiceItems(mItems, checkedItem,
1711 new AlertDialog.OnClickListener() {
1712 public void onClick(DialogInterface dialog, int which) {
1713 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09001714 if (mIms == null || mIms.length <= which
1715 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001716 return;
1717 }
1718 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09001719 int subtypeId = mSubtypeIds[which];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001720 hideInputMethodMenu();
1721 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09001722 if ((subtypeId < 0)
1723 || (subtypeId >= im.getSubtypes().size())) {
1724 subtypeId = NOT_A_SUBTYPE_ID;
1725 }
1726 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001727 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08001728 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001729 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001730 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001731
satok7f35c8c2010-10-07 21:13:11 +09001732 if (showSubtypes) {
1733 mDialogBuilder.setPositiveButton(com.android.internal.R.string.more_item_label,
1734 new DialogInterface.OnClickListener() {
1735 public void onClick(DialogInterface dialog, int whichButton) {
1736 showInputMethodAndSubtypeEnabler();
1737 }
1738 });
1739 }
satok0ff647b2010-10-08 13:49:28 +09001740 mDialogBuilder.setNegativeButton(com.android.internal.R.string.cancel,
1741 new DialogInterface.OnClickListener() {
1742 public void onClick(DialogInterface dialog, int whichButton) {
1743 hideInputMethodMenu();
1744 }
1745 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001746 mSwitchingDialog = mDialogBuilder.create();
1747 mSwitchingDialog.getWindow().setType(
1748 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
1749 mSwitchingDialog.show();
1750 }
1751 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001752
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001753 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07001754 synchronized (mMethodMap) {
1755 hideInputMethodMenuLocked();
1756 }
1757 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001758
The Android Open Source Project10592532009-03-18 17:39:46 -07001759 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001760 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001761
The Android Open Source Project10592532009-03-18 17:39:46 -07001762 if (mSwitchingDialog != null) {
1763 mSwitchingDialog.dismiss();
1764 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001765 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001766
The Android Open Source Project10592532009-03-18 17:39:46 -07001767 mDialogBuilder = null;
1768 mItems = null;
1769 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001770 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001771
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001772 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001773
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001774 public boolean setInputMethodEnabled(String id, boolean enabled) {
1775 synchronized (mMethodMap) {
1776 if (mContext.checkCallingOrSelfPermission(
1777 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1778 != PackageManager.PERMISSION_GRANTED) {
1779 throw new SecurityException(
1780 "Requires permission "
1781 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1782 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001783
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001784 long ident = Binder.clearCallingIdentity();
1785 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001786 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001787 } finally {
1788 Binder.restoreCallingIdentity(ident);
1789 }
1790 }
1791 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001792
1793 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
1794 // Make sure this is a valid input method.
1795 InputMethodInfo imm = mMethodMap.get(id);
1796 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09001797 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001798 }
1799
satokd87c2592010-09-29 11:52:06 +09001800 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
1801 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001802
satokd87c2592010-09-29 11:52:06 +09001803 if (enabled) {
1804 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
1805 if (pair.first.equals(id)) {
1806 // We are enabling this input method, but it is already enabled.
1807 // Nothing to do. The previous state was enabled.
1808 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001809 }
1810 }
satokd87c2592010-09-29 11:52:06 +09001811 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
1812 // Previous state was disabled.
1813 return false;
1814 } else {
1815 StringBuilder builder = new StringBuilder();
1816 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
1817 builder, enabledInputMethodsList, id)) {
1818 // Disabled input method is currently selected, switch to another one.
1819 String selId = Settings.Secure.getString(mContext.getContentResolver(),
1820 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09001821 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
1822 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
1823 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09001824 }
1825 // Previous state was enabled.
1826 return true;
1827 } else {
1828 // We are disabling the input method but it is already disabled.
1829 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001830 return false;
1831 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001832 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001833 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001834
satok723a27e2010-11-11 14:58:11 +09001835 private void saveCurrentInputMethodAndSubtypeToHistory() {
1836 String subtypeId = NOT_A_SUBTYPE_ID_STR;
1837 if (mCurrentSubtype != null) {
1838 subtypeId = String.valueOf(mCurrentSubtype.hashCode());
1839 }
1840 mSettings.addSubtypeToHistory(mCurMethodId, subtypeId);
satokab751aa2010-09-14 19:17:36 +09001841 }
1842
satok723a27e2010-11-11 14:58:11 +09001843 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
1844 boolean setSubtypeOnly) {
1845 // Update the history of InputMethod and Subtype
1846 saveCurrentInputMethodAndSubtypeToHistory();
1847
1848 // Set Subtype here
1849 if (imi == null || subtypeId < 0) {
1850 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08001851 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09001852 } else {
1853 final ArrayList<InputMethodSubtype> subtypes = imi.getSubtypes();
1854 if (subtypeId < subtypes.size()) {
1855 mSettings.putSelectedSubtype(subtypes.get(subtypeId).hashCode());
1856 mCurrentSubtype = subtypes.get(subtypeId);
1857 } else {
1858 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
1859 mCurrentSubtype = null;
1860 }
satokab751aa2010-09-14 19:17:36 +09001861 }
satok723a27e2010-11-11 14:58:11 +09001862
1863 if (!setSubtypeOnly) {
1864 // Set InputMethod here
1865 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
1866 }
1867 }
1868
1869 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
1870 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
1871 int lastSubtypeId = NOT_A_SUBTYPE_ID;
1872 // newDefaultIme is empty when there is no candidate for the selected IME.
1873 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
1874 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
1875 if (subtypeHashCode != null) {
1876 try {
1877 lastSubtypeId = getSubtypeIdFromHashCode(
1878 imi, Integer.valueOf(subtypeHashCode));
1879 } catch (NumberFormatException e) {
1880 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
1881 }
1882 }
1883 }
1884 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09001885 }
1886
1887 private int getSelectedInputMethodSubtypeId(String id) {
1888 InputMethodInfo imi = mMethodMap.get(id);
1889 if (imi == null) {
1890 return NOT_A_SUBTYPE_ID;
1891 }
satokab751aa2010-09-14 19:17:36 +09001892 int subtypeId;
1893 try {
1894 subtypeId = Settings.Secure.getInt(mContext.getContentResolver(),
1895 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE);
1896 } catch (SettingNotFoundException e) {
1897 return NOT_A_SUBTYPE_ID;
1898 }
satok723a27e2010-11-11 14:58:11 +09001899 return getSubtypeIdFromHashCode(imi, subtypeId);
1900 }
1901
1902 private int getSubtypeIdFromHashCode(InputMethodInfo imi, int subtypeHashCode) {
1903 ArrayList<InputMethodSubtype> subtypes = imi.getSubtypes();
satokab751aa2010-09-14 19:17:36 +09001904 for (int i = 0; i < subtypes.size(); ++i) {
1905 InputMethodSubtype ims = subtypes.get(i);
satok723a27e2010-11-11 14:58:11 +09001906 if (subtypeHashCode == ims.hashCode()) {
satokab751aa2010-09-14 19:17:36 +09001907 return i;
1908 }
1909 }
1910 return NOT_A_SUBTYPE_ID;
1911 }
1912
satok8fbb1e82010-11-02 23:15:58 +09001913 // If there are no selected subtypes, tries finding the most applicable one according to the
1914 // current system locale
satok3ef8b292010-11-23 06:06:29 +09001915 private int findApplicableSubtypeLocked(String id) {
satok8fbb1e82010-11-02 23:15:58 +09001916 InputMethodInfo imi = mMethodMap.get(id);
1917 if (imi == null) {
1918 return NOT_A_SUBTYPE_ID;
1919 }
1920 ArrayList<InputMethodSubtype> subtypes = imi.getSubtypes();
1921 if (subtypes == null || subtypes.size() == 0) {
1922 return NOT_A_SUBTYPE_ID;
1923 }
1924 final String locale = mContext.getResources().getConfiguration().locale.toString();
1925 final String language = locale.substring(0, 2);
1926 boolean partialMatchFound = false;
1927 int applicableSubtypeId = DEFAULT_SUBTYPE_ID;
1928 for (int i = 0; i < subtypes.size(); ++i) {
1929 final String subtypeLocale = subtypes.get(i).getLocale();
satok9ef02832010-11-04 21:17:48 +09001930 // An applicable subtype should be a keyboard subtype
1931 if (subtypes.get(i).getMode().equalsIgnoreCase("keyboard")) {
1932 if (locale.equals(subtypeLocale)) {
1933 // Exact match (e.g. system locale is "en_US" and subtype locale is "en_US")
1934 applicableSubtypeId = i;
1935 break;
1936 } else if (!partialMatchFound && subtypeLocale.startsWith(language)) {
1937 // Partial match (e.g. system locale is "en_US" and subtype locale is "en")
1938 applicableSubtypeId = i;
1939 partialMatchFound = true;
1940 }
satok8fbb1e82010-11-02 23:15:58 +09001941 }
1942 }
1943
1944 // The first subtype applicable to the system locale will be defined as the most applicable
1945 // subtype.
1946 if (DEBUG) {
satok03eb319a2010-11-11 18:17:42 +09001947 Slog.d(TAG, "Applicable InputMethodSubtype was found: " + applicableSubtypeId + ","
satok8fbb1e82010-11-02 23:15:58 +09001948 + subtypes.get(applicableSubtypeId).getLocale());
1949 }
1950 return applicableSubtypeId;
1951 }
1952
satokab751aa2010-09-14 19:17:36 +09001953 /**
1954 * @return Return the current subtype of this input method.
1955 */
1956 public InputMethodSubtype getCurrentInputMethodSubtype() {
satok3ef8b292010-11-23 06:06:29 +09001957 synchronized (mMethodMap) {
1958 boolean subtypeIsSelected = false;
1959 try {
1960 subtypeIsSelected = Settings.Secure.getInt(mContext.getContentResolver(),
1961 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE) != NOT_A_SUBTYPE_ID;
1962 } catch (SettingNotFoundException e) {
satok8fbb1e82010-11-02 23:15:58 +09001963 }
satok3ef8b292010-11-23 06:06:29 +09001964 if (!subtypeIsSelected || mCurrentSubtype == null) {
1965 String lastInputMethodId =
1966 Settings.Secure.getString(mContext.getContentResolver(),
1967 Settings.Secure.DEFAULT_INPUT_METHOD);
1968 int subtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
1969 if (subtypeId == NOT_A_SUBTYPE_ID) {
1970 subtypeId = findApplicableSubtypeLocked(lastInputMethodId);
1971 }
1972 if (subtypeId != NOT_A_SUBTYPE_ID) {
1973 mCurrentSubtype =
1974 mMethodMap.get(lastInputMethodId).getSubtypes().get(subtypeId);
1975 }
satok8fbb1e82010-11-02 23:15:58 +09001976 }
satok3ef8b292010-11-23 06:06:29 +09001977 return mCurrentSubtype;
satok8fbb1e82010-11-02 23:15:58 +09001978 }
satokab751aa2010-09-14 19:17:36 +09001979 }
1980
satokb66d2872010-11-10 01:04:04 +09001981 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
1982 synchronized (mMethodMap) {
1983 if (subtype != null && mCurMethodId != null) {
1984 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1985 int subtypeId = getSubtypeIdFromHashCode(imi, subtype.hashCode());
1986 if (subtypeId != NOT_A_SUBTYPE_ID) {
1987 setInputMethodLocked(mCurMethodId, subtypeId);
1988 return true;
1989 }
1990 }
1991 return false;
1992 }
1993 }
1994
satokd87c2592010-09-29 11:52:06 +09001995 /**
1996 * Utility class for putting and getting settings for InputMethod
1997 * TODO: Move all putters and getters of settings to this class.
1998 */
1999 private static class InputMethodSettings {
2000 // The string for enabled input method is saved as follows:
2001 // example: ("ime0;subtype0;subtype1;subtype2:ime1:ime2;subtype0")
2002 private static final char INPUT_METHOD_SEPARATER = ':';
2003 private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';';
satok723a27e2010-11-11 14:58:11 +09002004 private final TextUtils.SimpleStringSplitter mInputMethodSplitter =
satokd87c2592010-09-29 11:52:06 +09002005 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER);
2006
satok723a27e2010-11-11 14:58:11 +09002007 private final TextUtils.SimpleStringSplitter mSubtypeSplitter =
satokd87c2592010-09-29 11:52:06 +09002008 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATER);
2009
2010 private final ContentResolver mResolver;
2011 private final HashMap<String, InputMethodInfo> mMethodMap;
2012 private final ArrayList<InputMethodInfo> mMethodList;
2013
2014 private String mEnabledInputMethodsStrCache;
2015
2016 private static void buildEnabledInputMethodsSettingString(
2017 StringBuilder builder, Pair<String, ArrayList<String>> pair) {
2018 String id = pair.first;
2019 ArrayList<String> subtypes = pair.second;
2020 builder.append(id);
satok57c767c2010-11-01 22:34:08 +09002021 // Inputmethod and subtypes are saved in the settings as follows:
2022 // ime0;subtype0;subtype1:ime1;subtype0:ime2:ime3;subtype0;subtype1
2023 for (String subtypeId: subtypes) {
2024 builder.append(INPUT_METHOD_SUBTYPE_SEPARATER).append(subtypeId);
satokd87c2592010-09-29 11:52:06 +09002025 }
2026 }
2027
2028 public InputMethodSettings(
2029 ContentResolver resolver, HashMap<String, InputMethodInfo> methodMap,
2030 ArrayList<InputMethodInfo> methodList) {
2031 mResolver = resolver;
2032 mMethodMap = methodMap;
2033 mMethodList = methodList;
2034 }
2035
2036 public List<InputMethodInfo> getEnabledInputMethodListLocked() {
2037 return createEnabledInputMethodListLocked(
2038 getEnabledInputMethodsAndSubtypeListLocked());
2039 }
2040
satok7f35c8c2010-10-07 21:13:11 +09002041 public List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002042 getEnabledInputMethodAndSubtypeHashCodeListLocked() {
2043 return createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002044 getEnabledInputMethodsAndSubtypeListLocked());
2045 }
2046
satok67ddf9c2010-11-17 09:45:54 +09002047 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(
2048 InputMethodInfo imi) {
2049 List<Pair<String, ArrayList<String>>> imsList =
2050 getEnabledInputMethodsAndSubtypeListLocked();
2051 ArrayList<InputMethodSubtype> enabledSubtypes =
2052 new ArrayList<InputMethodSubtype>();
satok884ef9a2010-11-18 10:39:46 +09002053 if (imi != null) {
2054 for (Pair<String, ArrayList<String>> imsPair : imsList) {
2055 InputMethodInfo info = mMethodMap.get(imsPair.first);
2056 if (info != null && info.getId().equals(imi.getId())) {
2057 ArrayList<InputMethodSubtype> subtypes = info.getSubtypes();
2058 for (InputMethodSubtype ims: subtypes) {
2059 for (String s: imsPair.second) {
2060 if (String.valueOf(ims.hashCode()).equals(s)) {
2061 enabledSubtypes.add(ims);
2062 }
satok67ddf9c2010-11-17 09:45:54 +09002063 }
2064 }
satok884ef9a2010-11-18 10:39:46 +09002065 break;
satok67ddf9c2010-11-17 09:45:54 +09002066 }
satok67ddf9c2010-11-17 09:45:54 +09002067 }
2068 }
2069 return enabledSubtypes;
2070 }
2071
satokd87c2592010-09-29 11:52:06 +09002072 // At the initial boot, the settings for input methods are not set,
2073 // so we need to enable IME in that case.
2074 public void enableAllIMEsIfThereIsNoEnabledIME() {
2075 if (TextUtils.isEmpty(getEnabledInputMethodsStr())) {
2076 StringBuilder sb = new StringBuilder();
2077 final int N = mMethodList.size();
2078 for (int i = 0; i < N; i++) {
2079 InputMethodInfo imi = mMethodList.get(i);
2080 Slog.i(TAG, "Adding: " + imi.getId());
2081 if (i > 0) sb.append(':');
2082 sb.append(imi.getId());
2083 }
2084 putEnabledInputMethodsStr(sb.toString());
2085 }
2086 }
2087
2088 public List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() {
2089 ArrayList<Pair<String, ArrayList<String>>> imsList
2090 = new ArrayList<Pair<String, ArrayList<String>>>();
2091 final String enabledInputMethodsStr = getEnabledInputMethodsStr();
2092 if (TextUtils.isEmpty(enabledInputMethodsStr)) {
2093 return imsList;
2094 }
satok723a27e2010-11-11 14:58:11 +09002095 mInputMethodSplitter.setString(enabledInputMethodsStr);
2096 while (mInputMethodSplitter.hasNext()) {
2097 String nextImsStr = mInputMethodSplitter.next();
2098 mSubtypeSplitter.setString(nextImsStr);
2099 if (mSubtypeSplitter.hasNext()) {
satokd87c2592010-09-29 11:52:06 +09002100 ArrayList<String> subtypeHashes = new ArrayList<String>();
2101 // The first element is ime id.
satok723a27e2010-11-11 14:58:11 +09002102 String imeId = mSubtypeSplitter.next();
2103 while (mSubtypeSplitter.hasNext()) {
2104 subtypeHashes.add(mSubtypeSplitter.next());
satokd87c2592010-09-29 11:52:06 +09002105 }
2106 imsList.add(new Pair<String, ArrayList<String>>(imeId, subtypeHashes));
2107 }
2108 }
2109 return imsList;
2110 }
2111
2112 public void appendAndPutEnabledInputMethodLocked(String id, boolean reloadInputMethodStr) {
2113 if (reloadInputMethodStr) {
2114 getEnabledInputMethodsStr();
2115 }
2116 if (TextUtils.isEmpty(mEnabledInputMethodsStrCache)) {
2117 // Add in the newly enabled input method.
2118 putEnabledInputMethodsStr(id);
2119 } else {
2120 putEnabledInputMethodsStr(
2121 mEnabledInputMethodsStrCache + INPUT_METHOD_SEPARATER + id);
2122 }
2123 }
2124
2125 /**
2126 * Build and put a string of EnabledInputMethods with removing specified Id.
2127 * @return the specified id was removed or not.
2128 */
2129 public boolean buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2130 StringBuilder builder, List<Pair<String, ArrayList<String>>> imsList, String id) {
2131 boolean isRemoved = false;
2132 boolean needsAppendSeparator = false;
2133 for (Pair<String, ArrayList<String>> ims: imsList) {
2134 String curId = ims.first;
2135 if (curId.equals(id)) {
2136 // We are disabling this input method, and it is
2137 // currently enabled. Skip it to remove from the
2138 // new list.
2139 isRemoved = true;
2140 } else {
2141 if (needsAppendSeparator) {
2142 builder.append(INPUT_METHOD_SEPARATER);
2143 } else {
2144 needsAppendSeparator = true;
2145 }
2146 buildEnabledInputMethodsSettingString(builder, ims);
2147 }
2148 }
2149 if (isRemoved) {
2150 // Update the setting with the new list of input methods.
2151 putEnabledInputMethodsStr(builder.toString());
2152 }
2153 return isRemoved;
2154 }
2155
2156 private List<InputMethodInfo> createEnabledInputMethodListLocked(
2157 List<Pair<String, ArrayList<String>>> imsList) {
2158 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
2159 for (Pair<String, ArrayList<String>> ims: imsList) {
2160 InputMethodInfo info = mMethodMap.get(ims.first);
2161 if (info != null) {
2162 res.add(info);
2163 }
2164 }
2165 return res;
2166 }
2167
satok7f35c8c2010-10-07 21:13:11 +09002168 private List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002169 createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002170 List<Pair<String, ArrayList<String>>> imsList) {
2171 final ArrayList<Pair<InputMethodInfo, ArrayList<String>>> res
2172 = new ArrayList<Pair<InputMethodInfo, ArrayList<String>>>();
2173 for (Pair<String, ArrayList<String>> ims : imsList) {
2174 InputMethodInfo info = mMethodMap.get(ims.first);
2175 if (info != null) {
2176 res.add(new Pair<InputMethodInfo, ArrayList<String>>(info, ims.second));
2177 }
2178 }
2179 return res;
2180 }
2181
satokd87c2592010-09-29 11:52:06 +09002182 private void putEnabledInputMethodsStr(String str) {
2183 Settings.Secure.putString(mResolver, Settings.Secure.ENABLED_INPUT_METHODS, str);
2184 mEnabledInputMethodsStrCache = str;
2185 }
2186
2187 private String getEnabledInputMethodsStr() {
2188 mEnabledInputMethodsStrCache = Settings.Secure.getString(
2189 mResolver, Settings.Secure.ENABLED_INPUT_METHODS);
satok723a27e2010-11-11 14:58:11 +09002190 if (DEBUG) {
2191 Slog.d(TAG, "getEnabledInputMethodsStr: " + mEnabledInputMethodsStrCache);
2192 }
satokd87c2592010-09-29 11:52:06 +09002193 return mEnabledInputMethodsStrCache;
2194 }
satok723a27e2010-11-11 14:58:11 +09002195
2196 private void saveSubtypeHistory(
2197 List<Pair<String, String>> savedImes, String newImeId, String newSubtypeId) {
2198 StringBuilder builder = new StringBuilder();
2199 boolean isImeAdded = false;
2200 if (!TextUtils.isEmpty(newImeId) && !TextUtils.isEmpty(newSubtypeId)) {
2201 builder.append(newImeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2202 newSubtypeId);
2203 isImeAdded = true;
2204 }
2205 for (Pair<String, String> ime: savedImes) {
2206 String imeId = ime.first;
2207 String subtypeId = ime.second;
2208 if (TextUtils.isEmpty(subtypeId)) {
2209 subtypeId = NOT_A_SUBTYPE_ID_STR;
2210 }
2211 if (isImeAdded) {
2212 builder.append(INPUT_METHOD_SEPARATER);
2213 } else {
2214 isImeAdded = true;
2215 }
2216 builder.append(imeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2217 subtypeId);
2218 }
2219 // Remove the last INPUT_METHOD_SEPARATER
2220 putSubtypeHistoryStr(builder.toString());
2221 }
2222
2223 public void addSubtypeToHistory(String imeId, String subtypeId) {
2224 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
2225 for (Pair<String, String> ime: subtypeHistory) {
2226 if (ime.first.equals(imeId)) {
2227 if (DEBUG) {
2228 Slog.v(TAG, "Subtype found in the history: " + imeId
2229 + ime.second);
2230 }
2231 // We should break here
2232 subtypeHistory.remove(ime);
2233 break;
2234 }
2235 }
2236 saveSubtypeHistory(subtypeHistory, imeId, subtypeId);
2237 }
2238
2239 private void putSubtypeHistoryStr(String str) {
2240 if (DEBUG) {
2241 Slog.d(TAG, "putSubtypeHistoryStr: " + str);
2242 }
2243 Settings.Secure.putString(
2244 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, str);
2245 }
2246
2247 public Pair<String, String> getLastInputMethodAndSubtypeLocked() {
2248 // Gets the first one from the history
2249 return getLastSubtypeForInputMethodLockedInternal(null);
2250 }
2251
2252 public String getLastSubtypeForInputMethodLocked(String imeId) {
2253 Pair<String, String> ime = getLastSubtypeForInputMethodLockedInternal(imeId);
2254 if (ime != null) {
2255 return ime.second;
2256 } else {
2257 return null;
2258 }
2259 }
2260
2261 private Pair<String, String> getLastSubtypeForInputMethodLockedInternal(String imeId) {
2262 List<Pair<String, ArrayList<String>>> enabledImes =
2263 getEnabledInputMethodsAndSubtypeListLocked();
2264 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
2265 for (Pair<String, String> imeAndSubtype: subtypeHistory) {
2266 final String imeInTheHistory = imeAndSubtype.first;
2267 // If imeId is empty, returns the first IME and subtype in the history
2268 if (TextUtils.isEmpty(imeId) || imeInTheHistory.equals(imeId)) {
2269 final String subtypeInTheHistory = imeAndSubtype.second;
2270 final String subtypeHashCode = getEnabledSubtypeForInputMethodAndSubtypeLocked(
2271 enabledImes, imeInTheHistory, subtypeInTheHistory);
2272 if (!TextUtils.isEmpty(subtypeHashCode)) {
2273 if (DEBUG) {
2274 Slog.d(TAG, "Enabled subtype found in the history:" + subtypeHashCode);
2275 }
2276 return new Pair<String, String>(imeInTheHistory, subtypeHashCode);
2277 }
2278 }
2279 }
2280 if (DEBUG) {
2281 Slog.d(TAG, "No enabled IME found in the history");
2282 }
2283 return null;
2284 }
2285
2286 private String getEnabledSubtypeForInputMethodAndSubtypeLocked(List<Pair<String,
2287 ArrayList<String>>> enabledImes, String imeId, String subtypeHashCode) {
2288 for (Pair<String, ArrayList<String>> enabledIme: enabledImes) {
2289 if (enabledIme.first.equals(imeId)) {
2290 for (String s: enabledIme.second) {
2291 if (s.equals(subtypeHashCode)) {
2292 // If both imeId and subtypeId are enabled, return subtypeId.
2293 return s;
2294 }
2295 }
2296 // If imeId was enabled but subtypeId was disabled.
2297 return NOT_A_SUBTYPE_ID_STR;
2298 }
2299 }
2300 // If both imeId and subtypeId are disabled, return null
2301 return null;
2302 }
2303
2304 private List<Pair<String, String>> loadInputMethodAndSubtypeHistoryLocked() {
2305 ArrayList<Pair<String, String>> imsList = new ArrayList<Pair<String, String>>();
2306 final String subtypeHistoryStr = getSubtypeHistoryStr();
2307 if (TextUtils.isEmpty(subtypeHistoryStr)) {
2308 return imsList;
2309 }
2310 mInputMethodSplitter.setString(subtypeHistoryStr);
2311 while (mInputMethodSplitter.hasNext()) {
2312 String nextImsStr = mInputMethodSplitter.next();
2313 mSubtypeSplitter.setString(nextImsStr);
2314 if (mSubtypeSplitter.hasNext()) {
2315 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2316 // The first element is ime id.
2317 String imeId = mSubtypeSplitter.next();
2318 while (mSubtypeSplitter.hasNext()) {
2319 subtypeId = mSubtypeSplitter.next();
2320 break;
2321 }
2322 imsList.add(new Pair<String, String>(imeId, subtypeId));
2323 }
2324 }
2325 return imsList;
2326 }
2327
2328 private String getSubtypeHistoryStr() {
2329 if (DEBUG) {
2330 Slog.d(TAG, "getSubtypeHistoryStr: " + Settings.Secure.getString(
2331 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY));
2332 }
2333 return Settings.Secure.getString(
2334 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY);
2335 }
2336
2337 public void putSelectedInputMethod(String imeId) {
2338 Settings.Secure.putString(mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, imeId);
2339 }
2340
2341 public void putSelectedSubtype(int subtypeId) {
2342 Settings.Secure.putInt(
2343 mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, subtypeId);
2344 }
satokd87c2592010-09-29 11:52:06 +09002345 }
2346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002347 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002349 @Override
2350 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2351 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
2352 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002354 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
2355 + Binder.getCallingPid()
2356 + ", uid=" + Binder.getCallingUid());
2357 return;
2358 }
2359
2360 IInputMethod method;
2361 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002363 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002364
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002365 synchronized (mMethodMap) {
2366 p.println("Current Input Method Manager state:");
2367 int N = mMethodList.size();
2368 p.println(" Input Methods:");
2369 for (int i=0; i<N; i++) {
2370 InputMethodInfo info = mMethodList.get(i);
2371 p.println(" InputMethod #" + i + ":");
2372 info.dump(p, " ");
2373 }
2374 p.println(" Clients:");
2375 for (ClientState ci : mClients.values()) {
2376 p.println(" Client " + ci + ":");
2377 p.println(" client=" + ci.client);
2378 p.println(" inputContext=" + ci.inputContext);
2379 p.println(" sessionRequested=" + ci.sessionRequested);
2380 p.println(" curSession=" + ci.curSession);
2381 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002382 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002383 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002384 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
2385 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002386 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
2387 + " mBoundToMethod=" + mBoundToMethod);
2388 p.println(" mCurToken=" + mCurToken);
2389 p.println(" mCurIntent=" + mCurIntent);
2390 method = mCurMethod;
2391 p.println(" mCurMethod=" + mCurMethod);
2392 p.println(" mEnabledSession=" + mEnabledSession);
2393 p.println(" mShowRequested=" + mShowRequested
2394 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
2395 + " mShowForced=" + mShowForced
2396 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07002397 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002398 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002399
Jeff Brownb88102f2010-09-08 11:49:43 -07002400 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002401 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002402 pw.flush();
2403 try {
2404 client.client.asBinder().dump(fd, args);
2405 } catch (RemoteException e) {
2406 p.println("Input method client dead: " + e);
2407 }
Jeff Brownb88102f2010-09-08 11:49:43 -07002408 } else {
2409 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002410 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002411
Jeff Brownb88102f2010-09-08 11:49:43 -07002412 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002413 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002414 pw.flush();
2415 try {
2416 method.asBinder().dump(fd, args);
2417 } catch (RemoteException e) {
2418 p.println("Input method service dead: " + e);
2419 }
Jeff Brownb88102f2010-09-08 11:49:43 -07002420 } else {
2421 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002422 }
2423 }
2424}