blob: e69c9a464760b94c8288ad17a26e4d28a9b81e94 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 * use this file except in compliance with the License. You may obtain a copy of
5 * the License at
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007 * http://www.apache.org/licenses/LICENSE-2.0
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 * License for the specific language governing permissions and limitations under
13 * the License.
14 */
15
16package com.android.server;
17
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080018import com.android.internal.content.PackageMonitor;
Satoshi Kataokad7443c82013-10-15 17:45:43 +090019import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController;
Satoshi Kataokad787f692013-10-26 04:44:21 +090020import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController.ImeSubtypeListItem;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +090021import com.android.internal.inputmethod.InputMethodUtils;
22import com.android.internal.inputmethod.InputMethodUtils.InputMethodSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import com.android.internal.os.HandlerCaller;
Svetoslav Ganov758143e2012-08-06 16:40:27 -070024import com.android.internal.os.SomeArgs;
satoke7c6998e2011-06-03 17:57:59 +090025import com.android.internal.util.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import com.android.internal.view.IInputContext;
27import com.android.internal.view.IInputMethod;
Michael Wright52a53522013-03-14 10:59:38 -070028import com.android.internal.view.IInputSessionCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import com.android.internal.view.IInputMethodClient;
30import com.android.internal.view.IInputMethodManager;
31import com.android.internal.view.IInputMethodSession;
32import com.android.internal.view.InputBindResult;
Adam Lesinskief2ea1f2013-12-05 16:48:06 -080033import com.android.server.statusbar.StatusBarManagerService;
satok01038492012-04-09 21:08:27 +090034import com.android.server.wm.WindowManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035
satoke7c6998e2011-06-03 17:57:59 +090036import org.xmlpull.v1.XmlPullParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import org.xmlpull.v1.XmlPullParserException;
satoke7c6998e2011-06-03 17:57:59 +090038import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039
40import android.app.ActivityManagerNative;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090041import android.app.AppGlobals;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.app.AlertDialog;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090043import android.app.IUserSwitchObserver;
satokf90a33e2011-07-19 11:55:52 +090044import android.app.KeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +090045import android.app.Notification;
46import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070047import android.app.PendingIntent;
satok5b927c432012-05-01 20:09:34 +090048import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.content.ComponentName;
50import android.content.ContentResolver;
51import android.content.Context;
52import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.content.DialogInterface.OnCancelListener;
54import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090055import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070057import android.content.pm.ApplicationInfo;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090058import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.content.pm.PackageManager;
60import android.content.pm.ResolveInfo;
61import android.content.pm.ServiceInfo;
Amith Yamasani734983f2014-03-04 16:48:05 -080062import android.content.pm.UserInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070063import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.content.res.Resources;
65import android.content.res.TypedArray;
66import android.database.ContentObserver;
Joe Onorato857fd9b2011-01-27 15:08:35 -080067import android.inputmethodservice.InputMethodService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.os.Binder;
satoke7c6998e2011-06-03 17:57:59 +090069import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.os.Handler;
71import android.os.IBinder;
72import android.os.IInterface;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090073import android.os.IRemoteCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074import android.os.Message;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090075import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076import android.os.Parcel;
77import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080078import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079import android.os.ServiceManager;
80import android.os.SystemClock;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090081import android.os.UserHandle;
Amith Yamasani734983f2014-03-04 16:48:05 -080082import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083import android.provider.Settings;
84import android.text.TextUtils;
satokf9f01002011-05-19 21:31:50 +090085import android.text.style.SuggestionSpan;
Dianne Hackborn39606a02012-07-31 17:54:35 -070086import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +090088import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +090089import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090import android.util.PrintWriterPrinter;
91import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +090092import android.util.Slog;
93import android.util.Xml;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094import android.view.IWindowManager;
Jeff Brownc28867a2013-03-26 15:42:39 -070095import android.view.InputChannel;
Ken Wakasa05dbb652011-08-22 15:22:43 +090096import android.view.LayoutInflater;
97import android.view.View;
98import android.view.ViewGroup;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099import android.view.WindowManager;
satokab751aa2010-09-14 19:17:36 +0900100import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101import android.view.inputmethod.InputBinding;
102import android.view.inputmethod.InputMethod;
103import android.view.inputmethod.InputMethodInfo;
104import android.view.inputmethod.InputMethodManager;
satokab751aa2010-09-14 19:17:36 +0900105import android.view.inputmethod.InputMethodSubtype;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900106import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +0900107import android.widget.CompoundButton;
108import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900109import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +0900110import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900111import android.widget.TextView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112
satoke7c6998e2011-06-03 17:57:59 +0900113import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900115import java.io.FileInputStream;
116import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117import java.io.IOException;
118import java.io.PrintWriter;
119import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900120import java.util.Collections;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121import java.util.HashMap;
122import java.util.List;
satok5b927c432012-05-01 20:09:34 +0900123import java.util.Locale;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124
125/**
126 * This class provides a system service that manages input methods.
127 */
128public class InputMethodManagerService extends IInputMethodManager.Stub
129 implements ServiceConnection, Handler.Callback {
130 static final boolean DEBUG = false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700131 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132
133 static final int MSG_SHOW_IM_PICKER = 1;
satokab751aa2010-09-14 19:17:36 +0900134 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 2;
satok47a44912010-10-06 16:03:58 +0900135 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 3;
satok217f5482010-12-15 05:19:19 +0900136 static final int MSG_SHOW_IM_CONFIG = 4;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 static final int MSG_UNBIND_INPUT = 1000;
139 static final int MSG_BIND_INPUT = 1010;
140 static final int MSG_SHOW_SOFT_INPUT = 1020;
141 static final int MSG_HIDE_SOFT_INPUT = 1030;
142 static final int MSG_ATTACH_TOKEN = 1040;
143 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 static final int MSG_START_INPUT = 2000;
146 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 static final int MSG_UNBIND_METHOD = 3000;
149 static final int MSG_BIND_METHOD = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700150 static final int MSG_SET_ACTIVE = 3020;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800151
satok01038492012-04-09 21:08:27 +0900152 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
153
Satoshi Kataokabcacc322013-10-21 17:57:27 -0700154 static final long TIME_TO_RECONNECT = 3 * 1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800155
satokf9f01002011-05-19 21:31:50 +0900156 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
157
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900158 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
satokb6359412011-06-28 17:47:41 +0900159 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900160
satok4e4569d2010-11-19 18:45:53 +0900161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800163 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900165 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 final IWindowManager mIWindowManager;
168 final HandlerCaller mCaller;
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700169 final boolean mHasFeature;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900170 private InputMethodFileManager mFileManager;
satok01038492012-04-09 21:08:27 +0900171 private final HardKeyboardListener mHardKeyboardListener;
172 private final WindowManagerService mWindowManagerService;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800173
Jeff Brownc28867a2013-03-26 15:42:39 -0700174 final InputBindResult mNoBinding = new InputBindResult(null, null, null, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 // All known input methods. mMethodMap also serves as the global
177 // lock for this class.
satokd87c2592010-09-29 11:52:06 +0900178 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<InputMethodInfo>();
179 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<String, InputMethodInfo>();
satokf9f01002011-05-19 21:31:50 +0900180 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
181 new LruCache<SuggestionSpan, InputMethodInfo>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Satoshi Kataokad787f692013-10-26 04:44:21 +0900182 private final InputMethodSubtypeSwitchingController mSwitchingController;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800183
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700184 // Used to bring IME service up to visible adjustment while it is being shown.
185 final ServiceConnection mVisibleConnection = new ServiceConnection() {
186 @Override public void onServiceConnected(ComponentName name, IBinder service) {
187 }
188
189 @Override public void onServiceDisconnected(ComponentName name) {
190 }
191 };
192 boolean mVisibleBound = false;
193
satok7cfc0ed2011-06-20 21:29:36 +0900194 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700195 private NotificationManager mNotificationManager;
196 private KeyguardManager mKeyguardManager;
197 private StatusBarManagerService mStatusBar;
198 private Notification mImeSwitcherNotification;
199 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900200 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900201 private boolean mNotificationShown;
satok0a1bcf42012-05-16 19:26:31 +0900202 private final boolean mImeSelectedOnBoot;
satok7cfc0ed2011-06-20 21:29:36 +0900203
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 class SessionState {
205 final ClientState client;
206 final IInputMethod method;
Jeff Brownc28867a2013-03-26 15:42:39 -0700207
208 IInputMethodSession session;
209 InputChannel channel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 @Override
212 public String toString() {
213 return "SessionState{uid " + client.uid + " pid " + client.pid
214 + " method " + Integer.toHexString(
215 System.identityHashCode(method))
216 + " session " + Integer.toHexString(
217 System.identityHashCode(session))
Jeff Brownc28867a2013-03-26 15:42:39 -0700218 + " channel " + channel
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 + "}";
220 }
221
222 SessionState(ClientState _client, IInputMethod _method,
Jeff Brownc28867a2013-03-26 15:42:39 -0700223 IInputMethodSession _session, InputChannel _channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 client = _client;
225 method = _method;
226 session = _session;
Jeff Brownc28867a2013-03-26 15:42:39 -0700227 channel = _channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 }
229 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800230
Jeff Brownc28867a2013-03-26 15:42:39 -0700231 static final class ClientState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 final IInputMethodClient client;
233 final IInputContext inputContext;
234 final int uid;
235 final int pid;
236 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800237
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 boolean sessionRequested;
239 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 @Override
242 public String toString() {
243 return "ClientState{" + Integer.toHexString(
244 System.identityHashCode(this)) + " uid " + uid
245 + " pid " + pid + "}";
246 }
247
248 ClientState(IInputMethodClient _client, IInputContext _inputContext,
249 int _uid, int _pid) {
250 client = _client;
251 inputContext = _inputContext;
252 uid = _uid;
253 pid = _pid;
254 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
255 }
256 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 final HashMap<IBinder, ClientState> mClients
259 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700262 * Set once the system is ready to run third party code.
263 */
264 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800265
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700266 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 * Id of the currently selected input method.
268 */
269 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 /**
272 * The current binding sequence number, incremented every time there is
273 * a new bind performed.
274 */
275 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 /**
278 * The client that is currently bound to an input method.
279 */
280 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700283 * The last window token that gained focus.
284 */
285 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800286
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700287 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 * The input context last provided by the current client.
289 */
290 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 /**
293 * The attributes last provided by the current client.
294 */
295 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297 /**
298 * The input method ID of the input method service that we are currently
299 * connected to or in the process of connecting to.
300 */
301 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 /**
satokab751aa2010-09-14 19:17:36 +0900304 * The current subtype of the current input method.
305 */
306 private InputMethodSubtype mCurrentSubtype;
307
satok4e4569d2010-11-19 18:45:53 +0900308 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900309 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
310 mShortcutInputMethodsAndSubtypes =
311 new HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>();
satokab751aa2010-09-14 19:17:36 +0900312
John Spurlocke0980502013-10-25 11:59:29 -0400313 // Was the keyguard locked when this client became current?
314 private boolean mCurClientInKeyguard;
315
satokab751aa2010-09-14 19:17:36 +0900316 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 * Set to true if our ServiceConnection is currently actively bound to
318 * a service (whether or not we have gotten its IBinder back yet).
319 */
320 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322 /**
323 * Set if the client has asked for the input method to be shown.
324 */
325 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 /**
328 * Set if we were explicitly told to show the input method.
329 */
330 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332 /**
333 * Set if we were forced to be shown.
334 */
335 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 /**
338 * Set if we last told the input method to show itself.
339 */
340 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800341
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 /**
343 * The Intent used to connect to the current input method.
344 */
345 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 /**
348 * The token we have made for the currently active input method, to
349 * identify it in the future.
350 */
351 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 /**
354 * If non-null, this is the input method service we are currently connected
355 * to.
356 */
357 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 /**
360 * Time that we last initiated a bind to the input method, to determine
361 * if we should try to disconnect and reconnect to it.
362 */
363 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800364
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365 /**
366 * Have we called mCurMethod.bindInput()?
367 */
368 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 /**
371 * Currently enabled session. Only touched by service thread, not
372 * protected by a lock.
373 */
374 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 /**
377 * True if the screen is on. The value is true initially.
378 */
379 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800380
Joe Onorato857fd9b2011-01-27 15:08:35 -0800381 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
382 int mImeWindowVis;
383
Ken Wakasa05dbb652011-08-22 15:22:43 +0900384 private AlertDialog.Builder mDialogBuilder;
385 private AlertDialog mSwitchingDialog;
satok01038492012-04-09 21:08:27 +0900386 private View mSwitchingDialogTitleView;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900387 private InputMethodInfo[] mIms;
388 private int[] mSubtypeIds;
satok5b927c432012-05-01 20:09:34 +0900389 private Locale mLastSystemLocale;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900390 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
391 private final IPackageManager mIPackageManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800392
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 class SettingsObserver extends ContentObserver {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800394 String mLastEnabled = "";
395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 SettingsObserver(Handler handler) {
397 super(handler);
398 ContentResolver resolver = mContext.getContentResolver();
399 resolver.registerContentObserver(Settings.Secure.getUriFor(
400 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
satokab751aa2010-09-14 19:17:36 +0900401 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokb6109bb2011-02-03 22:24:54 +0900402 Settings.Secure.ENABLED_INPUT_METHODS), false, this);
403 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokab751aa2010-09-14 19:17:36 +0900404 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407 @Override public void onChange(boolean selfChange) {
408 synchronized (mMethodMap) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800409 boolean enabledChanged = false;
410 String newEnabled = mSettings.getEnabledInputMethodsStr();
411 if (!mLastEnabled.equals(newEnabled)) {
412 mLastEnabled = newEnabled;
413 enabledChanged = true;
414 }
415 updateFromSettingsLocked(enabledChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 }
417 }
418 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800419
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900420 class ImmsBroadcastReceiver extends android.content.BroadcastReceiver {
421 private void updateActive() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 // Inform the current client of the change in active status
Dianne Hackborna6e41342012-05-22 16:30:34 -0700423 if (mCurClient != null && mCurClient.client != null) {
424 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
425 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426 }
427 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900428
429 @Override
430 public void onReceive(Context context, Intent intent) {
431 final String action = intent.getAction();
432 if (Intent.ACTION_SCREEN_ON.equals(action)) {
433 mScreenOn = true;
434 refreshImeWindowVisibilityLocked();
435 updateActive();
436 return;
437 } else if (Intent.ACTION_SCREEN_OFF.equals(action)) {
438 mScreenOn = false;
439 setImeWindowVisibilityStatusHiddenLocked();
440 updateActive();
441 return;
442 } else if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
443 hideInputMethodMenu();
444 // No need to updateActive
445 return;
Amith Yamasani734983f2014-03-04 16:48:05 -0800446 } else if (Intent.ACTION_USER_ADDED.equals(action)
447 || Intent.ACTION_USER_REMOVED.equals(action)) {
Kenny Guy2a764942014-04-02 13:29:20 +0100448 updateCurrentProfileIds();
Amith Yamasani734983f2014-03-04 16:48:05 -0800449 return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900450 } else {
451 Slog.w(TAG, "Unexpected intent " + intent);
452 }
453 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800455
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800456 class MyPackageMonitor extends PackageMonitor {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900457 private boolean isChangingPackagesOfCurrentUser() {
458 final int userId = getChangingUserId();
459 final boolean retval = userId == mSettings.getCurrentUserId();
460 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900461 if (!retval) {
462 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
463 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900464 }
465 return retval;
466 }
467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800469 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900470 if (!isChangingPackagesOfCurrentUser()) {
471 return false;
472 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900474 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475 final int N = mMethodList.size();
476 if (curInputMethodId != null) {
477 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800478 InputMethodInfo imi = mMethodList.get(i);
479 if (imi.getId().equals(curInputMethodId)) {
480 for (String pkg : packages) {
481 if (imi.getPackageName().equals(pkg)) {
482 if (!doit) {
483 return true;
484 }
satok723a27e2010-11-11 14:58:11 +0900485 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800486 chooseNewDefaultIMELocked();
487 return true;
488 }
489 }
490 }
491 }
492 }
493 }
494 return false;
495 }
496
497 @Override
498 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900499 if (!isChangingPackagesOfCurrentUser()) {
500 return;
501 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800502 synchronized (mMethodMap) {
503 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900504 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800505 final int N = mMethodList.size();
506 if (curInputMethodId != null) {
507 for (int i=0; i<N; i++) {
508 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900509 final String imiId = imi.getId();
510 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800511 curIm = imi;
512 }
satoke7c6998e2011-06-03 17:57:59 +0900513
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800514 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900515 if (isPackageModified(imi.getPackageName())) {
516 mFileManager.deleteAllInputMethodSubtypes(imiId);
517 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800518 if (change == PACKAGE_TEMPORARY_CHANGE
519 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800520 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800521 + imi.getComponent());
522 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523 }
524 }
525 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800526
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900527 buildInputMethodListLocked(
528 mMethodList, mMethodMap, false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800529
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800531
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800532 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800533 int change = isPackageDisappearing(curIm.getPackageName());
534 if (change == PACKAGE_TEMPORARY_CHANGE
535 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800536 ServiceInfo si = null;
537 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900538 si = mIPackageManager.getServiceInfo(
539 curIm.getComponent(), 0, mSettings.getCurrentUserId());
540 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800541 }
542 if (si == null) {
543 // Uh oh, current input method is no longer around!
544 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800545 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
satok15452a42011-10-28 17:58:28 +0900546 setImeWindowVisibilityStatusHiddenLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800547 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800548 changed = true;
549 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800550 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900551 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800552 }
553 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800554 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800555 }
satokab751aa2010-09-14 19:17:36 +0900556
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800557 if (curIm == null) {
558 // We currently don't have a default input method... is
559 // one now available?
560 changed = chooseNewDefaultIMELocked();
561 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800562
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800563 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800564 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 }
566 }
567 }
568 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800569
Jeff Brownc28867a2013-03-26 15:42:39 -0700570 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900571 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -0700572 private final IInputMethod mMethod;
573 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800574
Jeff Brownc28867a2013-03-26 15:42:39 -0700575 MethodCallback(InputMethodManagerService imms, IInputMethod method,
576 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900577 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -0700578 mMethod = method;
579 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800580 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800581
satoke7c6998e2011-06-03 17:57:59 +0900582 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -0700583 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -0700584 long ident = Binder.clearCallingIdentity();
585 try {
586 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
587 } finally {
588 Binder.restoreCallingIdentity(ident);
589 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800590 }
591 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800592
satok01038492012-04-09 21:08:27 +0900593 private class HardKeyboardListener
594 implements WindowManagerService.OnHardKeyboardStatusChangeListener {
595 @Override
596 public void onHardKeyboardStatusChange(boolean available, boolean enabled) {
597 mHandler.sendMessage(mHandler.obtainMessage(
598 MSG_HARD_KEYBOARD_SWITCH_CHANGED, available ? 1 : 0, enabled ? 1 : 0));
599 }
600
601 public void handleHardKeyboardStatusChange(boolean available, boolean enabled) {
602 if (DEBUG) {
603 Slog.w(TAG, "HardKeyboardStatusChanged: available = " + available + ", enabled = "
604 + enabled);
605 }
606 synchronized(mMethodMap) {
607 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
608 && mSwitchingDialog.isShowing()) {
609 mSwitchingDialogTitleView.findViewById(
610 com.android.internal.R.id.hard_keyboard_section).setVisibility(
611 available ? View.VISIBLE : View.GONE);
612 }
613 }
614 }
615 }
616
617 public InputMethodManagerService(Context context, WindowManagerService windowManager) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900618 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800620 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 mHandler = new Handler(this);
622 mIWindowManager = IWindowManager.Stub.asInterface(
623 ServiceManager.getService(Context.WINDOW_SERVICE));
Mita Yuned218c72012-12-06 17:18:25 -0800624 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900625 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626 public void executeMessage(Message msg) {
627 handleMessage(msg);
628 }
Mita Yuned218c72012-12-06 17:18:25 -0800629 }, true /*asyncHandler*/);
satok01038492012-04-09 21:08:27 +0900630 mWindowManagerService = windowManager;
631 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700632 mHasFeature = context.getPackageManager().hasSystemFeature(
633 PackageManager.FEATURE_INPUT_METHODS);
satok7cfc0ed2011-06-20 21:29:36 +0900634
satok7cfc0ed2011-06-20 21:29:36 +0900635 mImeSwitcherNotification = new Notification();
636 mImeSwitcherNotification.icon = com.android.internal.R.drawable.ic_notification_ime_default;
637 mImeSwitcherNotification.when = 0;
638 mImeSwitcherNotification.flags = Notification.FLAG_ONGOING_EVENT;
639 mImeSwitcherNotification.tickerText = null;
640 mImeSwitcherNotification.defaults = 0; // please be quiet
641 mImeSwitcherNotification.sound = null;
642 mImeSwitcherNotification.vibrate = null;
Daniel Sandler590d5152012-06-14 16:10:13 -0400643
644 // Tag this notification specially so SystemUI knows it's important
John Spurlockfd7f1e02014-03-18 16:41:57 -0400645 mImeSwitcherNotification.extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
646 mImeSwitcherNotification.category = Notification.CATEGORY_SYSTEM;
Daniel Sandler590d5152012-06-14 16:10:13 -0400647
satok7cfc0ed2011-06-20 21:29:36 +0900648 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900649 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900650
651 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900652
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900653 final IntentFilter broadcastFilter = new IntentFilter();
654 broadcastFilter.addAction(Intent.ACTION_SCREEN_ON);
655 broadcastFilter.addAction(Intent.ACTION_SCREEN_OFF);
656 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Amith Yamasani734983f2014-03-04 16:48:05 -0800657 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
658 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900659 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800660
satok7cfc0ed2011-06-20 21:29:36 +0900661 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900662 int userId = 0;
663 try {
664 ActivityManagerNative.getDefault().registerUserSwitchObserver(
665 new IUserSwitchObserver.Stub() {
666 @Override
667 public void onUserSwitching(int newUserId, IRemoteCallback reply) {
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900668 synchronized(mMethodMap) {
669 switchUserLocked(newUserId);
670 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900671 if (reply != null) {
672 try {
673 reply.sendResult(null);
674 } catch (RemoteException e) {
675 }
676 }
677 }
678
679 @Override
680 public void onUserSwitchComplete(int newUserId) throws RemoteException {
681 }
682 });
683 userId = ActivityManagerNative.getDefault().getCurrentUser().id;
684 } catch (RemoteException e) {
685 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
686 }
satok81f8b7c2012-12-04 20:42:56 +0900687 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
satok913a8922010-08-26 21:53:41 +0900688
satokd87c2592010-09-29 11:52:06 +0900689 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900690 mSettings = new InputMethodSettings(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900691 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId);
Kenny Guy2a764942014-04-02 13:29:20 +0100692 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900693 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Satoshi Kataokad787f692013-10-26 04:44:21 +0900694 mSwitchingController = new InputMethodSubtypeSwitchingController(mSettings);
695 mSwitchingController.resetCircularListLocked(context);
satok0a1bcf42012-05-16 19:26:31 +0900696
697 // Just checking if defaultImiId is empty or not
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900698 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900699 if (DEBUG) {
700 Slog.d(TAG, "Initial default ime = " + defaultImiId);
701 }
satok0a1bcf42012-05-16 19:26:31 +0900702 mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
703
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900704 buildInputMethodListLocked(mMethodList, mMethodMap,
705 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satokd87c2592010-09-29 11:52:06 +0900706 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800707
satok0a1bcf42012-05-16 19:26:31 +0900708 if (!mImeSelectedOnBoot) {
709 Slog.w(TAG, "No IME selected. Choose the most applicable IME.");
satok5b927c432012-05-01 20:09:34 +0900710 resetDefaultImeLocked(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800712
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 mSettingsObserver = new SettingsObserver(mHandler);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800714 updateFromSettingsLocked(true);
satok5b927c432012-05-01 20:09:34 +0900715
716 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME
717 // according to the new system locale.
718 final IntentFilter filter = new IntentFilter();
719 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
720 mContext.registerReceiver(
721 new BroadcastReceiver() {
722 @Override
723 public void onReceive(Context context, Intent intent) {
724 synchronized(mMethodMap) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900725 resetStateIfCurrentLocaleChangedLocked();
satok5b927c432012-05-01 20:09:34 +0900726 }
727 }
728 }, filter);
729 }
730
satok5b927c432012-05-01 20:09:34 +0900731 private void resetDefaultImeLocked(Context context) {
732 // Do not reset the default (current) IME when it is a 3rd-party IME
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900733 if (mCurMethodId != null
734 && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +0900735 return;
736 }
737
738 InputMethodInfo defIm = null;
739 for (InputMethodInfo imi : mMethodList) {
740 if (defIm == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900741 if (InputMethodUtils.isValidSystemDefaultIme(
742 mSystemReady, imi, context)) {
satok5b927c432012-05-01 20:09:34 +0900743 defIm = imi;
744 Slog.i(TAG, "Selected default: " + imi.getId());
745 }
746 }
747 }
748 if (defIm == null && mMethodList.size() > 0) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900749 defIm = InputMethodUtils.getMostApplicableDefaultIME(
750 mSettings.getEnabledInputMethodListLocked());
satok5b927c432012-05-01 20:09:34 +0900751 Slog.i(TAG, "No default found, using " + defIm.getId());
752 }
753 if (defIm != null) {
754 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
755 }
756 }
757
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900758 private void resetAllInternalStateLocked(final boolean updateOnlyWhenLocaleChanged,
759 final boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900760 if (!mSystemReady) {
761 // not system ready
762 return;
763 }
764 final Locale newLocale = mRes.getConfiguration().locale;
765 if (!updateOnlyWhenLocaleChanged
766 || (newLocale != null && !newLocale.equals(mLastSystemLocale))) {
767 if (!updateOnlyWhenLocaleChanged) {
768 hideCurrentInputLocked(0, null);
769 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -0700770 unbindCurrentMethodLocked(true, false);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900771 }
772 if (DEBUG) {
773 Slog.i(TAG, "Locale has been changed to " + newLocale);
774 }
Satoshi Kataokad787f692013-10-26 04:44:21 +0900775 // CircularList should be reset when the locale is changed.
776 mSwitchingController.resetCircularListLocked(mContext);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900777 buildInputMethodListLocked(mMethodList, mMethodMap, resetDefaultEnabledIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900778 if (!updateOnlyWhenLocaleChanged) {
779 final String selectedImiId = mSettings.getSelectedInputMethod();
780 if (TextUtils.isEmpty(selectedImiId)) {
781 // This is the first time of the user switch and
782 // set the current ime to the proper one.
783 resetDefaultImeLocked(mContext);
784 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +0900785 } else {
786 // If the locale is changed, needs to reset the default ime
787 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900788 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800789 updateFromSettingsLocked(true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900790 mLastSystemLocale = newLocale;
791 if (!updateOnlyWhenLocaleChanged) {
792 try {
793 startInputInnerLocked();
794 } catch (RuntimeException e) {
795 Slog.w(TAG, "Unexpected exception", e);
796 }
797 }
798 }
799 }
800
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900801 private void resetStateIfCurrentLocaleChangedLocked() {
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900802 resetAllInternalStateLocked(true /* updateOnlyWhenLocaleChanged */,
803 true /* resetDefaultImeLocked */);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900804 }
805
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900806 private void switchUserLocked(int newUserId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900807 mSettings.setCurrentUserId(newUserId);
Kenny Guy2a764942014-04-02 13:29:20 +0100808 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900809 // InputMethodFileManager should be reset when the user is changed
810 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900811 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +0900812 // For secondary users, the list of enabled IMEs may not have been updated since the
813 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
814 // not be empty even if the IME has been uninstalled by the primary user.
815 // Even in such cases, IMMS works fine because it will find the most applicable
816 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900817 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900818 if (DEBUG) {
819 Slog.d(TAG, "Switch user: " + newUserId + " current ime = " + defaultImiId);
820 }
821 resetAllInternalStateLocked(false /* updateOnlyWhenLocaleChanged */,
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900822 initialUserSwitch /* needsToResetDefaultIme */);
823 if (initialUserSwitch) {
824 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mContext.getPackageManager(),
825 mSettings.getEnabledInputMethodListLocked());
826 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900827 }
828
Kenny Guy2a764942014-04-02 13:29:20 +0100829 void updateCurrentProfileIds() {
830 List<UserInfo> profiles =
831 UserManager.get(mContext).getProfiles(mSettings.getCurrentUserId());
832 int[] currentProfileIds = new int[profiles.size()]; // profiles will not be null
833 for (int i = 0; i < currentProfileIds.length; i++) {
834 currentProfileIds[i] = profiles.get(i).id;
Amith Yamasani734983f2014-03-04 16:48:05 -0800835 }
Kenny Guy2a764942014-04-02 13:29:20 +0100836 mSettings.setCurrentProfileIds(currentProfileIds);
Amith Yamasani734983f2014-03-04 16:48:05 -0800837 }
838
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800839 @Override
840 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
841 throws RemoteException {
842 try {
843 return super.onTransact(code, data, reply, flags);
844 } catch (RuntimeException e) {
845 // The input method manager only throws security exceptions, so let's
846 // log all others.
847 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -0700848 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800849 }
850 throw e;
851 }
852 }
853
Svetoslav Ganova0027152013-06-25 14:59:53 -0700854 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700855 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900856 if (DEBUG) {
857 Slog.d(TAG, "--- systemReady");
858 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700859 if (!mSystemReady) {
860 mSystemReady = true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900861 mKeyguardManager =
862 (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700863 mNotificationManager = (NotificationManager)
864 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
865 mStatusBar = statusBar;
866 statusBar.setIconVisibility("ime", false);
867 updateImeWindowStatusLocked();
satokb858c732011-07-22 19:54:34 +0900868 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
869 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +0900870 if (mShowOngoingImeSwitcherForPhones) {
871 mWindowManagerService.setOnHardKeyboardStatusChangeListener(
872 mHardKeyboardListener);
873 }
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900874 buildInputMethodListLocked(mMethodList, mMethodMap,
875 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satok0a1bcf42012-05-16 19:26:31 +0900876 if (!mImeSelectedOnBoot) {
877 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900878 resetStateIfCurrentLocaleChangedLocked();
879 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(
880 mContext.getPackageManager(),
881 mSettings.getEnabledInputMethodListLocked());
satok0a1bcf42012-05-16 19:26:31 +0900882 }
883 mLastSystemLocale = mRes.getConfiguration().locale;
Dianne Hackborncc278702009-09-02 23:07:23 -0700884 try {
885 startInputInnerLocked();
886 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800887 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700888 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700889 }
890 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800891 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800892
satok15452a42011-10-28 17:58:28 +0900893 private void setImeWindowVisibilityStatusHiddenLocked() {
894 mImeWindowVis = 0;
895 updateImeWindowStatusLocked();
896 }
897
satok3afd6c02011-11-18 08:38:19 +0900898 private void refreshImeWindowVisibilityLocked() {
899 final Configuration conf = mRes.getConfiguration();
900 final boolean haveHardKeyboard = conf.keyboard
901 != Configuration.KEYBOARD_NOKEYS;
902 final boolean hardKeyShown = haveHardKeyboard
903 && conf.hardKeyboardHidden
904 != Configuration.HARDKEYBOARDHIDDEN_YES;
John Spurlocke0980502013-10-25 11:59:29 -0400905
906 final boolean isScreenLocked = isKeyguardLocked();
907 final boolean inputActive = !isScreenLocked && (mInputShown || hardKeyShown);
John Spurlock36439b42013-10-23 16:36:47 -0400908 // We assume the softkeyboard is shown when the input is active as long as the
909 // hard keyboard is not shown.
910 final boolean inputVisible = inputActive && !hardKeyShown;
911 mImeWindowVis = (inputActive ? InputMethodService.IME_ACTIVE : 0)
912 | (inputVisible ? InputMethodService.IME_VISIBLE : 0);
satok3afd6c02011-11-18 08:38:19 +0900913 updateImeWindowStatusLocked();
914 }
915
satok15452a42011-10-28 17:58:28 +0900916 private void updateImeWindowStatusLocked() {
satokdbf29502011-08-25 15:28:23 +0900917 setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700918 }
919
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900920 // ---------------------------------------------------------------------------------------
921 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
922 // 1) it comes from the system process
923 // 2) the calling process' user id is identical to the current user id IMMS thinks.
924 private boolean calledFromValidUser() {
925 final int uid = Binder.getCallingUid();
926 final int userId = UserHandle.getUserId(uid);
927 if (DEBUG) {
928 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
929 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
930 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +0900931 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
932 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900933 }
Kenny Guy2a764942014-04-02 13:29:20 +0100934 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900935 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900936 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +0900937
938 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
939 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
940 // must not manage background users' states in any functions.
941 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
942 // by a token.
943 if (mContext.checkCallingOrSelfPermission(
944 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
945 == PackageManager.PERMISSION_GRANTED) {
946 if (DEBUG) {
947 Slog.d(TAG, "--- Access granted because the calling process has "
948 + "the INTERACT_ACROSS_USERS_FULL permission");
949 }
950 return true;
951 }
Satoshi Kataoka0766eb02013-07-31 18:30:13 +0900952 Slog.w(TAG, "--- IPC called from background users. Ignore. \n"
953 + InputMethodUtils.getStackTrace());
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +0900954 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900955 }
956
957 private boolean bindCurrentInputMethodService(
958 Intent service, ServiceConnection conn, int flags) {
959 if (service == null || conn == null) {
960 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
961 return false;
962 }
Amith Yamasani27b89e62013-01-16 12:30:11 -0800963 return mContext.bindServiceAsUser(service, conn, flags,
964 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900965 }
966
satoke7c6998e2011-06-03 17:57:59 +0900967 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800968 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900969 // TODO: Make this work even for non-current users?
970 if (!calledFromValidUser()) {
971 return Collections.emptyList();
972 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800973 synchronized (mMethodMap) {
974 return new ArrayList<InputMethodInfo>(mMethodList);
975 }
976 }
977
satoke7c6998e2011-06-03 17:57:59 +0900978 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800979 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900980 // TODO: Make this work even for non-current users?
981 if (!calledFromValidUser()) {
982 return Collections.emptyList();
983 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800984 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900985 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800986 }
987 }
988
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900989 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +0900990 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900991 * @return enabled subtypes of the specified imi
992 */
satoke7c6998e2011-06-03 17:57:59 +0900993 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +0900994 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +0900995 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900996 // TODO: Make this work even for non-current users?
997 if (!calledFromValidUser()) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +0900998 return Collections.<InputMethodSubtype>emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900999 }
satok67ddf9c2010-11-17 09:45:54 +09001000 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001001 final InputMethodInfo imi;
1002 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001003 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001004 } else {
1005 imi = mMethodMap.get(imiId);
1006 }
1007 if (imi == null) {
1008 return Collections.<InputMethodSubtype>emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001009 }
1010 return mSettings.getEnabledInputMethodSubtypeListLocked(
1011 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001012 }
1013 }
1014
satoke7c6998e2011-06-03 17:57:59 +09001015 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001016 public void addClient(IInputMethodClient client,
1017 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001018 if (!calledFromValidUser()) {
1019 return;
1020 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 synchronized (mMethodMap) {
1022 mClients.put(client.asBinder(), new ClientState(client,
1023 inputContext, uid, pid));
1024 }
1025 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001026
satoke7c6998e2011-06-03 17:57:59 +09001027 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001028 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001029 if (!calledFromValidUser()) {
1030 return;
1031 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001033 ClientState cs = mClients.remove(client.asBinder());
1034 if (cs != null) {
1035 clearClientSessionLocked(cs);
1036 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 }
1038 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001039
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040 void executeOrSendMessage(IInterface target, Message msg) {
1041 if (target.asBinder() instanceof Binder) {
1042 mCaller.sendMessage(msg);
1043 } else {
1044 handleMessage(msg);
1045 msg.recycle();
1046 }
1047 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001048
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001049 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001050 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001051 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052 + mCurClient.client.asBinder());
1053 if (mBoundToMethod) {
1054 mBoundToMethod = false;
1055 if (mCurMethod != null) {
1056 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1057 MSG_UNBIND_INPUT, mCurMethod));
1058 }
1059 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001060
1061 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1062 MSG_SET_ACTIVE, 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001063 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1064 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1065 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001066 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001067
The Android Open Source Project10592532009-03-18 17:39:46 -07001068 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069 }
1070 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001072 private int getImeShowFlags() {
1073 int flags = 0;
1074 if (mShowForced) {
1075 flags |= InputMethod.SHOW_FORCED
1076 | InputMethod.SHOW_EXPLICIT;
1077 } else if (mShowExplicitlyRequested) {
1078 flags |= InputMethod.SHOW_EXPLICIT;
1079 }
1080 return flags;
1081 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 private int getAppShowFlags() {
1084 int flags = 0;
1085 if (mShowForced) {
1086 flags |= InputMethodManager.SHOW_FORCED;
1087 } else if (!mShowExplicitlyRequested) {
1088 flags |= InputMethodManager.SHOW_IMPLICIT;
1089 }
1090 return flags;
1091 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001092
Dianne Hackborn7663d802012-02-24 13:08:49 -08001093 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094 if (!mBoundToMethod) {
1095 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1096 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1097 mBoundToMethod = true;
1098 }
1099 final SessionState session = mCurClient.curSession;
1100 if (initial) {
1101 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1102 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
1103 } else {
1104 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1105 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
1106 }
1107 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001108 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001109 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001110 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001111 return new InputBindResult(session.session,
1112 session.channel != null ? session.channel.dup() : null, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001113 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001115 InputBindResult startInputLocked(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001116 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001117 // If no method is currently selected, do nothing.
1118 if (mCurMethodId == null) {
1119 return mNoBinding;
1120 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001122 ClientState cs = mClients.get(client.asBinder());
1123 if (cs == null) {
1124 throw new IllegalArgumentException("unknown client "
1125 + client.asBinder());
1126 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001128 try {
1129 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1130 // Check with the window manager to make sure this client actually
1131 // has a window with focus. If not, reject. This is thread safe
1132 // because if the focus changes some time before or after, the
1133 // next client receiving focus that has any interest in input will
1134 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001135 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001136 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1137 return null;
1138 }
1139 } catch (RemoteException e) {
1140 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001141
Dianne Hackborn7663d802012-02-24 13:08:49 -08001142 return startInputUncheckedLocked(cs, inputContext, attribute, controlFlags);
1143 }
1144
1145 InputBindResult startInputUncheckedLocked(ClientState cs,
1146 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
1147 // If no method is currently selected, do nothing.
1148 if (mCurMethodId == null) {
1149 return mNoBinding;
1150 }
1151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001152 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001153 // Was the keyguard locked when switching over to the new client?
1154 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001155 // If the client is changing, we need to switch over to the new
1156 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001157 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001158 if (DEBUG) Slog.v(TAG, "switching to client: client = "
John Spurlocke0980502013-10-25 11:59:29 -04001159 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160
1161 // If the screen is on, inform the new client it is active
1162 if (mScreenOn) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001163 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
1164 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001165 }
1166 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001168 // Bump up the sequence for this client and attach it.
1169 mCurSeq++;
1170 if (mCurSeq <= 0) mCurSeq = 1;
1171 mCurClient = cs;
1172 mCurInputContext = inputContext;
1173 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001175 // Check if the input method is changing.
1176 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1177 if (cs.curSession != null) {
1178 // Fast case: if we are already connected to the input method,
1179 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001180 return attachNewInputLocked(
1181 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001182 }
1183 if (mHaveConnection) {
1184 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185 // Return to client, and we will get back with it when
1186 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001187 requestClientSessionLocked(cs);
1188 return new InputBindResult(null, null, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 } else if (SystemClock.uptimeMillis()
1190 < (mLastBindTime+TIME_TO_RECONNECT)) {
1191 // In this case we have connected to the service, but
1192 // don't yet have its interface. If it hasn't been too
1193 // long since we did the connection, we'll return to
1194 // the client and wait to get the service interface so
1195 // we can report back. If it has been too long, we want
1196 // to fall through so we can try a disconnect/reconnect
1197 // to see if we can get back in touch with the service.
Jeff Brownc28867a2013-03-26 15:42:39 -07001198 return new InputBindResult(null, null, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001199 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001200 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1201 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001202 }
1203 }
1204 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001205
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001206 return startInputInnerLocked();
1207 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001208
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001209 InputBindResult startInputInnerLocked() {
1210 if (mCurMethodId == null) {
1211 return mNoBinding;
1212 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001213
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001214 if (!mSystemReady) {
1215 // If the system is not yet ready, we shouldn't be running third
1216 // party code.
Jeff Brownc28867a2013-03-26 15:42:39 -07001217 return new InputBindResult(null, null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001218 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001220 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1221 if (info == null) {
1222 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1223 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001224
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001225 unbindCurrentMethodLocked(false, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001226
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1228 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001229 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1230 com.android.internal.R.string.input_method_binding_label);
1231 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1232 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001233 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001234 | Context.BIND_NOT_VISIBLE | Context.BIND_NOT_FOREGROUND
1235 | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001236 mLastBindTime = SystemClock.uptimeMillis();
1237 mHaveConnection = true;
1238 mCurId = info.getId();
1239 mCurToken = new Binder();
1240 try {
Craig Mautnerca0ac712013-03-14 09:43:02 -07001241 if (true || DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001242 mIWindowManager.addWindowToken(mCurToken,
1243 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1244 } catch (RemoteException e) {
1245 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001246 return new InputBindResult(null, null, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001247 } else {
1248 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001249 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001250 + mCurIntent);
1251 }
1252 return null;
1253 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001254
satoke7c6998e2011-06-03 17:57:59 +09001255 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001256 public InputBindResult startInput(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001257 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001258 if (!calledFromValidUser()) {
1259 return null;
1260 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001261 synchronized (mMethodMap) {
1262 final long ident = Binder.clearCallingIdentity();
1263 try {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001264 return startInputLocked(client, inputContext, attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001265 } finally {
1266 Binder.restoreCallingIdentity(ident);
1267 }
1268 }
1269 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001270
satoke7c6998e2011-06-03 17:57:59 +09001271 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001272 public void finishInput(IInputMethodClient client) {
1273 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001274
satoke7c6998e2011-06-03 17:57:59 +09001275 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001276 public void onServiceConnected(ComponentName name, IBinder service) {
1277 synchronized (mMethodMap) {
1278 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1279 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001280 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001281 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001282 unbindCurrentMethodLocked(false, false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001283 return;
1284 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001285 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001286 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1287 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001289 clearClientSessionLocked(mCurClient);
1290 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001291 }
1292 }
1293 }
1294 }
1295
Jeff Brownc28867a2013-03-26 15:42:39 -07001296 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1297 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001298 synchronized (mMethodMap) {
1299 if (mCurMethod != null && method != null
1300 && mCurMethod.asBinder() == method.asBinder()) {
1301 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001302 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001303 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001304 method, session, channel);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001305 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001306 if (res.method != null) {
1307 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
1308 MSG_BIND_METHOD, mCurClient.client, res));
1309 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001310 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001311 }
1312 }
1313 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001314
1315 // Session abandoned. Close its associated input channel.
1316 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001317 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001318
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001319 void unbindCurrentMethodLocked(boolean reportToClient, boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001320 if (mVisibleBound) {
1321 mContext.unbindService(mVisibleConnection);
1322 mVisibleBound = false;
1323 }
1324
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001325 if (mHaveConnection) {
1326 mContext.unbindService(this);
1327 mHaveConnection = false;
1328 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001329
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001330 if (mCurToken != null) {
1331 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001332 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001333 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001334 // The current IME is shown. Hence an IME switch (transition) is happening.
1335 mWindowManagerService.saveLastInputMethodWindowForTransition();
1336 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001337 mIWindowManager.removeWindowToken(mCurToken);
1338 } catch (RemoteException e) {
1339 }
1340 mCurToken = null;
1341 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001342
The Android Open Source Project10592532009-03-18 17:39:46 -07001343 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001344 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001345
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001346 if (reportToClient && mCurClient != null) {
1347 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1348 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1349 }
1350 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001351
1352 void requestClientSessionLocked(ClientState cs) {
1353 if (!cs.sessionRequested) {
1354 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1355 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1356 cs.sessionRequested = true;
1357 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1358 MSG_CREATE_SESSION, mCurMethod, channels[1],
1359 new MethodCallback(this, mCurMethod, channels[0])));
1360 }
1361 }
1362
1363 void clearClientSessionLocked(ClientState cs) {
1364 finishSessionLocked(cs.curSession);
1365 cs.curSession = null;
1366 cs.sessionRequested = false;
1367 }
1368
1369 private void finishSessionLocked(SessionState sessionState) {
1370 if (sessionState != null) {
1371 if (sessionState.session != null) {
1372 try {
1373 sessionState.session.finishSession();
1374 } catch (RemoteException e) {
1375 Slog.w(TAG, "Session failed to close due to remote exception", e);
1376 setImeWindowVisibilityStatusHiddenLocked();
1377 }
1378 sessionState.session = null;
1379 }
1380 if (sessionState.channel != null) {
1381 sessionState.channel.dispose();
1382 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001383 }
1384 }
1385 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001386
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001387 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001388 if (mCurMethod != null) {
1389 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001390 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001391 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001392
Jeff Brownc28867a2013-03-26 15:42:39 -07001393 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001394 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001395 mCurMethod = null;
1396 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001397 if (mStatusBar != null) {
1398 mStatusBar.setIconVisibility("ime", false);
1399 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001400 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001401
satoke7c6998e2011-06-03 17:57:59 +09001402 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001403 public void onServiceDisconnected(ComponentName name) {
1404 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001405 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001406 + " mCurIntent=" + mCurIntent);
1407 if (mCurMethod != null && mCurIntent != null
1408 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001409 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001410 // We consider this to be a new bind attempt, since the system
1411 // should now try to restart the service for us.
1412 mLastBindTime = SystemClock.uptimeMillis();
1413 mShowRequested = mInputShown;
1414 mInputShown = false;
1415 if (mCurClient != null) {
1416 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1417 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1418 }
1419 }
1420 }
1421 }
1422
satokf9f01002011-05-19 21:31:50 +09001423 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001425 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001426 long ident = Binder.clearCallingIdentity();
1427 try {
1428 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001429 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001430 return;
1431 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001433 synchronized (mMethodMap) {
1434 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001435 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001436 if (mStatusBar != null) {
1437 mStatusBar.setIconVisibility("ime", false);
1438 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001439 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001440 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001441 CharSequence contentDescription = null;
1442 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001443 // Use PackageManager to load label
1444 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001445 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001446 mIPackageManager.getApplicationInfo(packageName, 0,
1447 mSettings.getCurrentUserId()));
1448 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001449 /* ignore */
1450 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001451 if (mStatusBar != null) {
1452 mStatusBar.setIcon("ime", packageName, iconId, 0,
1453 contentDescription != null
1454 ? contentDescription.toString() : null);
1455 mStatusBar.setIconVisibility("ime", true);
1456 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001457 }
1458 }
1459 } finally {
1460 Binder.restoreCallingIdentity(ident);
1461 }
1462 }
1463
satok7cfc0ed2011-06-20 21:29:36 +09001464 private boolean needsToShowImeSwitchOngoingNotification() {
1465 if (!mShowOngoingImeSwitcherForPhones) return false;
satok2c93efc2012-04-02 19:33:47 +09001466 if (isScreenLocked()) return false;
satok7cfc0ed2011-06-20 21:29:36 +09001467 synchronized (mMethodMap) {
1468 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1469 final int N = imis.size();
satokb6359412011-06-28 17:47:41 +09001470 if (N > 2) return true;
1471 if (N < 1) return false;
1472 int nonAuxCount = 0;
1473 int auxCount = 0;
1474 InputMethodSubtype nonAuxSubtype = null;
1475 InputMethodSubtype auxSubtype = null;
satok7cfc0ed2011-06-20 21:29:36 +09001476 for(int i = 0; i < N; ++i) {
1477 final InputMethodInfo imi = imis.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001478 final List<InputMethodSubtype> subtypes =
1479 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satok7cfc0ed2011-06-20 21:29:36 +09001480 final int subtypeCount = subtypes.size();
1481 if (subtypeCount == 0) {
satokb6359412011-06-28 17:47:41 +09001482 ++nonAuxCount;
satok7cfc0ed2011-06-20 21:29:36 +09001483 } else {
1484 for (int j = 0; j < subtypeCount; ++j) {
satokb6359412011-06-28 17:47:41 +09001485 final InputMethodSubtype subtype = subtypes.get(j);
1486 if (!subtype.isAuxiliary()) {
1487 ++nonAuxCount;
1488 nonAuxSubtype = subtype;
1489 } else {
1490 ++auxCount;
1491 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001492 }
1493 }
1494 }
satok7cfc0ed2011-06-20 21:29:36 +09001495 }
satokb6359412011-06-28 17:47:41 +09001496 if (nonAuxCount > 1 || auxCount > 1) {
1497 return true;
1498 } else if (nonAuxCount == 1 && auxCount == 1) {
1499 if (nonAuxSubtype != null && auxSubtype != null
satok9747f892011-09-12 15:56:40 +09001500 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1501 || auxSubtype.overridesImplicitlyEnabledSubtype()
1502 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
satokb6359412011-06-28 17:47:41 +09001503 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1504 return false;
1505 }
1506 return true;
1507 }
1508 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001509 }
satok7cfc0ed2011-06-20 21:29:36 +09001510 }
1511
John Spurlocke0980502013-10-25 11:59:29 -04001512 private boolean isKeyguardLocked() {
1513 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1514 }
1515
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001516 // Caution! This method is called in this class. Handle multi-user carefully
satokdbf29502011-08-25 15:28:23 +09001517 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001518 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001519 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001520 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001521 try {
1522 if (token == null || mCurToken != token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001523 int uid = Binder.getCallingUid();
Joe Onorato857fd9b2011-01-27 15:08:35 -08001524 Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token);
satok06487a52010-10-29 11:37:18 +09001525 return;
1526 }
satok06487a52010-10-29 11:37:18 +09001527 synchronized (mMethodMap) {
John Spurlocke0980502013-10-25 11:59:29 -04001528 // apply policy for binder calls
1529 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
1530 vis = 0;
1531 }
Joe Onorato857fd9b2011-01-27 15:08:35 -08001532 mImeWindowVis = vis;
1533 mBackDisposition = backDisposition;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001534 if (mStatusBar != null) {
1535 mStatusBar.setImeWindowStatus(token, vis, backDisposition);
1536 }
jungheang.lee217fd292013-02-26 16:53:22 +09001537 final boolean iconVisibility = ((vis & (InputMethodService.IME_ACTIVE)) != 0)
1538 && (mWindowManagerService.isHardKeyboardAvailable()
1539 || (vis & (InputMethodService.IME_VISIBLE)) != 0);
satok5bc8e732011-07-22 21:07:23 +09001540 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1541 if (imi != null && iconVisibility && needsToShowImeSwitchOngoingNotification()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001542 // Used to load label
satok7cfc0ed2011-06-20 21:29:36 +09001543 final CharSequence title = mRes.getText(
1544 com.android.internal.R.string.select_input_method);
Satoshi Kataokab2827262013-07-04 19:43:14 +09001545 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
1546 mContext, imi, mCurrentSubtype);
satok5bc8e732011-07-22 21:07:23 +09001547
satok7cfc0ed2011-06-20 21:29:36 +09001548 mImeSwitcherNotification.setLatestEventInfo(
satok5bc8e732011-07-22 21:07:23 +09001549 mContext, title, summary, mImeSwitchPendingIntent);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001550 if (mNotificationManager != null) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001551 if (DEBUG) {
Satoshi Kataokab2827262013-07-04 19:43:14 +09001552 Slog.d(TAG, "--- show notification: label = " + summary);
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001553 }
1554 mNotificationManager.notifyAsUser(null,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001555 com.android.internal.R.string.select_input_method,
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001556 mImeSwitcherNotification, UserHandle.ALL);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001557 mNotificationShown = true;
1558 }
satok7cfc0ed2011-06-20 21:29:36 +09001559 } else {
Dianne Hackborn661cd522011-08-22 00:26:20 -07001560 if (mNotificationShown && mNotificationManager != null) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001561 if (DEBUG) {
1562 Slog.d(TAG, "--- hide notification");
1563 }
1564 mNotificationManager.cancelAsUser(null,
1565 com.android.internal.R.string.select_input_method, UserHandle.ALL);
satok7cfc0ed2011-06-20 21:29:36 +09001566 mNotificationShown = false;
1567 }
1568 }
satok06487a52010-10-29 11:37:18 +09001569 }
1570 } finally {
1571 Binder.restoreCallingIdentity(ident);
1572 }
1573 }
1574
satoke7c6998e2011-06-03 17:57:59 +09001575 @Override
satokf9f01002011-05-19 21:31:50 +09001576 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001577 if (!calledFromValidUser()) {
1578 return;
1579 }
satokf9f01002011-05-19 21:31:50 +09001580 synchronized (mMethodMap) {
1581 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1582 for (int i = 0; i < spans.length; ++i) {
1583 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001584 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001585 mSecureSuggestionSpans.put(ss, currentImi);
1586 }
1587 }
1588 }
1589 }
1590
satoke7c6998e2011-06-03 17:57:59 +09001591 @Override
satokf9f01002011-05-19 21:31:50 +09001592 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001593 if (!calledFromValidUser()) {
1594 return false;
1595 }
satokf9f01002011-05-19 21:31:50 +09001596 synchronized (mMethodMap) {
1597 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1598 // TODO: Do not send the intent if the process of the targetImi is already dead.
1599 if (targetImi != null) {
1600 final String[] suggestions = span.getSuggestions();
1601 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001602 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001603 final Intent intent = new Intent();
1604 // Ensures that only a class in the original IME package will receive the
1605 // notification.
satok42c5a162011-05-26 16:46:14 +09001606 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001607 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1608 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1609 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1610 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001611 final long ident = Binder.clearCallingIdentity();
1612 try {
1613 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1614 } finally {
1615 Binder.restoreCallingIdentity(ident);
1616 }
satokf9f01002011-05-19 21:31:50 +09001617 return true;
1618 }
1619 }
1620 return false;
1621 }
1622
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001623 void updateFromSettingsLocked(boolean enabledMayChange) {
1624 if (enabledMayChange) {
1625 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1626 for (int i=0; i<enabled.size(); i++) {
1627 // We allow the user to select "disabled until used" apps, so if they
1628 // are enabling one of those here we now need to make it enabled.
1629 InputMethodInfo imm = enabled.get(i);
1630 try {
1631 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1632 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1633 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09001634 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001635 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001636 if (DEBUG) {
1637 Slog.d(TAG, "Update state(" + imm.getId()
1638 + "): DISABLED_UNTIL_USED -> DEFAULT");
1639 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001640 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1641 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001642 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
1643 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001644 }
1645 } catch (RemoteException e) {
1646 }
1647 }
1648 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001649 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1650 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1651 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1652 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001653 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001654 // There is no input method selected, try to choose new applicable input method.
1655 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001656 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001657 }
1658 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001659 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001660 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001661 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001662 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001663 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001664 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001665 }
satokf3db1af2010-11-23 13:34:33 +09001666 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001667 } else {
1668 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001669 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001670 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001671 }
1672 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001673
satokab751aa2010-09-14 19:17:36 +09001674 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001675 InputMethodInfo info = mMethodMap.get(id);
1676 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001677 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001678 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001679
satokd81e9502012-05-21 12:58:45 +09001680 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001681 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09001682 final int subtypeCount = info.getSubtypeCount();
1683 if (subtypeCount <= 0) {
1684 return;
satokcd7cd292010-11-20 15:46:23 +09001685 }
satokd81e9502012-05-21 12:58:45 +09001686 final InputMethodSubtype oldSubtype = mCurrentSubtype;
1687 final InputMethodSubtype newSubtype;
1688 if (subtypeId >= 0 && subtypeId < subtypeCount) {
1689 newSubtype = info.getSubtypeAt(subtypeId);
1690 } else {
1691 // If subtype is null, try to find the most applicable one from
1692 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001693 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09001694 }
1695 if (newSubtype == null || oldSubtype == null) {
1696 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
1697 + ", new subtype = " + newSubtype);
1698 return;
1699 }
1700 if (newSubtype != oldSubtype) {
1701 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1702 if (mCurMethod != null) {
1703 try {
1704 refreshImeWindowVisibilityLocked();
1705 mCurMethod.changeInputMethodSubtype(newSubtype);
1706 } catch (RemoteException e) {
1707 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
satokab751aa2010-09-14 19:17:36 +09001708 }
1709 }
1710 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001711 return;
1712 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001713
satokd81e9502012-05-21 12:58:45 +09001714 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001715 final long ident = Binder.clearCallingIdentity();
1716 try {
satokab751aa2010-09-14 19:17:36 +09001717 // Set a subtype to this input method.
1718 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001719 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1720 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1721 // because mCurMethodId is stored as a history in
1722 // setSelectedInputMethodAndSubtypeLocked().
1723 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001724
1725 if (ActivityManagerNative.isSystemReady()) {
1726 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001727 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001728 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07001729 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001730 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001731 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001732 } finally {
1733 Binder.restoreCallingIdentity(ident);
1734 }
1735 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001736
satok42c5a162011-05-26 16:46:14 +09001737 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001738 public boolean showSoftInput(IInputMethodClient client, int flags,
1739 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001740 if (!calledFromValidUser()) {
1741 return false;
1742 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001743 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001744 long ident = Binder.clearCallingIdentity();
1745 try {
1746 synchronized (mMethodMap) {
1747 if (mCurClient == null || client == null
1748 || mCurClient.client.asBinder() != client.asBinder()) {
1749 try {
1750 // We need to check if this is the current client with
1751 // focus in the window manager, to allow this call to
1752 // be made before input is started in it.
1753 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001754 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001755 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001756 }
1757 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001758 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001759 }
1760 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001761
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001762 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001763 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001764 }
1765 } finally {
1766 Binder.restoreCallingIdentity(ident);
1767 }
1768 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001769
The Android Open Source Project4df24232009-03-05 14:34:35 -08001770 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001771 mShowRequested = true;
1772 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1773 mShowExplicitlyRequested = true;
1774 }
1775 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1776 mShowExplicitlyRequested = true;
1777 mShowForced = true;
1778 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001779
Dianne Hackborncc278702009-09-02 23:07:23 -07001780 if (!mSystemReady) {
1781 return false;
1782 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001783
The Android Open Source Project4df24232009-03-05 14:34:35 -08001784 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001785 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001786 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001787 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1788 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1789 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001790 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001791 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001792 bindCurrentInputMethodService(
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001793 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE
1794 | Context.BIND_TREAT_LIKE_ACTIVITY);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001795 mVisibleBound = true;
1796 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001797 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001798 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09001799 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001800 // The client has asked to have the input method shown, but
1801 // we have been sitting here too long with a connection to the
1802 // service and no interface received, so let's disconnect/connect
1803 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001804 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001805 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09001806 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001807 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001808 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001809 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001810 } else {
1811 if (DEBUG) {
1812 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
1813 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
1814 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001815 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001816
The Android Open Source Project4df24232009-03-05 14:34:35 -08001817 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001818 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001819
satok42c5a162011-05-26 16:46:14 +09001820 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001821 public boolean hideSoftInput(IInputMethodClient client, int flags,
1822 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001823 if (!calledFromValidUser()) {
1824 return false;
1825 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001826 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001827 long ident = Binder.clearCallingIdentity();
1828 try {
1829 synchronized (mMethodMap) {
1830 if (mCurClient == null || client == null
1831 || mCurClient.client.asBinder() != client.asBinder()) {
1832 try {
1833 // We need to check if this is the current client with
1834 // focus in the window manager, to allow this call to
1835 // be made before input is started in it.
1836 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001837 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1838 + uid + ": " + client);
satok15452a42011-10-28 17:58:28 +09001839 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001840 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001841 }
1842 } catch (RemoteException e) {
satok15452a42011-10-28 17:58:28 +09001843 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001844 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001845 }
1846 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001847
Joe Onorato8a9b2202010-02-26 18:56:32 -08001848 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001849 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001850 }
1851 } finally {
1852 Binder.restoreCallingIdentity(ident);
1853 }
1854 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001855
The Android Open Source Project4df24232009-03-05 14:34:35 -08001856 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001857 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1858 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001859 if (DEBUG) Slog.v(TAG, "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001860 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001861 }
1862 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001863 if (DEBUG) Slog.v(TAG, "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001864 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001865 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001866 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001867 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001868 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1869 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1870 res = true;
1871 } else {
1872 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001873 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001874 if (mHaveConnection && mVisibleBound) {
1875 mContext.unbindService(mVisibleConnection);
1876 mVisibleBound = false;
1877 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001878 mInputShown = false;
1879 mShowRequested = false;
1880 mShowExplicitlyRequested = false;
1881 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001882 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001883 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001884
satok42c5a162011-05-26 16:46:14 +09001885 @Override
Dianne Hackborn7663d802012-02-24 13:08:49 -08001886 public InputBindResult windowGainedFocus(IInputMethodClient client, IBinder windowToken,
1887 int controlFlags, int softInputMode, int windowFlags,
1888 EditorInfo attribute, IInputContext inputContext) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09001889 // Needs to check the validity before clearing calling identity
1890 final boolean calledFromValidUser = calledFromValidUser();
1891
Dianne Hackborn7663d802012-02-24 13:08:49 -08001892 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001893 long ident = Binder.clearCallingIdentity();
1894 try {
1895 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001896 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
Dianne Hackborn7663d802012-02-24 13:08:49 -08001897 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001898 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08001899 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001900
Dianne Hackborn7663d802012-02-24 13:08:49 -08001901 ClientState cs = mClients.get(client.asBinder());
1902 if (cs == null) {
1903 throw new IllegalArgumentException("unknown client "
1904 + client.asBinder());
1905 }
1906
1907 try {
1908 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1909 // Check with the window manager to make sure this client actually
1910 // has a window with focus. If not, reject. This is thread safe
1911 // because if the focus changes some time before or after, the
1912 // next client receiving focus that has any interest in input will
1913 // be calling through here after that change happens.
1914 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
1915 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1916 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001917 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001918 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001919 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001920
Satoshi Kataoka8d033052012-11-19 17:30:40 +09001921 if (!calledFromValidUser) {
1922 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
1923 Slog.w(TAG, "If you want to interect with IME, you need "
1924 + "android.permission.INTERACT_ACROSS_USERS_FULL");
1925 hideCurrentInputLocked(0, null);
1926 return null;
1927 }
1928
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001929 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07001930 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09001931 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001932 if (attribute != null) {
1933 return startInputUncheckedLocked(cs, inputContext, attribute,
1934 controlFlags);
1935 }
1936 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001937 }
1938 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001939
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001940 // Should we auto-show the IME even if the caller has not
1941 // specified what should be done with it?
1942 // We only do this automatically if the window can resize
1943 // to accommodate the IME (so what the user sees will give
1944 // them good context without input information being obscured
1945 // by the IME) or if running on a large screen where there
1946 // is more room for the target window + IME.
1947 final boolean doAutoShow =
1948 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1949 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1950 || mRes.getConfiguration().isLayoutSizeAtLeast(
1951 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001952 final boolean isTextEditor =
1953 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
1954
1955 // We want to start input before showing the IME, but after closing
1956 // it. We want to do this after closing it to help the IME disappear
1957 // more quickly (not get stuck behind it initializing itself for the
1958 // new focused input, even if its window wants to hide the IME).
1959 boolean didStart = false;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001960
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001961 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1962 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001963 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001964 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1965 // There is no focus view, and this window will
1966 // be behind any soft input window, so hide the
1967 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001968 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001969 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001970 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001971 } else if (isTextEditor && doAutoShow && (softInputMode &
1972 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001973 // There is a focus view, and we are navigating forward
1974 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001975 // We only do this automatically if the window can resize
1976 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001977 // them good context without input information being obscured
1978 // by the IME) or if running on a large screen where there
1979 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001980 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001981 if (attribute != null) {
1982 res = startInputUncheckedLocked(cs, inputContext, attribute,
1983 controlFlags);
1984 didStart = true;
1985 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001986 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001987 }
1988 break;
1989 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1990 // Do nothing.
1991 break;
1992 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1993 if ((softInputMode &
1994 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001995 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001996 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001997 }
1998 break;
1999 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002000 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002001 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002002 break;
2003 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2004 if ((softInputMode &
2005 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002006 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002007 if (attribute != null) {
2008 res = startInputUncheckedLocked(cs, inputContext, attribute,
2009 controlFlags);
2010 didStart = true;
2011 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002012 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002013 }
2014 break;
2015 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002016 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002017 if (attribute != null) {
2018 res = startInputUncheckedLocked(cs, inputContext, attribute,
2019 controlFlags);
2020 didStart = true;
2021 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002022 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002023 break;
2024 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002025
2026 if (!didStart && attribute != null) {
2027 res = startInputUncheckedLocked(cs, inputContext, attribute,
2028 controlFlags);
2029 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002030 }
2031 } finally {
2032 Binder.restoreCallingIdentity(ident);
2033 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002034
2035 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002036 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002037
satok42c5a162011-05-26 16:46:14 +09002038 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002039 public void showInputMethodPickerFromClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002040 if (!calledFromValidUser()) {
2041 return;
2042 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002043 synchronized (mMethodMap) {
2044 if (mCurClient == null || client == null
2045 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002046 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002047 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002048 }
2049
satok440aab52010-11-25 09:43:11 +09002050 // Always call subtype picker, because subtype picker is a superset of input method
2051 // picker.
satokab751aa2010-09-14 19:17:36 +09002052 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
2053 }
2054 }
2055
satok42c5a162011-05-26 16:46:14 +09002056 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002057 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002058 if (!calledFromValidUser()) {
2059 return;
2060 }
satok28203512010-11-24 11:06:49 +09002061 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2062 }
2063
satok42c5a162011-05-26 16:46:14 +09002064 @Override
satok28203512010-11-24 11:06:49 +09002065 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002066 if (!calledFromValidUser()) {
2067 return;
2068 }
satok28203512010-11-24 11:06:49 +09002069 synchronized (mMethodMap) {
2070 if (subtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002071 setInputMethodWithSubtypeId(token, id, InputMethodUtils.getSubtypeIdFromHashCode(
satok28203512010-11-24 11:06:49 +09002072 mMethodMap.get(id), subtype.hashCode()));
2073 } else {
2074 setInputMethod(token, id);
2075 }
2076 }
satokab751aa2010-09-14 19:17:36 +09002077 }
2078
satok42c5a162011-05-26 16:46:14 +09002079 @Override
satokb416a712010-11-25 20:42:14 +09002080 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002081 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002082 if (!calledFromValidUser()) {
2083 return;
2084 }
satokb416a712010-11-25 20:42:14 +09002085 synchronized (mMethodMap) {
2086 if (mCurClient == null || client == null
2087 || mCurClient.client.asBinder() != client.asBinder()) {
2088 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
2089 }
satok7fee71f2010-12-17 18:54:26 +09002090 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2091 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002092 }
2093 }
2094
satok4fc87d62011-05-20 16:13:43 +09002095 @Override
satok735cf382010-11-11 20:40:09 +09002096 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002097 if (!calledFromValidUser()) {
2098 return false;
2099 }
satok735cf382010-11-11 20:40:09 +09002100 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002101 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002102 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002103 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002104 lastImi = mMethodMap.get(lastIme.first);
2105 } else {
2106 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002107 }
satok4fc87d62011-05-20 16:13:43 +09002108 String targetLastImiId = null;
2109 int subtypeId = NOT_A_SUBTYPE_ID;
2110 if (lastIme != null && lastImi != null) {
2111 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
2112 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
2113 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2114 : mCurrentSubtype.hashCode();
2115 // If the last IME is the same as the current IME and the last subtype is not
2116 // defined, there is no need to switch to the last IME.
2117 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2118 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002119 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002120 }
2121 }
2122
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002123 if (TextUtils.isEmpty(targetLastImiId)
2124 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002125 // This is a safety net. If the currentSubtype can't be added to the history
2126 // and the framework couldn't find the last ime, we will make the last ime be
2127 // the most applicable enabled keyboard subtype of the system imes.
2128 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2129 if (enabled != null) {
2130 final int N = enabled.size();
2131 final String locale = mCurrentSubtype == null
2132 ? mRes.getConfiguration().locale.toString()
2133 : mCurrentSubtype.getLocale();
2134 for (int i = 0; i < N; ++i) {
2135 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002136 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002137 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002138 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2139 InputMethodUtils.getSubtypes(imi),
2140 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002141 if (keyboardSubtype != null) {
2142 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002143 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002144 imi, keyboardSubtype.hashCode());
2145 if(keyboardSubtype.getLocale().equals(locale)) {
2146 break;
2147 }
2148 }
2149 }
2150 }
2151 }
2152 }
2153
2154 if (!TextUtils.isEmpty(targetLastImiId)) {
2155 if (DEBUG) {
2156 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2157 + ", from: " + mCurMethodId + ", " + subtypeId);
2158 }
2159 setInputMethodWithSubtypeId(token, targetLastImiId, subtypeId);
2160 return true;
2161 } else {
2162 return false;
2163 }
satok735cf382010-11-11 20:40:09 +09002164 }
2165 }
2166
satoke7c6998e2011-06-03 17:57:59 +09002167 @Override
satok688bd472012-02-09 20:09:17 +09002168 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002169 if (!calledFromValidUser()) {
2170 return false;
2171 }
satok688bd472012-02-09 20:09:17 +09002172 synchronized (mMethodMap) {
Satoshi Kataokad787f692013-10-26 04:44:21 +09002173 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethod(
satok688bd472012-02-09 20:09:17 +09002174 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2175 if (nextSubtype == null) {
2176 return false;
2177 }
2178 setInputMethodWithSubtypeId(token, nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
2179 return true;
2180 }
2181 }
2182
2183 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002184 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2185 if (!calledFromValidUser()) {
2186 return false;
2187 }
2188 synchronized (mMethodMap) {
Satoshi Kataokad787f692013-10-26 04:44:21 +09002189 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethod(
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002190 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2191 if (nextSubtype == null) {
2192 return false;
2193 }
2194 return true;
2195 }
2196 }
2197
2198 @Override
satok68f1b782011-04-11 14:26:04 +09002199 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002200 if (!calledFromValidUser()) {
2201 return null;
2202 }
satok68f1b782011-04-11 14:26:04 +09002203 synchronized (mMethodMap) {
2204 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2205 // TODO: Handle the case of the last IME with no subtypes
2206 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2207 || TextUtils.isEmpty(lastIme.second)) return null;
2208 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2209 if (lastImi == null) return null;
2210 try {
2211 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002212 final int lastSubtypeId =
2213 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002214 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2215 return null;
2216 }
2217 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002218 } catch (NumberFormatException e) {
2219 return null;
2220 }
2221 }
2222 }
2223
satoke7c6998e2011-06-03 17:57:59 +09002224 @Override
satokee5e77c2011-09-02 18:50:15 +09002225 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002226 if (!calledFromValidUser()) {
2227 return;
2228 }
satok91e88122011-07-18 11:11:42 +09002229 // By this IPC call, only a process which shares the same uid with the IME can add
2230 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09002231 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09002232 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002233 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002234 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002235 final String[] packageInfos;
2236 try {
2237 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2238 } catch (RemoteException e) {
2239 Slog.e(TAG, "Failed to get package infos");
2240 return;
2241 }
satok91e88122011-07-18 11:11:42 +09002242 if (packageInfos != null) {
2243 final int packageNum = packageInfos.length;
2244 for (int i = 0; i < packageNum; ++i) {
2245 if (packageInfos[i].equals(imi.getPackageName())) {
2246 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002247 final long ident = Binder.clearCallingIdentity();
2248 try {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002249 buildInputMethodListLocked(mMethodList, mMethodMap,
2250 false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002251 } finally {
2252 Binder.restoreCallingIdentity(ident);
2253 }
satokee5e77c2011-09-02 18:50:15 +09002254 return;
satok91e88122011-07-18 11:11:42 +09002255 }
2256 }
2257 }
satoke7c6998e2011-06-03 17:57:59 +09002258 }
satokee5e77c2011-09-02 18:50:15 +09002259 return;
satoke7c6998e2011-06-03 17:57:59 +09002260 }
2261
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002262 @Override
2263 public int getInputMethodWindowVisibleHeight() {
2264 return mWindowManagerService.getInputMethodWindowVisibleHeight();
2265 }
2266
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002267 @Override
2268 public void notifyTextCommitted() {
2269 if (DEBUG) {
2270 Slog.d(TAG, "Got the notification of commitText");
2271 }
2272 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2273 if (imi != null) {
2274 mSwitchingController.onCommitText(imi, mCurrentSubtype);
2275 }
2276 }
2277
satok28203512010-11-24 11:06:49 +09002278 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002279 synchronized (mMethodMap) {
2280 if (token == null) {
2281 if (mContext.checkCallingOrSelfPermission(
2282 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2283 != PackageManager.PERMISSION_GRANTED) {
2284 throw new SecurityException(
2285 "Using null token requires permission "
2286 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2287 }
2288 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002289 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2290 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002291 return;
2292 }
2293
satokc5933802011-08-31 21:26:04 +09002294 final long ident = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002295 try {
satokab751aa2010-09-14 19:17:36 +09002296 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002297 } finally {
2298 Binder.restoreCallingIdentity(ident);
2299 }
2300 }
2301 }
2302
satok42c5a162011-05-26 16:46:14 +09002303 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002304 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002305 if (!calledFromValidUser()) {
2306 return;
2307 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002308 synchronized (mMethodMap) {
2309 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002310 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
2311 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002312 return;
2313 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002314 long ident = Binder.clearCallingIdentity();
2315 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002316 hideCurrentInputLocked(flags, null);
2317 } finally {
2318 Binder.restoreCallingIdentity(ident);
2319 }
2320 }
2321 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002322
satok42c5a162011-05-26 16:46:14 +09002323 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002324 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002325 if (!calledFromValidUser()) {
2326 return;
2327 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002328 synchronized (mMethodMap) {
2329 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002330 Slog.w(TAG, "Ignoring showMySoftInput of uid "
2331 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002332 return;
2333 }
2334 long ident = Binder.clearCallingIdentity();
2335 try {
2336 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002337 } finally {
2338 Binder.restoreCallingIdentity(ident);
2339 }
2340 }
2341 }
2342
2343 void setEnabledSessionInMainThread(SessionState session) {
2344 if (mEnabledSession != session) {
2345 if (mEnabledSession != null) {
2346 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002347 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002348 mEnabledSession.method.setSessionEnabled(
2349 mEnabledSession.session, false);
2350 } catch (RemoteException e) {
2351 }
2352 }
2353 mEnabledSession = session;
2354 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002355 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002356 session.method.setSessionEnabled(
2357 session.session, true);
2358 } catch (RemoteException e) {
2359 }
2360 }
2361 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002362
satok42c5a162011-05-26 16:46:14 +09002363 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002364 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002365 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002366 switch (msg.what) {
2367 case MSG_SHOW_IM_PICKER:
2368 showInputMethodMenu();
2369 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002370
satokab751aa2010-09-14 19:17:36 +09002371 case MSG_SHOW_IM_SUBTYPE_PICKER:
2372 showInputMethodSubtypeMenu();
2373 return true;
2374
satok47a44912010-10-06 16:03:58 +09002375 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002376 args = (SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09002377 showInputMethodAndSubtypeEnabler((String)args.arg1);
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002378 args.recycle();
satok217f5482010-12-15 05:19:19 +09002379 return true;
2380
2381 case MSG_SHOW_IM_CONFIG:
2382 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002383 return true;
2384
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002385 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002386
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002387 case MSG_UNBIND_INPUT:
2388 try {
2389 ((IInputMethod)msg.obj).unbindInput();
2390 } catch (RemoteException e) {
2391 // There is nothing interesting about the method dying.
2392 }
2393 return true;
2394 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002395 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002396 try {
2397 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2398 } catch (RemoteException e) {
2399 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002400 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002401 return true;
2402 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002403 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002404 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002405 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002406 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002407 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002408 } catch (RemoteException e) {
2409 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002410 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002411 return true;
2412 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002413 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002414 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002415 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002416 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002417 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002418 } catch (RemoteException e) {
2419 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002420 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002421 return true;
2422 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002423 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002424 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002425 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002426 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2427 } catch (RemoteException e) {
2428 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002429 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002430 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002431 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002432 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002433 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002434 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002435 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002436 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002437 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002438 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002439 // Dispose the channel if the input method is not local to this process
2440 // because the remote proxy will get its own copy when unparceled.
2441 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002442 channel.dispose();
2443 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002444 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002445 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002446 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002447 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002448 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002450 case MSG_START_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002451 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002452 try {
2453 SessionState session = (SessionState)args.arg1;
2454 setEnabledSessionInMainThread(session);
2455 session.method.startInput((IInputContext)args.arg2,
2456 (EditorInfo)args.arg3);
2457 } catch (RemoteException e) {
2458 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002459 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002460 return true;
2461 case MSG_RESTART_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002462 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002463 try {
2464 SessionState session = (SessionState)args.arg1;
2465 setEnabledSessionInMainThread(session);
2466 session.method.restartInput((IInputContext)args.arg2,
2467 (EditorInfo)args.arg3);
2468 } catch (RemoteException e) {
2469 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002470 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002471 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002472
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002473 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002474
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002475 case MSG_UNBIND_METHOD:
2476 try {
2477 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
2478 } catch (RemoteException e) {
2479 // There is nothing interesting about the last client dying.
2480 }
2481 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002482 case MSG_BIND_METHOD: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002483 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002484 IInputMethodClient client = (IInputMethodClient)args.arg1;
2485 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002486 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002487 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002488 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002489 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002490 } finally {
2491 // Dispose the channel if the input method is not local to this process
2492 // because the remote proxy will get its own copy when unparceled.
2493 if (res.channel != null && Binder.isProxy(client)) {
2494 res.channel.dispose();
2495 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002496 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002497 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002498 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002499 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002500 case MSG_SET_ACTIVE:
2501 try {
2502 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2503 } catch (RemoteException e) {
2504 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2505 + ((ClientState)msg.obj).pid + " uid "
2506 + ((ClientState)msg.obj).uid);
2507 }
2508 return true;
satok01038492012-04-09 21:08:27 +09002509
2510 // --------------------------------------------------------------
2511 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
2512 mHardKeyboardListener.handleHardKeyboardStatusChange(
2513 msg.arg1 == 1, msg.arg2 == 1);
2514 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002515 }
2516 return false;
2517 }
2518
satokdc9ddae2011-10-06 12:22:36 +09002519 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002520 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
2521 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09002522 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002523 if (DEBUG) {
2524 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2525 }
satok723a27e2010-11-11 14:58:11 +09002526 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002527 return true;
2528 }
2529
2530 return false;
2531 }
2532
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002533 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002534 HashMap<String, InputMethodInfo> map, boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002535 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002536 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Satoshi Kataoka0766eb02013-07-31 18:30:13 +09002537 + " \n ------ \n" + InputMethodUtils.getStackTrace());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002538 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002539 list.clear();
2540 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002541
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002542 // Use for queryIntentServicesAsUser
2543 final PackageManager pm = mContext.getPackageManager();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002544 String disabledSysImes = mSettings.getDisabledSystemInputMethods();
Amith Yamasanie861ec12010-03-24 21:39:27 -07002545 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002546
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002547 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002548 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002549 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2550 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002551
satoke7c6998e2011-06-03 17:57:59 +09002552 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2553 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002554 for (int i = 0; i < services.size(); ++i) {
2555 ResolveInfo ri = services.get(i);
2556 ServiceInfo si = ri.serviceInfo;
2557 ComponentName compName = new ComponentName(si.packageName, si.name);
2558 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2559 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002560 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002561 + ": it does not require the permission "
2562 + android.Manifest.permission.BIND_INPUT_METHOD);
2563 continue;
2564 }
2565
Joe Onorato8a9b2202010-02-26 18:56:32 -08002566 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002567
2568 try {
satoke7c6998e2011-06-03 17:57:59 +09002569 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002570 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002571 final String id = p.getId();
2572 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002573
2574 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002575 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002576 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002577
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002578 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002579 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002580 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002581 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002582 }
2583 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002584
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002585 if (resetDefaultEnabledIme) {
2586 final ArrayList<InputMethodInfo> defaultEnabledIme =
2587 InputMethodUtils.getDefaultEnabledImes(mContext, mSystemReady, list);
2588 for (int i = 0; i < defaultEnabledIme.size(); ++i) {
2589 final InputMethodInfo imi = defaultEnabledIme.get(i);
2590 if (DEBUG) {
2591 Slog.d(TAG, "--- enable ime = " + imi);
2592 }
2593 setInputMethodEnabledLocked(imi.getId(), true);
2594 }
2595 }
2596
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002597 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09002598 if (!TextUtils.isEmpty(defaultImiId)) {
2599 if (!map.containsKey(defaultImiId)) {
2600 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
2601 if (chooseNewDefaultIMELocked()) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002602 updateFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09002603 }
2604 } else {
2605 // Double check that the default IME is certainly enabled.
2606 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002607 }
2608 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002609 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002610
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002611 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002612
satokab751aa2010-09-14 19:17:36 +09002613 private void showInputMethodMenu() {
2614 showInputMethodMenuInternal(false);
2615 }
2616
2617 private void showInputMethodSubtypeMenu() {
2618 showInputMethodMenuInternal(true);
2619 }
2620
satok217f5482010-12-15 05:19:19 +09002621 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002622 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002623 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002624 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2625 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002626 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002627 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002628 }
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002629 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok217f5482010-12-15 05:19:19 +09002630 }
2631
2632 private void showConfigureInputMethods() {
2633 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2634 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2635 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2636 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002637 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09002638 }
2639
satok2c93efc2012-04-02 19:33:47 +09002640 private boolean isScreenLocked() {
2641 return mKeyguardManager != null
2642 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
2643 }
satokab751aa2010-09-14 19:17:36 +09002644 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002645 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002646
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002647 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09002648 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002649
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002650 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002651 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002652 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002653
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002654 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002655 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09002656 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
2657 mContext);
satok7f35c8c2010-10-07 21:13:11 +09002658 if (immis == null || immis.size() == 0) {
2659 return;
2660 }
2661
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002662 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002663
satok688bd472012-02-09 20:09:17 +09002664 final List<ImeSubtypeListItem> imList =
Satoshi Kataokad787f692013-10-26 04:44:21 +09002665 mSwitchingController.getSortedInputMethodAndSubtypeList(
satok688bd472012-02-09 20:09:17 +09002666 showSubtypes, mInputShown, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09002667
satokc3690562012-01-10 20:14:43 +09002668 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002669 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09002670 if (currentSubtype != null) {
2671 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002672 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
2673 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09002674 }
2675 }
2676
Ken Wakasa761eb372011-03-04 19:06:18 +09002677 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09002678 mIms = new InputMethodInfo[N];
2679 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002680 int checkedItem = 0;
2681 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002682 final ImeSubtypeListItem item = imList.get(i);
2683 mIms[i] = item.mImi;
2684 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002685 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002686 int subtypeId = mSubtypeIds[i];
2687 if ((subtypeId == NOT_A_SUBTYPE_ID)
2688 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2689 || (subtypeId == lastInputMethodSubtypeId)) {
2690 checkedItem = i;
2691 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002692 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002693 }
Ken Wakasa05dbb652011-08-22 15:22:43 +09002694 final TypedArray a = context.obtainStyledAttributes(null,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002695 com.android.internal.R.styleable.DialogPreference,
2696 com.android.internal.R.attr.alertDialogStyle, 0);
2697 mDialogBuilder = new AlertDialog.Builder(context)
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002698 .setOnCancelListener(new OnCancelListener() {
satok42c5a162011-05-26 16:46:14 +09002699 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002700 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002701 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002702 }
2703 })
2704 .setIcon(a.getDrawable(
2705 com.android.internal.R.styleable.DialogPreference_dialogTitle));
2706 a.recycle();
satok01038492012-04-09 21:08:27 +09002707 final LayoutInflater inflater =
2708 (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2709 final View tv = inflater.inflate(
2710 com.android.internal.R.layout.input_method_switch_dialog_title, null);
2711 mDialogBuilder.setCustomTitle(tv);
2712
2713 // Setup layout for a toggle switch of the hardware keyboard
2714 mSwitchingDialogTitleView = tv;
2715 mSwitchingDialogTitleView.findViewById(
2716 com.android.internal.R.id.hard_keyboard_section).setVisibility(
2717 mWindowManagerService.isHardKeyboardAvailable() ?
2718 View.VISIBLE : View.GONE);
2719 final Switch hardKeySwitch = ((Switch)mSwitchingDialogTitleView.findViewById(
2720 com.android.internal.R.id.hard_keyboard_switch));
2721 hardKeySwitch.setChecked(mWindowManagerService.isHardKeyboardEnabled());
2722 hardKeySwitch.setOnCheckedChangeListener(
2723 new OnCheckedChangeListener() {
2724 @Override
2725 public void onCheckedChanged(
2726 CompoundButton buttonView, boolean isChecked) {
2727 mWindowManagerService.setHardKeyboardEnabled(isChecked);
Satoshi Kataoka04dd24d2013-01-18 13:44:37 +09002728 // Ensure that the input method dialog is dismissed when changing
2729 // the hardware keyboard state.
2730 hideInputMethodMenu();
satok01038492012-04-09 21:08:27 +09002731 }
2732 });
satokd87c2592010-09-29 11:52:06 +09002733
Ken Wakasa05dbb652011-08-22 15:22:43 +09002734 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(context,
2735 com.android.internal.R.layout.simple_list_item_2_single_choice, imList,
2736 checkedItem);
2737
2738 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002739 new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002740 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002741 public void onClick(DialogInterface dialog, int which) {
2742 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09002743 if (mIms == null || mIms.length <= which
2744 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002745 return;
2746 }
2747 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09002748 int subtypeId = mSubtypeIds[which];
Satoshi Kataokad2142962012-11-12 18:43:06 +09002749 adapter.mCheckedItem = which;
2750 adapter.notifyDataSetChanged();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002751 hideInputMethodMenu();
2752 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09002753 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09002754 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09002755 subtypeId = NOT_A_SUBTYPE_ID;
2756 }
2757 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002758 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08002759 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002760 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002761 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002762
satokbc81b692011-08-26 16:22:22 +09002763 if (showSubtypes && !isScreenLocked) {
satok82beadf2010-12-27 19:03:06 +09002764 mDialogBuilder.setPositiveButton(
2765 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09002766 new DialogInterface.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002767 @Override
satok7f35c8c2010-10-07 21:13:11 +09002768 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09002769 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09002770 }
2771 });
2772 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002773 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002774 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002775 mSwitchingDialog.getWindow().setType(
2776 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09002777 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
2778 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002779 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002780 mSwitchingDialog.show();
2781 }
2782 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002783
Ken Wakasa05dbb652011-08-22 15:22:43 +09002784 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
2785 private final LayoutInflater mInflater;
2786 private final int mTextViewResourceId;
2787 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09002788 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09002789 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
2790 List<ImeSubtypeListItem> itemsList, int checkedItem) {
2791 super(context, textViewResourceId, itemsList);
2792 mTextViewResourceId = textViewResourceId;
2793 mItemsList = itemsList;
2794 mCheckedItem = checkedItem;
2795 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2796 }
2797
2798 @Override
2799 public View getView(int position, View convertView, ViewGroup parent) {
2800 final View view = convertView != null ? convertView
2801 : mInflater.inflate(mTextViewResourceId, null);
2802 if (position < 0 || position >= mItemsList.size()) return view;
2803 final ImeSubtypeListItem item = mItemsList.get(position);
2804 final CharSequence imeName = item.mImeName;
2805 final CharSequence subtypeName = item.mSubtypeName;
2806 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
2807 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
2808 if (TextUtils.isEmpty(subtypeName)) {
2809 firstTextView.setText(imeName);
2810 secondTextView.setVisibility(View.GONE);
2811 } else {
2812 firstTextView.setText(subtypeName);
2813 secondTextView.setText(imeName);
2814 secondTextView.setVisibility(View.VISIBLE);
2815 }
2816 final RadioButton radioButton =
2817 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
2818 radioButton.setChecked(position == mCheckedItem);
2819 return view;
2820 }
2821 }
2822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002823 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07002824 synchronized (mMethodMap) {
2825 hideInputMethodMenuLocked();
2826 }
2827 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002828
The Android Open Source Project10592532009-03-18 17:39:46 -07002829 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002830 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002831
The Android Open Source Project10592532009-03-18 17:39:46 -07002832 if (mSwitchingDialog != null) {
2833 mSwitchingDialog.dismiss();
2834 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002835 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002836
The Android Open Source Project10592532009-03-18 17:39:46 -07002837 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07002838 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002839 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002840
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002841 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002842
satok42c5a162011-05-26 16:46:14 +09002843 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002844 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002845 // TODO: Make this work even for non-current users?
2846 if (!calledFromValidUser()) {
2847 return false;
2848 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002849 synchronized (mMethodMap) {
2850 if (mContext.checkCallingOrSelfPermission(
2851 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2852 != PackageManager.PERMISSION_GRANTED) {
2853 throw new SecurityException(
2854 "Requires permission "
2855 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2856 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002857
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002858 long ident = Binder.clearCallingIdentity();
2859 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002860 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002861 } finally {
2862 Binder.restoreCallingIdentity(ident);
2863 }
2864 }
2865 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002866
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002867 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
2868 // Make sure this is a valid input method.
2869 InputMethodInfo imm = mMethodMap.get(id);
2870 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09002871 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002872 }
2873
satokd87c2592010-09-29 11:52:06 +09002874 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
2875 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002876
satokd87c2592010-09-29 11:52:06 +09002877 if (enabled) {
2878 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
2879 if (pair.first.equals(id)) {
2880 // We are enabling this input method, but it is already enabled.
2881 // Nothing to do. The previous state was enabled.
2882 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002883 }
2884 }
satokd87c2592010-09-29 11:52:06 +09002885 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
2886 // Previous state was disabled.
2887 return false;
2888 } else {
2889 StringBuilder builder = new StringBuilder();
2890 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2891 builder, enabledInputMethodsList, id)) {
2892 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002893 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002894 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
2895 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
2896 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09002897 }
2898 // Previous state was enabled.
2899 return true;
2900 } else {
2901 // We are disabling the input method but it is already disabled.
2902 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002903 return false;
2904 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002905 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002906 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002907
satok723a27e2010-11-11 14:58:11 +09002908 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
2909 boolean setSubtypeOnly) {
2910 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002911 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09002912
2913 // Set Subtype here
2914 if (imi == null || subtypeId < 0) {
2915 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08002916 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09002917 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09002918 if (subtypeId < imi.getSubtypeCount()) {
2919 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
2920 mSettings.putSelectedSubtype(subtype.hashCode());
2921 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09002922 } else {
2923 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09002924 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002925 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09002926 }
satokab751aa2010-09-14 19:17:36 +09002927 }
satok723a27e2010-11-11 14:58:11 +09002928
satok4c0e7152012-06-20 20:08:44 +09002929 // Workaround.
2930 // ASEC is not ready in the IMMS constructor. Accordingly, forward-locked
2931 // IMEs are not recognized and considered uninstalled.
2932 // Actually, we can't move everything after SystemReady because
2933 // IMMS needs to run in the encryption lock screen. So, we just skip changing
2934 // the default IME here and try cheking the default IME again in systemReady().
2935 // TODO: Do nothing before system ready and implement a separated logic for
2936 // the encryption lock screen.
2937 // TODO: ASEC should be ready before IMMS is instantiated.
2938 if (mSystemReady && !setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09002939 // Set InputMethod here
2940 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
2941 }
2942 }
2943
2944 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
2945 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
2946 int lastSubtypeId = NOT_A_SUBTYPE_ID;
2947 // newDefaultIme is empty when there is no candidate for the selected IME.
2948 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
2949 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
2950 if (subtypeHashCode != null) {
2951 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002952 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok723a27e2010-11-11 14:58:11 +09002953 imi, Integer.valueOf(subtypeHashCode));
2954 } catch (NumberFormatException e) {
2955 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
2956 }
2957 }
2958 }
2959 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09002960 }
2961
satok4e4569d2010-11-19 18:45:53 +09002962 // If there are no selected shortcuts, tries finding the most applicable ones.
2963 private Pair<InputMethodInfo, InputMethodSubtype>
2964 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
2965 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2966 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09002967 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09002968 boolean foundInSystemIME = false;
2969
2970 // Search applicable subtype for each InputMethodInfo
2971 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09002972 final String imiId = imi.getId();
2973 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
2974 continue;
2975 }
satokcd7cd292010-11-20 15:46:23 +09002976 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09002977 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002978 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09002979 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09002980 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002981 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002982 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09002983 }
satokdf31ae62011-01-15 06:19:44 +09002984 // 2. Search by the system locale from enabledSubtypes.
2985 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09002986 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002987 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002988 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002989 }
satoka86f5e42011-09-02 17:12:42 +09002990 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002991 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09002992 final ArrayList<InputMethodSubtype> subtypesForSearch =
2993 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002994 ? InputMethodUtils.getSubtypes(imi)
2995 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09002996 // 4. Search by the current subtype's locale from all subtypes.
2997 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002998 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09002999 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003000 }
3001 // 5. Search by the system locale from all subtypes.
3002 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003003 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003004 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003005 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003006 }
satokcd7cd292010-11-20 15:46:23 +09003007 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003008 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003009 // The current input method is the most applicable IME.
3010 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003011 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003012 break;
satok7599a7f2010-12-22 13:45:23 +09003013 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003014 // The system input method is 2nd applicable IME.
3015 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003016 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003017 if ((imi.getServiceInfo().applicationInfo.flags
3018 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3019 foundInSystemIME = true;
3020 }
satok4e4569d2010-11-19 18:45:53 +09003021 }
3022 }
3023 }
3024 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003025 if (mostApplicableIMI != null) {
3026 Slog.w(TAG, "Most applicable shortcut input method was:"
3027 + mostApplicableIMI.getId());
3028 if (mostApplicableSubtype != null) {
3029 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3030 + "," + mostApplicableSubtype.getMode() + ","
3031 + mostApplicableSubtype.getLocale());
3032 }
3033 }
satok4e4569d2010-11-19 18:45:53 +09003034 }
satokcd7cd292010-11-20 15:46:23 +09003035 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09003036 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09003037 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003038 } else {
3039 return null;
3040 }
3041 }
3042
satokab751aa2010-09-14 19:17:36 +09003043 /**
3044 * @return Return the current subtype of this input method.
3045 */
satok42c5a162011-05-26 16:46:14 +09003046 @Override
satokab751aa2010-09-14 19:17:36 +09003047 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003048 // TODO: Make this work even for non-current users?
3049 if (!calledFromValidUser()) {
3050 return null;
3051 }
3052 synchronized (mMethodMap) {
3053 return getCurrentInputMethodSubtypeLocked();
3054 }
3055 }
3056
3057 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003058 if (mCurMethodId == null) {
3059 return null;
3060 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003061 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003062 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3063 if (imi == null || imi.getSubtypeCount() == 0) {
3064 return null;
satok4e4569d2010-11-19 18:45:53 +09003065 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003066 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003067 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3068 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003069 if (subtypeId == NOT_A_SUBTYPE_ID) {
3070 // If there are no selected subtypes, the framework will try to find
3071 // the most applicable subtype from explicitly or implicitly enabled
3072 // subtypes.
3073 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003074 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003075 // If there is only one explicitly or implicitly enabled subtype,
3076 // just returns it.
3077 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3078 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3079 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003080 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003081 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003082 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003083 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003084 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003085 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3086 true);
satok4e4569d2010-11-19 18:45:53 +09003087 }
satok3ef8b292010-11-23 06:06:29 +09003088 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003089 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003090 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003091 }
3092 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003093 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003094 }
3095
satokf3db1af2010-11-23 13:34:33 +09003096 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
3097 InputMethodSubtype subtype) {
3098 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
3099 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
3100 } else {
3101 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3102 subtypes.add(subtype);
3103 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
3104 }
3105 }
3106
satok4e4569d2010-11-19 18:45:53 +09003107 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003108 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003109 @Override
satok4e4569d2010-11-19 18:45:53 +09003110 public List getShortcutInputMethodsAndSubtypes() {
3111 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09003112 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09003113 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003114 // If there are no selected shortcut subtypes, the framework will try to find
3115 // the most applicable subtype from all subtypes whose mode is
3116 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003117 Pair<InputMethodInfo, InputMethodSubtype> info =
3118 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003119 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003120 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003121 ret.add(info.first);
3122 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003123 }
satok3da92232011-01-11 22:46:30 +09003124 return ret;
satokf3db1af2010-11-23 13:34:33 +09003125 }
satokf3db1af2010-11-23 13:34:33 +09003126 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3127 ret.add(imi);
3128 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3129 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003130 }
3131 }
satokf3db1af2010-11-23 13:34:33 +09003132 return ret;
satok4e4569d2010-11-19 18:45:53 +09003133 }
3134 }
3135
satok42c5a162011-05-26 16:46:14 +09003136 @Override
satokb66d2872010-11-10 01:04:04 +09003137 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003138 // TODO: Make this work even for non-current users?
3139 if (!calledFromValidUser()) {
3140 return false;
3141 }
satokb66d2872010-11-10 01:04:04 +09003142 synchronized (mMethodMap) {
3143 if (subtype != null && mCurMethodId != null) {
3144 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003145 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003146 if (subtypeId != NOT_A_SUBTYPE_ID) {
3147 setInputMethodLocked(mCurMethodId, subtypeId);
3148 return true;
3149 }
3150 }
3151 return false;
3152 }
3153 }
3154
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003155 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003156 private static class InputMethodFileManager {
3157 private static final String SYSTEM_PATH = "system";
3158 private static final String INPUT_METHOD_PATH = "inputmethod";
3159 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3160 private static final String NODE_SUBTYPES = "subtypes";
3161 private static final String NODE_SUBTYPE = "subtype";
3162 private static final String NODE_IMI = "imi";
3163 private static final String ATTR_ID = "id";
3164 private static final String ATTR_LABEL = "label";
3165 private static final String ATTR_ICON = "icon";
3166 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3167 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3168 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3169 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3170 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3171 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003172 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
satoke7c6998e2011-06-03 17:57:59 +09003173 new HashMap<String, List<InputMethodSubtype>>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003174 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003175 if (methodMap == null) {
3176 throw new NullPointerException("methodMap is null");
3177 }
3178 mMethodMap = methodMap;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003179 final File systemDir = userId == UserHandle.USER_OWNER
3180 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3181 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003182 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3183 if (!inputMethodDir.mkdirs()) {
3184 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3185 }
3186 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3187 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3188 if (!subtypeFile.exists()) {
3189 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003190 writeAdditionalInputMethodSubtypes(
3191 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003192 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003193 readAdditionalInputMethodSubtypes(
3194 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003195 }
3196 }
3197
3198 private void deleteAllInputMethodSubtypes(String imiId) {
3199 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003200 mAdditionalSubtypesMap.remove(imiId);
3201 writeAdditionalInputMethodSubtypes(
3202 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003203 }
3204 }
3205
3206 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003207 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003208 synchronized (mMethodMap) {
3209 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3210 final int N = additionalSubtypes.length;
3211 for (int i = 0; i < N; ++i) {
3212 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003213 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003214 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003215 } else {
3216 Slog.w(TAG, "Duplicated subtype definition found: "
3217 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003218 }
3219 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003220 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3221 writeAdditionalInputMethodSubtypes(
3222 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003223 }
3224 }
3225
3226 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3227 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003228 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003229 }
3230 }
3231
3232 private static void writeAdditionalInputMethodSubtypes(
3233 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3234 HashMap<String, InputMethodInfo> methodMap) {
3235 // Safety net for the case that this function is called before methodMap is set.
3236 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3237 FileOutputStream fos = null;
3238 try {
3239 fos = subtypesFile.startWrite();
3240 final XmlSerializer out = new FastXmlSerializer();
3241 out.setOutput(fos, "utf-8");
3242 out.startDocument(null, true);
3243 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3244 out.startTag(null, NODE_SUBTYPES);
3245 for (String imiId : allSubtypes.keySet()) {
3246 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3247 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3248 continue;
3249 }
3250 out.startTag(null, NODE_IMI);
3251 out.attribute(null, ATTR_ID, imiId);
3252 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3253 final int N = subtypesList.size();
3254 for (int i = 0; i < N; ++i) {
3255 final InputMethodSubtype subtype = subtypesList.get(i);
3256 out.startTag(null, NODE_SUBTYPE);
3257 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3258 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3259 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3260 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3261 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3262 out.attribute(null, ATTR_IS_AUXILIARY,
3263 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3264 out.endTag(null, NODE_SUBTYPE);
3265 }
3266 out.endTag(null, NODE_IMI);
3267 }
3268 out.endTag(null, NODE_SUBTYPES);
3269 out.endDocument();
3270 subtypesFile.finishWrite(fos);
3271 } catch (java.io.IOException e) {
3272 Slog.w(TAG, "Error writing subtypes", e);
3273 if (fos != null) {
3274 subtypesFile.failWrite(fos);
3275 }
3276 }
3277 }
3278
3279 private static void readAdditionalInputMethodSubtypes(
3280 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3281 if (allSubtypes == null || subtypesFile == null) return;
3282 allSubtypes.clear();
3283 FileInputStream fis = null;
3284 try {
3285 fis = subtypesFile.openRead();
3286 final XmlPullParser parser = Xml.newPullParser();
3287 parser.setInput(fis, null);
3288 int type = parser.getEventType();
3289 // Skip parsing until START_TAG
3290 while ((type = parser.next()) != XmlPullParser.START_TAG
3291 && type != XmlPullParser.END_DOCUMENT) {}
3292 String firstNodeName = parser.getName();
3293 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3294 throw new XmlPullParserException("Xml doesn't start with subtypes");
3295 }
3296 final int depth =parser.getDepth();
3297 String currentImiId = null;
3298 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3299 while (((type = parser.next()) != XmlPullParser.END_TAG
3300 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3301 if (type != XmlPullParser.START_TAG)
3302 continue;
3303 final String nodeName = parser.getName();
3304 if (NODE_IMI.equals(nodeName)) {
3305 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3306 if (TextUtils.isEmpty(currentImiId)) {
3307 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3308 continue;
3309 }
3310 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
3311 allSubtypes.put(currentImiId, tempSubtypesArray);
3312 } else if (NODE_SUBTYPE.equals(nodeName)) {
3313 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3314 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3315 continue;
3316 }
3317 final int icon = Integer.valueOf(
3318 parser.getAttributeValue(null, ATTR_ICON));
3319 final int label = Integer.valueOf(
3320 parser.getAttributeValue(null, ATTR_LABEL));
3321 final String imeSubtypeLocale =
3322 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3323 final String imeSubtypeMode =
3324 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3325 final String imeSubtypeExtraValue =
3326 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003327 final boolean isAuxiliary = "1".equals(String.valueOf(
3328 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
satoke7c6998e2011-06-03 17:57:59 +09003329 final InputMethodSubtype subtype =
3330 new InputMethodSubtype(label, icon, imeSubtypeLocale,
3331 imeSubtypeMode, imeSubtypeExtraValue, isAuxiliary);
3332 tempSubtypesArray.add(subtype);
3333 }
3334 }
3335 } catch (XmlPullParserException e) {
3336 Slog.w(TAG, "Error reading subtypes: " + e);
3337 return;
3338 } catch (java.io.IOException e) {
3339 Slog.w(TAG, "Error reading subtypes: " + e);
3340 return;
3341 } catch (NumberFormatException e) {
3342 Slog.w(TAG, "Error reading subtypes: " + e);
3343 return;
3344 } finally {
3345 if (fis != null) {
3346 try {
3347 fis.close();
3348 } catch (java.io.IOException e1) {
3349 Slog.w(TAG, "Failed to close.");
3350 }
3351 }
3352 }
3353 }
3354 }
3355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003356 @Override
3357 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3358 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3359 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003361 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3362 + Binder.getCallingPid()
3363 + ", uid=" + Binder.getCallingUid());
3364 return;
3365 }
3366
3367 IInputMethod method;
3368 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003370 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003371
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003372 synchronized (mMethodMap) {
3373 p.println("Current Input Method Manager state:");
3374 int N = mMethodList.size();
3375 p.println(" Input Methods:");
3376 for (int i=0; i<N; i++) {
3377 InputMethodInfo info = mMethodList.get(i);
3378 p.println(" InputMethod #" + i + ":");
3379 info.dump(p, " ");
3380 }
3381 p.println(" Clients:");
3382 for (ClientState ci : mClients.values()) {
3383 p.println(" Client " + ci + ":");
3384 p.println(" client=" + ci.client);
3385 p.println(" inputContext=" + ci.inputContext);
3386 p.println(" sessionRequested=" + ci.sessionRequested);
3387 p.println(" curSession=" + ci.curSession);
3388 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003389 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003390 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003391 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3392 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003393 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3394 + " mBoundToMethod=" + mBoundToMethod);
3395 p.println(" mCurToken=" + mCurToken);
3396 p.println(" mCurIntent=" + mCurIntent);
3397 method = mCurMethod;
3398 p.println(" mCurMethod=" + mCurMethod);
3399 p.println(" mEnabledSession=" + mEnabledSession);
3400 p.println(" mShowRequested=" + mShowRequested
3401 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3402 + " mShowForced=" + mShowForced
3403 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07003404 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003405 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003406
Jeff Brownb88102f2010-09-08 11:49:43 -07003407 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003408 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003409 pw.flush();
3410 try {
3411 client.client.asBinder().dump(fd, args);
3412 } catch (RemoteException e) {
3413 p.println("Input method client dead: " + e);
3414 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003415 } else {
3416 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003417 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003418
Jeff Brownb88102f2010-09-08 11:49:43 -07003419 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003420 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003421 pw.flush();
3422 try {
3423 method.asBinder().dump(fd, args);
3424 } catch (RemoteException e) {
3425 p.println("Input method service dead: " + e);
3426 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003427 } else {
3428 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003429 }
3430 }
3431}