blob: 43f131dfab44e5e5305e12b57b9a2c0dd1b6bceb [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;
Dianne Hackborna924dc0d2011-02-17 14:22:17 -080033import com.android.server.EventLogTags;
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 Yamasanie861ec12010-03-24 21:39:27 -070062import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import android.content.res.Resources;
64import android.content.res.TypedArray;
65import android.database.ContentObserver;
Joe Onorato857fd9b2011-01-27 15:08:35 -080066import android.inputmethodservice.InputMethodService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.os.Binder;
satoke7c6998e2011-06-03 17:57:59 +090068import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.os.Handler;
70import android.os.IBinder;
71import android.os.IInterface;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090072import android.os.IRemoteCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073import android.os.Message;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090074import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.os.Parcel;
76import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080077import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078import android.os.ServiceManager;
79import android.os.SystemClock;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090080import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081import android.provider.Settings;
82import android.text.TextUtils;
satokf9f01002011-05-19 21:31:50 +090083import android.text.style.SuggestionSpan;
Dianne Hackborn39606a02012-07-31 17:54:35 -070084import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +090086import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +090087import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088import android.util.PrintWriterPrinter;
89import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +090090import android.util.Slog;
91import android.util.Xml;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092import android.view.IWindowManager;
Jeff Brownc28867a2013-03-26 15:42:39 -070093import android.view.InputChannel;
Ken Wakasa05dbb652011-08-22 15:22:43 +090094import android.view.LayoutInflater;
95import android.view.View;
96import android.view.ViewGroup;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097import android.view.WindowManager;
satokab751aa2010-09-14 19:17:36 +090098import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099import android.view.inputmethod.InputBinding;
100import android.view.inputmethod.InputMethod;
101import android.view.inputmethod.InputMethodInfo;
102import android.view.inputmethod.InputMethodManager;
satokab751aa2010-09-14 19:17:36 +0900103import android.view.inputmethod.InputMethodSubtype;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900104import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +0900105import android.widget.CompoundButton;
106import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900107import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +0900108import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900109import android.widget.TextView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110
satoke7c6998e2011-06-03 17:57:59 +0900111import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900113import java.io.FileInputStream;
114import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115import java.io.IOException;
116import java.io.PrintWriter;
117import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900118import java.util.Collections;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119import java.util.HashMap;
120import java.util.List;
satok5b927c432012-05-01 20:09:34 +0900121import java.util.Locale;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122
123/**
124 * This class provides a system service that manages input methods.
125 */
126public class InputMethodManagerService extends IInputMethodManager.Stub
127 implements ServiceConnection, Handler.Callback {
128 static final boolean DEBUG = false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700129 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130
131 static final int MSG_SHOW_IM_PICKER = 1;
satokab751aa2010-09-14 19:17:36 +0900132 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 2;
satok47a44912010-10-06 16:03:58 +0900133 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 3;
satok217f5482010-12-15 05:19:19 +0900134 static final int MSG_SHOW_IM_CONFIG = 4;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 static final int MSG_UNBIND_INPUT = 1000;
137 static final int MSG_BIND_INPUT = 1010;
138 static final int MSG_SHOW_SOFT_INPUT = 1020;
139 static final int MSG_HIDE_SOFT_INPUT = 1030;
140 static final int MSG_ATTACH_TOKEN = 1040;
141 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 static final int MSG_START_INPUT = 2000;
144 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800145
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 static final int MSG_UNBIND_METHOD = 3000;
147 static final int MSG_BIND_METHOD = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700148 static final int MSG_SET_ACTIVE = 3020;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800149
satok01038492012-04-09 21:08:27 +0900150 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
151
Satoshi Kataokabcacc322013-10-21 17:57:27 -0700152 static final long TIME_TO_RECONNECT = 3 * 1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800153
satokf9f01002011-05-19 21:31:50 +0900154 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
155
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900156 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
satokb6359412011-06-28 17:47:41 +0900157 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900158
satok4e4569d2010-11-19 18:45:53 +0900159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800161 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900163 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 final IWindowManager mIWindowManager;
166 final HandlerCaller mCaller;
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700167 final boolean mHasFeature;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900168 private InputMethodFileManager mFileManager;
satok01038492012-04-09 21:08:27 +0900169 private final HardKeyboardListener mHardKeyboardListener;
170 private final WindowManagerService mWindowManagerService;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800171
Jeff Brownc28867a2013-03-26 15:42:39 -0700172 final InputBindResult mNoBinding = new InputBindResult(null, null, null, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 // All known input methods. mMethodMap also serves as the global
175 // lock for this class.
satokd87c2592010-09-29 11:52:06 +0900176 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<InputMethodInfo>();
177 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<String, InputMethodInfo>();
satokf9f01002011-05-19 21:31:50 +0900178 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
179 new LruCache<SuggestionSpan, InputMethodInfo>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Satoshi Kataokad787f692013-10-26 04:44:21 +0900180 private final InputMethodSubtypeSwitchingController mSwitchingController;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800181
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700182 // Used to bring IME service up to visible adjustment while it is being shown.
183 final ServiceConnection mVisibleConnection = new ServiceConnection() {
184 @Override public void onServiceConnected(ComponentName name, IBinder service) {
185 }
186
187 @Override public void onServiceDisconnected(ComponentName name) {
188 }
189 };
190 boolean mVisibleBound = false;
191
satok7cfc0ed2011-06-20 21:29:36 +0900192 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700193 private NotificationManager mNotificationManager;
194 private KeyguardManager mKeyguardManager;
195 private StatusBarManagerService mStatusBar;
196 private Notification mImeSwitcherNotification;
197 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900198 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900199 private boolean mNotificationShown;
satok0a1bcf42012-05-16 19:26:31 +0900200 private final boolean mImeSelectedOnBoot;
satok7cfc0ed2011-06-20 21:29:36 +0900201
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 class SessionState {
203 final ClientState client;
204 final IInputMethod method;
Jeff Brownc28867a2013-03-26 15:42:39 -0700205
206 IInputMethodSession session;
207 InputChannel channel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 @Override
210 public String toString() {
211 return "SessionState{uid " + client.uid + " pid " + client.pid
212 + " method " + Integer.toHexString(
213 System.identityHashCode(method))
214 + " session " + Integer.toHexString(
215 System.identityHashCode(session))
Jeff Brownc28867a2013-03-26 15:42:39 -0700216 + " channel " + channel
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 + "}";
218 }
219
220 SessionState(ClientState _client, IInputMethod _method,
Jeff Brownc28867a2013-03-26 15:42:39 -0700221 IInputMethodSession _session, InputChannel _channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 client = _client;
223 method = _method;
224 session = _session;
Jeff Brownc28867a2013-03-26 15:42:39 -0700225 channel = _channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 }
227 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800228
Jeff Brownc28867a2013-03-26 15:42:39 -0700229 static final class ClientState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 final IInputMethodClient client;
231 final IInputContext inputContext;
232 final int uid;
233 final int pid;
234 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800235
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 boolean sessionRequested;
237 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800238
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 @Override
240 public String toString() {
241 return "ClientState{" + Integer.toHexString(
242 System.identityHashCode(this)) + " uid " + uid
243 + " pid " + pid + "}";
244 }
245
246 ClientState(IInputMethodClient _client, IInputContext _inputContext,
247 int _uid, int _pid) {
248 client = _client;
249 inputContext = _inputContext;
250 uid = _uid;
251 pid = _pid;
252 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
253 }
254 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 final HashMap<IBinder, ClientState> mClients
257 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700260 * Set once the system is ready to run third party code.
261 */
262 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800263
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700264 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 * Id of the currently selected input method.
266 */
267 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800268
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 /**
270 * The current binding sequence number, incremented every time there is
271 * a new bind performed.
272 */
273 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800274
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 /**
276 * The client that is currently bound to an input method.
277 */
278 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700281 * The last window token that gained focus.
282 */
283 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800284
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700285 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286 * The input context last provided by the current client.
287 */
288 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 /**
291 * The attributes last provided by the current client.
292 */
293 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 /**
296 * The input method ID of the input method service that we are currently
297 * connected to or in the process of connecting to.
298 */
299 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800300
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301 /**
satokab751aa2010-09-14 19:17:36 +0900302 * The current subtype of the current input method.
303 */
304 private InputMethodSubtype mCurrentSubtype;
305
satok4e4569d2010-11-19 18:45:53 +0900306 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900307 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
308 mShortcutInputMethodsAndSubtypes =
309 new HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>();
satokab751aa2010-09-14 19:17:36 +0900310
311 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 * Set to true if our ServiceConnection is currently actively bound to
313 * a service (whether or not we have gotten its IBinder back yet).
314 */
315 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800316
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 /**
318 * Set if the client has asked for the input method to be shown.
319 */
320 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322 /**
323 * Set if we were explicitly told to show the input method.
324 */
325 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 /**
328 * Set if we were forced to be shown.
329 */
330 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332 /**
333 * Set if we last told the input method to show itself.
334 */
335 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 /**
338 * The Intent used to connect to the current input method.
339 */
340 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800341
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 /**
343 * The token we have made for the currently active input method, to
344 * identify it in the future.
345 */
346 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800347
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 /**
349 * If non-null, this is the input method service we are currently connected
350 * to.
351 */
352 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 /**
355 * Time that we last initiated a bind to the input method, to determine
356 * if we should try to disconnect and reconnect to it.
357 */
358 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 /**
361 * Have we called mCurMethod.bindInput()?
362 */
363 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800364
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365 /**
366 * Currently enabled session. Only touched by service thread, not
367 * protected by a lock.
368 */
369 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 /**
372 * True if the screen is on. The value is true initially.
373 */
374 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800375
Joe Onorato857fd9b2011-01-27 15:08:35 -0800376 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
377 int mImeWindowVis;
378
Ken Wakasa05dbb652011-08-22 15:22:43 +0900379 private AlertDialog.Builder mDialogBuilder;
380 private AlertDialog mSwitchingDialog;
satok01038492012-04-09 21:08:27 +0900381 private View mSwitchingDialogTitleView;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900382 private InputMethodInfo[] mIms;
383 private int[] mSubtypeIds;
satok5b927c432012-05-01 20:09:34 +0900384 private Locale mLastSystemLocale;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900385 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
386 private final IPackageManager mIPackageManager;
John Spurlockbc7b6fc2012-11-14 08:51:07 -0500387 private boolean mInputBoundToKeyguard;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800388
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389 class SettingsObserver extends ContentObserver {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800390 String mLastEnabled = "";
391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 SettingsObserver(Handler handler) {
393 super(handler);
394 ContentResolver resolver = mContext.getContentResolver();
395 resolver.registerContentObserver(Settings.Secure.getUriFor(
396 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
satokab751aa2010-09-14 19:17:36 +0900397 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokb6109bb2011-02-03 22:24:54 +0900398 Settings.Secure.ENABLED_INPUT_METHODS), false, this);
399 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokab751aa2010-09-14 19:17:36 +0900400 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800402
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 @Override public void onChange(boolean selfChange) {
404 synchronized (mMethodMap) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800405 boolean enabledChanged = false;
406 String newEnabled = mSettings.getEnabledInputMethodsStr();
407 if (!mLastEnabled.equals(newEnabled)) {
408 mLastEnabled = newEnabled;
409 enabledChanged = true;
410 }
411 updateFromSettingsLocked(enabledChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 }
413 }
414 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800415
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900416 class ImmsBroadcastReceiver extends android.content.BroadcastReceiver {
417 private void updateActive() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418 // Inform the current client of the change in active status
Dianne Hackborna6e41342012-05-22 16:30:34 -0700419 if (mCurClient != null && mCurClient.client != null) {
420 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
421 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 }
423 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900424
425 @Override
426 public void onReceive(Context context, Intent intent) {
427 final String action = intent.getAction();
428 if (Intent.ACTION_SCREEN_ON.equals(action)) {
429 mScreenOn = true;
430 refreshImeWindowVisibilityLocked();
431 updateActive();
432 return;
433 } else if (Intent.ACTION_SCREEN_OFF.equals(action)) {
434 mScreenOn = false;
435 setImeWindowVisibilityStatusHiddenLocked();
436 updateActive();
437 return;
438 } else if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
439 hideInputMethodMenu();
440 // No need to updateActive
441 return;
442 } else {
443 Slog.w(TAG, "Unexpected intent " + intent);
444 }
445 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800447
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800448 class MyPackageMonitor extends PackageMonitor {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900449 private boolean isChangingPackagesOfCurrentUser() {
450 final int userId = getChangingUserId();
451 final boolean retval = userId == mSettings.getCurrentUserId();
452 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900453 if (!retval) {
454 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
455 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900456 }
457 return retval;
458 }
459
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800461 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900462 if (!isChangingPackagesOfCurrentUser()) {
463 return false;
464 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900466 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 final int N = mMethodList.size();
468 if (curInputMethodId != null) {
469 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800470 InputMethodInfo imi = mMethodList.get(i);
471 if (imi.getId().equals(curInputMethodId)) {
472 for (String pkg : packages) {
473 if (imi.getPackageName().equals(pkg)) {
474 if (!doit) {
475 return true;
476 }
satok723a27e2010-11-11 14:58:11 +0900477 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800478 chooseNewDefaultIMELocked();
479 return true;
480 }
481 }
482 }
483 }
484 }
485 }
486 return false;
487 }
488
489 @Override
490 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900491 if (!isChangingPackagesOfCurrentUser()) {
492 return;
493 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800494 synchronized (mMethodMap) {
495 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900496 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800497 final int N = mMethodList.size();
498 if (curInputMethodId != null) {
499 for (int i=0; i<N; i++) {
500 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900501 final String imiId = imi.getId();
502 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800503 curIm = imi;
504 }
satoke7c6998e2011-06-03 17:57:59 +0900505
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800506 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900507 if (isPackageModified(imi.getPackageName())) {
508 mFileManager.deleteAllInputMethodSubtypes(imiId);
509 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800510 if (change == PACKAGE_TEMPORARY_CHANGE
511 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800512 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800513 + imi.getComponent());
514 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 }
516 }
517 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800518
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900519 buildInputMethodListLocked(
520 mMethodList, mMethodMap, false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800523
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800524 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800525 int change = isPackageDisappearing(curIm.getPackageName());
526 if (change == PACKAGE_TEMPORARY_CHANGE
527 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800528 ServiceInfo si = null;
529 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900530 si = mIPackageManager.getServiceInfo(
531 curIm.getComponent(), 0, mSettings.getCurrentUserId());
532 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800533 }
534 if (si == null) {
535 // Uh oh, current input method is no longer around!
536 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800537 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
satok15452a42011-10-28 17:58:28 +0900538 setImeWindowVisibilityStatusHiddenLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800539 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800540 changed = true;
541 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800542 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900543 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800544 }
545 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800546 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800547 }
satokab751aa2010-09-14 19:17:36 +0900548
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800549 if (curIm == null) {
550 // We currently don't have a default input method... is
551 // one now available?
552 changed = chooseNewDefaultIMELocked();
553 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800554
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800555 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800556 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557 }
558 }
559 }
560 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800561
Jeff Brownc28867a2013-03-26 15:42:39 -0700562 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900563 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -0700564 private final IInputMethod mMethod;
565 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800566
Jeff Brownc28867a2013-03-26 15:42:39 -0700567 MethodCallback(InputMethodManagerService imms, IInputMethod method,
568 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900569 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -0700570 mMethod = method;
571 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800572 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800573
satoke7c6998e2011-06-03 17:57:59 +0900574 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -0700575 public void sessionCreated(IInputMethodSession session) {
576 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577 }
578 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800579
satok01038492012-04-09 21:08:27 +0900580 private class HardKeyboardListener
581 implements WindowManagerService.OnHardKeyboardStatusChangeListener {
582 @Override
583 public void onHardKeyboardStatusChange(boolean available, boolean enabled) {
584 mHandler.sendMessage(mHandler.obtainMessage(
585 MSG_HARD_KEYBOARD_SWITCH_CHANGED, available ? 1 : 0, enabled ? 1 : 0));
586 }
587
588 public void handleHardKeyboardStatusChange(boolean available, boolean enabled) {
589 if (DEBUG) {
590 Slog.w(TAG, "HardKeyboardStatusChanged: available = " + available + ", enabled = "
591 + enabled);
592 }
593 synchronized(mMethodMap) {
594 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
595 && mSwitchingDialog.isShowing()) {
596 mSwitchingDialogTitleView.findViewById(
597 com.android.internal.R.id.hard_keyboard_section).setVisibility(
598 available ? View.VISIBLE : View.GONE);
599 }
600 }
601 }
602 }
603
604 public InputMethodManagerService(Context context, WindowManagerService windowManager) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900605 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800607 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800608 mHandler = new Handler(this);
609 mIWindowManager = IWindowManager.Stub.asInterface(
610 ServiceManager.getService(Context.WINDOW_SERVICE));
Mita Yuned218c72012-12-06 17:18:25 -0800611 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900612 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800613 public void executeMessage(Message msg) {
614 handleMessage(msg);
615 }
Mita Yuned218c72012-12-06 17:18:25 -0800616 }, true /*asyncHandler*/);
satok01038492012-04-09 21:08:27 +0900617 mWindowManagerService = windowManager;
618 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700619 mHasFeature = context.getPackageManager().hasSystemFeature(
620 PackageManager.FEATURE_INPUT_METHODS);
satok7cfc0ed2011-06-20 21:29:36 +0900621
satok7cfc0ed2011-06-20 21:29:36 +0900622 mImeSwitcherNotification = new Notification();
623 mImeSwitcherNotification.icon = com.android.internal.R.drawable.ic_notification_ime_default;
624 mImeSwitcherNotification.when = 0;
625 mImeSwitcherNotification.flags = Notification.FLAG_ONGOING_EVENT;
626 mImeSwitcherNotification.tickerText = null;
627 mImeSwitcherNotification.defaults = 0; // please be quiet
628 mImeSwitcherNotification.sound = null;
629 mImeSwitcherNotification.vibrate = null;
Daniel Sandler590d5152012-06-14 16:10:13 -0400630
631 // Tag this notification specially so SystemUI knows it's important
632 mImeSwitcherNotification.kind = new String[] { "android.system.imeswitcher" };
633
satok7cfc0ed2011-06-20 21:29:36 +0900634 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900635 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900636
637 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900638
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900639 final IntentFilter broadcastFilter = new IntentFilter();
640 broadcastFilter.addAction(Intent.ACTION_SCREEN_ON);
641 broadcastFilter.addAction(Intent.ACTION_SCREEN_OFF);
642 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
643 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800644
satok7cfc0ed2011-06-20 21:29:36 +0900645 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900646 int userId = 0;
647 try {
648 ActivityManagerNative.getDefault().registerUserSwitchObserver(
649 new IUserSwitchObserver.Stub() {
650 @Override
651 public void onUserSwitching(int newUserId, IRemoteCallback reply) {
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900652 synchronized(mMethodMap) {
653 switchUserLocked(newUserId);
654 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900655 if (reply != null) {
656 try {
657 reply.sendResult(null);
658 } catch (RemoteException e) {
659 }
660 }
661 }
662
663 @Override
664 public void onUserSwitchComplete(int newUserId) throws RemoteException {
665 }
666 });
667 userId = ActivityManagerNative.getDefault().getCurrentUser().id;
668 } catch (RemoteException e) {
669 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
670 }
satok81f8b7c2012-12-04 20:42:56 +0900671 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
satok913a8922010-08-26 21:53:41 +0900672
satokd87c2592010-09-29 11:52:06 +0900673 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900674 mSettings = new InputMethodSettings(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900675 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId);
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900676 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Satoshi Kataokad787f692013-10-26 04:44:21 +0900677 mSwitchingController = new InputMethodSubtypeSwitchingController(mSettings);
678 mSwitchingController.resetCircularListLocked(context);
satok0a1bcf42012-05-16 19:26:31 +0900679
680 // Just checking if defaultImiId is empty or not
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900681 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900682 if (DEBUG) {
683 Slog.d(TAG, "Initial default ime = " + defaultImiId);
684 }
satok0a1bcf42012-05-16 19:26:31 +0900685 mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
686
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900687 buildInputMethodListLocked(mMethodList, mMethodMap,
688 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satokd87c2592010-09-29 11:52:06 +0900689 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800690
satok0a1bcf42012-05-16 19:26:31 +0900691 if (!mImeSelectedOnBoot) {
692 Slog.w(TAG, "No IME selected. Choose the most applicable IME.");
satok5b927c432012-05-01 20:09:34 +0900693 resetDefaultImeLocked(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800694 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800695
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800696 mSettingsObserver = new SettingsObserver(mHandler);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800697 updateFromSettingsLocked(true);
satok5b927c432012-05-01 20:09:34 +0900698
699 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME
700 // according to the new system locale.
701 final IntentFilter filter = new IntentFilter();
702 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
703 mContext.registerReceiver(
704 new BroadcastReceiver() {
705 @Override
706 public void onReceive(Context context, Intent intent) {
707 synchronized(mMethodMap) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900708 resetStateIfCurrentLocaleChangedLocked();
satok5b927c432012-05-01 20:09:34 +0900709 }
710 }
711 }, filter);
712 }
713
satok5b927c432012-05-01 20:09:34 +0900714 private void resetDefaultImeLocked(Context context) {
715 // Do not reset the default (current) IME when it is a 3rd-party IME
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900716 if (mCurMethodId != null
717 && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +0900718 return;
719 }
720
721 InputMethodInfo defIm = null;
722 for (InputMethodInfo imi : mMethodList) {
723 if (defIm == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900724 if (InputMethodUtils.isValidSystemDefaultIme(
725 mSystemReady, imi, context)) {
satok5b927c432012-05-01 20:09:34 +0900726 defIm = imi;
727 Slog.i(TAG, "Selected default: " + imi.getId());
728 }
729 }
730 }
731 if (defIm == null && mMethodList.size() > 0) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900732 defIm = InputMethodUtils.getMostApplicableDefaultIME(
733 mSettings.getEnabledInputMethodListLocked());
satok5b927c432012-05-01 20:09:34 +0900734 Slog.i(TAG, "No default found, using " + defIm.getId());
735 }
736 if (defIm != null) {
737 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
738 }
739 }
740
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900741 private void resetAllInternalStateLocked(final boolean updateOnlyWhenLocaleChanged,
742 final boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900743 if (!mSystemReady) {
744 // not system ready
745 return;
746 }
747 final Locale newLocale = mRes.getConfiguration().locale;
748 if (!updateOnlyWhenLocaleChanged
749 || (newLocale != null && !newLocale.equals(mLastSystemLocale))) {
750 if (!updateOnlyWhenLocaleChanged) {
751 hideCurrentInputLocked(0, null);
752 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -0700753 unbindCurrentMethodLocked(true, false);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900754 }
755 if (DEBUG) {
756 Slog.i(TAG, "Locale has been changed to " + newLocale);
757 }
Satoshi Kataokad787f692013-10-26 04:44:21 +0900758 // CircularList should be reset when the locale is changed.
759 mSwitchingController.resetCircularListLocked(mContext);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900760 buildInputMethodListLocked(mMethodList, mMethodMap, resetDefaultEnabledIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900761 if (!updateOnlyWhenLocaleChanged) {
762 final String selectedImiId = mSettings.getSelectedInputMethod();
763 if (TextUtils.isEmpty(selectedImiId)) {
764 // This is the first time of the user switch and
765 // set the current ime to the proper one.
766 resetDefaultImeLocked(mContext);
767 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +0900768 } else {
769 // If the locale is changed, needs to reset the default ime
770 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900771 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800772 updateFromSettingsLocked(true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900773 mLastSystemLocale = newLocale;
774 if (!updateOnlyWhenLocaleChanged) {
775 try {
776 startInputInnerLocked();
777 } catch (RuntimeException e) {
778 Slog.w(TAG, "Unexpected exception", e);
779 }
780 }
781 }
782 }
783
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900784 private void resetStateIfCurrentLocaleChangedLocked() {
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900785 resetAllInternalStateLocked(true /* updateOnlyWhenLocaleChanged */,
786 true /* resetDefaultImeLocked */);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900787 }
788
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900789 private void switchUserLocked(int newUserId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900790 mSettings.setCurrentUserId(newUserId);
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900791 // InputMethodFileManager should be reset when the user is changed
792 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900793 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +0900794 // For secondary users, the list of enabled IMEs may not have been updated since the
795 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
796 // not be empty even if the IME has been uninstalled by the primary user.
797 // Even in such cases, IMMS works fine because it will find the most applicable
798 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900799 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900800 if (DEBUG) {
801 Slog.d(TAG, "Switch user: " + newUserId + " current ime = " + defaultImiId);
802 }
803 resetAllInternalStateLocked(false /* updateOnlyWhenLocaleChanged */,
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900804 initialUserSwitch /* needsToResetDefaultIme */);
805 if (initialUserSwitch) {
806 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mContext.getPackageManager(),
807 mSettings.getEnabledInputMethodListLocked());
808 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900809 }
810
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800811 @Override
812 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
813 throws RemoteException {
814 try {
815 return super.onTransact(code, data, reply, flags);
816 } catch (RuntimeException e) {
817 // The input method manager only throws security exceptions, so let's
818 // log all others.
819 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -0700820 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800821 }
822 throw e;
823 }
824 }
825
Svetoslav Ganova0027152013-06-25 14:59:53 -0700826 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700827 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900828 if (DEBUG) {
829 Slog.d(TAG, "--- systemReady");
830 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700831 if (!mSystemReady) {
832 mSystemReady = true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900833 mKeyguardManager =
834 (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700835 mNotificationManager = (NotificationManager)
836 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
837 mStatusBar = statusBar;
838 statusBar.setIconVisibility("ime", false);
839 updateImeWindowStatusLocked();
satokb858c732011-07-22 19:54:34 +0900840 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
841 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +0900842 if (mShowOngoingImeSwitcherForPhones) {
843 mWindowManagerService.setOnHardKeyboardStatusChangeListener(
844 mHardKeyboardListener);
845 }
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900846 buildInputMethodListLocked(mMethodList, mMethodMap,
847 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satok0a1bcf42012-05-16 19:26:31 +0900848 if (!mImeSelectedOnBoot) {
849 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900850 resetStateIfCurrentLocaleChangedLocked();
851 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(
852 mContext.getPackageManager(),
853 mSettings.getEnabledInputMethodListLocked());
satok0a1bcf42012-05-16 19:26:31 +0900854 }
855 mLastSystemLocale = mRes.getConfiguration().locale;
Dianne Hackborncc278702009-09-02 23:07:23 -0700856 try {
857 startInputInnerLocked();
858 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800859 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700860 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700861 }
862 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800863 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800864
satok15452a42011-10-28 17:58:28 +0900865 private void setImeWindowVisibilityStatusHiddenLocked() {
866 mImeWindowVis = 0;
867 updateImeWindowStatusLocked();
868 }
869
satok3afd6c02011-11-18 08:38:19 +0900870 private void refreshImeWindowVisibilityLocked() {
871 final Configuration conf = mRes.getConfiguration();
872 final boolean haveHardKeyboard = conf.keyboard
873 != Configuration.KEYBOARD_NOKEYS;
874 final boolean hardKeyShown = haveHardKeyboard
875 && conf.hardKeyboardHidden
876 != Configuration.HARDKEYBOARDHIDDEN_YES;
John Spurlockbc7b6fc2012-11-14 08:51:07 -0500877 final boolean isScreenLocked =
878 mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
879 final boolean isScreenSecurelyLocked =
880 isScreenLocked && mKeyguardManager.isKeyguardSecure();
881 final boolean inputShown = mInputShown && (!isScreenLocked || mInputBoundToKeyguard);
John Spurlock36439b42013-10-23 16:36:47 -0400882 final boolean inputActive = !isScreenSecurelyLocked && (inputShown || hardKeyShown);
883 // We assume the softkeyboard is shown when the input is active as long as the
884 // hard keyboard is not shown.
885 final boolean inputVisible = inputActive && !hardKeyShown;
886 mImeWindowVis = (inputActive ? InputMethodService.IME_ACTIVE : 0)
887 | (inputVisible ? InputMethodService.IME_VISIBLE : 0);
satok3afd6c02011-11-18 08:38:19 +0900888 updateImeWindowStatusLocked();
889 }
890
satok15452a42011-10-28 17:58:28 +0900891 private void updateImeWindowStatusLocked() {
satokdbf29502011-08-25 15:28:23 +0900892 setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700893 }
894
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900895 // ---------------------------------------------------------------------------------------
896 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
897 // 1) it comes from the system process
898 // 2) the calling process' user id is identical to the current user id IMMS thinks.
899 private boolean calledFromValidUser() {
900 final int uid = Binder.getCallingUid();
901 final int userId = UserHandle.getUserId(uid);
902 if (DEBUG) {
903 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
904 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
905 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +0900906 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
907 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900908 }
909 if (uid == Process.SYSTEM_UID || userId == mSettings.getCurrentUserId()) {
910 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900911 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +0900912
913 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
914 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
915 // must not manage background users' states in any functions.
916 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
917 // by a token.
918 if (mContext.checkCallingOrSelfPermission(
919 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
920 == PackageManager.PERMISSION_GRANTED) {
921 if (DEBUG) {
922 Slog.d(TAG, "--- Access granted because the calling process has "
923 + "the INTERACT_ACROSS_USERS_FULL permission");
924 }
925 return true;
926 }
Satoshi Kataoka0766eb02013-07-31 18:30:13 +0900927 Slog.w(TAG, "--- IPC called from background users. Ignore. \n"
928 + InputMethodUtils.getStackTrace());
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +0900929 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900930 }
931
932 private boolean bindCurrentInputMethodService(
933 Intent service, ServiceConnection conn, int flags) {
934 if (service == null || conn == null) {
935 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
936 return false;
937 }
Amith Yamasani27b89e62013-01-16 12:30:11 -0800938 return mContext.bindServiceAsUser(service, conn, flags,
939 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900940 }
941
satoke7c6998e2011-06-03 17:57:59 +0900942 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800943 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900944 // TODO: Make this work even for non-current users?
945 if (!calledFromValidUser()) {
946 return Collections.emptyList();
947 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948 synchronized (mMethodMap) {
949 return new ArrayList<InputMethodInfo>(mMethodList);
950 }
951 }
952
satoke7c6998e2011-06-03 17:57:59 +0900953 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800954 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900955 // TODO: Make this work even for non-current users?
956 if (!calledFromValidUser()) {
957 return Collections.emptyList();
958 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800959 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900960 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800961 }
962 }
963
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900964 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +0900965 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900966 * @return enabled subtypes of the specified imi
967 */
satoke7c6998e2011-06-03 17:57:59 +0900968 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +0900969 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +0900970 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900971 // TODO: Make this work even for non-current users?
972 if (!calledFromValidUser()) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +0900973 return Collections.<InputMethodSubtype>emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900974 }
satok67ddf9c2010-11-17 09:45:54 +0900975 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +0900976 final InputMethodInfo imi;
977 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900978 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +0900979 } else {
980 imi = mMethodMap.get(imiId);
981 }
982 if (imi == null) {
983 return Collections.<InputMethodSubtype>emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900984 }
985 return mSettings.getEnabledInputMethodSubtypeListLocked(
986 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +0900987 }
988 }
989
satoke7c6998e2011-06-03 17:57:59 +0900990 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800991 public void addClient(IInputMethodClient client,
992 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900993 if (!calledFromValidUser()) {
994 return;
995 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800996 synchronized (mMethodMap) {
997 mClients.put(client.asBinder(), new ClientState(client,
998 inputContext, uid, pid));
999 }
1000 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001001
satoke7c6998e2011-06-03 17:57:59 +09001002 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001003 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001004 if (!calledFromValidUser()) {
1005 return;
1006 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001008 ClientState cs = mClients.remove(client.asBinder());
1009 if (cs != null) {
1010 clearClientSessionLocked(cs);
1011 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012 }
1013 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001014
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015 void executeOrSendMessage(IInterface target, Message msg) {
1016 if (target.asBinder() instanceof Binder) {
1017 mCaller.sendMessage(msg);
1018 } else {
1019 handleMessage(msg);
1020 msg.recycle();
1021 }
1022 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001023
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001024 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001026 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027 + mCurClient.client.asBinder());
1028 if (mBoundToMethod) {
1029 mBoundToMethod = false;
1030 if (mCurMethod != null) {
1031 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1032 MSG_UNBIND_INPUT, mCurMethod));
1033 }
1034 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001035
1036 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1037 MSG_SET_ACTIVE, 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001038 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1039 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1040 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001041 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001042
The Android Open Source Project10592532009-03-18 17:39:46 -07001043 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001044 }
1045 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001046
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001047 private int getImeShowFlags() {
1048 int flags = 0;
1049 if (mShowForced) {
1050 flags |= InputMethod.SHOW_FORCED
1051 | InputMethod.SHOW_EXPLICIT;
1052 } else if (mShowExplicitlyRequested) {
1053 flags |= InputMethod.SHOW_EXPLICIT;
1054 }
1055 return flags;
1056 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001057
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001058 private int getAppShowFlags() {
1059 int flags = 0;
1060 if (mShowForced) {
1061 flags |= InputMethodManager.SHOW_FORCED;
1062 } else if (!mShowExplicitlyRequested) {
1063 flags |= InputMethodManager.SHOW_IMPLICIT;
1064 }
1065 return flags;
1066 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001067
Dianne Hackborn7663d802012-02-24 13:08:49 -08001068 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069 if (!mBoundToMethod) {
1070 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1071 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1072 mBoundToMethod = true;
1073 }
1074 final SessionState session = mCurClient.curSession;
1075 if (initial) {
1076 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1077 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
1078 } else {
1079 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1080 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
1081 }
1082 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001083 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001084 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001085 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001086 return new InputBindResult(session.session,
1087 session.channel != null ? session.channel.dup() : null, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001088 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001089
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001090 InputBindResult startInputLocked(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001091 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 // If no method is currently selected, do nothing.
1093 if (mCurMethodId == null) {
1094 return mNoBinding;
1095 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001096
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097 ClientState cs = mClients.get(client.asBinder());
1098 if (cs == null) {
1099 throw new IllegalArgumentException("unknown client "
1100 + client.asBinder());
1101 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001103 try {
1104 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1105 // Check with the window manager to make sure this client actually
1106 // has a window with focus. If not, reject. This is thread safe
1107 // because if the focus changes some time before or after, the
1108 // next client receiving focus that has any interest in input will
1109 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001110 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1112 return null;
1113 }
1114 } catch (RemoteException e) {
1115 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001116
Dianne Hackborn7663d802012-02-24 13:08:49 -08001117 return startInputUncheckedLocked(cs, inputContext, attribute, controlFlags);
1118 }
1119
1120 InputBindResult startInputUncheckedLocked(ClientState cs,
1121 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
1122 // If no method is currently selected, do nothing.
1123 if (mCurMethodId == null) {
1124 return mNoBinding;
1125 }
1126
John Spurlockbc7b6fc2012-11-14 08:51:07 -05001127 if (mCurClient == null) {
1128 mInputBoundToKeyguard = mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1129 if (DEBUG) {
1130 Slog.v(TAG, "New bind. keyguard = " + mInputBoundToKeyguard);
1131 }
1132 }
1133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001134 if (mCurClient != cs) {
1135 // If the client is changing, we need to switch over to the new
1136 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001137 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001138 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001139 + cs.client.asBinder());
1140
1141 // If the screen is on, inform the new client it is active
1142 if (mScreenOn) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001143 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
1144 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001145 }
1146 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001148 // Bump up the sequence for this client and attach it.
1149 mCurSeq++;
1150 if (mCurSeq <= 0) mCurSeq = 1;
1151 mCurClient = cs;
1152 mCurInputContext = inputContext;
1153 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001155 // Check if the input method is changing.
1156 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1157 if (cs.curSession != null) {
1158 // Fast case: if we are already connected to the input method,
1159 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001160 return attachNewInputLocked(
1161 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001162 }
1163 if (mHaveConnection) {
1164 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001165 // Return to client, and we will get back with it when
1166 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001167 requestClientSessionLocked(cs);
1168 return new InputBindResult(null, null, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 } else if (SystemClock.uptimeMillis()
1170 < (mLastBindTime+TIME_TO_RECONNECT)) {
1171 // In this case we have connected to the service, but
1172 // don't yet have its interface. If it hasn't been too
1173 // long since we did the connection, we'll return to
1174 // the client and wait to get the service interface so
1175 // we can report back. If it has been too long, we want
1176 // to fall through so we can try a disconnect/reconnect
1177 // to see if we can get back in touch with the service.
Jeff Brownc28867a2013-03-26 15:42:39 -07001178 return new InputBindResult(null, null, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001179 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001180 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1181 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001182 }
1183 }
1184 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001185
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001186 return startInputInnerLocked();
1187 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001188
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001189 InputBindResult startInputInnerLocked() {
1190 if (mCurMethodId == null) {
1191 return mNoBinding;
1192 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001193
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001194 if (!mSystemReady) {
1195 // If the system is not yet ready, we shouldn't be running third
1196 // party code.
Jeff Brownc28867a2013-03-26 15:42:39 -07001197 return new InputBindResult(null, null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001198 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001200 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1201 if (info == null) {
1202 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1203 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001204
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001205 unbindCurrentMethodLocked(false, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001207 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1208 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001209 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1210 com.android.internal.R.string.input_method_binding_label);
1211 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1212 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001213 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornc8230512013-07-13 21:32:12 -07001214 | Context.BIND_NOT_VISIBLE | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 mLastBindTime = SystemClock.uptimeMillis();
1216 mHaveConnection = true;
1217 mCurId = info.getId();
1218 mCurToken = new Binder();
1219 try {
Craig Mautnerca0ac712013-03-14 09:43:02 -07001220 if (true || DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 mIWindowManager.addWindowToken(mCurToken,
1222 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1223 } catch (RemoteException e) {
1224 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001225 return new InputBindResult(null, null, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 } else {
1227 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001228 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 + mCurIntent);
1230 }
1231 return null;
1232 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001233
satoke7c6998e2011-06-03 17:57:59 +09001234 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 public InputBindResult startInput(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001236 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001237 if (!calledFromValidUser()) {
1238 return null;
1239 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001240 synchronized (mMethodMap) {
1241 final long ident = Binder.clearCallingIdentity();
1242 try {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001243 return startInputLocked(client, inputContext, attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001244 } finally {
1245 Binder.restoreCallingIdentity(ident);
1246 }
1247 }
1248 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001249
satoke7c6998e2011-06-03 17:57:59 +09001250 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001251 public void finishInput(IInputMethodClient client) {
1252 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001253
satoke7c6998e2011-06-03 17:57:59 +09001254 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255 public void onServiceConnected(ComponentName name, IBinder service) {
1256 synchronized (mMethodMap) {
1257 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1258 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001259 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001260 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001261 unbindCurrentMethodLocked(false, false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001262 return;
1263 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001264 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001265 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1266 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001267 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001268 clearClientSessionLocked(mCurClient);
1269 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001270 }
1271 }
1272 }
1273 }
1274
Jeff Brownc28867a2013-03-26 15:42:39 -07001275 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1276 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 synchronized (mMethodMap) {
1278 if (mCurMethod != null && method != null
1279 && mCurMethod.asBinder() == method.asBinder()) {
1280 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001281 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001282 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001283 method, session, channel);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001284 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001285 if (res.method != null) {
1286 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
1287 MSG_BIND_METHOD, mCurClient.client, res));
1288 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001289 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001290 }
1291 }
1292 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001293
1294 // Session abandoned. Close its associated input channel.
1295 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001296 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001297
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001298 void unbindCurrentMethodLocked(boolean reportToClient, boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001299 if (mVisibleBound) {
1300 mContext.unbindService(mVisibleConnection);
1301 mVisibleBound = false;
1302 }
1303
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001304 if (mHaveConnection) {
1305 mContext.unbindService(this);
1306 mHaveConnection = false;
1307 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001308
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001309 if (mCurToken != null) {
1310 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001311 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001312 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001313 // The current IME is shown. Hence an IME switch (transition) is happening.
1314 mWindowManagerService.saveLastInputMethodWindowForTransition();
1315 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001316 mIWindowManager.removeWindowToken(mCurToken);
1317 } catch (RemoteException e) {
1318 }
1319 mCurToken = null;
1320 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001321
The Android Open Source Project10592532009-03-18 17:39:46 -07001322 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001323 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001324
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001325 if (reportToClient && mCurClient != null) {
1326 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1327 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1328 }
1329 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001330
1331 void requestClientSessionLocked(ClientState cs) {
1332 if (!cs.sessionRequested) {
1333 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1334 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1335 cs.sessionRequested = true;
1336 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1337 MSG_CREATE_SESSION, mCurMethod, channels[1],
1338 new MethodCallback(this, mCurMethod, channels[0])));
1339 }
1340 }
1341
1342 void clearClientSessionLocked(ClientState cs) {
1343 finishSessionLocked(cs.curSession);
1344 cs.curSession = null;
1345 cs.sessionRequested = false;
1346 }
1347
1348 private void finishSessionLocked(SessionState sessionState) {
1349 if (sessionState != null) {
1350 if (sessionState.session != null) {
1351 try {
1352 sessionState.session.finishSession();
1353 } catch (RemoteException e) {
1354 Slog.w(TAG, "Session failed to close due to remote exception", e);
1355 setImeWindowVisibilityStatusHiddenLocked();
1356 }
1357 sessionState.session = null;
1358 }
1359 if (sessionState.channel != null) {
1360 sessionState.channel.dispose();
1361 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001362 }
1363 }
1364 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001365
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001366 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001367 if (mCurMethod != null) {
1368 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001369 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001370 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001371
Jeff Brownc28867a2013-03-26 15:42:39 -07001372 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001373 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001374 mCurMethod = null;
1375 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001376 if (mStatusBar != null) {
1377 mStatusBar.setIconVisibility("ime", false);
1378 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001379 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001380
satoke7c6998e2011-06-03 17:57:59 +09001381 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001382 public void onServiceDisconnected(ComponentName name) {
1383 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001384 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001385 + " mCurIntent=" + mCurIntent);
1386 if (mCurMethod != null && mCurIntent != null
1387 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001388 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001389 // We consider this to be a new bind attempt, since the system
1390 // should now try to restart the service for us.
1391 mLastBindTime = SystemClock.uptimeMillis();
1392 mShowRequested = mInputShown;
1393 mInputShown = false;
1394 if (mCurClient != null) {
1395 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1396 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1397 }
1398 }
1399 }
1400 }
1401
satokf9f01002011-05-19 21:31:50 +09001402 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001403 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001404 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001405 long ident = Binder.clearCallingIdentity();
1406 try {
1407 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001408 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001409 return;
1410 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001411
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001412 synchronized (mMethodMap) {
1413 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001414 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001415 if (mStatusBar != null) {
1416 mStatusBar.setIconVisibility("ime", false);
1417 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001418 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001419 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001420 CharSequence contentDescription = null;
1421 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001422 // Use PackageManager to load label
1423 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001424 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001425 mIPackageManager.getApplicationInfo(packageName, 0,
1426 mSettings.getCurrentUserId()));
1427 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001428 /* ignore */
1429 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001430 if (mStatusBar != null) {
1431 mStatusBar.setIcon("ime", packageName, iconId, 0,
1432 contentDescription != null
1433 ? contentDescription.toString() : null);
1434 mStatusBar.setIconVisibility("ime", true);
1435 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001436 }
1437 }
1438 } finally {
1439 Binder.restoreCallingIdentity(ident);
1440 }
1441 }
1442
satok7cfc0ed2011-06-20 21:29:36 +09001443 private boolean needsToShowImeSwitchOngoingNotification() {
1444 if (!mShowOngoingImeSwitcherForPhones) return false;
satok2c93efc2012-04-02 19:33:47 +09001445 if (isScreenLocked()) return false;
satok7cfc0ed2011-06-20 21:29:36 +09001446 synchronized (mMethodMap) {
1447 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1448 final int N = imis.size();
satokb6359412011-06-28 17:47:41 +09001449 if (N > 2) return true;
1450 if (N < 1) return false;
1451 int nonAuxCount = 0;
1452 int auxCount = 0;
1453 InputMethodSubtype nonAuxSubtype = null;
1454 InputMethodSubtype auxSubtype = null;
satok7cfc0ed2011-06-20 21:29:36 +09001455 for(int i = 0; i < N; ++i) {
1456 final InputMethodInfo imi = imis.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001457 final List<InputMethodSubtype> subtypes =
1458 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satok7cfc0ed2011-06-20 21:29:36 +09001459 final int subtypeCount = subtypes.size();
1460 if (subtypeCount == 0) {
satokb6359412011-06-28 17:47:41 +09001461 ++nonAuxCount;
satok7cfc0ed2011-06-20 21:29:36 +09001462 } else {
1463 for (int j = 0; j < subtypeCount; ++j) {
satokb6359412011-06-28 17:47:41 +09001464 final InputMethodSubtype subtype = subtypes.get(j);
1465 if (!subtype.isAuxiliary()) {
1466 ++nonAuxCount;
1467 nonAuxSubtype = subtype;
1468 } else {
1469 ++auxCount;
1470 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001471 }
1472 }
1473 }
satok7cfc0ed2011-06-20 21:29:36 +09001474 }
satokb6359412011-06-28 17:47:41 +09001475 if (nonAuxCount > 1 || auxCount > 1) {
1476 return true;
1477 } else if (nonAuxCount == 1 && auxCount == 1) {
1478 if (nonAuxSubtype != null && auxSubtype != null
satok9747f892011-09-12 15:56:40 +09001479 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1480 || auxSubtype.overridesImplicitlyEnabledSubtype()
1481 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
satokb6359412011-06-28 17:47:41 +09001482 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1483 return false;
1484 }
1485 return true;
1486 }
1487 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001488 }
satok7cfc0ed2011-06-20 21:29:36 +09001489 }
1490
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001491 // Caution! This method is called in this class. Handle multi-user carefully
satokdbf29502011-08-25 15:28:23 +09001492 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001493 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001494 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001495 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001496 try {
1497 if (token == null || mCurToken != token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001498 int uid = Binder.getCallingUid();
Joe Onorato857fd9b2011-01-27 15:08:35 -08001499 Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token);
satok06487a52010-10-29 11:37:18 +09001500 return;
1501 }
1502
1503 synchronized (mMethodMap) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001504 mImeWindowVis = vis;
1505 mBackDisposition = backDisposition;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001506 if (mStatusBar != null) {
1507 mStatusBar.setImeWindowStatus(token, vis, backDisposition);
1508 }
jungheang.lee217fd292013-02-26 16:53:22 +09001509 final boolean iconVisibility = ((vis & (InputMethodService.IME_ACTIVE)) != 0)
1510 && (mWindowManagerService.isHardKeyboardAvailable()
1511 || (vis & (InputMethodService.IME_VISIBLE)) != 0);
satok5bc8e732011-07-22 21:07:23 +09001512 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1513 if (imi != null && iconVisibility && needsToShowImeSwitchOngoingNotification()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001514 // Used to load label
satok7cfc0ed2011-06-20 21:29:36 +09001515 final CharSequence title = mRes.getText(
1516 com.android.internal.R.string.select_input_method);
Satoshi Kataokab2827262013-07-04 19:43:14 +09001517 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
1518 mContext, imi, mCurrentSubtype);
satok5bc8e732011-07-22 21:07:23 +09001519
satok7cfc0ed2011-06-20 21:29:36 +09001520 mImeSwitcherNotification.setLatestEventInfo(
satok5bc8e732011-07-22 21:07:23 +09001521 mContext, title, summary, mImeSwitchPendingIntent);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001522 if (mNotificationManager != null) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001523 if (DEBUG) {
Satoshi Kataokab2827262013-07-04 19:43:14 +09001524 Slog.d(TAG, "--- show notification: label = " + summary);
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001525 }
1526 mNotificationManager.notifyAsUser(null,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001527 com.android.internal.R.string.select_input_method,
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001528 mImeSwitcherNotification, UserHandle.ALL);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001529 mNotificationShown = true;
1530 }
satok7cfc0ed2011-06-20 21:29:36 +09001531 } else {
Dianne Hackborn661cd522011-08-22 00:26:20 -07001532 if (mNotificationShown && mNotificationManager != null) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001533 if (DEBUG) {
1534 Slog.d(TAG, "--- hide notification");
1535 }
1536 mNotificationManager.cancelAsUser(null,
1537 com.android.internal.R.string.select_input_method, UserHandle.ALL);
satok7cfc0ed2011-06-20 21:29:36 +09001538 mNotificationShown = false;
1539 }
1540 }
satok06487a52010-10-29 11:37:18 +09001541 }
1542 } finally {
1543 Binder.restoreCallingIdentity(ident);
1544 }
1545 }
1546
satoke7c6998e2011-06-03 17:57:59 +09001547 @Override
satokf9f01002011-05-19 21:31:50 +09001548 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001549 if (!calledFromValidUser()) {
1550 return;
1551 }
satokf9f01002011-05-19 21:31:50 +09001552 synchronized (mMethodMap) {
1553 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1554 for (int i = 0; i < spans.length; ++i) {
1555 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001556 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001557 mSecureSuggestionSpans.put(ss, currentImi);
1558 }
1559 }
1560 }
1561 }
1562
satoke7c6998e2011-06-03 17:57:59 +09001563 @Override
satokf9f01002011-05-19 21:31:50 +09001564 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001565 if (!calledFromValidUser()) {
1566 return false;
1567 }
satokf9f01002011-05-19 21:31:50 +09001568 synchronized (mMethodMap) {
1569 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1570 // TODO: Do not send the intent if the process of the targetImi is already dead.
1571 if (targetImi != null) {
1572 final String[] suggestions = span.getSuggestions();
1573 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001574 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001575 final Intent intent = new Intent();
1576 // Ensures that only a class in the original IME package will receive the
1577 // notification.
satok42c5a162011-05-26 16:46:14 +09001578 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001579 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1580 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1581 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1582 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001583 final long ident = Binder.clearCallingIdentity();
1584 try {
1585 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1586 } finally {
1587 Binder.restoreCallingIdentity(ident);
1588 }
satokf9f01002011-05-19 21:31:50 +09001589 return true;
1590 }
1591 }
1592 return false;
1593 }
1594
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001595 void updateFromSettingsLocked(boolean enabledMayChange) {
1596 if (enabledMayChange) {
1597 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1598 for (int i=0; i<enabled.size(); i++) {
1599 // We allow the user to select "disabled until used" apps, so if they
1600 // are enabling one of those here we now need to make it enabled.
1601 InputMethodInfo imm = enabled.get(i);
1602 try {
1603 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1604 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1605 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09001606 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001607 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001608 if (DEBUG) {
1609 Slog.d(TAG, "Update state(" + imm.getId()
1610 + "): DISABLED_UNTIL_USED -> DEFAULT");
1611 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001612 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1613 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001614 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
1615 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001616 }
1617 } catch (RemoteException e) {
1618 }
1619 }
1620 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001621 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1622 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1623 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1624 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001625 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001626 // There is no input method selected, try to choose new applicable input method.
1627 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001628 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001629 }
1630 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001631 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001632 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001633 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001634 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001635 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001636 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001637 }
satokf3db1af2010-11-23 13:34:33 +09001638 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001639 } else {
1640 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001641 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001642 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001643 }
1644 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001645
satokab751aa2010-09-14 19:17:36 +09001646 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001647 InputMethodInfo info = mMethodMap.get(id);
1648 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001649 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001650 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001651
satokd81e9502012-05-21 12:58:45 +09001652 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001653 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09001654 final int subtypeCount = info.getSubtypeCount();
1655 if (subtypeCount <= 0) {
1656 return;
satokcd7cd292010-11-20 15:46:23 +09001657 }
satokd81e9502012-05-21 12:58:45 +09001658 final InputMethodSubtype oldSubtype = mCurrentSubtype;
1659 final InputMethodSubtype newSubtype;
1660 if (subtypeId >= 0 && subtypeId < subtypeCount) {
1661 newSubtype = info.getSubtypeAt(subtypeId);
1662 } else {
1663 // If subtype is null, try to find the most applicable one from
1664 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001665 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09001666 }
1667 if (newSubtype == null || oldSubtype == null) {
1668 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
1669 + ", new subtype = " + newSubtype);
1670 return;
1671 }
1672 if (newSubtype != oldSubtype) {
1673 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1674 if (mCurMethod != null) {
1675 try {
1676 refreshImeWindowVisibilityLocked();
1677 mCurMethod.changeInputMethodSubtype(newSubtype);
1678 } catch (RemoteException e) {
1679 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
satokab751aa2010-09-14 19:17:36 +09001680 }
1681 }
1682 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001683 return;
1684 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001685
satokd81e9502012-05-21 12:58:45 +09001686 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001687 final long ident = Binder.clearCallingIdentity();
1688 try {
satokab751aa2010-09-14 19:17:36 +09001689 // Set a subtype to this input method.
1690 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001691 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1692 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1693 // because mCurMethodId is stored as a history in
1694 // setSelectedInputMethodAndSubtypeLocked().
1695 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001696
1697 if (ActivityManagerNative.isSystemReady()) {
1698 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001699 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001700 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07001701 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001702 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001703 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001704 } finally {
1705 Binder.restoreCallingIdentity(ident);
1706 }
1707 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001708
satok42c5a162011-05-26 16:46:14 +09001709 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001710 public boolean showSoftInput(IInputMethodClient client, int flags,
1711 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001712 if (!calledFromValidUser()) {
1713 return false;
1714 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001715 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001716 long ident = Binder.clearCallingIdentity();
1717 try {
1718 synchronized (mMethodMap) {
1719 if (mCurClient == null || client == null
1720 || mCurClient.client.asBinder() != client.asBinder()) {
1721 try {
1722 // We need to check if this is the current client with
1723 // focus in the window manager, to allow this call to
1724 // be made before input is started in it.
1725 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001726 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001727 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001728 }
1729 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001730 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001731 }
1732 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001733
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001734 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001735 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001736 }
1737 } finally {
1738 Binder.restoreCallingIdentity(ident);
1739 }
1740 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001741
The Android Open Source Project4df24232009-03-05 14:34:35 -08001742 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001743 mShowRequested = true;
1744 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1745 mShowExplicitlyRequested = true;
1746 }
1747 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1748 mShowExplicitlyRequested = true;
1749 mShowForced = true;
1750 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001751
Dianne Hackborncc278702009-09-02 23:07:23 -07001752 if (!mSystemReady) {
1753 return false;
1754 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001755
The Android Open Source Project4df24232009-03-05 14:34:35 -08001756 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001757 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001758 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001759 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1760 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1761 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001762 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001763 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001764 bindCurrentInputMethodService(
1765 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001766 mVisibleBound = true;
1767 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001768 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001769 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09001770 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001771 // The client has asked to have the input method shown, but
1772 // we have been sitting here too long with a connection to the
1773 // service and no interface received, so let's disconnect/connect
1774 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001775 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001776 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09001777 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001778 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001779 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001780 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001781 } else {
1782 if (DEBUG) {
1783 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
1784 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
1785 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001786 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001787
The Android Open Source Project4df24232009-03-05 14:34:35 -08001788 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001789 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001790
satok42c5a162011-05-26 16:46:14 +09001791 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001792 public boolean hideSoftInput(IInputMethodClient client, int flags,
1793 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001794 if (!calledFromValidUser()) {
1795 return false;
1796 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001797 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001798 long ident = Binder.clearCallingIdentity();
1799 try {
1800 synchronized (mMethodMap) {
1801 if (mCurClient == null || client == null
1802 || mCurClient.client.asBinder() != client.asBinder()) {
1803 try {
1804 // We need to check if this is the current client with
1805 // focus in the window manager, to allow this call to
1806 // be made before input is started in it.
1807 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001808 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1809 + uid + ": " + client);
satok15452a42011-10-28 17:58:28 +09001810 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001811 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001812 }
1813 } catch (RemoteException e) {
satok15452a42011-10-28 17:58:28 +09001814 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001815 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001816 }
1817 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001818
Joe Onorato8a9b2202010-02-26 18:56:32 -08001819 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001820 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001821 }
1822 } finally {
1823 Binder.restoreCallingIdentity(ident);
1824 }
1825 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001826
The Android Open Source Project4df24232009-03-05 14:34:35 -08001827 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001828 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1829 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001830 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 -08001831 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001832 }
1833 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001834 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 -08001835 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001836 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001837 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001838 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001839 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1840 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1841 res = true;
1842 } else {
1843 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001844 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001845 if (mHaveConnection && mVisibleBound) {
1846 mContext.unbindService(mVisibleConnection);
1847 mVisibleBound = false;
1848 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001849 mInputShown = false;
1850 mShowRequested = false;
1851 mShowExplicitlyRequested = false;
1852 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001853 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001854 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001855
satok42c5a162011-05-26 16:46:14 +09001856 @Override
Dianne Hackborn7663d802012-02-24 13:08:49 -08001857 public InputBindResult windowGainedFocus(IInputMethodClient client, IBinder windowToken,
1858 int controlFlags, int softInputMode, int windowFlags,
1859 EditorInfo attribute, IInputContext inputContext) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09001860 // Needs to check the validity before clearing calling identity
1861 final boolean calledFromValidUser = calledFromValidUser();
1862
Dianne Hackborn7663d802012-02-24 13:08:49 -08001863 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001864 long ident = Binder.clearCallingIdentity();
1865 try {
1866 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001867 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
Dianne Hackborn7663d802012-02-24 13:08:49 -08001868 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001869 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08001870 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001871
Dianne Hackborn7663d802012-02-24 13:08:49 -08001872 ClientState cs = mClients.get(client.asBinder());
1873 if (cs == null) {
1874 throw new IllegalArgumentException("unknown client "
1875 + client.asBinder());
1876 }
1877
1878 try {
1879 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1880 // Check with the window manager to make sure this client actually
1881 // has a window with focus. If not, reject. This is thread safe
1882 // because if the focus changes some time before or after, the
1883 // next client receiving focus that has any interest in input will
1884 // be calling through here after that change happens.
1885 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
1886 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1887 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001888 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001889 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001890 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001891
Satoshi Kataoka8d033052012-11-19 17:30:40 +09001892 if (!calledFromValidUser) {
1893 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
1894 Slog.w(TAG, "If you want to interect with IME, you need "
1895 + "android.permission.INTERACT_ACROSS_USERS_FULL");
1896 hideCurrentInputLocked(0, null);
1897 return null;
1898 }
1899
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001900 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07001901 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09001902 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001903 if (attribute != null) {
1904 return startInputUncheckedLocked(cs, inputContext, attribute,
1905 controlFlags);
1906 }
1907 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001908 }
1909 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001910
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001911 // Should we auto-show the IME even if the caller has not
1912 // specified what should be done with it?
1913 // We only do this automatically if the window can resize
1914 // to accommodate the IME (so what the user sees will give
1915 // them good context without input information being obscured
1916 // by the IME) or if running on a large screen where there
1917 // is more room for the target window + IME.
1918 final boolean doAutoShow =
1919 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1920 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1921 || mRes.getConfiguration().isLayoutSizeAtLeast(
1922 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001923 final boolean isTextEditor =
1924 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
1925
1926 // We want to start input before showing the IME, but after closing
1927 // it. We want to do this after closing it to help the IME disappear
1928 // more quickly (not get stuck behind it initializing itself for the
1929 // new focused input, even if its window wants to hide the IME).
1930 boolean didStart = false;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001931
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001932 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1933 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001934 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001935 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1936 // There is no focus view, and this window will
1937 // be behind any soft input window, so hide the
1938 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001939 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001940 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001941 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001942 } else if (isTextEditor && doAutoShow && (softInputMode &
1943 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001944 // There is a focus view, and we are navigating forward
1945 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001946 // We only do this automatically if the window can resize
1947 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001948 // them good context without input information being obscured
1949 // by the IME) or if running on a large screen where there
1950 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001951 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001952 if (attribute != null) {
1953 res = startInputUncheckedLocked(cs, inputContext, attribute,
1954 controlFlags);
1955 didStart = true;
1956 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001957 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001958 }
1959 break;
1960 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1961 // Do nothing.
1962 break;
1963 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1964 if ((softInputMode &
1965 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001966 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001967 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001968 }
1969 break;
1970 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001971 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001972 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001973 break;
1974 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1975 if ((softInputMode &
1976 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001977 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001978 if (attribute != null) {
1979 res = startInputUncheckedLocked(cs, inputContext, attribute,
1980 controlFlags);
1981 didStart = true;
1982 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001983 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001984 }
1985 break;
1986 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001987 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001988 if (attribute != null) {
1989 res = startInputUncheckedLocked(cs, inputContext, attribute,
1990 controlFlags);
1991 didStart = true;
1992 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001993 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001994 break;
1995 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001996
1997 if (!didStart && attribute != null) {
1998 res = startInputUncheckedLocked(cs, inputContext, attribute,
1999 controlFlags);
2000 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002001 }
2002 } finally {
2003 Binder.restoreCallingIdentity(ident);
2004 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002005
2006 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002007 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002008
satok42c5a162011-05-26 16:46:14 +09002009 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002010 public void showInputMethodPickerFromClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002011 if (!calledFromValidUser()) {
2012 return;
2013 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002014 synchronized (mMethodMap) {
2015 if (mCurClient == null || client == null
2016 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002017 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002018 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002019 }
2020
satok440aab52010-11-25 09:43:11 +09002021 // Always call subtype picker, because subtype picker is a superset of input method
2022 // picker.
satokab751aa2010-09-14 19:17:36 +09002023 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
2024 }
2025 }
2026
satok42c5a162011-05-26 16:46:14 +09002027 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002028 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002029 if (!calledFromValidUser()) {
2030 return;
2031 }
satok28203512010-11-24 11:06:49 +09002032 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2033 }
2034
satok42c5a162011-05-26 16:46:14 +09002035 @Override
satok28203512010-11-24 11:06:49 +09002036 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002037 if (!calledFromValidUser()) {
2038 return;
2039 }
satok28203512010-11-24 11:06:49 +09002040 synchronized (mMethodMap) {
2041 if (subtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002042 setInputMethodWithSubtypeId(token, id, InputMethodUtils.getSubtypeIdFromHashCode(
satok28203512010-11-24 11:06:49 +09002043 mMethodMap.get(id), subtype.hashCode()));
2044 } else {
2045 setInputMethod(token, id);
2046 }
2047 }
satokab751aa2010-09-14 19:17:36 +09002048 }
2049
satok42c5a162011-05-26 16:46:14 +09002050 @Override
satokb416a712010-11-25 20:42:14 +09002051 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002052 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002053 if (!calledFromValidUser()) {
2054 return;
2055 }
satokb416a712010-11-25 20:42:14 +09002056 synchronized (mMethodMap) {
2057 if (mCurClient == null || client == null
2058 || mCurClient.client.asBinder() != client.asBinder()) {
2059 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
2060 }
satok7fee71f2010-12-17 18:54:26 +09002061 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2062 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002063 }
2064 }
2065
satok4fc87d62011-05-20 16:13:43 +09002066 @Override
satok735cf382010-11-11 20:40:09 +09002067 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002068 if (!calledFromValidUser()) {
2069 return false;
2070 }
satok735cf382010-11-11 20:40:09 +09002071 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002072 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002073 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002074 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002075 lastImi = mMethodMap.get(lastIme.first);
2076 } else {
2077 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002078 }
satok4fc87d62011-05-20 16:13:43 +09002079 String targetLastImiId = null;
2080 int subtypeId = NOT_A_SUBTYPE_ID;
2081 if (lastIme != null && lastImi != null) {
2082 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
2083 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
2084 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2085 : mCurrentSubtype.hashCode();
2086 // If the last IME is the same as the current IME and the last subtype is not
2087 // defined, there is no need to switch to the last IME.
2088 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2089 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002090 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002091 }
2092 }
2093
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002094 if (TextUtils.isEmpty(targetLastImiId)
2095 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002096 // This is a safety net. If the currentSubtype can't be added to the history
2097 // and the framework couldn't find the last ime, we will make the last ime be
2098 // the most applicable enabled keyboard subtype of the system imes.
2099 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2100 if (enabled != null) {
2101 final int N = enabled.size();
2102 final String locale = mCurrentSubtype == null
2103 ? mRes.getConfiguration().locale.toString()
2104 : mCurrentSubtype.getLocale();
2105 for (int i = 0; i < N; ++i) {
2106 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002107 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002108 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002109 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2110 InputMethodUtils.getSubtypes(imi),
2111 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002112 if (keyboardSubtype != null) {
2113 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002114 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002115 imi, keyboardSubtype.hashCode());
2116 if(keyboardSubtype.getLocale().equals(locale)) {
2117 break;
2118 }
2119 }
2120 }
2121 }
2122 }
2123 }
2124
2125 if (!TextUtils.isEmpty(targetLastImiId)) {
2126 if (DEBUG) {
2127 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2128 + ", from: " + mCurMethodId + ", " + subtypeId);
2129 }
2130 setInputMethodWithSubtypeId(token, targetLastImiId, subtypeId);
2131 return true;
2132 } else {
2133 return false;
2134 }
satok735cf382010-11-11 20:40:09 +09002135 }
2136 }
2137
satoke7c6998e2011-06-03 17:57:59 +09002138 @Override
satok688bd472012-02-09 20:09:17 +09002139 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002140 if (!calledFromValidUser()) {
2141 return false;
2142 }
satok688bd472012-02-09 20:09:17 +09002143 synchronized (mMethodMap) {
Satoshi Kataokad787f692013-10-26 04:44:21 +09002144 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethod(
satok688bd472012-02-09 20:09:17 +09002145 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2146 if (nextSubtype == null) {
2147 return false;
2148 }
2149 setInputMethodWithSubtypeId(token, nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
2150 return true;
2151 }
2152 }
2153
2154 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002155 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2156 if (!calledFromValidUser()) {
2157 return false;
2158 }
2159 synchronized (mMethodMap) {
Satoshi Kataokad787f692013-10-26 04:44:21 +09002160 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethod(
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002161 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2162 if (nextSubtype == null) {
2163 return false;
2164 }
2165 return true;
2166 }
2167 }
2168
2169 @Override
satok68f1b782011-04-11 14:26:04 +09002170 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002171 if (!calledFromValidUser()) {
2172 return null;
2173 }
satok68f1b782011-04-11 14:26:04 +09002174 synchronized (mMethodMap) {
2175 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2176 // TODO: Handle the case of the last IME with no subtypes
2177 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2178 || TextUtils.isEmpty(lastIme.second)) return null;
2179 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2180 if (lastImi == null) return null;
2181 try {
2182 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002183 final int lastSubtypeId =
2184 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002185 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2186 return null;
2187 }
2188 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002189 } catch (NumberFormatException e) {
2190 return null;
2191 }
2192 }
2193 }
2194
satoke7c6998e2011-06-03 17:57:59 +09002195 @Override
satokee5e77c2011-09-02 18:50:15 +09002196 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002197 if (!calledFromValidUser()) {
2198 return;
2199 }
satok91e88122011-07-18 11:11:42 +09002200 // By this IPC call, only a process which shares the same uid with the IME can add
2201 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09002202 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09002203 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002204 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002205 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002206 final String[] packageInfos;
2207 try {
2208 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2209 } catch (RemoteException e) {
2210 Slog.e(TAG, "Failed to get package infos");
2211 return;
2212 }
satok91e88122011-07-18 11:11:42 +09002213 if (packageInfos != null) {
2214 final int packageNum = packageInfos.length;
2215 for (int i = 0; i < packageNum; ++i) {
2216 if (packageInfos[i].equals(imi.getPackageName())) {
2217 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002218 final long ident = Binder.clearCallingIdentity();
2219 try {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002220 buildInputMethodListLocked(mMethodList, mMethodMap,
2221 false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002222 } finally {
2223 Binder.restoreCallingIdentity(ident);
2224 }
satokee5e77c2011-09-02 18:50:15 +09002225 return;
satok91e88122011-07-18 11:11:42 +09002226 }
2227 }
2228 }
satoke7c6998e2011-06-03 17:57:59 +09002229 }
satokee5e77c2011-09-02 18:50:15 +09002230 return;
satoke7c6998e2011-06-03 17:57:59 +09002231 }
2232
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002233 @Override
2234 public int getInputMethodWindowVisibleHeight() {
2235 return mWindowManagerService.getInputMethodWindowVisibleHeight();
2236 }
2237
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002238 @Override
2239 public void notifyTextCommitted() {
2240 if (DEBUG) {
2241 Slog.d(TAG, "Got the notification of commitText");
2242 }
2243 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2244 if (imi != null) {
2245 mSwitchingController.onCommitText(imi, mCurrentSubtype);
2246 }
2247 }
2248
satok28203512010-11-24 11:06:49 +09002249 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002250 synchronized (mMethodMap) {
2251 if (token == null) {
2252 if (mContext.checkCallingOrSelfPermission(
2253 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2254 != PackageManager.PERMISSION_GRANTED) {
2255 throw new SecurityException(
2256 "Using null token requires permission "
2257 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2258 }
2259 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002260 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2261 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002262 return;
2263 }
2264
satokc5933802011-08-31 21:26:04 +09002265 final long ident = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002266 try {
satokab751aa2010-09-14 19:17:36 +09002267 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002268 } finally {
2269 Binder.restoreCallingIdentity(ident);
2270 }
2271 }
2272 }
2273
satok42c5a162011-05-26 16:46:14 +09002274 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002275 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002276 if (!calledFromValidUser()) {
2277 return;
2278 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002279 synchronized (mMethodMap) {
2280 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002281 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
2282 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002283 return;
2284 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002285 long ident = Binder.clearCallingIdentity();
2286 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002287 hideCurrentInputLocked(flags, null);
2288 } finally {
2289 Binder.restoreCallingIdentity(ident);
2290 }
2291 }
2292 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002293
satok42c5a162011-05-26 16:46:14 +09002294 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002295 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002296 if (!calledFromValidUser()) {
2297 return;
2298 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002299 synchronized (mMethodMap) {
2300 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002301 Slog.w(TAG, "Ignoring showMySoftInput of uid "
2302 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002303 return;
2304 }
2305 long ident = Binder.clearCallingIdentity();
2306 try {
2307 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002308 } finally {
2309 Binder.restoreCallingIdentity(ident);
2310 }
2311 }
2312 }
2313
2314 void setEnabledSessionInMainThread(SessionState session) {
2315 if (mEnabledSession != session) {
2316 if (mEnabledSession != null) {
2317 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002318 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002319 mEnabledSession.method.setSessionEnabled(
2320 mEnabledSession.session, false);
2321 } catch (RemoteException e) {
2322 }
2323 }
2324 mEnabledSession = session;
2325 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002326 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002327 session.method.setSessionEnabled(
2328 session.session, true);
2329 } catch (RemoteException e) {
2330 }
2331 }
2332 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002333
satok42c5a162011-05-26 16:46:14 +09002334 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002335 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002336 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002337 switch (msg.what) {
2338 case MSG_SHOW_IM_PICKER:
2339 showInputMethodMenu();
2340 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002341
satokab751aa2010-09-14 19:17:36 +09002342 case MSG_SHOW_IM_SUBTYPE_PICKER:
2343 showInputMethodSubtypeMenu();
2344 return true;
2345
satok47a44912010-10-06 16:03:58 +09002346 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002347 args = (SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09002348 showInputMethodAndSubtypeEnabler((String)args.arg1);
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002349 args.recycle();
satok217f5482010-12-15 05:19:19 +09002350 return true;
2351
2352 case MSG_SHOW_IM_CONFIG:
2353 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002354 return true;
2355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002356 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002357
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002358 case MSG_UNBIND_INPUT:
2359 try {
2360 ((IInputMethod)msg.obj).unbindInput();
2361 } catch (RemoteException e) {
2362 // There is nothing interesting about the method dying.
2363 }
2364 return true;
2365 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002366 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002367 try {
2368 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2369 } catch (RemoteException e) {
2370 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002371 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002372 return true;
2373 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002374 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002375 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002376 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002377 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002378 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002379 } catch (RemoteException e) {
2380 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002381 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002382 return true;
2383 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002384 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002385 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002386 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002387 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002388 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002389 } catch (RemoteException e) {
2390 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002391 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002392 return true;
2393 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002394 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002395 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002396 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002397 ((IInputMethod)args.arg1).attachToken((IBinder)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;
Jeff Brownc28867a2013-03-26 15:42:39 -07002402 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002403 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002404 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002405 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002406 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002407 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002408 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002409 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002410 // Dispose the channel if the input method is not local to this process
2411 // because the remote proxy will get its own copy when unparceled.
2412 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002413 channel.dispose();
2414 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002415 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002416 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002417 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002418 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002419 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002420
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002421 case MSG_START_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002422 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002423 try {
2424 SessionState session = (SessionState)args.arg1;
2425 setEnabledSessionInMainThread(session);
2426 session.method.startInput((IInputContext)args.arg2,
2427 (EditorInfo)args.arg3);
2428 } catch (RemoteException e) {
2429 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002430 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002431 return true;
2432 case MSG_RESTART_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002433 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002434 try {
2435 SessionState session = (SessionState)args.arg1;
2436 setEnabledSessionInMainThread(session);
2437 session.method.restartInput((IInputContext)args.arg2,
2438 (EditorInfo)args.arg3);
2439 } catch (RemoteException e) {
2440 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002441 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002442 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002444 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002445
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002446 case MSG_UNBIND_METHOD:
2447 try {
2448 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
2449 } catch (RemoteException e) {
2450 // There is nothing interesting about the last client dying.
2451 }
2452 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002453 case MSG_BIND_METHOD: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002454 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002455 IInputMethodClient client = (IInputMethodClient)args.arg1;
2456 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002457 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002458 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002459 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002460 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002461 } finally {
2462 // Dispose the channel if the input method is not local to this process
2463 // because the remote proxy will get its own copy when unparceled.
2464 if (res.channel != null && Binder.isProxy(client)) {
2465 res.channel.dispose();
2466 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002467 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002468 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002469 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002470 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002471 case MSG_SET_ACTIVE:
2472 try {
2473 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2474 } catch (RemoteException e) {
2475 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2476 + ((ClientState)msg.obj).pid + " uid "
2477 + ((ClientState)msg.obj).uid);
2478 }
2479 return true;
satok01038492012-04-09 21:08:27 +09002480
2481 // --------------------------------------------------------------
2482 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
2483 mHardKeyboardListener.handleHardKeyboardStatusChange(
2484 msg.arg1 == 1, msg.arg2 == 1);
2485 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002486 }
2487 return false;
2488 }
2489
satokdc9ddae2011-10-06 12:22:36 +09002490 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002491 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
2492 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09002493 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002494 if (DEBUG) {
2495 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2496 }
satok723a27e2010-11-11 14:58:11 +09002497 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002498 return true;
2499 }
2500
2501 return false;
2502 }
2503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002504 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002505 HashMap<String, InputMethodInfo> map, boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002506 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002507 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Satoshi Kataoka0766eb02013-07-31 18:30:13 +09002508 + " \n ------ \n" + InputMethodUtils.getStackTrace());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002509 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002510 list.clear();
2511 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002512
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002513 // Use for queryIntentServicesAsUser
2514 final PackageManager pm = mContext.getPackageManager();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002515 String disabledSysImes = mSettings.getDisabledSystemInputMethods();
Amith Yamasanie861ec12010-03-24 21:39:27 -07002516 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002517
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002518 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002519 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002520 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2521 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002522
satoke7c6998e2011-06-03 17:57:59 +09002523 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2524 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002525 for (int i = 0; i < services.size(); ++i) {
2526 ResolveInfo ri = services.get(i);
2527 ServiceInfo si = ri.serviceInfo;
2528 ComponentName compName = new ComponentName(si.packageName, si.name);
2529 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2530 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002531 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002532 + ": it does not require the permission "
2533 + android.Manifest.permission.BIND_INPUT_METHOD);
2534 continue;
2535 }
2536
Joe Onorato8a9b2202010-02-26 18:56:32 -08002537 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002538
2539 try {
satoke7c6998e2011-06-03 17:57:59 +09002540 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002541 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002542 final String id = p.getId();
2543 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002544
2545 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002546 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002547 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002548
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002549 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002550 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002551 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002552 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002553 }
2554 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002555
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002556 if (resetDefaultEnabledIme) {
2557 final ArrayList<InputMethodInfo> defaultEnabledIme =
2558 InputMethodUtils.getDefaultEnabledImes(mContext, mSystemReady, list);
2559 for (int i = 0; i < defaultEnabledIme.size(); ++i) {
2560 final InputMethodInfo imi = defaultEnabledIme.get(i);
2561 if (DEBUG) {
2562 Slog.d(TAG, "--- enable ime = " + imi);
2563 }
2564 setInputMethodEnabledLocked(imi.getId(), true);
2565 }
2566 }
2567
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002568 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09002569 if (!TextUtils.isEmpty(defaultImiId)) {
2570 if (!map.containsKey(defaultImiId)) {
2571 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
2572 if (chooseNewDefaultIMELocked()) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002573 updateFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09002574 }
2575 } else {
2576 // Double check that the default IME is certainly enabled.
2577 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002578 }
2579 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002580 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002581
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002582 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002583
satokab751aa2010-09-14 19:17:36 +09002584 private void showInputMethodMenu() {
2585 showInputMethodMenuInternal(false);
2586 }
2587
2588 private void showInputMethodSubtypeMenu() {
2589 showInputMethodMenuInternal(true);
2590 }
2591
satok217f5482010-12-15 05:19:19 +09002592 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002593 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002594 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002595 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2596 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002597 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002598 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002599 }
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002600 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok217f5482010-12-15 05:19:19 +09002601 }
2602
2603 private void showConfigureInputMethods() {
2604 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2605 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2606 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2607 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002608 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09002609 }
2610
satok2c93efc2012-04-02 19:33:47 +09002611 private boolean isScreenLocked() {
2612 return mKeyguardManager != null
2613 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
2614 }
satokab751aa2010-09-14 19:17:36 +09002615 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002616 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002617
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002618 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09002619 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002620
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002621 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002622 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002623 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002624
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002625 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002626 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09002627 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
2628 mContext);
satok7f35c8c2010-10-07 21:13:11 +09002629 if (immis == null || immis.size() == 0) {
2630 return;
2631 }
2632
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002633 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002634
satok688bd472012-02-09 20:09:17 +09002635 final List<ImeSubtypeListItem> imList =
Satoshi Kataokad787f692013-10-26 04:44:21 +09002636 mSwitchingController.getSortedInputMethodAndSubtypeList(
satok688bd472012-02-09 20:09:17 +09002637 showSubtypes, mInputShown, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09002638
satokc3690562012-01-10 20:14:43 +09002639 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002640 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09002641 if (currentSubtype != null) {
2642 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002643 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
2644 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09002645 }
2646 }
2647
Ken Wakasa761eb372011-03-04 19:06:18 +09002648 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09002649 mIms = new InputMethodInfo[N];
2650 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002651 int checkedItem = 0;
2652 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002653 final ImeSubtypeListItem item = imList.get(i);
2654 mIms[i] = item.mImi;
2655 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002656 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002657 int subtypeId = mSubtypeIds[i];
2658 if ((subtypeId == NOT_A_SUBTYPE_ID)
2659 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2660 || (subtypeId == lastInputMethodSubtypeId)) {
2661 checkedItem = i;
2662 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002663 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002664 }
Ken Wakasa05dbb652011-08-22 15:22:43 +09002665 final TypedArray a = context.obtainStyledAttributes(null,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002666 com.android.internal.R.styleable.DialogPreference,
2667 com.android.internal.R.attr.alertDialogStyle, 0);
2668 mDialogBuilder = new AlertDialog.Builder(context)
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002669 .setOnCancelListener(new OnCancelListener() {
satok42c5a162011-05-26 16:46:14 +09002670 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002671 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002672 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002673 }
2674 })
2675 .setIcon(a.getDrawable(
2676 com.android.internal.R.styleable.DialogPreference_dialogTitle));
2677 a.recycle();
satok01038492012-04-09 21:08:27 +09002678 final LayoutInflater inflater =
2679 (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2680 final View tv = inflater.inflate(
2681 com.android.internal.R.layout.input_method_switch_dialog_title, null);
2682 mDialogBuilder.setCustomTitle(tv);
2683
2684 // Setup layout for a toggle switch of the hardware keyboard
2685 mSwitchingDialogTitleView = tv;
2686 mSwitchingDialogTitleView.findViewById(
2687 com.android.internal.R.id.hard_keyboard_section).setVisibility(
2688 mWindowManagerService.isHardKeyboardAvailable() ?
2689 View.VISIBLE : View.GONE);
2690 final Switch hardKeySwitch = ((Switch)mSwitchingDialogTitleView.findViewById(
2691 com.android.internal.R.id.hard_keyboard_switch));
2692 hardKeySwitch.setChecked(mWindowManagerService.isHardKeyboardEnabled());
2693 hardKeySwitch.setOnCheckedChangeListener(
2694 new OnCheckedChangeListener() {
2695 @Override
2696 public void onCheckedChanged(
2697 CompoundButton buttonView, boolean isChecked) {
2698 mWindowManagerService.setHardKeyboardEnabled(isChecked);
Satoshi Kataoka04dd24d2013-01-18 13:44:37 +09002699 // Ensure that the input method dialog is dismissed when changing
2700 // the hardware keyboard state.
2701 hideInputMethodMenu();
satok01038492012-04-09 21:08:27 +09002702 }
2703 });
satokd87c2592010-09-29 11:52:06 +09002704
Ken Wakasa05dbb652011-08-22 15:22:43 +09002705 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(context,
2706 com.android.internal.R.layout.simple_list_item_2_single_choice, imList,
2707 checkedItem);
2708
2709 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002710 new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002711 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002712 public void onClick(DialogInterface dialog, int which) {
2713 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09002714 if (mIms == null || mIms.length <= which
2715 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002716 return;
2717 }
2718 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09002719 int subtypeId = mSubtypeIds[which];
Satoshi Kataokad2142962012-11-12 18:43:06 +09002720 adapter.mCheckedItem = which;
2721 adapter.notifyDataSetChanged();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002722 hideInputMethodMenu();
2723 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09002724 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09002725 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09002726 subtypeId = NOT_A_SUBTYPE_ID;
2727 }
2728 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002729 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08002730 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002731 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002732 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002733
satokbc81b692011-08-26 16:22:22 +09002734 if (showSubtypes && !isScreenLocked) {
satok82beadf2010-12-27 19:03:06 +09002735 mDialogBuilder.setPositiveButton(
2736 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09002737 new DialogInterface.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002738 @Override
satok7f35c8c2010-10-07 21:13:11 +09002739 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09002740 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09002741 }
2742 });
2743 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002744 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002745 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002746 mSwitchingDialog.getWindow().setType(
2747 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09002748 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
2749 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002750 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002751 mSwitchingDialog.show();
2752 }
2753 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002754
Ken Wakasa05dbb652011-08-22 15:22:43 +09002755 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
2756 private final LayoutInflater mInflater;
2757 private final int mTextViewResourceId;
2758 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09002759 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09002760 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
2761 List<ImeSubtypeListItem> itemsList, int checkedItem) {
2762 super(context, textViewResourceId, itemsList);
2763 mTextViewResourceId = textViewResourceId;
2764 mItemsList = itemsList;
2765 mCheckedItem = checkedItem;
2766 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2767 }
2768
2769 @Override
2770 public View getView(int position, View convertView, ViewGroup parent) {
2771 final View view = convertView != null ? convertView
2772 : mInflater.inflate(mTextViewResourceId, null);
2773 if (position < 0 || position >= mItemsList.size()) return view;
2774 final ImeSubtypeListItem item = mItemsList.get(position);
2775 final CharSequence imeName = item.mImeName;
2776 final CharSequence subtypeName = item.mSubtypeName;
2777 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
2778 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
2779 if (TextUtils.isEmpty(subtypeName)) {
2780 firstTextView.setText(imeName);
2781 secondTextView.setVisibility(View.GONE);
2782 } else {
2783 firstTextView.setText(subtypeName);
2784 secondTextView.setText(imeName);
2785 secondTextView.setVisibility(View.VISIBLE);
2786 }
2787 final RadioButton radioButton =
2788 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
2789 radioButton.setChecked(position == mCheckedItem);
2790 return view;
2791 }
2792 }
2793
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002794 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07002795 synchronized (mMethodMap) {
2796 hideInputMethodMenuLocked();
2797 }
2798 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002799
The Android Open Source Project10592532009-03-18 17:39:46 -07002800 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002801 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002802
The Android Open Source Project10592532009-03-18 17:39:46 -07002803 if (mSwitchingDialog != null) {
2804 mSwitchingDialog.dismiss();
2805 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002806 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002807
The Android Open Source Project10592532009-03-18 17:39:46 -07002808 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07002809 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002810 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002811
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002812 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002813
satok42c5a162011-05-26 16:46:14 +09002814 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002815 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002816 // TODO: Make this work even for non-current users?
2817 if (!calledFromValidUser()) {
2818 return false;
2819 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002820 synchronized (mMethodMap) {
2821 if (mContext.checkCallingOrSelfPermission(
2822 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2823 != PackageManager.PERMISSION_GRANTED) {
2824 throw new SecurityException(
2825 "Requires permission "
2826 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2827 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002828
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002829 long ident = Binder.clearCallingIdentity();
2830 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002831 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002832 } finally {
2833 Binder.restoreCallingIdentity(ident);
2834 }
2835 }
2836 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002837
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002838 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
2839 // Make sure this is a valid input method.
2840 InputMethodInfo imm = mMethodMap.get(id);
2841 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09002842 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002843 }
2844
satokd87c2592010-09-29 11:52:06 +09002845 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
2846 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002847
satokd87c2592010-09-29 11:52:06 +09002848 if (enabled) {
2849 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
2850 if (pair.first.equals(id)) {
2851 // We are enabling this input method, but it is already enabled.
2852 // Nothing to do. The previous state was enabled.
2853 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002854 }
2855 }
satokd87c2592010-09-29 11:52:06 +09002856 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
2857 // Previous state was disabled.
2858 return false;
2859 } else {
2860 StringBuilder builder = new StringBuilder();
2861 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2862 builder, enabledInputMethodsList, id)) {
2863 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002864 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002865 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
2866 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
2867 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09002868 }
2869 // Previous state was enabled.
2870 return true;
2871 } else {
2872 // We are disabling the input method but it is already disabled.
2873 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002874 return false;
2875 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002876 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002877 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002878
satok723a27e2010-11-11 14:58:11 +09002879 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
2880 boolean setSubtypeOnly) {
2881 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002882 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09002883
2884 // Set Subtype here
2885 if (imi == null || subtypeId < 0) {
2886 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08002887 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09002888 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09002889 if (subtypeId < imi.getSubtypeCount()) {
2890 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
2891 mSettings.putSelectedSubtype(subtype.hashCode());
2892 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09002893 } else {
2894 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09002895 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002896 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09002897 }
satokab751aa2010-09-14 19:17:36 +09002898 }
satok723a27e2010-11-11 14:58:11 +09002899
satok4c0e7152012-06-20 20:08:44 +09002900 // Workaround.
2901 // ASEC is not ready in the IMMS constructor. Accordingly, forward-locked
2902 // IMEs are not recognized and considered uninstalled.
2903 // Actually, we can't move everything after SystemReady because
2904 // IMMS needs to run in the encryption lock screen. So, we just skip changing
2905 // the default IME here and try cheking the default IME again in systemReady().
2906 // TODO: Do nothing before system ready and implement a separated logic for
2907 // the encryption lock screen.
2908 // TODO: ASEC should be ready before IMMS is instantiated.
2909 if (mSystemReady && !setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09002910 // Set InputMethod here
2911 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
2912 }
2913 }
2914
2915 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
2916 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
2917 int lastSubtypeId = NOT_A_SUBTYPE_ID;
2918 // newDefaultIme is empty when there is no candidate for the selected IME.
2919 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
2920 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
2921 if (subtypeHashCode != null) {
2922 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002923 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok723a27e2010-11-11 14:58:11 +09002924 imi, Integer.valueOf(subtypeHashCode));
2925 } catch (NumberFormatException e) {
2926 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
2927 }
2928 }
2929 }
2930 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09002931 }
2932
satok4e4569d2010-11-19 18:45:53 +09002933 // If there are no selected shortcuts, tries finding the most applicable ones.
2934 private Pair<InputMethodInfo, InputMethodSubtype>
2935 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
2936 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2937 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09002938 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09002939 boolean foundInSystemIME = false;
2940
2941 // Search applicable subtype for each InputMethodInfo
2942 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09002943 final String imiId = imi.getId();
2944 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
2945 continue;
2946 }
satokcd7cd292010-11-20 15:46:23 +09002947 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09002948 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002949 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09002950 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09002951 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002952 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002953 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09002954 }
satokdf31ae62011-01-15 06:19:44 +09002955 // 2. Search by the system locale from enabledSubtypes.
2956 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09002957 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002958 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002959 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002960 }
satoka86f5e42011-09-02 17:12:42 +09002961 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002962 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09002963 final ArrayList<InputMethodSubtype> subtypesForSearch =
2964 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002965 ? InputMethodUtils.getSubtypes(imi)
2966 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09002967 // 4. Search by the current subtype's locale from all subtypes.
2968 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002969 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09002970 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09002971 }
2972 // 5. Search by the system locale from all subtypes.
2973 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09002974 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002975 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09002976 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002977 }
satokcd7cd292010-11-20 15:46:23 +09002978 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09002979 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09002980 // The current input method is the most applicable IME.
2981 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002982 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09002983 break;
satok7599a7f2010-12-22 13:45:23 +09002984 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09002985 // The system input method is 2nd applicable IME.
2986 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002987 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09002988 if ((imi.getServiceInfo().applicationInfo.flags
2989 & ApplicationInfo.FLAG_SYSTEM) != 0) {
2990 foundInSystemIME = true;
2991 }
satok4e4569d2010-11-19 18:45:53 +09002992 }
2993 }
2994 }
2995 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09002996 if (mostApplicableIMI != null) {
2997 Slog.w(TAG, "Most applicable shortcut input method was:"
2998 + mostApplicableIMI.getId());
2999 if (mostApplicableSubtype != null) {
3000 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3001 + "," + mostApplicableSubtype.getMode() + ","
3002 + mostApplicableSubtype.getLocale());
3003 }
3004 }
satok4e4569d2010-11-19 18:45:53 +09003005 }
satokcd7cd292010-11-20 15:46:23 +09003006 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09003007 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09003008 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003009 } else {
3010 return null;
3011 }
3012 }
3013
satokab751aa2010-09-14 19:17:36 +09003014 /**
3015 * @return Return the current subtype of this input method.
3016 */
satok42c5a162011-05-26 16:46:14 +09003017 @Override
satokab751aa2010-09-14 19:17:36 +09003018 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003019 // TODO: Make this work even for non-current users?
3020 if (!calledFromValidUser()) {
3021 return null;
3022 }
3023 synchronized (mMethodMap) {
3024 return getCurrentInputMethodSubtypeLocked();
3025 }
3026 }
3027
3028 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003029 if (mCurMethodId == null) {
3030 return null;
3031 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003032 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003033 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3034 if (imi == null || imi.getSubtypeCount() == 0) {
3035 return null;
satok4e4569d2010-11-19 18:45:53 +09003036 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003037 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003038 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3039 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003040 if (subtypeId == NOT_A_SUBTYPE_ID) {
3041 // If there are no selected subtypes, the framework will try to find
3042 // the most applicable subtype from explicitly or implicitly enabled
3043 // subtypes.
3044 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003045 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003046 // If there is only one explicitly or implicitly enabled subtype,
3047 // just returns it.
3048 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3049 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3050 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003051 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003052 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003053 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003054 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003055 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003056 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3057 true);
satok4e4569d2010-11-19 18:45:53 +09003058 }
satok3ef8b292010-11-23 06:06:29 +09003059 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003060 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003061 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003062 }
3063 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003064 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003065 }
3066
satokf3db1af2010-11-23 13:34:33 +09003067 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
3068 InputMethodSubtype subtype) {
3069 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
3070 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
3071 } else {
3072 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3073 subtypes.add(subtype);
3074 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
3075 }
3076 }
3077
satok4e4569d2010-11-19 18:45:53 +09003078 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003079 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003080 @Override
satok4e4569d2010-11-19 18:45:53 +09003081 public List getShortcutInputMethodsAndSubtypes() {
3082 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09003083 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09003084 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003085 // If there are no selected shortcut subtypes, the framework will try to find
3086 // the most applicable subtype from all subtypes whose mode is
3087 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003088 Pair<InputMethodInfo, InputMethodSubtype> info =
3089 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003090 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003091 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003092 ret.add(info.first);
3093 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003094 }
satok3da92232011-01-11 22:46:30 +09003095 return ret;
satokf3db1af2010-11-23 13:34:33 +09003096 }
satokf3db1af2010-11-23 13:34:33 +09003097 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3098 ret.add(imi);
3099 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3100 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003101 }
3102 }
satokf3db1af2010-11-23 13:34:33 +09003103 return ret;
satok4e4569d2010-11-19 18:45:53 +09003104 }
3105 }
3106
satok42c5a162011-05-26 16:46:14 +09003107 @Override
satokb66d2872010-11-10 01:04:04 +09003108 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003109 // TODO: Make this work even for non-current users?
3110 if (!calledFromValidUser()) {
3111 return false;
3112 }
satokb66d2872010-11-10 01:04:04 +09003113 synchronized (mMethodMap) {
3114 if (subtype != null && mCurMethodId != null) {
3115 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003116 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003117 if (subtypeId != NOT_A_SUBTYPE_ID) {
3118 setInputMethodLocked(mCurMethodId, subtypeId);
3119 return true;
3120 }
3121 }
3122 return false;
3123 }
3124 }
3125
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003126 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003127 private static class InputMethodFileManager {
3128 private static final String SYSTEM_PATH = "system";
3129 private static final String INPUT_METHOD_PATH = "inputmethod";
3130 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3131 private static final String NODE_SUBTYPES = "subtypes";
3132 private static final String NODE_SUBTYPE = "subtype";
3133 private static final String NODE_IMI = "imi";
3134 private static final String ATTR_ID = "id";
3135 private static final String ATTR_LABEL = "label";
3136 private static final String ATTR_ICON = "icon";
3137 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3138 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3139 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3140 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3141 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3142 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003143 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
satoke7c6998e2011-06-03 17:57:59 +09003144 new HashMap<String, List<InputMethodSubtype>>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003145 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003146 if (methodMap == null) {
3147 throw new NullPointerException("methodMap is null");
3148 }
3149 mMethodMap = methodMap;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003150 final File systemDir = userId == UserHandle.USER_OWNER
3151 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3152 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003153 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3154 if (!inputMethodDir.mkdirs()) {
3155 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3156 }
3157 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3158 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3159 if (!subtypeFile.exists()) {
3160 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003161 writeAdditionalInputMethodSubtypes(
3162 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003163 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003164 readAdditionalInputMethodSubtypes(
3165 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003166 }
3167 }
3168
3169 private void deleteAllInputMethodSubtypes(String imiId) {
3170 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003171 mAdditionalSubtypesMap.remove(imiId);
3172 writeAdditionalInputMethodSubtypes(
3173 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003174 }
3175 }
3176
3177 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003178 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003179 synchronized (mMethodMap) {
3180 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3181 final int N = additionalSubtypes.length;
3182 for (int i = 0; i < N; ++i) {
3183 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003184 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003185 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003186 } else {
3187 Slog.w(TAG, "Duplicated subtype definition found: "
3188 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003189 }
3190 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003191 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3192 writeAdditionalInputMethodSubtypes(
3193 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003194 }
3195 }
3196
3197 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3198 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003199 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003200 }
3201 }
3202
3203 private static void writeAdditionalInputMethodSubtypes(
3204 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3205 HashMap<String, InputMethodInfo> methodMap) {
3206 // Safety net for the case that this function is called before methodMap is set.
3207 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3208 FileOutputStream fos = null;
3209 try {
3210 fos = subtypesFile.startWrite();
3211 final XmlSerializer out = new FastXmlSerializer();
3212 out.setOutput(fos, "utf-8");
3213 out.startDocument(null, true);
3214 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3215 out.startTag(null, NODE_SUBTYPES);
3216 for (String imiId : allSubtypes.keySet()) {
3217 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3218 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3219 continue;
3220 }
3221 out.startTag(null, NODE_IMI);
3222 out.attribute(null, ATTR_ID, imiId);
3223 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3224 final int N = subtypesList.size();
3225 for (int i = 0; i < N; ++i) {
3226 final InputMethodSubtype subtype = subtypesList.get(i);
3227 out.startTag(null, NODE_SUBTYPE);
3228 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3229 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3230 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3231 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3232 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3233 out.attribute(null, ATTR_IS_AUXILIARY,
3234 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3235 out.endTag(null, NODE_SUBTYPE);
3236 }
3237 out.endTag(null, NODE_IMI);
3238 }
3239 out.endTag(null, NODE_SUBTYPES);
3240 out.endDocument();
3241 subtypesFile.finishWrite(fos);
3242 } catch (java.io.IOException e) {
3243 Slog.w(TAG, "Error writing subtypes", e);
3244 if (fos != null) {
3245 subtypesFile.failWrite(fos);
3246 }
3247 }
3248 }
3249
3250 private static void readAdditionalInputMethodSubtypes(
3251 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3252 if (allSubtypes == null || subtypesFile == null) return;
3253 allSubtypes.clear();
3254 FileInputStream fis = null;
3255 try {
3256 fis = subtypesFile.openRead();
3257 final XmlPullParser parser = Xml.newPullParser();
3258 parser.setInput(fis, null);
3259 int type = parser.getEventType();
3260 // Skip parsing until START_TAG
3261 while ((type = parser.next()) != XmlPullParser.START_TAG
3262 && type != XmlPullParser.END_DOCUMENT) {}
3263 String firstNodeName = parser.getName();
3264 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3265 throw new XmlPullParserException("Xml doesn't start with subtypes");
3266 }
3267 final int depth =parser.getDepth();
3268 String currentImiId = null;
3269 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3270 while (((type = parser.next()) != XmlPullParser.END_TAG
3271 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3272 if (type != XmlPullParser.START_TAG)
3273 continue;
3274 final String nodeName = parser.getName();
3275 if (NODE_IMI.equals(nodeName)) {
3276 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3277 if (TextUtils.isEmpty(currentImiId)) {
3278 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3279 continue;
3280 }
3281 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
3282 allSubtypes.put(currentImiId, tempSubtypesArray);
3283 } else if (NODE_SUBTYPE.equals(nodeName)) {
3284 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3285 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3286 continue;
3287 }
3288 final int icon = Integer.valueOf(
3289 parser.getAttributeValue(null, ATTR_ICON));
3290 final int label = Integer.valueOf(
3291 parser.getAttributeValue(null, ATTR_LABEL));
3292 final String imeSubtypeLocale =
3293 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3294 final String imeSubtypeMode =
3295 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3296 final String imeSubtypeExtraValue =
3297 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003298 final boolean isAuxiliary = "1".equals(String.valueOf(
3299 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
satoke7c6998e2011-06-03 17:57:59 +09003300 final InputMethodSubtype subtype =
3301 new InputMethodSubtype(label, icon, imeSubtypeLocale,
3302 imeSubtypeMode, imeSubtypeExtraValue, isAuxiliary);
3303 tempSubtypesArray.add(subtype);
3304 }
3305 }
3306 } catch (XmlPullParserException e) {
3307 Slog.w(TAG, "Error reading subtypes: " + e);
3308 return;
3309 } catch (java.io.IOException e) {
3310 Slog.w(TAG, "Error reading subtypes: " + e);
3311 return;
3312 } catch (NumberFormatException e) {
3313 Slog.w(TAG, "Error reading subtypes: " + e);
3314 return;
3315 } finally {
3316 if (fis != null) {
3317 try {
3318 fis.close();
3319 } catch (java.io.IOException e1) {
3320 Slog.w(TAG, "Failed to close.");
3321 }
3322 }
3323 }
3324 }
3325 }
3326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003327 @Override
3328 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3329 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3330 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003332 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3333 + Binder.getCallingPid()
3334 + ", uid=" + Binder.getCallingUid());
3335 return;
3336 }
3337
3338 IInputMethod method;
3339 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003341 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003343 synchronized (mMethodMap) {
3344 p.println("Current Input Method Manager state:");
3345 int N = mMethodList.size();
3346 p.println(" Input Methods:");
3347 for (int i=0; i<N; i++) {
3348 InputMethodInfo info = mMethodList.get(i);
3349 p.println(" InputMethod #" + i + ":");
3350 info.dump(p, " ");
3351 }
3352 p.println(" Clients:");
3353 for (ClientState ci : mClients.values()) {
3354 p.println(" Client " + ci + ":");
3355 p.println(" client=" + ci.client);
3356 p.println(" inputContext=" + ci.inputContext);
3357 p.println(" sessionRequested=" + ci.sessionRequested);
3358 p.println(" curSession=" + ci.curSession);
3359 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003360 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003361 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003362 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3363 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003364 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3365 + " mBoundToMethod=" + mBoundToMethod);
3366 p.println(" mCurToken=" + mCurToken);
3367 p.println(" mCurIntent=" + mCurIntent);
3368 method = mCurMethod;
3369 p.println(" mCurMethod=" + mCurMethod);
3370 p.println(" mEnabledSession=" + mEnabledSession);
3371 p.println(" mShowRequested=" + mShowRequested
3372 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3373 + " mShowForced=" + mShowForced
3374 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07003375 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003376 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003377
Jeff Brownb88102f2010-09-08 11:49:43 -07003378 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003379 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003380 pw.flush();
3381 try {
3382 client.client.asBinder().dump(fd, args);
3383 } catch (RemoteException e) {
3384 p.println("Input method client dead: " + e);
3385 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003386 } else {
3387 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003388 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003389
Jeff Brownb88102f2010-09-08 11:49:43 -07003390 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003391 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003392 pw.flush();
3393 try {
3394 method.asBinder().dump(fd, args);
3395 } catch (RemoteException e) {
3396 p.println("Input method service dead: " + e);
3397 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003398 } else {
3399 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003400 }
3401 }
3402}