blob: 4f8862c13ba2897ff65c5b6839608ad5d77b57d3 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006-2008 The Android Open Source Project
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * the License at
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008 * http://www.apache.org/licenses/LICENSE-2.0
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17package com.android.server;
18
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080019import com.android.internal.content.PackageMonitor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import com.android.internal.os.HandlerCaller;
21import com.android.internal.view.IInputContext;
22import com.android.internal.view.IInputMethod;
23import com.android.internal.view.IInputMethodCallback;
24import com.android.internal.view.IInputMethodClient;
25import com.android.internal.view.IInputMethodManager;
26import com.android.internal.view.IInputMethodSession;
27import com.android.internal.view.InputBindResult;
28
Joe Onorato7a0f36b2010-06-07 10:24:36 -070029import com.android.server.StatusBarManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030
31import org.xmlpull.v1.XmlPullParserException;
32
33import android.app.ActivityManagerNative;
34import android.app.AlertDialog;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070035import android.app.PendingIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.content.ComponentName;
37import android.content.ContentResolver;
38import android.content.Context;
39import android.content.DialogInterface;
40import android.content.IntentFilter;
41import android.content.DialogInterface.OnCancelListener;
42import android.content.Intent;
43import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070044import android.content.pm.ApplicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.content.pm.PackageManager;
46import android.content.pm.ResolveInfo;
47import android.content.pm.ServiceInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070048import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.content.res.Resources;
50import android.content.res.TypedArray;
51import android.database.ContentObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.os.Binder;
53import android.os.Handler;
54import android.os.IBinder;
55import android.os.IInterface;
56import android.os.Message;
57import android.os.Parcel;
58import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080059import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.os.ServiceManager;
61import android.os.SystemClock;
62import android.provider.Settings;
Amith Yamasanie861ec12010-03-24 21:39:27 -070063import android.provider.Settings.Secure;
satokab751aa2010-09-14 19:17:36 +090064import android.provider.Settings.SettingNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.text.TextUtils;
66import android.util.EventLog;
satokab751aa2010-09-14 19:17:36 +090067import android.util.Pair;
Joe Onorato8a9b2202010-02-26 18:56:32 -080068import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.util.PrintWriterPrinter;
70import android.util.Printer;
71import android.view.IWindowManager;
72import android.view.WindowManager;
satokab751aa2010-09-14 19:17:36 +090073import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074import android.view.inputmethod.InputBinding;
75import android.view.inputmethod.InputMethod;
76import android.view.inputmethod.InputMethodInfo;
77import android.view.inputmethod.InputMethodManager;
satokab751aa2010-09-14 19:17:36 +090078import android.view.inputmethod.InputMethodSubtype;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079
80import java.io.FileDescriptor;
81import java.io.IOException;
82import java.io.PrintWriter;
satok913a8922010-08-26 21:53:41 +090083import java.text.Collator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084import java.util.ArrayList;
85import java.util.HashMap;
satok7f35c8c2010-10-07 21:13:11 +090086import java.util.HashSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087import java.util.List;
satok913a8922010-08-26 21:53:41 +090088import java.util.Map;
89import java.util.TreeMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090
91/**
92 * This class provides a system service that manages input methods.
93 */
94public class InputMethodManagerService extends IInputMethodManager.Stub
95 implements ServiceConnection, Handler.Callback {
96 static final boolean DEBUG = false;
97 static final String TAG = "InputManagerService";
98
99 static final int MSG_SHOW_IM_PICKER = 1;
satokab751aa2010-09-14 19:17:36 +0900100 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 2;
satok47a44912010-10-06 16:03:58 +0900101 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 3;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 static final int MSG_UNBIND_INPUT = 1000;
104 static final int MSG_BIND_INPUT = 1010;
105 static final int MSG_SHOW_SOFT_INPUT = 1020;
106 static final int MSG_HIDE_SOFT_INPUT = 1030;
107 static final int MSG_ATTACH_TOKEN = 1040;
108 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800109
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110 static final int MSG_START_INPUT = 2000;
111 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 static final int MSG_UNBIND_METHOD = 3000;
114 static final int MSG_BIND_METHOD = 3010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 static final long TIME_TO_RECONNECT = 10*1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800117
satokab751aa2010-09-14 19:17:36 +0900118 private static final int NOT_A_SUBTYPE_ID = -1;
satok723a27e2010-11-11 14:58:11 +0900119 private static final String NOT_A_SUBTYPE_ID_STR = String.valueOf(NOT_A_SUBTYPE_ID);
satok8fbb1e82010-11-02 23:15:58 +0900120 // If IME doesn't support the system locale, the default subtype will be the first defined one.
121 private static final int DEFAULT_SUBTYPE_ID = 0;
satokab751aa2010-09-14 19:17:36 +0900122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 final Context mContext;
124 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900125 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126 final SettingsObserver mSettingsObserver;
Joe Onorato089de882010-04-12 08:18:45 -0700127 final StatusBarManagerService mStatusBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 final IWindowManager mIWindowManager;
129 final HandlerCaller mCaller;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 final InputBindResult mNoBinding = new InputBindResult(null, null, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 // All known input methods. mMethodMap also serves as the global
134 // lock for this class.
satokd87c2592010-09-29 11:52:06 +0900135 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<InputMethodInfo>();
136 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<String, InputMethodInfo>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 class SessionState {
139 final ClientState client;
140 final IInputMethod method;
141 final IInputMethodSession session;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 @Override
144 public String toString() {
145 return "SessionState{uid " + client.uid + " pid " + client.pid
146 + " method " + Integer.toHexString(
147 System.identityHashCode(method))
148 + " session " + Integer.toHexString(
149 System.identityHashCode(session))
150 + "}";
151 }
152
153 SessionState(ClientState _client, IInputMethod _method,
154 IInputMethodSession _session) {
155 client = _client;
156 method = _method;
157 session = _session;
158 }
159 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 class ClientState {
162 final IInputMethodClient client;
163 final IInputContext inputContext;
164 final int uid;
165 final int pid;
166 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 boolean sessionRequested;
169 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800170
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 @Override
172 public String toString() {
173 return "ClientState{" + Integer.toHexString(
174 System.identityHashCode(this)) + " uid " + uid
175 + " pid " + pid + "}";
176 }
177
178 ClientState(IInputMethodClient _client, IInputContext _inputContext,
179 int _uid, int _pid) {
180 client = _client;
181 inputContext = _inputContext;
182 uid = _uid;
183 pid = _pid;
184 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
185 }
186 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 final HashMap<IBinder, ClientState> mClients
189 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700192 * Set once the system is ready to run third party code.
193 */
194 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800195
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700196 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 * Id of the currently selected input method.
198 */
199 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 /**
202 * The current binding sequence number, incremented every time there is
203 * a new bind performed.
204 */
205 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 /**
208 * The client that is currently bound to an input method.
209 */
210 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700213 * The last window token that gained focus.
214 */
215 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800216
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700217 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 * The input context last provided by the current client.
219 */
220 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800221
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 /**
223 * The attributes last provided by the current client.
224 */
225 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800226
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 /**
228 * The input method ID of the input method service that we are currently
229 * connected to or in the process of connecting to.
230 */
231 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 /**
satokab751aa2010-09-14 19:17:36 +0900234 * The current subtype of the current input method.
235 */
236 private InputMethodSubtype mCurrentSubtype;
237
238
239 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 * Set to true if our ServiceConnection is currently actively bound to
241 * a service (whether or not we have gotten its IBinder back yet).
242 */
243 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 /**
246 * Set if the client has asked for the input method to be shown.
247 */
248 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 /**
251 * Set if we were explicitly told to show the input method.
252 */
253 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 /**
256 * Set if we were forced to be shown.
257 */
258 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 /**
261 * Set if we last told the input method to show itself.
262 */
263 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800264
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 /**
266 * The Intent used to connect to the current input method.
267 */
268 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 /**
271 * The token we have made for the currently active input method, to
272 * identify it in the future.
273 */
274 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800275
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 /**
277 * If non-null, this is the input method service we are currently connected
278 * to.
279 */
280 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 /**
283 * Time that we last initiated a bind to the input method, to determine
284 * if we should try to disconnect and reconnect to it.
285 */
286 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 /**
289 * Have we called mCurMethod.bindInput()?
290 */
291 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 /**
294 * Currently enabled session. Only touched by service thread, not
295 * protected by a lock.
296 */
297 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 /**
300 * True if the screen is on. The value is true initially.
301 */
302 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800303
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 AlertDialog.Builder mDialogBuilder;
305 AlertDialog mSwitchingDialog;
306 InputMethodInfo[] mIms;
307 CharSequence[] mItems;
satokab751aa2010-09-14 19:17:36 +0900308 int[] mSubtypeIds;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310 class SettingsObserver extends ContentObserver {
311 SettingsObserver(Handler handler) {
312 super(handler);
313 ContentResolver resolver = mContext.getContentResolver();
314 resolver.registerContentObserver(Settings.Secure.getUriFor(
315 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
satokab751aa2010-09-14 19:17:36 +0900316 resolver.registerContentObserver(Settings.Secure.getUriFor(
317 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 @Override public void onChange(boolean selfChange) {
321 synchronized (mMethodMap) {
322 updateFromSettingsLocked();
323 }
324 }
325 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 class ScreenOnOffReceiver extends android.content.BroadcastReceiver {
328 @Override
329 public void onReceive(Context context, Intent intent) {
330 if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
331 mScreenOn = true;
332 } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
333 mScreenOn = false;
The Android Open Source Project10592532009-03-18 17:39:46 -0700334 } else if (intent.getAction().equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
335 hideInputMethodMenu();
336 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800338 Slog.w(TAG, "Unexpected intent " + intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 }
340
341 // Inform the current client of the change in active status
342 try {
343 if (mCurClient != null && mCurClient.client != null) {
344 mCurClient.client.setActive(mScreenOn);
345 }
346 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800347 Slog.w(TAG, "Got RemoteException sending 'screen on/off' notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 + mCurClient.pid + " uid " + mCurClient.uid);
349 }
350 }
351 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800352
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800353 class MyPackageMonitor extends PackageMonitor {
354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800356 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 synchronized (mMethodMap) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800358 String curInputMethodId = Settings.Secure.getString(mContext
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
360 final int N = mMethodList.size();
361 if (curInputMethodId != null) {
362 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800363 InputMethodInfo imi = mMethodList.get(i);
364 if (imi.getId().equals(curInputMethodId)) {
365 for (String pkg : packages) {
366 if (imi.getPackageName().equals(pkg)) {
367 if (!doit) {
368 return true;
369 }
satok723a27e2010-11-11 14:58:11 +0900370 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800371 chooseNewDefaultIMELocked();
372 return true;
373 }
374 }
375 }
376 }
377 }
378 }
379 return false;
380 }
381
382 @Override
383 public void onSomePackagesChanged() {
384 synchronized (mMethodMap) {
385 InputMethodInfo curIm = null;
386 String curInputMethodId = Settings.Secure.getString(mContext
387 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
388 final int N = mMethodList.size();
389 if (curInputMethodId != null) {
390 for (int i=0; i<N; i++) {
391 InputMethodInfo imi = mMethodList.get(i);
392 if (imi.getId().equals(curInputMethodId)) {
393 curIm = imi;
394 }
395 int change = isPackageDisappearing(imi.getPackageName());
396 if (change == PACKAGE_TEMPORARY_CHANGE
397 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800398 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800399 + imi.getComponent());
400 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 }
402 }
403 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800404
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800405 buildInputMethodListLocked(mMethodList, mMethodMap);
406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800408
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800409 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800410 int change = isPackageDisappearing(curIm.getPackageName());
411 if (change == PACKAGE_TEMPORARY_CHANGE
412 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800413 ServiceInfo si = null;
414 try {
415 si = mContext.getPackageManager().getServiceInfo(
416 curIm.getComponent(), 0);
417 } catch (PackageManager.NameNotFoundException ex) {
418 }
419 if (si == null) {
420 // Uh oh, current input method is no longer around!
421 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800422 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800423 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800424 changed = true;
425 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800426 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900427 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800428 }
429 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800430 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800431 }
satokab751aa2010-09-14 19:17:36 +0900432
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800433 if (curIm == null) {
434 // We currently don't have a default input method... is
435 // one now available?
436 changed = chooseNewDefaultIMELocked();
437 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800438
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800439 if (changed) {
440 updateFromSettingsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441 }
442 }
443 }
444 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800445
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446 class MethodCallback extends IInputMethodCallback.Stub {
447 final IInputMethod mMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800448
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 MethodCallback(IInputMethod method) {
450 mMethod = method;
451 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800452
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 public void finishedEvent(int seq, boolean handled) throws RemoteException {
454 }
455
456 public void sessionCreated(IInputMethodSession session) throws RemoteException {
457 onSessionCreated(mMethod, session);
458 }
459 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800460
Joe Onorato089de882010-04-12 08:18:45 -0700461 public InputMethodManagerService(Context context, StatusBarManagerService statusBar) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 mContext = context;
463 mHandler = new Handler(this);
464 mIWindowManager = IWindowManager.Stub.asInterface(
465 ServiceManager.getService(Context.WINDOW_SERVICE));
466 mCaller = new HandlerCaller(context, new HandlerCaller.Callback() {
467 public void executeMessage(Message msg) {
468 handleMessage(msg);
469 }
470 });
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800471
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800472 (new MyPackageMonitor()).register(mContext, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 IntentFilter screenOnOffFilt = new IntentFilter();
475 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_ON);
476 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_OFF);
The Android Open Source Project10592532009-03-18 17:39:46 -0700477 screenOnOffFilt.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 mContext.registerReceiver(new ScreenOnOffReceiver(), screenOnOffFilt);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800479
satok913a8922010-08-26 21:53:41 +0900480 mStatusBar = statusBar;
481 statusBar.setIconVisibility("ime", false);
482
satokd87c2592010-09-29 11:52:06 +0900483 // mSettings should be created before buildInputMethodListLocked
484 mSettings = new InputMethodSettings(context.getContentResolver(), mMethodMap, mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485 buildInputMethodListLocked(mMethodList, mMethodMap);
satokd87c2592010-09-29 11:52:06 +0900486 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487
satokd87c2592010-09-29 11:52:06 +0900488 if (TextUtils.isEmpty(Settings.Secure.getString(
489 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 InputMethodInfo defIm = null;
satokd87c2592010-09-29 11:52:06 +0900491 for (InputMethodInfo imi: mMethodList) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 if (defIm == null && imi.getIsDefaultResourceId() != 0) {
493 try {
satokd87c2592010-09-29 11:52:06 +0900494 Resources res = context.createPackageContext(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 imi.getPackageName(), 0).getResources();
496 if (res.getBoolean(imi.getIsDefaultResourceId())) {
497 defIm = imi;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800498 Slog.i(TAG, "Selected default: " + imi.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 }
500 } catch (PackageManager.NameNotFoundException ex) {
501 } catch (Resources.NotFoundException ex) {
502 }
503 }
504 }
satokd87c2592010-09-29 11:52:06 +0900505 if (defIm == null && mMethodList.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506 defIm = mMethodList.get(0);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800507 Slog.i(TAG, "No default found, using " + defIm.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 if (defIm != null) {
satok723a27e2010-11-11 14:58:11 +0900510 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511 }
512 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 mSettingsObserver = new SettingsObserver(mHandler);
515 updateFromSettingsLocked();
516 }
517
518 @Override
519 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
520 throws RemoteException {
521 try {
522 return super.onTransact(code, data, reply, flags);
523 } catch (RuntimeException e) {
524 // The input method manager only throws security exceptions, so let's
525 // log all others.
526 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800527 Slog.e(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528 }
529 throw e;
530 }
531 }
532
533 public void systemReady() {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700534 synchronized (mMethodMap) {
535 if (!mSystemReady) {
536 mSystemReady = true;
Dianne Hackborncc278702009-09-02 23:07:23 -0700537 try {
538 startInputInnerLocked();
539 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800540 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700541 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700542 }
543 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800545
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800546 public List<InputMethodInfo> getInputMethodList() {
547 synchronized (mMethodMap) {
548 return new ArrayList<InputMethodInfo>(mMethodList);
549 }
550 }
551
552 public List<InputMethodInfo> getEnabledInputMethodList() {
553 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900554 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 }
556 }
557
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 public void addClient(IInputMethodClient client,
559 IInputContext inputContext, int uid, int pid) {
560 synchronized (mMethodMap) {
561 mClients.put(client.asBinder(), new ClientState(client,
562 inputContext, uid, pid));
563 }
564 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800565
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566 public void removeClient(IInputMethodClient client) {
567 synchronized (mMethodMap) {
568 mClients.remove(client.asBinder());
569 }
570 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800572 void executeOrSendMessage(IInterface target, Message msg) {
573 if (target.asBinder() instanceof Binder) {
574 mCaller.sendMessage(msg);
575 } else {
576 handleMessage(msg);
577 msg.recycle();
578 }
579 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800580
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700581 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800583 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 + mCurClient.client.asBinder());
585 if (mBoundToMethod) {
586 mBoundToMethod = false;
587 if (mCurMethod != null) {
588 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
589 MSG_UNBIND_INPUT, mCurMethod));
590 }
591 }
592 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
593 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
594 mCurClient.sessionRequested = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800595
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596 // Call setActive(false) on the old client
597 try {
598 mCurClient.client.setActive(false);
599 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800600 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 + mCurClient.pid + " uid " + mCurClient.uid);
602 }
603 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800604
The Android Open Source Project10592532009-03-18 17:39:46 -0700605 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606 }
607 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800608
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609 private int getImeShowFlags() {
610 int flags = 0;
611 if (mShowForced) {
612 flags |= InputMethod.SHOW_FORCED
613 | InputMethod.SHOW_EXPLICIT;
614 } else if (mShowExplicitlyRequested) {
615 flags |= InputMethod.SHOW_EXPLICIT;
616 }
617 return flags;
618 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800619
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 private int getAppShowFlags() {
621 int flags = 0;
622 if (mShowForced) {
623 flags |= InputMethodManager.SHOW_FORCED;
624 } else if (!mShowExplicitlyRequested) {
625 flags |= InputMethodManager.SHOW_IMPLICIT;
626 }
627 return flags;
628 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800629
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630 InputBindResult attachNewInputLocked(boolean initial, boolean needResult) {
631 if (!mBoundToMethod) {
632 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
633 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
634 mBoundToMethod = true;
635 }
636 final SessionState session = mCurClient.curSession;
637 if (initial) {
638 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
639 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
640 } else {
641 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
642 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
643 }
644 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800645 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -0800646 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800647 }
648 return needResult
649 ? new InputBindResult(session.session, mCurId, mCurSeq)
650 : null;
651 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800652
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800653 InputBindResult startInputLocked(IInputMethodClient client,
654 IInputContext inputContext, EditorInfo attribute,
655 boolean initial, boolean needResult) {
656 // If no method is currently selected, do nothing.
657 if (mCurMethodId == null) {
658 return mNoBinding;
659 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800660
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800661 ClientState cs = mClients.get(client.asBinder());
662 if (cs == null) {
663 throw new IllegalArgumentException("unknown client "
664 + client.asBinder());
665 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800666
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 try {
668 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
669 // Check with the window manager to make sure this client actually
670 // has a window with focus. If not, reject. This is thread safe
671 // because if the focus changes some time before or after, the
672 // next client receiving focus that has any interest in input will
673 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800674 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
676 return null;
677 }
678 } catch (RemoteException e) {
679 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800680
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 if (mCurClient != cs) {
682 // If the client is changing, we need to switch over to the new
683 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700684 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800685 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800686 + cs.client.asBinder());
687
688 // If the screen is on, inform the new client it is active
689 if (mScreenOn) {
690 try {
691 cs.client.setActive(mScreenOn);
692 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800693 Slog.w(TAG, "Got RemoteException sending setActive notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800694 + cs.pid + " uid " + cs.uid);
695 }
696 }
697 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800698
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800699 // Bump up the sequence for this client and attach it.
700 mCurSeq++;
701 if (mCurSeq <= 0) mCurSeq = 1;
702 mCurClient = cs;
703 mCurInputContext = inputContext;
704 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 // Check if the input method is changing.
707 if (mCurId != null && mCurId.equals(mCurMethodId)) {
708 if (cs.curSession != null) {
709 // Fast case: if we are already connected to the input method,
710 // then just return it.
711 return attachNewInputLocked(initial, needResult);
712 }
713 if (mHaveConnection) {
714 if (mCurMethod != null) {
715 if (!cs.sessionRequested) {
716 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800717 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800718 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
719 MSG_CREATE_SESSION, mCurMethod,
720 new MethodCallback(mCurMethod)));
721 }
722 // Return to client, and we will get back with it when
723 // we have had a session made for it.
724 return new InputBindResult(null, mCurId, mCurSeq);
725 } else if (SystemClock.uptimeMillis()
726 < (mLastBindTime+TIME_TO_RECONNECT)) {
727 // In this case we have connected to the service, but
728 // don't yet have its interface. If it hasn't been too
729 // long since we did the connection, we'll return to
730 // the client and wait to get the service interface so
731 // we can report back. If it has been too long, we want
732 // to fall through so we can try a disconnect/reconnect
733 // to see if we can get back in touch with the service.
734 return new InputBindResult(null, mCurId, mCurSeq);
735 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800736 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
737 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800738 }
739 }
740 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800741
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700742 return startInputInnerLocked();
743 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800744
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700745 InputBindResult startInputInnerLocked() {
746 if (mCurMethodId == null) {
747 return mNoBinding;
748 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800749
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700750 if (!mSystemReady) {
751 // If the system is not yet ready, we shouldn't be running third
752 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -0700753 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700754 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800755
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800756 InputMethodInfo info = mMethodMap.get(mCurMethodId);
757 if (info == null) {
758 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
759 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800760
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700761 unbindCurrentMethodLocked(false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800762
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800763 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
764 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -0700765 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
766 com.android.internal.R.string.input_method_binding_label);
767 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
768 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800769 if (mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE)) {
770 mLastBindTime = SystemClock.uptimeMillis();
771 mHaveConnection = true;
772 mCurId = info.getId();
773 mCurToken = new Binder();
774 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800775 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800776 mIWindowManager.addWindowToken(mCurToken,
777 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
778 } catch (RemoteException e) {
779 }
780 return new InputBindResult(null, mCurId, mCurSeq);
781 } else {
782 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800783 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800784 + mCurIntent);
785 }
786 return null;
787 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800788
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800789 public InputBindResult startInput(IInputMethodClient client,
790 IInputContext inputContext, EditorInfo attribute,
791 boolean initial, boolean needResult) {
792 synchronized (mMethodMap) {
793 final long ident = Binder.clearCallingIdentity();
794 try {
795 return startInputLocked(client, inputContext, attribute,
796 initial, needResult);
797 } finally {
798 Binder.restoreCallingIdentity(ident);
799 }
800 }
801 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800802
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800803 public void finishInput(IInputMethodClient client) {
804 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800805
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800806 public void onServiceConnected(ComponentName name, IBinder service) {
807 synchronized (mMethodMap) {
808 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
809 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -0700810 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800811 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborncc278702009-09-02 23:07:23 -0700812 unbindCurrentMethodLocked(false);
813 return;
814 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800815 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -0700816 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
817 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800818 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800819 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -0700820 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800821 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -0700822 MSG_CREATE_SESSION, mCurMethod,
823 new MethodCallback(mCurMethod)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800824 }
825 }
826 }
827 }
828
829 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
830 synchronized (mMethodMap) {
831 if (mCurMethod != null && method != null
832 && mCurMethod.asBinder() == method.asBinder()) {
833 if (mCurClient != null) {
834 mCurClient.curSession = new SessionState(mCurClient,
835 method, session);
836 mCurClient.sessionRequested = false;
837 InputBindResult res = attachNewInputLocked(true, true);
838 if (res.method != null) {
839 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
840 MSG_BIND_METHOD, mCurClient.client, res));
841 }
842 }
843 }
844 }
845 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800846
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700847 void unbindCurrentMethodLocked(boolean reportToClient) {
848 if (mHaveConnection) {
849 mContext.unbindService(this);
850 mHaveConnection = false;
851 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800852
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700853 if (mCurToken != null) {
854 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800855 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700856 mIWindowManager.removeWindowToken(mCurToken);
857 } catch (RemoteException e) {
858 }
859 mCurToken = null;
860 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800861
The Android Open Source Project10592532009-03-18 17:39:46 -0700862 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700863 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800864
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700865 if (reportToClient && mCurClient != null) {
866 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
867 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
868 }
869 }
870
Devin Taylor0c33ed22010-02-23 13:26:46 -0600871 private void finishSession(SessionState sessionState) {
872 if (sessionState != null && sessionState.session != null) {
873 try {
874 sessionState.session.finishSession();
875 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -0700876 Slog.w(TAG, "Session failed to close due to remote exception", e);
Devin Taylor0c33ed22010-02-23 13:26:46 -0600877 }
878 }
879 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800880
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700881 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800882 if (mCurMethod != null) {
883 for (ClientState cs : mClients.values()) {
884 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -0600885 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800886 cs.curSession = null;
887 }
Devin Taylor0c33ed22010-02-23 13:26:46 -0600888
889 finishSession(mEnabledSession);
890 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800891 mCurMethod = null;
892 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700893 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800894 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800895
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800896 public void onServiceDisconnected(ComponentName name) {
897 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800898 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800899 + " mCurIntent=" + mCurIntent);
900 if (mCurMethod != null && mCurIntent != null
901 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700902 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800903 // We consider this to be a new bind attempt, since the system
904 // should now try to restart the service for us.
905 mLastBindTime = SystemClock.uptimeMillis();
906 mShowRequested = mInputShown;
907 mInputShown = false;
908 if (mCurClient != null) {
909 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
910 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
911 }
912 }
913 }
914 }
915
916 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700917 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800918 long ident = Binder.clearCallingIdentity();
919 try {
920 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700921 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800922 return;
923 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800925 synchronized (mMethodMap) {
926 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800927 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -0700928 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800929 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800930 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -0700931 mStatusBar.setIcon("ime", packageName, iconId, 0);
932 mStatusBar.setIconVisibility("ime", true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800933 }
934 }
935 } finally {
936 Binder.restoreCallingIdentity(ident);
937 }
938 }
939
satok06487a52010-10-29 11:37:18 +0900940 public void setIMEButtonVisible(IBinder token, boolean visible) {
941 int uid = Binder.getCallingUid();
942 long ident = Binder.clearCallingIdentity();
943 try {
944 if (token == null || mCurToken != token) {
945 Slog.w(TAG, "Ignoring setIMEButtonVisible of uid " + uid + " token: " + token);
946 return;
947 }
948
949 synchronized (mMethodMap) {
950 mStatusBar.setIMEButtonVisible(visible);
951 }
952 } finally {
953 Binder.restoreCallingIdentity(ident);
954 }
955 }
956
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800957 void updateFromSettingsLocked() {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700958 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
959 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
960 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
961 // enabled.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800962 String id = Settings.Secure.getString(mContext.getContentResolver(),
satokab751aa2010-09-14 19:17:36 +0900963 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +0900964 // There is no input method selected, try to choose new applicable input method.
965 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
966 id = Settings.Secure.getString(mContext.getContentResolver(),
967 Settings.Secure.DEFAULT_INPUT_METHOD);
968 }
969 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800970 try {
satokab751aa2010-09-14 19:17:36 +0900971 setInputMethodLocked(id, getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800972 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800973 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -0700974 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700975 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800976 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700977 } else {
978 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -0700979 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700980 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800981 }
982 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800983
satokab751aa2010-09-14 19:17:36 +0900984 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800985 InputMethodInfo info = mMethodMap.get(id);
986 if (info == null) {
satok913a8922010-08-26 21:53:41 +0900987 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800989
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990 if (id.equals(mCurMethodId)) {
satokb66d2872010-11-10 01:04:04 +0900991 ArrayList<InputMethodSubtype> subtypes = info.getSubtypes();
992 if (subtypeId >= 0 && subtypeId < subtypes.size()) {
993 InputMethodSubtype subtype = subtypes.get(subtypeId);
satokab751aa2010-09-14 19:17:36 +0900994 if (subtype != mCurrentSubtype) {
995 synchronized (mMethodMap) {
996 if (mCurMethod != null) {
997 try {
satok723a27e2010-11-11 14:58:11 +0900998 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
satok06e07442010-11-02 19:46:55 +0900999 if (mInputShown) {
1000 // If mInputShown is false, there is no IME button on the
1001 // system bar.
1002 // Thus there is no need to make it invisible explicitly.
1003 mStatusBar.setIMEButtonVisible(true);
1004 }
satokab751aa2010-09-14 19:17:36 +09001005 mCurMethod.changeInputMethodSubtype(subtype);
1006 } catch (RemoteException e) {
1007 return;
1008 }
1009 }
1010 }
1011 }
1012 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001013 return;
1014 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001015
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001016 final long ident = Binder.clearCallingIdentity();
1017 try {
satokab751aa2010-09-14 19:17:36 +09001018 // Set a subtype to this input method.
1019 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001020 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1021 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1022 // because mCurMethodId is stored as a history in
1023 // setSelectedInputMethodAndSubtypeLocked().
1024 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025
1026 if (ActivityManagerNative.isSystemReady()) {
1027 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001028 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001029 intent.putExtra("input_method_id", id);
1030 mContext.sendBroadcast(intent);
1031 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001032 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001033 } finally {
1034 Binder.restoreCallingIdentity(ident);
1035 }
1036 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001037
The Android Open Source Project4df24232009-03-05 14:34:35 -08001038 public boolean showSoftInput(IInputMethodClient client, int flags,
1039 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001040 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001041 long ident = Binder.clearCallingIdentity();
1042 try {
1043 synchronized (mMethodMap) {
1044 if (mCurClient == null || client == null
1045 || mCurClient.client.asBinder() != client.asBinder()) {
1046 try {
1047 // We need to check if this is the current client with
1048 // focus in the window manager, to allow this call to
1049 // be made before input is started in it.
1050 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001051 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001052 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001053 }
1054 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001055 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001056 }
1057 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001058
Joe Onorato8a9b2202010-02-26 18:56:32 -08001059 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001060 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001061 }
1062 } finally {
1063 Binder.restoreCallingIdentity(ident);
1064 }
1065 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001066
The Android Open Source Project4df24232009-03-05 14:34:35 -08001067 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001068 mShowRequested = true;
1069 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1070 mShowExplicitlyRequested = true;
1071 }
1072 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1073 mShowExplicitlyRequested = true;
1074 mShowForced = true;
1075 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001076
Dianne Hackborncc278702009-09-02 23:07:23 -07001077 if (!mSystemReady) {
1078 return false;
1079 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001080
The Android Open Source Project4df24232009-03-05 14:34:35 -08001081 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001082 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001083 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1084 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1085 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001086 mInputShown = true;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001087 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001088 } else if (mHaveConnection && SystemClock.uptimeMillis()
1089 < (mLastBindTime+TIME_TO_RECONNECT)) {
1090 // The client has asked to have the input method shown, but
1091 // we have been sitting here too long with a connection to the
1092 // service and no interface received, so let's disconnect/connect
1093 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001094 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095 SystemClock.uptimeMillis()-mLastBindTime,1);
1096 mContext.unbindService(this);
1097 mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE);
1098 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001099
The Android Open Source Project4df24232009-03-05 14:34:35 -08001100 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001101 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001102
The Android Open Source Project4df24232009-03-05 14:34:35 -08001103 public boolean hideSoftInput(IInputMethodClient client, int flags,
1104 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001105 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001106 long ident = Binder.clearCallingIdentity();
1107 try {
1108 synchronized (mMethodMap) {
1109 if (mCurClient == null || client == null
1110 || mCurClient.client.asBinder() != client.asBinder()) {
1111 try {
1112 // We need to check if this is the current client with
1113 // focus in the window manager, to allow this call to
1114 // be made before input is started in it.
1115 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001116 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1117 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001118 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001119 }
1120 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001121 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001122 }
1123 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001124
Joe Onorato8a9b2202010-02-26 18:56:32 -08001125 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001126 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001127 }
1128 } finally {
1129 Binder.restoreCallingIdentity(ident);
1130 }
1131 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001132
The Android Open Source Project4df24232009-03-05 14:34:35 -08001133 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001134 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1135 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001136 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001137 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001138 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001139 }
1140 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001141 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001142 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001143 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001145 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001146 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001147 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1148 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1149 res = true;
1150 } else {
1151 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001152 }
1153 mInputShown = false;
1154 mShowRequested = false;
1155 mShowExplicitlyRequested = false;
1156 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001157 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001158 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001159
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001160 public void windowGainedFocus(IInputMethodClient client, IBinder windowToken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001161 boolean viewHasFocus, boolean isTextEditor, int softInputMode,
1162 boolean first, int windowFlags) {
1163 long ident = Binder.clearCallingIdentity();
1164 try {
1165 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001166 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001167 + " viewHasFocus=" + viewHasFocus
1168 + " isTextEditor=" + isTextEditor
1169 + " softInputMode=#" + Integer.toHexString(softInputMode)
1170 + " first=" + first + " flags=#"
1171 + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 if (mCurClient == null || client == null
1174 || mCurClient.client.asBinder() != client.asBinder()) {
1175 try {
1176 // We need to check if this is the current client with
1177 // focus in the window manager, to allow this call to
1178 // be made before input is started in it.
1179 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001180 Slog.w(TAG, "Client not active, ignoring focus gain of: " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 return;
1182 }
1183 } catch (RemoteException e) {
1184 }
1185 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001186
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001187 if (mCurFocusedWindow == windowToken) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001188 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001189 return;
1190 }
1191 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001192
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001193 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1194 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
1195 if (!isTextEditor || (softInputMode &
1196 WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1197 != WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE) {
1198 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1199 // There is no focus view, and this window will
1200 // be behind any soft input window, so hide the
1201 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001202 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001203 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204 }
1205 } else if (isTextEditor && (softInputMode &
1206 WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1207 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1208 && (softInputMode &
1209 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
1210 // There is a focus view, and we are navigating forward
1211 // into the window, so show the input window for the user.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001212 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001213 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 }
1215 break;
1216 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1217 // Do nothing.
1218 break;
1219 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1220 if ((softInputMode &
1221 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001222 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001223 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224 }
1225 break;
1226 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001227 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001228 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 break;
1230 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1231 if ((softInputMode &
1232 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001233 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001234 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 }
1236 break;
1237 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001238 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001239 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001240 break;
1241 }
1242 }
1243 } finally {
1244 Binder.restoreCallingIdentity(ident);
1245 }
1246 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001248 public void showInputMethodPickerFromClient(IInputMethodClient client) {
1249 synchronized (mMethodMap) {
1250 if (mCurClient == null || client == null
1251 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001252 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001253 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001254 }
1255
1256 mHandler.sendEmptyMessage(MSG_SHOW_IM_PICKER);
1257 }
1258 }
1259
satokab751aa2010-09-14 19:17:36 +09001260 public void showInputMethodSubtypePickerFromClient(IInputMethodClient client) {
1261 synchronized (mMethodMap) {
1262 if (mCurClient == null || client == null
1263 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001264 Slog.w(TAG, "Ignoring showInputMethodSubtypePickerFromClient of: " + client);
satokab751aa2010-09-14 19:17:36 +09001265 }
1266
1267 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1268 }
1269 }
1270
satok47a44912010-10-06 16:03:58 +09001271 public void showInputMethodAndSubtypeEnablerFromClient(
1272 IInputMethodClient client, String topId) {
1273 // TODO: Handle topId for setting the top position of the list activity
1274 synchronized (mMethodMap) {
1275 if (mCurClient == null || client == null
1276 || mCurClient.client.asBinder() != client.asBinder()) {
1277 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
1278 }
1279
1280 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_ENABLER);
1281 }
1282 }
1283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 public void setInputMethod(IBinder token, String id) {
satokab751aa2010-09-14 19:17:36 +09001285 setInputMethodWithSubtype(token, id, NOT_A_SUBTYPE_ID);
1286 }
1287
satok735cf382010-11-11 20:40:09 +09001288 public boolean switchToLastInputMethod(IBinder token) {
1289 synchronized (mMethodMap) {
1290 Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
1291 if (lastIme != null) {
1292 InputMethodInfo imi = mMethodMap.get(lastIme.first);
1293 if (imi != null) {
1294 setInputMethodWithSubtype(token, lastIme.first, getSubtypeIdFromHashCode(
1295 imi, Integer.valueOf(lastIme.second)));
1296 return true;
1297 }
1298 }
1299 return false;
1300 }
1301 }
1302
satokab751aa2010-09-14 19:17:36 +09001303 private void setInputMethodWithSubtype(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001304 synchronized (mMethodMap) {
1305 if (token == null) {
1306 if (mContext.checkCallingOrSelfPermission(
1307 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1308 != PackageManager.PERMISSION_GRANTED) {
1309 throw new SecurityException(
1310 "Using null token requires permission "
1311 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1312 }
1313 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001314 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
1315 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001316 return;
1317 }
1318
1319 long ident = Binder.clearCallingIdentity();
1320 try {
satokab751aa2010-09-14 19:17:36 +09001321 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001322 } finally {
1323 Binder.restoreCallingIdentity(ident);
1324 }
1325 }
1326 }
1327
1328 public void hideMySoftInput(IBinder token, int flags) {
1329 synchronized (mMethodMap) {
1330 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001331 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
1332 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001333 return;
1334 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001335 long ident = Binder.clearCallingIdentity();
1336 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001337 hideCurrentInputLocked(flags, null);
1338 } finally {
1339 Binder.restoreCallingIdentity(ident);
1340 }
1341 }
1342 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001343
The Android Open Source Project4df24232009-03-05 14:34:35 -08001344 public void showMySoftInput(IBinder token, int flags) {
1345 synchronized (mMethodMap) {
1346 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001347 Slog.w(TAG, "Ignoring showMySoftInput of uid "
1348 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001349 return;
1350 }
1351 long ident = Binder.clearCallingIdentity();
1352 try {
1353 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001354 } finally {
1355 Binder.restoreCallingIdentity(ident);
1356 }
1357 }
1358 }
1359
1360 void setEnabledSessionInMainThread(SessionState session) {
1361 if (mEnabledSession != session) {
1362 if (mEnabledSession != null) {
1363 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001364 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001365 mEnabledSession.method.setSessionEnabled(
1366 mEnabledSession.session, false);
1367 } catch (RemoteException e) {
1368 }
1369 }
1370 mEnabledSession = session;
1371 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001372 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001373 session.method.setSessionEnabled(
1374 session.session, true);
1375 } catch (RemoteException e) {
1376 }
1377 }
1378 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001379
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001380 public boolean handleMessage(Message msg) {
1381 HandlerCaller.SomeArgs args;
1382 switch (msg.what) {
1383 case MSG_SHOW_IM_PICKER:
1384 showInputMethodMenu();
1385 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001386
satokab751aa2010-09-14 19:17:36 +09001387 case MSG_SHOW_IM_SUBTYPE_PICKER:
1388 showInputMethodSubtypeMenu();
1389 return true;
1390
satok47a44912010-10-06 16:03:58 +09001391 case MSG_SHOW_IM_SUBTYPE_ENABLER:
1392 showInputMethodAndSubtypeEnabler();
1393 return true;
1394
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001395 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001396
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001397 case MSG_UNBIND_INPUT:
1398 try {
1399 ((IInputMethod)msg.obj).unbindInput();
1400 } catch (RemoteException e) {
1401 // There is nothing interesting about the method dying.
1402 }
1403 return true;
1404 case MSG_BIND_INPUT:
1405 args = (HandlerCaller.SomeArgs)msg.obj;
1406 try {
1407 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
1408 } catch (RemoteException e) {
1409 }
1410 return true;
1411 case MSG_SHOW_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001412 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001413 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001414 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
1415 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001416 } catch (RemoteException e) {
1417 }
1418 return true;
1419 case MSG_HIDE_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001420 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001421 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001422 ((IInputMethod)args.arg1).hideSoftInput(0,
1423 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424 } catch (RemoteException e) {
1425 }
1426 return true;
1427 case MSG_ATTACH_TOKEN:
1428 args = (HandlerCaller.SomeArgs)msg.obj;
1429 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001430 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001431 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
1432 } catch (RemoteException e) {
1433 }
1434 return true;
1435 case MSG_CREATE_SESSION:
1436 args = (HandlerCaller.SomeArgs)msg.obj;
1437 try {
1438 ((IInputMethod)args.arg1).createSession(
1439 (IInputMethodCallback)args.arg2);
1440 } catch (RemoteException e) {
1441 }
1442 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001443 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001444
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001445 case MSG_START_INPUT:
1446 args = (HandlerCaller.SomeArgs)msg.obj;
1447 try {
1448 SessionState session = (SessionState)args.arg1;
1449 setEnabledSessionInMainThread(session);
1450 session.method.startInput((IInputContext)args.arg2,
1451 (EditorInfo)args.arg3);
1452 } catch (RemoteException e) {
1453 }
1454 return true;
1455 case MSG_RESTART_INPUT:
1456 args = (HandlerCaller.SomeArgs)msg.obj;
1457 try {
1458 SessionState session = (SessionState)args.arg1;
1459 setEnabledSessionInMainThread(session);
1460 session.method.restartInput((IInputContext)args.arg2,
1461 (EditorInfo)args.arg3);
1462 } catch (RemoteException e) {
1463 }
1464 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001466 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001468 case MSG_UNBIND_METHOD:
1469 try {
1470 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
1471 } catch (RemoteException e) {
1472 // There is nothing interesting about the last client dying.
1473 }
1474 return true;
1475 case MSG_BIND_METHOD:
1476 args = (HandlerCaller.SomeArgs)msg.obj;
1477 try {
1478 ((IInputMethodClient)args.arg1).onBindMethod(
1479 (InputBindResult)args.arg2);
1480 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001481 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001482 }
1483 return true;
1484 }
1485 return false;
1486 }
1487
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001488 private boolean isSystemIme(InputMethodInfo inputMethod) {
1489 return (inputMethod.getServiceInfo().applicationInfo.flags
1490 & ApplicationInfo.FLAG_SYSTEM) != 0;
1491 }
1492
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001493 private boolean chooseNewDefaultIMELocked() {
satokd87c2592010-09-29 11:52:06 +09001494 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001495 if (enabled != null && enabled.size() > 0) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07001496 // We'd prefer to fall back on a system IME, since that is safer.
1497 int i=enabled.size();
1498 while (i > 0) {
1499 i--;
1500 if ((enabled.get(i).getServiceInfo().applicationInfo.flags
1501 & ApplicationInfo.FLAG_SYSTEM) != 0) {
1502 break;
1503 }
1504 }
satokab751aa2010-09-14 19:17:36 +09001505 InputMethodInfo imi = enabled.get(i);
satok03eb319a2010-11-11 18:17:42 +09001506 if (DEBUG) {
1507 Slog.d(TAG, "New default IME was selected: " + imi.getId());
1508 }
satok723a27e2010-11-11 14:58:11 +09001509 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001510 return true;
1511 }
1512
1513 return false;
1514 }
1515
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001516 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
1517 HashMap<String, InputMethodInfo> map) {
1518 list.clear();
1519 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001520
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001521 PackageManager pm = mContext.getPackageManager();
Amith Yamasanie861ec12010-03-24 21:39:27 -07001522 final Configuration config = mContext.getResources().getConfiguration();
1523 final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY;
1524 String disabledSysImes = Settings.Secure.getString(mContext.getContentResolver(),
1525 Secure.DISABLED_SYSTEM_INPUT_METHODS);
1526 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001527
1528 List<ResolveInfo> services = pm.queryIntentServices(
1529 new Intent(InputMethod.SERVICE_INTERFACE),
1530 PackageManager.GET_META_DATA);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001531
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001532 for (int i = 0; i < services.size(); ++i) {
1533 ResolveInfo ri = services.get(i);
1534 ServiceInfo si = ri.serviceInfo;
1535 ComponentName compName = new ComponentName(si.packageName, si.name);
1536 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
1537 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001538 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001539 + ": it does not require the permission "
1540 + android.Manifest.permission.BIND_INPUT_METHOD);
1541 continue;
1542 }
1543
Joe Onorato8a9b2202010-02-26 18:56:32 -08001544 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001545
1546 try {
1547 InputMethodInfo p = new InputMethodInfo(mContext, ri);
1548 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07001549 final String id = p.getId();
1550 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001551
Amith Yamasanie861ec12010-03-24 21:39:27 -07001552 // System IMEs are enabled by default, unless there's a hard keyboard
1553 // and the system IME was explicitly disabled
1554 if (isSystemIme(p) && (!haveHardKeyboard || disabledSysImes.indexOf(id) < 0)) {
1555 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001556 }
1557
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001558 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001559 Slog.d(TAG, "Found a third-party input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001560 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001561
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001562 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001563 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001564 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001565 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001566 }
1567 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001568
1569 String defaultIme = Settings.Secure.getString(mContext
1570 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok913a8922010-08-26 21:53:41 +09001571 if (!TextUtils.isEmpty(defaultIme) && !map.containsKey(defaultIme)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001572 if (chooseNewDefaultIMELocked()) {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001573 updateFromSettingsLocked();
1574 }
1575 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001576 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001577
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001578 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001579
satokab751aa2010-09-14 19:17:36 +09001580 private void showInputMethodMenu() {
1581 showInputMethodMenuInternal(false);
1582 }
1583
1584 private void showInputMethodSubtypeMenu() {
1585 showInputMethodMenuInternal(true);
1586 }
1587
satok47a44912010-10-06 16:03:58 +09001588 private void showInputMethodAndSubtypeEnabler() {
satok86417ea2010-10-27 14:11:03 +09001589 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_AND_SUBTYPE_ENABLER);
satok47a44912010-10-06 16:03:58 +09001590 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09001591 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
1592 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok47a44912010-10-06 16:03:58 +09001593 mContext.startActivity(intent);
1594 }
1595
satokab751aa2010-09-14 19:17:36 +09001596 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001597 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001598
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001599 final Context context = mContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001600
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001601 final PackageManager pm = context.getPackageManager();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001602
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001603 String lastInputMethodId = Settings.Secure.getString(context
1604 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satokab751aa2010-09-14 19:17:36 +09001605 int lastInputMethodSubtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08001606 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001607
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001608 synchronized (mMethodMap) {
satok7f35c8c2010-10-07 21:13:11 +09001609 final List<Pair<InputMethodInfo, ArrayList<String>>> immis =
1610 mSettings.getEnabledInputMethodAndSubtypeListLocked();
1611 ArrayList<Integer> subtypeIds = new ArrayList<Integer>();
1612
1613 if (immis == null || immis.size() == 0) {
1614 return;
1615 }
1616
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001617 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001618
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001619 int N = immis.size();
satok913a8922010-08-26 21:53:41 +09001620
satokab751aa2010-09-14 19:17:36 +09001621 final Map<CharSequence, Pair<InputMethodInfo, Integer>> imMap =
1622 new TreeMap<CharSequence, Pair<InputMethodInfo, Integer>>(Collator.getInstance());
satok913a8922010-08-26 21:53:41 +09001623
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001624 for (int i = 0; i < N; ++i) {
satok7f35c8c2010-10-07 21:13:11 +09001625 InputMethodInfo property = immis.get(i).first;
1626 final ArrayList<String> enabledSubtypeIds = immis.get(i).second;
1627 HashSet<String> enabledSubtypeSet = new HashSet<String>();
1628 for (String s : enabledSubtypeIds) {
1629 enabledSubtypeSet.add(s);
1630 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001631 if (property == null) {
1632 continue;
1633 }
satokab751aa2010-09-14 19:17:36 +09001634 ArrayList<InputMethodSubtype> subtypes = property.getSubtypes();
1635 CharSequence label = property.loadLabel(pm);
satok7f35c8c2010-10-07 21:13:11 +09001636 if (showSubtypes && enabledSubtypeSet.size() > 0) {
satokab751aa2010-09-14 19:17:36 +09001637 for (int j = 0; j < subtypes.size(); ++j) {
1638 InputMethodSubtype subtype = subtypes.get(j);
satok7f35c8c2010-10-07 21:13:11 +09001639 if (enabledSubtypeSet.contains(String.valueOf(subtype.hashCode()))) {
1640 CharSequence title;
1641 int nameResId = subtype.getNameResId();
satok9ef02832010-11-04 21:17:48 +09001642 String mode = subtype.getMode();
satok7f35c8c2010-10-07 21:13:11 +09001643 if (nameResId != 0) {
1644 title = pm.getText(property.getPackageName(), nameResId,
1645 property.getServiceInfo().applicationInfo);
1646 } else {
1647 CharSequence language = subtype.getLocale();
satok7f35c8c2010-10-07 21:13:11 +09001648 // TODO: Use more friendly Title and UI
1649 title = label + "," + (mode == null ? "" : mode) + ","
1650 + (language == null ? "" : language);
1651 }
1652 imMap.put(title, new Pair<InputMethodInfo, Integer>(property, j));
satokab751aa2010-09-14 19:17:36 +09001653 }
satokab751aa2010-09-14 19:17:36 +09001654 }
1655 } else {
1656 imMap.put(label,
1657 new Pair<InputMethodInfo, Integer>(property, NOT_A_SUBTYPE_ID));
1658 subtypeIds.add(0);
1659 }
Dianne Hackborn97106ab2010-03-03 00:08:31 -08001660 }
satok913a8922010-08-26 21:53:41 +09001661
1662 N = imMap.size();
1663 mItems = imMap.keySet().toArray(new CharSequence[N]);
satokab751aa2010-09-14 19:17:36 +09001664 mIms = new InputMethodInfo[N];
1665 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001666 int checkedItem = 0;
1667 for (int i = 0; i < N; ++i) {
satokab751aa2010-09-14 19:17:36 +09001668 Pair<InputMethodInfo, Integer> value = imMap.get(mItems[i]);
1669 mIms[i] = value.first;
1670 mSubtypeIds[i] = value.second;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001671 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09001672 int subtypeId = mSubtypeIds[i];
1673 if ((subtypeId == NOT_A_SUBTYPE_ID)
1674 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
1675 || (subtypeId == lastInputMethodSubtypeId)) {
1676 checkedItem = i;
1677 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001678 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001679 }
satokab751aa2010-09-14 19:17:36 +09001680
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001681 AlertDialog.OnClickListener adocl = new AlertDialog.OnClickListener() {
1682 public void onClick(DialogInterface dialog, int which) {
1683 hideInputMethodMenu();
1684 }
1685 };
satokd87c2592010-09-29 11:52:06 +09001686
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001687 TypedArray a = context.obtainStyledAttributes(null,
1688 com.android.internal.R.styleable.DialogPreference,
1689 com.android.internal.R.attr.alertDialogStyle, 0);
1690 mDialogBuilder = new AlertDialog.Builder(context)
1691 .setTitle(com.android.internal.R.string.select_input_method)
1692 .setOnCancelListener(new OnCancelListener() {
1693 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001694 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001695 }
1696 })
1697 .setIcon(a.getDrawable(
1698 com.android.internal.R.styleable.DialogPreference_dialogTitle));
1699 a.recycle();
satokd87c2592010-09-29 11:52:06 +09001700
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001701 mDialogBuilder.setSingleChoiceItems(mItems, checkedItem,
1702 new AlertDialog.OnClickListener() {
1703 public void onClick(DialogInterface dialog, int which) {
1704 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09001705 if (mIms == null || mIms.length <= which
1706 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001707 return;
1708 }
1709 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09001710 int subtypeId = mSubtypeIds[which];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001711 hideInputMethodMenu();
1712 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09001713 if ((subtypeId < 0)
1714 || (subtypeId >= im.getSubtypes().size())) {
1715 subtypeId = NOT_A_SUBTYPE_ID;
1716 }
1717 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001718 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08001719 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001720 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001721 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001722
satok7f35c8c2010-10-07 21:13:11 +09001723 if (showSubtypes) {
1724 mDialogBuilder.setPositiveButton(com.android.internal.R.string.more_item_label,
1725 new DialogInterface.OnClickListener() {
1726 public void onClick(DialogInterface dialog, int whichButton) {
1727 showInputMethodAndSubtypeEnabler();
1728 }
1729 });
1730 }
satok0ff647b2010-10-08 13:49:28 +09001731 mDialogBuilder.setNegativeButton(com.android.internal.R.string.cancel,
1732 new DialogInterface.OnClickListener() {
1733 public void onClick(DialogInterface dialog, int whichButton) {
1734 hideInputMethodMenu();
1735 }
1736 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001737 mSwitchingDialog = mDialogBuilder.create();
1738 mSwitchingDialog.getWindow().setType(
1739 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
1740 mSwitchingDialog.show();
1741 }
1742 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001743
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001744 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07001745 synchronized (mMethodMap) {
1746 hideInputMethodMenuLocked();
1747 }
1748 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001749
The Android Open Source Project10592532009-03-18 17:39:46 -07001750 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001751 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001752
The Android Open Source Project10592532009-03-18 17:39:46 -07001753 if (mSwitchingDialog != null) {
1754 mSwitchingDialog.dismiss();
1755 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001756 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001757
The Android Open Source Project10592532009-03-18 17:39:46 -07001758 mDialogBuilder = null;
1759 mItems = null;
1760 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001761 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001762
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001763 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001764
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001765 public boolean setInputMethodEnabled(String id, boolean enabled) {
1766 synchronized (mMethodMap) {
1767 if (mContext.checkCallingOrSelfPermission(
1768 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1769 != PackageManager.PERMISSION_GRANTED) {
1770 throw new SecurityException(
1771 "Requires permission "
1772 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1773 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001774
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001775 long ident = Binder.clearCallingIdentity();
1776 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001777 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001778 } finally {
1779 Binder.restoreCallingIdentity(ident);
1780 }
1781 }
1782 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001783
1784 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
1785 // Make sure this is a valid input method.
1786 InputMethodInfo imm = mMethodMap.get(id);
1787 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09001788 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001789 }
1790
satokd87c2592010-09-29 11:52:06 +09001791 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
1792 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001793
satokd87c2592010-09-29 11:52:06 +09001794 if (enabled) {
1795 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
1796 if (pair.first.equals(id)) {
1797 // We are enabling this input method, but it is already enabled.
1798 // Nothing to do. The previous state was enabled.
1799 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001800 }
1801 }
satokd87c2592010-09-29 11:52:06 +09001802 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
1803 // Previous state was disabled.
1804 return false;
1805 } else {
1806 StringBuilder builder = new StringBuilder();
1807 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
1808 builder, enabledInputMethodsList, id)) {
1809 // Disabled input method is currently selected, switch to another one.
1810 String selId = Settings.Secure.getString(mContext.getContentResolver(),
1811 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09001812 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
1813 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
1814 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09001815 }
1816 // Previous state was enabled.
1817 return true;
1818 } else {
1819 // We are disabling the input method but it is already disabled.
1820 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001821 return false;
1822 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001823 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001824 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001825
satok723a27e2010-11-11 14:58:11 +09001826 private void saveCurrentInputMethodAndSubtypeToHistory() {
1827 String subtypeId = NOT_A_SUBTYPE_ID_STR;
1828 if (mCurrentSubtype != null) {
1829 subtypeId = String.valueOf(mCurrentSubtype.hashCode());
1830 }
1831 mSettings.addSubtypeToHistory(mCurMethodId, subtypeId);
satokab751aa2010-09-14 19:17:36 +09001832 }
1833
satok723a27e2010-11-11 14:58:11 +09001834 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
1835 boolean setSubtypeOnly) {
1836 // Update the history of InputMethod and Subtype
1837 saveCurrentInputMethodAndSubtypeToHistory();
1838
1839 // Set Subtype here
1840 if (imi == null || subtypeId < 0) {
1841 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08001842 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09001843 } else {
1844 final ArrayList<InputMethodSubtype> subtypes = imi.getSubtypes();
1845 if (subtypeId < subtypes.size()) {
1846 mSettings.putSelectedSubtype(subtypes.get(subtypeId).hashCode());
1847 mCurrentSubtype = subtypes.get(subtypeId);
1848 } else {
1849 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
1850 mCurrentSubtype = null;
1851 }
satokab751aa2010-09-14 19:17:36 +09001852 }
satok723a27e2010-11-11 14:58:11 +09001853
1854 if (!setSubtypeOnly) {
1855 // Set InputMethod here
1856 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
1857 }
1858 }
1859
1860 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
1861 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
1862 int lastSubtypeId = NOT_A_SUBTYPE_ID;
1863 // newDefaultIme is empty when there is no candidate for the selected IME.
1864 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
1865 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
1866 if (subtypeHashCode != null) {
1867 try {
1868 lastSubtypeId = getSubtypeIdFromHashCode(
1869 imi, Integer.valueOf(subtypeHashCode));
1870 } catch (NumberFormatException e) {
1871 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
1872 }
1873 }
1874 }
1875 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09001876 }
1877
1878 private int getSelectedInputMethodSubtypeId(String id) {
1879 InputMethodInfo imi = mMethodMap.get(id);
1880 if (imi == null) {
1881 return NOT_A_SUBTYPE_ID;
1882 }
satokab751aa2010-09-14 19:17:36 +09001883 int subtypeId;
1884 try {
1885 subtypeId = Settings.Secure.getInt(mContext.getContentResolver(),
1886 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE);
1887 } catch (SettingNotFoundException e) {
1888 return NOT_A_SUBTYPE_ID;
1889 }
satok723a27e2010-11-11 14:58:11 +09001890 return getSubtypeIdFromHashCode(imi, subtypeId);
1891 }
1892
1893 private int getSubtypeIdFromHashCode(InputMethodInfo imi, int subtypeHashCode) {
1894 ArrayList<InputMethodSubtype> subtypes = imi.getSubtypes();
satokab751aa2010-09-14 19:17:36 +09001895 for (int i = 0; i < subtypes.size(); ++i) {
1896 InputMethodSubtype ims = subtypes.get(i);
satok723a27e2010-11-11 14:58:11 +09001897 if (subtypeHashCode == ims.hashCode()) {
satokab751aa2010-09-14 19:17:36 +09001898 return i;
1899 }
1900 }
1901 return NOT_A_SUBTYPE_ID;
1902 }
1903
satok8fbb1e82010-11-02 23:15:58 +09001904 // If there are no selected subtypes, tries finding the most applicable one according to the
1905 // current system locale
1906 private int findApplicableSubtype(String id) {
1907 InputMethodInfo imi = mMethodMap.get(id);
1908 if (imi == null) {
1909 return NOT_A_SUBTYPE_ID;
1910 }
1911 ArrayList<InputMethodSubtype> subtypes = imi.getSubtypes();
1912 if (subtypes == null || subtypes.size() == 0) {
1913 return NOT_A_SUBTYPE_ID;
1914 }
1915 final String locale = mContext.getResources().getConfiguration().locale.toString();
1916 final String language = locale.substring(0, 2);
1917 boolean partialMatchFound = false;
1918 int applicableSubtypeId = DEFAULT_SUBTYPE_ID;
1919 for (int i = 0; i < subtypes.size(); ++i) {
1920 final String subtypeLocale = subtypes.get(i).getLocale();
satok9ef02832010-11-04 21:17:48 +09001921 // An applicable subtype should be a keyboard subtype
1922 if (subtypes.get(i).getMode().equalsIgnoreCase("keyboard")) {
1923 if (locale.equals(subtypeLocale)) {
1924 // Exact match (e.g. system locale is "en_US" and subtype locale is "en_US")
1925 applicableSubtypeId = i;
1926 break;
1927 } else if (!partialMatchFound && subtypeLocale.startsWith(language)) {
1928 // Partial match (e.g. system locale is "en_US" and subtype locale is "en")
1929 applicableSubtypeId = i;
1930 partialMatchFound = true;
1931 }
satok8fbb1e82010-11-02 23:15:58 +09001932 }
1933 }
1934
1935 // The first subtype applicable to the system locale will be defined as the most applicable
1936 // subtype.
1937 if (DEBUG) {
satok03eb319a2010-11-11 18:17:42 +09001938 Slog.d(TAG, "Applicable InputMethodSubtype was found: " + applicableSubtypeId + ","
satok8fbb1e82010-11-02 23:15:58 +09001939 + subtypes.get(applicableSubtypeId).getLocale());
1940 }
1941 return applicableSubtypeId;
1942 }
1943
satokab751aa2010-09-14 19:17:36 +09001944 /**
1945 * @return Return the current subtype of this input method.
1946 */
1947 public InputMethodSubtype getCurrentInputMethodSubtype() {
satok8fbb1e82010-11-02 23:15:58 +09001948 boolean subtypeIsSelected = false;
1949 try {
1950 subtypeIsSelected = Settings.Secure.getInt(mContext.getContentResolver(),
1951 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE) != NOT_A_SUBTYPE_ID;
1952 } catch (SettingNotFoundException e) {
1953 }
1954 if (!subtypeIsSelected || mCurrentSubtype == null) {
1955 String lastInputMethodId = Settings.Secure.getString(mContext
1956 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
1957 int subtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
1958 if (subtypeId == NOT_A_SUBTYPE_ID) {
1959 subtypeId = findApplicableSubtype(lastInputMethodId);
1960 }
1961 if (subtypeId != NOT_A_SUBTYPE_ID) {
1962 mCurrentSubtype = mMethodMap.get(lastInputMethodId).getSubtypes().get(subtypeId);
1963 }
1964 }
satokab751aa2010-09-14 19:17:36 +09001965 return mCurrentSubtype;
1966 }
1967
satokb66d2872010-11-10 01:04:04 +09001968 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
1969 synchronized (mMethodMap) {
1970 if (subtype != null && mCurMethodId != null) {
1971 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1972 int subtypeId = getSubtypeIdFromHashCode(imi, subtype.hashCode());
1973 if (subtypeId != NOT_A_SUBTYPE_ID) {
1974 setInputMethodLocked(mCurMethodId, subtypeId);
1975 return true;
1976 }
1977 }
1978 return false;
1979 }
1980 }
1981
satokd87c2592010-09-29 11:52:06 +09001982 /**
1983 * Utility class for putting and getting settings for InputMethod
1984 * TODO: Move all putters and getters of settings to this class.
1985 */
1986 private static class InputMethodSettings {
1987 // The string for enabled input method is saved as follows:
1988 // example: ("ime0;subtype0;subtype1;subtype2:ime1:ime2;subtype0")
1989 private static final char INPUT_METHOD_SEPARATER = ':';
1990 private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';';
satok723a27e2010-11-11 14:58:11 +09001991 private final TextUtils.SimpleStringSplitter mInputMethodSplitter =
satokd87c2592010-09-29 11:52:06 +09001992 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER);
1993
satok723a27e2010-11-11 14:58:11 +09001994 private final TextUtils.SimpleStringSplitter mSubtypeSplitter =
satokd87c2592010-09-29 11:52:06 +09001995 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATER);
1996
1997 private final ContentResolver mResolver;
1998 private final HashMap<String, InputMethodInfo> mMethodMap;
1999 private final ArrayList<InputMethodInfo> mMethodList;
2000
2001 private String mEnabledInputMethodsStrCache;
2002
2003 private static void buildEnabledInputMethodsSettingString(
2004 StringBuilder builder, Pair<String, ArrayList<String>> pair) {
2005 String id = pair.first;
2006 ArrayList<String> subtypes = pair.second;
2007 builder.append(id);
satok57c767c2010-11-01 22:34:08 +09002008 // Inputmethod and subtypes are saved in the settings as follows:
2009 // ime0;subtype0;subtype1:ime1;subtype0:ime2:ime3;subtype0;subtype1
2010 for (String subtypeId: subtypes) {
2011 builder.append(INPUT_METHOD_SUBTYPE_SEPARATER).append(subtypeId);
satokd87c2592010-09-29 11:52:06 +09002012 }
2013 }
2014
2015 public InputMethodSettings(
2016 ContentResolver resolver, HashMap<String, InputMethodInfo> methodMap,
2017 ArrayList<InputMethodInfo> methodList) {
2018 mResolver = resolver;
2019 mMethodMap = methodMap;
2020 mMethodList = methodList;
2021 }
2022
2023 public List<InputMethodInfo> getEnabledInputMethodListLocked() {
2024 return createEnabledInputMethodListLocked(
2025 getEnabledInputMethodsAndSubtypeListLocked());
2026 }
2027
satok7f35c8c2010-10-07 21:13:11 +09002028 public List<Pair<InputMethodInfo, ArrayList<String>>>
2029 getEnabledInputMethodAndSubtypeListLocked() {
2030 return createEnabledInputMethodAndSubtypeListLocked(
2031 getEnabledInputMethodsAndSubtypeListLocked());
2032 }
2033
satokd87c2592010-09-29 11:52:06 +09002034 // At the initial boot, the settings for input methods are not set,
2035 // so we need to enable IME in that case.
2036 public void enableAllIMEsIfThereIsNoEnabledIME() {
2037 if (TextUtils.isEmpty(getEnabledInputMethodsStr())) {
2038 StringBuilder sb = new StringBuilder();
2039 final int N = mMethodList.size();
2040 for (int i = 0; i < N; i++) {
2041 InputMethodInfo imi = mMethodList.get(i);
2042 Slog.i(TAG, "Adding: " + imi.getId());
2043 if (i > 0) sb.append(':');
2044 sb.append(imi.getId());
2045 }
2046 putEnabledInputMethodsStr(sb.toString());
2047 }
2048 }
2049
2050 public List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() {
2051 ArrayList<Pair<String, ArrayList<String>>> imsList
2052 = new ArrayList<Pair<String, ArrayList<String>>>();
2053 final String enabledInputMethodsStr = getEnabledInputMethodsStr();
2054 if (TextUtils.isEmpty(enabledInputMethodsStr)) {
2055 return imsList;
2056 }
satok723a27e2010-11-11 14:58:11 +09002057 mInputMethodSplitter.setString(enabledInputMethodsStr);
2058 while (mInputMethodSplitter.hasNext()) {
2059 String nextImsStr = mInputMethodSplitter.next();
2060 mSubtypeSplitter.setString(nextImsStr);
2061 if (mSubtypeSplitter.hasNext()) {
satokd87c2592010-09-29 11:52:06 +09002062 ArrayList<String> subtypeHashes = new ArrayList<String>();
2063 // The first element is ime id.
satok723a27e2010-11-11 14:58:11 +09002064 String imeId = mSubtypeSplitter.next();
2065 while (mSubtypeSplitter.hasNext()) {
2066 subtypeHashes.add(mSubtypeSplitter.next());
satokd87c2592010-09-29 11:52:06 +09002067 }
2068 imsList.add(new Pair<String, ArrayList<String>>(imeId, subtypeHashes));
2069 }
2070 }
2071 return imsList;
2072 }
2073
2074 public void appendAndPutEnabledInputMethodLocked(String id, boolean reloadInputMethodStr) {
2075 if (reloadInputMethodStr) {
2076 getEnabledInputMethodsStr();
2077 }
2078 if (TextUtils.isEmpty(mEnabledInputMethodsStrCache)) {
2079 // Add in the newly enabled input method.
2080 putEnabledInputMethodsStr(id);
2081 } else {
2082 putEnabledInputMethodsStr(
2083 mEnabledInputMethodsStrCache + INPUT_METHOD_SEPARATER + id);
2084 }
2085 }
2086
2087 /**
2088 * Build and put a string of EnabledInputMethods with removing specified Id.
2089 * @return the specified id was removed or not.
2090 */
2091 public boolean buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2092 StringBuilder builder, List<Pair<String, ArrayList<String>>> imsList, String id) {
2093 boolean isRemoved = false;
2094 boolean needsAppendSeparator = false;
2095 for (Pair<String, ArrayList<String>> ims: imsList) {
2096 String curId = ims.first;
2097 if (curId.equals(id)) {
2098 // We are disabling this input method, and it is
2099 // currently enabled. Skip it to remove from the
2100 // new list.
2101 isRemoved = true;
2102 } else {
2103 if (needsAppendSeparator) {
2104 builder.append(INPUT_METHOD_SEPARATER);
2105 } else {
2106 needsAppendSeparator = true;
2107 }
2108 buildEnabledInputMethodsSettingString(builder, ims);
2109 }
2110 }
2111 if (isRemoved) {
2112 // Update the setting with the new list of input methods.
2113 putEnabledInputMethodsStr(builder.toString());
2114 }
2115 return isRemoved;
2116 }
2117
2118 private List<InputMethodInfo> createEnabledInputMethodListLocked(
2119 List<Pair<String, ArrayList<String>>> imsList) {
2120 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
2121 for (Pair<String, ArrayList<String>> ims: imsList) {
2122 InputMethodInfo info = mMethodMap.get(ims.first);
2123 if (info != null) {
2124 res.add(info);
2125 }
2126 }
2127 return res;
2128 }
2129
satok7f35c8c2010-10-07 21:13:11 +09002130 private List<Pair<InputMethodInfo, ArrayList<String>>>
2131 createEnabledInputMethodAndSubtypeListLocked(
2132 List<Pair<String, ArrayList<String>>> imsList) {
2133 final ArrayList<Pair<InputMethodInfo, ArrayList<String>>> res
2134 = new ArrayList<Pair<InputMethodInfo, ArrayList<String>>>();
2135 for (Pair<String, ArrayList<String>> ims : imsList) {
2136 InputMethodInfo info = mMethodMap.get(ims.first);
2137 if (info != null) {
2138 res.add(new Pair<InputMethodInfo, ArrayList<String>>(info, ims.second));
2139 }
2140 }
2141 return res;
2142 }
2143
satokd87c2592010-09-29 11:52:06 +09002144 private void putEnabledInputMethodsStr(String str) {
2145 Settings.Secure.putString(mResolver, Settings.Secure.ENABLED_INPUT_METHODS, str);
2146 mEnabledInputMethodsStrCache = str;
2147 }
2148
2149 private String getEnabledInputMethodsStr() {
2150 mEnabledInputMethodsStrCache = Settings.Secure.getString(
2151 mResolver, Settings.Secure.ENABLED_INPUT_METHODS);
satok723a27e2010-11-11 14:58:11 +09002152 if (DEBUG) {
2153 Slog.d(TAG, "getEnabledInputMethodsStr: " + mEnabledInputMethodsStrCache);
2154 }
satokd87c2592010-09-29 11:52:06 +09002155 return mEnabledInputMethodsStrCache;
2156 }
satok723a27e2010-11-11 14:58:11 +09002157
2158 private void saveSubtypeHistory(
2159 List<Pair<String, String>> savedImes, String newImeId, String newSubtypeId) {
2160 StringBuilder builder = new StringBuilder();
2161 boolean isImeAdded = false;
2162 if (!TextUtils.isEmpty(newImeId) && !TextUtils.isEmpty(newSubtypeId)) {
2163 builder.append(newImeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2164 newSubtypeId);
2165 isImeAdded = true;
2166 }
2167 for (Pair<String, String> ime: savedImes) {
2168 String imeId = ime.first;
2169 String subtypeId = ime.second;
2170 if (TextUtils.isEmpty(subtypeId)) {
2171 subtypeId = NOT_A_SUBTYPE_ID_STR;
2172 }
2173 if (isImeAdded) {
2174 builder.append(INPUT_METHOD_SEPARATER);
2175 } else {
2176 isImeAdded = true;
2177 }
2178 builder.append(imeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2179 subtypeId);
2180 }
2181 // Remove the last INPUT_METHOD_SEPARATER
2182 putSubtypeHistoryStr(builder.toString());
2183 }
2184
2185 public void addSubtypeToHistory(String imeId, String subtypeId) {
2186 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
2187 for (Pair<String, String> ime: subtypeHistory) {
2188 if (ime.first.equals(imeId)) {
2189 if (DEBUG) {
2190 Slog.v(TAG, "Subtype found in the history: " + imeId
2191 + ime.second);
2192 }
2193 // We should break here
2194 subtypeHistory.remove(ime);
2195 break;
2196 }
2197 }
2198 saveSubtypeHistory(subtypeHistory, imeId, subtypeId);
2199 }
2200
2201 private void putSubtypeHistoryStr(String str) {
2202 if (DEBUG) {
2203 Slog.d(TAG, "putSubtypeHistoryStr: " + str);
2204 }
2205 Settings.Secure.putString(
2206 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, str);
2207 }
2208
2209 public Pair<String, String> getLastInputMethodAndSubtypeLocked() {
2210 // Gets the first one from the history
2211 return getLastSubtypeForInputMethodLockedInternal(null);
2212 }
2213
2214 public String getLastSubtypeForInputMethodLocked(String imeId) {
2215 Pair<String, String> ime = getLastSubtypeForInputMethodLockedInternal(imeId);
2216 if (ime != null) {
2217 return ime.second;
2218 } else {
2219 return null;
2220 }
2221 }
2222
2223 private Pair<String, String> getLastSubtypeForInputMethodLockedInternal(String imeId) {
2224 List<Pair<String, ArrayList<String>>> enabledImes =
2225 getEnabledInputMethodsAndSubtypeListLocked();
2226 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
2227 for (Pair<String, String> imeAndSubtype: subtypeHistory) {
2228 final String imeInTheHistory = imeAndSubtype.first;
2229 // If imeId is empty, returns the first IME and subtype in the history
2230 if (TextUtils.isEmpty(imeId) || imeInTheHistory.equals(imeId)) {
2231 final String subtypeInTheHistory = imeAndSubtype.second;
2232 final String subtypeHashCode = getEnabledSubtypeForInputMethodAndSubtypeLocked(
2233 enabledImes, imeInTheHistory, subtypeInTheHistory);
2234 if (!TextUtils.isEmpty(subtypeHashCode)) {
2235 if (DEBUG) {
2236 Slog.d(TAG, "Enabled subtype found in the history:" + subtypeHashCode);
2237 }
2238 return new Pair<String, String>(imeInTheHistory, subtypeHashCode);
2239 }
2240 }
2241 }
2242 if (DEBUG) {
2243 Slog.d(TAG, "No enabled IME found in the history");
2244 }
2245 return null;
2246 }
2247
2248 private String getEnabledSubtypeForInputMethodAndSubtypeLocked(List<Pair<String,
2249 ArrayList<String>>> enabledImes, String imeId, String subtypeHashCode) {
2250 for (Pair<String, ArrayList<String>> enabledIme: enabledImes) {
2251 if (enabledIme.first.equals(imeId)) {
2252 for (String s: enabledIme.second) {
2253 if (s.equals(subtypeHashCode)) {
2254 // If both imeId and subtypeId are enabled, return subtypeId.
2255 return s;
2256 }
2257 }
2258 // If imeId was enabled but subtypeId was disabled.
2259 return NOT_A_SUBTYPE_ID_STR;
2260 }
2261 }
2262 // If both imeId and subtypeId are disabled, return null
2263 return null;
2264 }
2265
2266 private List<Pair<String, String>> loadInputMethodAndSubtypeHistoryLocked() {
2267 ArrayList<Pair<String, String>> imsList = new ArrayList<Pair<String, String>>();
2268 final String subtypeHistoryStr = getSubtypeHistoryStr();
2269 if (TextUtils.isEmpty(subtypeHistoryStr)) {
2270 return imsList;
2271 }
2272 mInputMethodSplitter.setString(subtypeHistoryStr);
2273 while (mInputMethodSplitter.hasNext()) {
2274 String nextImsStr = mInputMethodSplitter.next();
2275 mSubtypeSplitter.setString(nextImsStr);
2276 if (mSubtypeSplitter.hasNext()) {
2277 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2278 // The first element is ime id.
2279 String imeId = mSubtypeSplitter.next();
2280 while (mSubtypeSplitter.hasNext()) {
2281 subtypeId = mSubtypeSplitter.next();
2282 break;
2283 }
2284 imsList.add(new Pair<String, String>(imeId, subtypeId));
2285 }
2286 }
2287 return imsList;
2288 }
2289
2290 private String getSubtypeHistoryStr() {
2291 if (DEBUG) {
2292 Slog.d(TAG, "getSubtypeHistoryStr: " + Settings.Secure.getString(
2293 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY));
2294 }
2295 return Settings.Secure.getString(
2296 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY);
2297 }
2298
2299 public void putSelectedInputMethod(String imeId) {
2300 Settings.Secure.putString(mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, imeId);
2301 }
2302
2303 public void putSelectedSubtype(int subtypeId) {
2304 Settings.Secure.putInt(
2305 mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, subtypeId);
2306 }
satokd87c2592010-09-29 11:52:06 +09002307 }
2308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002309 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002311 @Override
2312 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2313 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
2314 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002315
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002316 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
2317 + Binder.getCallingPid()
2318 + ", uid=" + Binder.getCallingUid());
2319 return;
2320 }
2321
2322 IInputMethod method;
2323 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002324
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002325 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002327 synchronized (mMethodMap) {
2328 p.println("Current Input Method Manager state:");
2329 int N = mMethodList.size();
2330 p.println(" Input Methods:");
2331 for (int i=0; i<N; i++) {
2332 InputMethodInfo info = mMethodList.get(i);
2333 p.println(" InputMethod #" + i + ":");
2334 info.dump(p, " ");
2335 }
2336 p.println(" Clients:");
2337 for (ClientState ci : mClients.values()) {
2338 p.println(" Client " + ci + ":");
2339 p.println(" client=" + ci.client);
2340 p.println(" inputContext=" + ci.inputContext);
2341 p.println(" sessionRequested=" + ci.sessionRequested);
2342 p.println(" curSession=" + ci.curSession);
2343 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002344 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002345 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002346 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
2347 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002348 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
2349 + " mBoundToMethod=" + mBoundToMethod);
2350 p.println(" mCurToken=" + mCurToken);
2351 p.println(" mCurIntent=" + mCurIntent);
2352 method = mCurMethod;
2353 p.println(" mCurMethod=" + mCurMethod);
2354 p.println(" mEnabledSession=" + mEnabledSession);
2355 p.println(" mShowRequested=" + mShowRequested
2356 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
2357 + " mShowForced=" + mShowForced
2358 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07002359 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002360 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002361
Jeff Brownb88102f2010-09-08 11:49:43 -07002362 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002363 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002364 pw.flush();
2365 try {
2366 client.client.asBinder().dump(fd, args);
2367 } catch (RemoteException e) {
2368 p.println("Input method client dead: " + e);
2369 }
Jeff Brownb88102f2010-09-08 11:49:43 -07002370 } else {
2371 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002372 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002373
Jeff Brownb88102f2010-09-08 11:49:43 -07002374 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002375 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002376 pw.flush();
2377 try {
2378 method.asBinder().dump(fd, args);
2379 } catch (RemoteException e) {
2380 p.println("Input method service dead: " + e);
2381 }
Jeff Brownb88102f2010-09-08 11:49:43 -07002382 } else {
2383 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002384 }
2385 }
2386}