blob: 0b3eb1275da2f3d24835ee7d9d19e431862a3054 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 * use this file except in compliance with the License. You may obtain a copy of
5 * the License at
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007 * http://www.apache.org/licenses/LICENSE-2.0
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 * License for the specific language governing permissions and limitations under
13 * the License.
14 */
15
16package com.android.server;
17
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080018import com.android.internal.content.PackageMonitor;
Satoshi Kataokad7443c82013-10-15 17:45:43 +090019import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController;
Satoshi Kataokad787f692013-10-26 04:44:21 +090020import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController.ImeSubtypeListItem;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +090021import com.android.internal.inputmethod.InputMethodUtils;
22import com.android.internal.inputmethod.InputMethodUtils.InputMethodSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import com.android.internal.os.HandlerCaller;
Svetoslav Ganov758143e2012-08-06 16:40:27 -070024import com.android.internal.os.SomeArgs;
satoke7c6998e2011-06-03 17:57:59 +090025import com.android.internal.util.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import com.android.internal.view.IInputContext;
27import com.android.internal.view.IInputMethod;
Michael Wright52a53522013-03-14 10:59:38 -070028import com.android.internal.view.IInputSessionCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import com.android.internal.view.IInputMethodClient;
30import com.android.internal.view.IInputMethodManager;
31import com.android.internal.view.IInputMethodSession;
32import com.android.internal.view.InputBindResult;
Adam Lesinskief2ea1f2013-12-05 16:48:06 -080033import com.android.server.statusbar.StatusBarManagerService;
satok01038492012-04-09 21:08:27 +090034import com.android.server.wm.WindowManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035
satoke7c6998e2011-06-03 17:57:59 +090036import org.xmlpull.v1.XmlPullParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import org.xmlpull.v1.XmlPullParserException;
satoke7c6998e2011-06-03 17:57:59 +090038import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039
40import android.app.ActivityManagerNative;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090041import android.app.AppGlobals;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.app.AlertDialog;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090043import android.app.IUserSwitchObserver;
satokf90a33e2011-07-19 11:55:52 +090044import android.app.KeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +090045import android.app.Notification;
46import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070047import android.app.PendingIntent;
satok5b927c432012-05-01 20:09:34 +090048import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.content.ComponentName;
50import android.content.ContentResolver;
51import android.content.Context;
52import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.content.DialogInterface.OnCancelListener;
54import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090055import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070057import android.content.pm.ApplicationInfo;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090058import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.content.pm.PackageManager;
60import android.content.pm.ResolveInfo;
61import android.content.pm.ServiceInfo;
Amith 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
John Spurlocke0980502013-10-25 11:59:29 -0400311 // Was the keyguard locked when this client became current?
312 private boolean mCurClientInKeyguard;
313
satokab751aa2010-09-14 19:17:36 +0900314 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 * Set to true if our ServiceConnection is currently actively bound to
316 * a service (whether or not we have gotten its IBinder back yet).
317 */
318 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 /**
321 * Set if the client has asked for the input method to be shown.
322 */
323 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800324
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 /**
326 * Set if we were explicitly told to show the input method.
327 */
328 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800329
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 /**
331 * Set if we were forced to be shown.
332 */
333 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800334
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 /**
336 * Set if we last told the input method to show itself.
337 */
338 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 /**
341 * The Intent used to connect to the current input method.
342 */
343 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 /**
346 * The token we have made for the currently active input method, to
347 * identify it in the future.
348 */
349 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800350
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 /**
352 * If non-null, this is the input method service we are currently connected
353 * to.
354 */
355 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800356
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 /**
358 * Time that we last initiated a bind to the input method, to determine
359 * if we should try to disconnect and reconnect to it.
360 */
361 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 /**
364 * Have we called mCurMethod.bindInput()?
365 */
366 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800367
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 /**
369 * Currently enabled session. Only touched by service thread, not
370 * protected by a lock.
371 */
372 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800373
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 /**
375 * True if the screen is on. The value is true initially.
376 */
377 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800378
Joe Onorato857fd9b2011-01-27 15:08:35 -0800379 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
380 int mImeWindowVis;
381
Ken Wakasa05dbb652011-08-22 15:22:43 +0900382 private AlertDialog.Builder mDialogBuilder;
383 private AlertDialog mSwitchingDialog;
satok01038492012-04-09 21:08:27 +0900384 private View mSwitchingDialogTitleView;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900385 private InputMethodInfo[] mIms;
386 private int[] mSubtypeIds;
satok5b927c432012-05-01 20:09:34 +0900387 private Locale mLastSystemLocale;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900388 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
389 private final IPackageManager mIPackageManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 class SettingsObserver extends ContentObserver {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800392 String mLastEnabled = "";
393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 SettingsObserver(Handler handler) {
395 super(handler);
396 ContentResolver resolver = mContext.getContentResolver();
397 resolver.registerContentObserver(Settings.Secure.getUriFor(
398 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
satokab751aa2010-09-14 19:17:36 +0900399 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokb6109bb2011-02-03 22:24:54 +0900400 Settings.Secure.ENABLED_INPUT_METHODS), false, this);
401 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokab751aa2010-09-14 19:17:36 +0900402 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800404
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 @Override public void onChange(boolean selfChange) {
406 synchronized (mMethodMap) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800407 boolean enabledChanged = false;
408 String newEnabled = mSettings.getEnabledInputMethodsStr();
409 if (!mLastEnabled.equals(newEnabled)) {
410 mLastEnabled = newEnabled;
411 enabledChanged = true;
412 }
413 updateFromSettingsLocked(enabledChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 }
415 }
416 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800417
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900418 class ImmsBroadcastReceiver extends android.content.BroadcastReceiver {
419 private void updateActive() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 // Inform the current client of the change in active status
Dianne Hackborna6e41342012-05-22 16:30:34 -0700421 if (mCurClient != null && mCurClient.client != null) {
422 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
423 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 }
425 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900426
427 @Override
428 public void onReceive(Context context, Intent intent) {
429 final String action = intent.getAction();
430 if (Intent.ACTION_SCREEN_ON.equals(action)) {
431 mScreenOn = true;
432 refreshImeWindowVisibilityLocked();
433 updateActive();
434 return;
435 } else if (Intent.ACTION_SCREEN_OFF.equals(action)) {
436 mScreenOn = false;
437 setImeWindowVisibilityStatusHiddenLocked();
438 updateActive();
439 return;
440 } else if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
441 hideInputMethodMenu();
442 // No need to updateActive
443 return;
444 } else {
445 Slog.w(TAG, "Unexpected intent " + intent);
446 }
447 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800449
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800450 class MyPackageMonitor extends PackageMonitor {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900451 private boolean isChangingPackagesOfCurrentUser() {
452 final int userId = getChangingUserId();
453 final boolean retval = userId == mSettings.getCurrentUserId();
454 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900455 if (!retval) {
456 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
457 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900458 }
459 return retval;
460 }
461
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800463 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900464 if (!isChangingPackagesOfCurrentUser()) {
465 return false;
466 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900468 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 final int N = mMethodList.size();
470 if (curInputMethodId != null) {
471 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800472 InputMethodInfo imi = mMethodList.get(i);
473 if (imi.getId().equals(curInputMethodId)) {
474 for (String pkg : packages) {
475 if (imi.getPackageName().equals(pkg)) {
476 if (!doit) {
477 return true;
478 }
satok723a27e2010-11-11 14:58:11 +0900479 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800480 chooseNewDefaultIMELocked();
481 return true;
482 }
483 }
484 }
485 }
486 }
487 }
488 return false;
489 }
490
491 @Override
492 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900493 if (!isChangingPackagesOfCurrentUser()) {
494 return;
495 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800496 synchronized (mMethodMap) {
497 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900498 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800499 final int N = mMethodList.size();
500 if (curInputMethodId != null) {
501 for (int i=0; i<N; i++) {
502 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900503 final String imiId = imi.getId();
504 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800505 curIm = imi;
506 }
satoke7c6998e2011-06-03 17:57:59 +0900507
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800508 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900509 if (isPackageModified(imi.getPackageName())) {
510 mFileManager.deleteAllInputMethodSubtypes(imiId);
511 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800512 if (change == PACKAGE_TEMPORARY_CHANGE
513 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800514 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800515 + imi.getComponent());
516 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 }
518 }
519 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800520
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900521 buildInputMethodListLocked(
522 mMethodList, mMethodMap, false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800523
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800525
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800526 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800527 int change = isPackageDisappearing(curIm.getPackageName());
528 if (change == PACKAGE_TEMPORARY_CHANGE
529 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800530 ServiceInfo si = null;
531 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900532 si = mIPackageManager.getServiceInfo(
533 curIm.getComponent(), 0, mSettings.getCurrentUserId());
534 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800535 }
536 if (si == null) {
537 // Uh oh, current input method is no longer around!
538 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800539 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
satok15452a42011-10-28 17:58:28 +0900540 setImeWindowVisibilityStatusHiddenLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800541 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800542 changed = true;
543 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800544 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900545 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800546 }
547 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800548 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800549 }
satokab751aa2010-09-14 19:17:36 +0900550
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800551 if (curIm == null) {
552 // We currently don't have a default input method... is
553 // one now available?
554 changed = chooseNewDefaultIMELocked();
555 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800556
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800557 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800558 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 }
560 }
561 }
562 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800563
Jeff Brownc28867a2013-03-26 15:42:39 -0700564 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900565 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -0700566 private final IInputMethod mMethod;
567 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800568
Jeff Brownc28867a2013-03-26 15:42:39 -0700569 MethodCallback(InputMethodManagerService imms, IInputMethod method,
570 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900571 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -0700572 mMethod = method;
573 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800575
satoke7c6998e2011-06-03 17:57:59 +0900576 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -0700577 public void sessionCreated(IInputMethodSession session) {
578 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800579 }
580 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800581
satok01038492012-04-09 21:08:27 +0900582 private class HardKeyboardListener
583 implements WindowManagerService.OnHardKeyboardStatusChangeListener {
584 @Override
585 public void onHardKeyboardStatusChange(boolean available, boolean enabled) {
586 mHandler.sendMessage(mHandler.obtainMessage(
587 MSG_HARD_KEYBOARD_SWITCH_CHANGED, available ? 1 : 0, enabled ? 1 : 0));
588 }
589
590 public void handleHardKeyboardStatusChange(boolean available, boolean enabled) {
591 if (DEBUG) {
592 Slog.w(TAG, "HardKeyboardStatusChanged: available = " + available + ", enabled = "
593 + enabled);
594 }
595 synchronized(mMethodMap) {
596 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
597 && mSwitchingDialog.isShowing()) {
598 mSwitchingDialogTitleView.findViewById(
599 com.android.internal.R.id.hard_keyboard_section).setVisibility(
600 available ? View.VISIBLE : View.GONE);
601 }
602 }
603 }
604 }
605
606 public InputMethodManagerService(Context context, WindowManagerService windowManager) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900607 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800608 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800609 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 mHandler = new Handler(this);
611 mIWindowManager = IWindowManager.Stub.asInterface(
612 ServiceManager.getService(Context.WINDOW_SERVICE));
Mita Yuned218c72012-12-06 17:18:25 -0800613 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900614 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615 public void executeMessage(Message msg) {
616 handleMessage(msg);
617 }
Mita Yuned218c72012-12-06 17:18:25 -0800618 }, true /*asyncHandler*/);
satok01038492012-04-09 21:08:27 +0900619 mWindowManagerService = windowManager;
620 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700621 mHasFeature = context.getPackageManager().hasSystemFeature(
622 PackageManager.FEATURE_INPUT_METHODS);
satok7cfc0ed2011-06-20 21:29:36 +0900623
satok7cfc0ed2011-06-20 21:29:36 +0900624 mImeSwitcherNotification = new Notification();
625 mImeSwitcherNotification.icon = com.android.internal.R.drawable.ic_notification_ime_default;
626 mImeSwitcherNotification.when = 0;
627 mImeSwitcherNotification.flags = Notification.FLAG_ONGOING_EVENT;
628 mImeSwitcherNotification.tickerText = null;
629 mImeSwitcherNotification.defaults = 0; // please be quiet
630 mImeSwitcherNotification.sound = null;
631 mImeSwitcherNotification.vibrate = null;
Daniel Sandler590d5152012-06-14 16:10:13 -0400632
633 // Tag this notification specially so SystemUI knows it's important
634 mImeSwitcherNotification.kind = new String[] { "android.system.imeswitcher" };
635
satok7cfc0ed2011-06-20 21:29:36 +0900636 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900637 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900638
639 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900640
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900641 final IntentFilter broadcastFilter = new IntentFilter();
642 broadcastFilter.addAction(Intent.ACTION_SCREEN_ON);
643 broadcastFilter.addAction(Intent.ACTION_SCREEN_OFF);
644 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
645 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800646
satok7cfc0ed2011-06-20 21:29:36 +0900647 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900648 int userId = 0;
649 try {
650 ActivityManagerNative.getDefault().registerUserSwitchObserver(
651 new IUserSwitchObserver.Stub() {
652 @Override
653 public void onUserSwitching(int newUserId, IRemoteCallback reply) {
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900654 synchronized(mMethodMap) {
655 switchUserLocked(newUserId);
656 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900657 if (reply != null) {
658 try {
659 reply.sendResult(null);
660 } catch (RemoteException e) {
661 }
662 }
663 }
664
665 @Override
666 public void onUserSwitchComplete(int newUserId) throws RemoteException {
667 }
668 });
669 userId = ActivityManagerNative.getDefault().getCurrentUser().id;
670 } catch (RemoteException e) {
671 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
672 }
satok81f8b7c2012-12-04 20:42:56 +0900673 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
satok913a8922010-08-26 21:53:41 +0900674
satokd87c2592010-09-29 11:52:06 +0900675 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900676 mSettings = new InputMethodSettings(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900677 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId);
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900678 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Satoshi Kataokad787f692013-10-26 04:44:21 +0900679 mSwitchingController = new InputMethodSubtypeSwitchingController(mSettings);
680 mSwitchingController.resetCircularListLocked(context);
satok0a1bcf42012-05-16 19:26:31 +0900681
682 // Just checking if defaultImiId is empty or not
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900683 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900684 if (DEBUG) {
685 Slog.d(TAG, "Initial default ime = " + defaultImiId);
686 }
satok0a1bcf42012-05-16 19:26:31 +0900687 mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
688
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900689 buildInputMethodListLocked(mMethodList, mMethodMap,
690 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satokd87c2592010-09-29 11:52:06 +0900691 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800692
satok0a1bcf42012-05-16 19:26:31 +0900693 if (!mImeSelectedOnBoot) {
694 Slog.w(TAG, "No IME selected. Choose the most applicable IME.");
satok5b927c432012-05-01 20:09:34 +0900695 resetDefaultImeLocked(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800696 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800697
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698 mSettingsObserver = new SettingsObserver(mHandler);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800699 updateFromSettingsLocked(true);
satok5b927c432012-05-01 20:09:34 +0900700
701 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME
702 // according to the new system locale.
703 final IntentFilter filter = new IntentFilter();
704 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
705 mContext.registerReceiver(
706 new BroadcastReceiver() {
707 @Override
708 public void onReceive(Context context, Intent intent) {
709 synchronized(mMethodMap) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900710 resetStateIfCurrentLocaleChangedLocked();
satok5b927c432012-05-01 20:09:34 +0900711 }
712 }
713 }, filter);
714 }
715
satok5b927c432012-05-01 20:09:34 +0900716 private void resetDefaultImeLocked(Context context) {
717 // Do not reset the default (current) IME when it is a 3rd-party IME
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900718 if (mCurMethodId != null
719 && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +0900720 return;
721 }
722
723 InputMethodInfo defIm = null;
724 for (InputMethodInfo imi : mMethodList) {
725 if (defIm == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900726 if (InputMethodUtils.isValidSystemDefaultIme(
727 mSystemReady, imi, context)) {
satok5b927c432012-05-01 20:09:34 +0900728 defIm = imi;
729 Slog.i(TAG, "Selected default: " + imi.getId());
730 }
731 }
732 }
733 if (defIm == null && mMethodList.size() > 0) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900734 defIm = InputMethodUtils.getMostApplicableDefaultIME(
735 mSettings.getEnabledInputMethodListLocked());
satok5b927c432012-05-01 20:09:34 +0900736 Slog.i(TAG, "No default found, using " + defIm.getId());
737 }
738 if (defIm != null) {
739 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
740 }
741 }
742
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900743 private void resetAllInternalStateLocked(final boolean updateOnlyWhenLocaleChanged,
744 final boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900745 if (!mSystemReady) {
746 // not system ready
747 return;
748 }
749 final Locale newLocale = mRes.getConfiguration().locale;
750 if (!updateOnlyWhenLocaleChanged
751 || (newLocale != null && !newLocale.equals(mLastSystemLocale))) {
752 if (!updateOnlyWhenLocaleChanged) {
753 hideCurrentInputLocked(0, null);
754 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -0700755 unbindCurrentMethodLocked(true, false);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900756 }
757 if (DEBUG) {
758 Slog.i(TAG, "Locale has been changed to " + newLocale);
759 }
Satoshi Kataokad787f692013-10-26 04:44:21 +0900760 // CircularList should be reset when the locale is changed.
761 mSwitchingController.resetCircularListLocked(mContext);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900762 buildInputMethodListLocked(mMethodList, mMethodMap, resetDefaultEnabledIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900763 if (!updateOnlyWhenLocaleChanged) {
764 final String selectedImiId = mSettings.getSelectedInputMethod();
765 if (TextUtils.isEmpty(selectedImiId)) {
766 // This is the first time of the user switch and
767 // set the current ime to the proper one.
768 resetDefaultImeLocked(mContext);
769 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +0900770 } else {
771 // If the locale is changed, needs to reset the default ime
772 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900773 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800774 updateFromSettingsLocked(true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900775 mLastSystemLocale = newLocale;
776 if (!updateOnlyWhenLocaleChanged) {
777 try {
778 startInputInnerLocked();
779 } catch (RuntimeException e) {
780 Slog.w(TAG, "Unexpected exception", e);
781 }
782 }
783 }
784 }
785
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900786 private void resetStateIfCurrentLocaleChangedLocked() {
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900787 resetAllInternalStateLocked(true /* updateOnlyWhenLocaleChanged */,
788 true /* resetDefaultImeLocked */);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900789 }
790
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900791 private void switchUserLocked(int newUserId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900792 mSettings.setCurrentUserId(newUserId);
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900793 // InputMethodFileManager should be reset when the user is changed
794 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900795 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +0900796 // For secondary users, the list of enabled IMEs may not have been updated since the
797 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
798 // not be empty even if the IME has been uninstalled by the primary user.
799 // Even in such cases, IMMS works fine because it will find the most applicable
800 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900801 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900802 if (DEBUG) {
803 Slog.d(TAG, "Switch user: " + newUserId + " current ime = " + defaultImiId);
804 }
805 resetAllInternalStateLocked(false /* updateOnlyWhenLocaleChanged */,
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900806 initialUserSwitch /* needsToResetDefaultIme */);
807 if (initialUserSwitch) {
808 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mContext.getPackageManager(),
809 mSettings.getEnabledInputMethodListLocked());
810 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900811 }
812
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800813 @Override
814 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
815 throws RemoteException {
816 try {
817 return super.onTransact(code, data, reply, flags);
818 } catch (RuntimeException e) {
819 // The input method manager only throws security exceptions, so let's
820 // log all others.
821 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -0700822 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800823 }
824 throw e;
825 }
826 }
827
Svetoslav Ganova0027152013-06-25 14:59:53 -0700828 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700829 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900830 if (DEBUG) {
831 Slog.d(TAG, "--- systemReady");
832 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700833 if (!mSystemReady) {
834 mSystemReady = true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900835 mKeyguardManager =
836 (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700837 mNotificationManager = (NotificationManager)
838 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
839 mStatusBar = statusBar;
840 statusBar.setIconVisibility("ime", false);
841 updateImeWindowStatusLocked();
satokb858c732011-07-22 19:54:34 +0900842 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
843 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +0900844 if (mShowOngoingImeSwitcherForPhones) {
845 mWindowManagerService.setOnHardKeyboardStatusChangeListener(
846 mHardKeyboardListener);
847 }
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900848 buildInputMethodListLocked(mMethodList, mMethodMap,
849 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satok0a1bcf42012-05-16 19:26:31 +0900850 if (!mImeSelectedOnBoot) {
851 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900852 resetStateIfCurrentLocaleChangedLocked();
853 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(
854 mContext.getPackageManager(),
855 mSettings.getEnabledInputMethodListLocked());
satok0a1bcf42012-05-16 19:26:31 +0900856 }
857 mLastSystemLocale = mRes.getConfiguration().locale;
Dianne Hackborncc278702009-09-02 23:07:23 -0700858 try {
859 startInputInnerLocked();
860 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800861 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700862 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700863 }
864 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800865 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800866
satok15452a42011-10-28 17:58:28 +0900867 private void setImeWindowVisibilityStatusHiddenLocked() {
868 mImeWindowVis = 0;
869 updateImeWindowStatusLocked();
870 }
871
satok3afd6c02011-11-18 08:38:19 +0900872 private void refreshImeWindowVisibilityLocked() {
873 final Configuration conf = mRes.getConfiguration();
874 final boolean haveHardKeyboard = conf.keyboard
875 != Configuration.KEYBOARD_NOKEYS;
876 final boolean hardKeyShown = haveHardKeyboard
877 && conf.hardKeyboardHidden
878 != Configuration.HARDKEYBOARDHIDDEN_YES;
John Spurlocke0980502013-10-25 11:59:29 -0400879
880 final boolean isScreenLocked = isKeyguardLocked();
881 final boolean inputActive = !isScreenLocked && (mInputShown || hardKeyShown);
John Spurlock36439b42013-10-23 16:36:47 -0400882 // We assume the softkeyboard is shown when the input is active as long as the
883 // hard keyboard is not shown.
884 final boolean inputVisible = inputActive && !hardKeyShown;
885 mImeWindowVis = (inputActive ? InputMethodService.IME_ACTIVE : 0)
886 | (inputVisible ? InputMethodService.IME_VISIBLE : 0);
satok3afd6c02011-11-18 08:38:19 +0900887 updateImeWindowStatusLocked();
888 }
889
satok15452a42011-10-28 17:58:28 +0900890 private void updateImeWindowStatusLocked() {
satokdbf29502011-08-25 15:28:23 +0900891 setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700892 }
893
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900894 // ---------------------------------------------------------------------------------------
895 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
896 // 1) it comes from the system process
897 // 2) the calling process' user id is identical to the current user id IMMS thinks.
898 private boolean calledFromValidUser() {
899 final int uid = Binder.getCallingUid();
900 final int userId = UserHandle.getUserId(uid);
901 if (DEBUG) {
902 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
903 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
904 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +0900905 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
906 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900907 }
908 if (uid == Process.SYSTEM_UID || userId == mSettings.getCurrentUserId()) {
909 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900910 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +0900911
912 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
913 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
914 // must not manage background users' states in any functions.
915 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
916 // by a token.
917 if (mContext.checkCallingOrSelfPermission(
918 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
919 == PackageManager.PERMISSION_GRANTED) {
920 if (DEBUG) {
921 Slog.d(TAG, "--- Access granted because the calling process has "
922 + "the INTERACT_ACROSS_USERS_FULL permission");
923 }
924 return true;
925 }
Satoshi Kataoka0766eb02013-07-31 18:30:13 +0900926 Slog.w(TAG, "--- IPC called from background users. Ignore. \n"
927 + InputMethodUtils.getStackTrace());
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +0900928 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900929 }
930
931 private boolean bindCurrentInputMethodService(
932 Intent service, ServiceConnection conn, int flags) {
933 if (service == null || conn == null) {
934 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
935 return false;
936 }
Amith Yamasani27b89e62013-01-16 12:30:11 -0800937 return mContext.bindServiceAsUser(service, conn, flags,
938 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900939 }
940
satoke7c6998e2011-06-03 17:57:59 +0900941 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800942 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900943 // TODO: Make this work even for non-current users?
944 if (!calledFromValidUser()) {
945 return Collections.emptyList();
946 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800947 synchronized (mMethodMap) {
948 return new ArrayList<InputMethodInfo>(mMethodList);
949 }
950 }
951
satoke7c6998e2011-06-03 17:57:59 +0900952 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900954 // TODO: Make this work even for non-current users?
955 if (!calledFromValidUser()) {
956 return Collections.emptyList();
957 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800958 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900959 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800960 }
961 }
962
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900963 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +0900964 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900965 * @return enabled subtypes of the specified imi
966 */
satoke7c6998e2011-06-03 17:57:59 +0900967 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +0900968 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +0900969 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900970 // TODO: Make this work even for non-current users?
971 if (!calledFromValidUser()) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +0900972 return Collections.<InputMethodSubtype>emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900973 }
satok67ddf9c2010-11-17 09:45:54 +0900974 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +0900975 final InputMethodInfo imi;
976 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900977 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +0900978 } else {
979 imi = mMethodMap.get(imiId);
980 }
981 if (imi == null) {
982 return Collections.<InputMethodSubtype>emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900983 }
984 return mSettings.getEnabledInputMethodSubtypeListLocked(
985 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +0900986 }
987 }
988
satoke7c6998e2011-06-03 17:57:59 +0900989 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990 public void addClient(IInputMethodClient client,
991 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900992 if (!calledFromValidUser()) {
993 return;
994 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800995 synchronized (mMethodMap) {
996 mClients.put(client.asBinder(), new ClientState(client,
997 inputContext, uid, pid));
998 }
999 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001000
satoke7c6998e2011-06-03 17:57:59 +09001001 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001002 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001003 if (!calledFromValidUser()) {
1004 return;
1005 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001006 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001007 ClientState cs = mClients.remove(client.asBinder());
1008 if (cs != null) {
1009 clearClientSessionLocked(cs);
1010 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011 }
1012 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001013
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001014 void executeOrSendMessage(IInterface target, Message msg) {
1015 if (target.asBinder() instanceof Binder) {
1016 mCaller.sendMessage(msg);
1017 } else {
1018 handleMessage(msg);
1019 msg.recycle();
1020 }
1021 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001022
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001023 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001025 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026 + mCurClient.client.asBinder());
1027 if (mBoundToMethod) {
1028 mBoundToMethod = false;
1029 if (mCurMethod != null) {
1030 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1031 MSG_UNBIND_INPUT, mCurMethod));
1032 }
1033 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001034
1035 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1036 MSG_SET_ACTIVE, 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1038 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1039 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001041
The Android Open Source Project10592532009-03-18 17:39:46 -07001042 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001043 }
1044 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001045
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 private int getImeShowFlags() {
1047 int flags = 0;
1048 if (mShowForced) {
1049 flags |= InputMethod.SHOW_FORCED
1050 | InputMethod.SHOW_EXPLICIT;
1051 } else if (mShowExplicitlyRequested) {
1052 flags |= InputMethod.SHOW_EXPLICIT;
1053 }
1054 return flags;
1055 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001056
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001057 private int getAppShowFlags() {
1058 int flags = 0;
1059 if (mShowForced) {
1060 flags |= InputMethodManager.SHOW_FORCED;
1061 } else if (!mShowExplicitlyRequested) {
1062 flags |= InputMethodManager.SHOW_IMPLICIT;
1063 }
1064 return flags;
1065 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001066
Dianne Hackborn7663d802012-02-24 13:08:49 -08001067 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001068 if (!mBoundToMethod) {
1069 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1070 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1071 mBoundToMethod = true;
1072 }
1073 final SessionState session = mCurClient.curSession;
1074 if (initial) {
1075 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1076 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
1077 } else {
1078 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1079 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
1080 }
1081 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001082 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001083 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001084 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001085 return new InputBindResult(session.session,
1086 session.channel != null ? session.channel.dup() : null, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001087 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001088
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001089 InputBindResult startInputLocked(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001090 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 // If no method is currently selected, do nothing.
1092 if (mCurMethodId == null) {
1093 return mNoBinding;
1094 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001096 ClientState cs = mClients.get(client.asBinder());
1097 if (cs == null) {
1098 throw new IllegalArgumentException("unknown client "
1099 + client.asBinder());
1100 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001102 try {
1103 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1104 // Check with the window manager to make sure this client actually
1105 // has a window with focus. If not, reject. This is thread safe
1106 // because if the focus changes some time before or after, the
1107 // next client receiving focus that has any interest in input will
1108 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001109 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001110 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1111 return null;
1112 }
1113 } catch (RemoteException e) {
1114 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001115
Dianne Hackborn7663d802012-02-24 13:08:49 -08001116 return startInputUncheckedLocked(cs, inputContext, attribute, controlFlags);
1117 }
1118
1119 InputBindResult startInputUncheckedLocked(ClientState cs,
1120 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
1121 // If no method is currently selected, do nothing.
1122 if (mCurMethodId == null) {
1123 return mNoBinding;
1124 }
1125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001126 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001127 // Was the keyguard locked when switching over to the new client?
1128 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001129 // If the client is changing, we need to switch over to the new
1130 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001131 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001132 if (DEBUG) Slog.v(TAG, "switching to client: client = "
John Spurlocke0980502013-10-25 11:59:29 -04001133 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001134
1135 // If the screen is on, inform the new client it is active
1136 if (mScreenOn) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001137 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
1138 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001139 }
1140 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001142 // Bump up the sequence for this client and attach it.
1143 mCurSeq++;
1144 if (mCurSeq <= 0) mCurSeq = 1;
1145 mCurClient = cs;
1146 mCurInputContext = inputContext;
1147 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001149 // Check if the input method is changing.
1150 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1151 if (cs.curSession != null) {
1152 // Fast case: if we are already connected to the input method,
1153 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001154 return attachNewInputLocked(
1155 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001156 }
1157 if (mHaveConnection) {
1158 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 // Return to client, and we will get back with it when
1160 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001161 requestClientSessionLocked(cs);
1162 return new InputBindResult(null, null, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163 } else if (SystemClock.uptimeMillis()
1164 < (mLastBindTime+TIME_TO_RECONNECT)) {
1165 // In this case we have connected to the service, but
1166 // don't yet have its interface. If it hasn't been too
1167 // long since we did the connection, we'll return to
1168 // the client and wait to get the service interface so
1169 // we can report back. If it has been too long, we want
1170 // to fall through so we can try a disconnect/reconnect
1171 // to see if we can get back in touch with the service.
Jeff Brownc28867a2013-03-26 15:42:39 -07001172 return new InputBindResult(null, null, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001174 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1175 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001176 }
1177 }
1178 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001179
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001180 return startInputInnerLocked();
1181 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001182
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001183 InputBindResult startInputInnerLocked() {
1184 if (mCurMethodId == null) {
1185 return mNoBinding;
1186 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001187
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001188 if (!mSystemReady) {
1189 // If the system is not yet ready, we shouldn't be running third
1190 // party code.
Jeff Brownc28867a2013-03-26 15:42:39 -07001191 return new InputBindResult(null, null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001192 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1195 if (info == null) {
1196 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1197 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001198
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001199 unbindCurrentMethodLocked(false, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001201 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1202 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001203 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1204 com.android.internal.R.string.input_method_binding_label);
1205 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1206 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001207 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornc8230512013-07-13 21:32:12 -07001208 | Context.BIND_NOT_VISIBLE | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209 mLastBindTime = SystemClock.uptimeMillis();
1210 mHaveConnection = true;
1211 mCurId = info.getId();
1212 mCurToken = new Binder();
1213 try {
Craig Mautnerca0ac712013-03-14 09:43:02 -07001214 if (true || DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 mIWindowManager.addWindowToken(mCurToken,
1216 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1217 } catch (RemoteException e) {
1218 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001219 return new InputBindResult(null, null, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001220 } else {
1221 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001222 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 + mCurIntent);
1224 }
1225 return null;
1226 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001227
satoke7c6998e2011-06-03 17:57:59 +09001228 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 public InputBindResult startInput(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001230 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001231 if (!calledFromValidUser()) {
1232 return null;
1233 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001234 synchronized (mMethodMap) {
1235 final long ident = Binder.clearCallingIdentity();
1236 try {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001237 return startInputLocked(client, inputContext, attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 } finally {
1239 Binder.restoreCallingIdentity(ident);
1240 }
1241 }
1242 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001243
satoke7c6998e2011-06-03 17:57:59 +09001244 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 public void finishInput(IInputMethodClient client) {
1246 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001247
satoke7c6998e2011-06-03 17:57:59 +09001248 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001249 public void onServiceConnected(ComponentName name, IBinder service) {
1250 synchronized (mMethodMap) {
1251 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1252 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001253 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001254 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001255 unbindCurrentMethodLocked(false, false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001256 return;
1257 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001258 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001259 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1260 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001261 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001262 clearClientSessionLocked(mCurClient);
1263 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 }
1265 }
1266 }
1267 }
1268
Jeff Brownc28867a2013-03-26 15:42:39 -07001269 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1270 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001271 synchronized (mMethodMap) {
1272 if (mCurMethod != null && method != null
1273 && mCurMethod.asBinder() == method.asBinder()) {
1274 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001275 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001276 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001277 method, session, channel);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001278 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001279 if (res.method != null) {
1280 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
1281 MSG_BIND_METHOD, mCurClient.client, res));
1282 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001283 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 }
1285 }
1286 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001287
1288 // Session abandoned. Close its associated input channel.
1289 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001290 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001291
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001292 void unbindCurrentMethodLocked(boolean reportToClient, boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001293 if (mVisibleBound) {
1294 mContext.unbindService(mVisibleConnection);
1295 mVisibleBound = false;
1296 }
1297
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001298 if (mHaveConnection) {
1299 mContext.unbindService(this);
1300 mHaveConnection = false;
1301 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001302
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001303 if (mCurToken != null) {
1304 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001305 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001306 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001307 // The current IME is shown. Hence an IME switch (transition) is happening.
1308 mWindowManagerService.saveLastInputMethodWindowForTransition();
1309 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001310 mIWindowManager.removeWindowToken(mCurToken);
1311 } catch (RemoteException e) {
1312 }
1313 mCurToken = null;
1314 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001315
The Android Open Source Project10592532009-03-18 17:39:46 -07001316 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001317 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001318
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001319 if (reportToClient && mCurClient != null) {
1320 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1321 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1322 }
1323 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001324
1325 void requestClientSessionLocked(ClientState cs) {
1326 if (!cs.sessionRequested) {
1327 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1328 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1329 cs.sessionRequested = true;
1330 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1331 MSG_CREATE_SESSION, mCurMethod, channels[1],
1332 new MethodCallback(this, mCurMethod, channels[0])));
1333 }
1334 }
1335
1336 void clearClientSessionLocked(ClientState cs) {
1337 finishSessionLocked(cs.curSession);
1338 cs.curSession = null;
1339 cs.sessionRequested = false;
1340 }
1341
1342 private void finishSessionLocked(SessionState sessionState) {
1343 if (sessionState != null) {
1344 if (sessionState.session != null) {
1345 try {
1346 sessionState.session.finishSession();
1347 } catch (RemoteException e) {
1348 Slog.w(TAG, "Session failed to close due to remote exception", e);
1349 setImeWindowVisibilityStatusHiddenLocked();
1350 }
1351 sessionState.session = null;
1352 }
1353 if (sessionState.channel != null) {
1354 sessionState.channel.dispose();
1355 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001356 }
1357 }
1358 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001359
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001360 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001361 if (mCurMethod != null) {
1362 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001363 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001364 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001365
Jeff Brownc28867a2013-03-26 15:42:39 -07001366 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001367 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001368 mCurMethod = null;
1369 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001370 if (mStatusBar != null) {
1371 mStatusBar.setIconVisibility("ime", false);
1372 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001373 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001374
satoke7c6998e2011-06-03 17:57:59 +09001375 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001376 public void onServiceDisconnected(ComponentName name) {
1377 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001378 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001379 + " mCurIntent=" + mCurIntent);
1380 if (mCurMethod != null && mCurIntent != null
1381 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001382 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001383 // We consider this to be a new bind attempt, since the system
1384 // should now try to restart the service for us.
1385 mLastBindTime = SystemClock.uptimeMillis();
1386 mShowRequested = mInputShown;
1387 mInputShown = false;
1388 if (mCurClient != null) {
1389 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1390 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1391 }
1392 }
1393 }
1394 }
1395
satokf9f01002011-05-19 21:31:50 +09001396 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001397 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001398 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001399 long ident = Binder.clearCallingIdentity();
1400 try {
1401 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001402 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001403 return;
1404 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001406 synchronized (mMethodMap) {
1407 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001408 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001409 if (mStatusBar != null) {
1410 mStatusBar.setIconVisibility("ime", false);
1411 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001412 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001413 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001414 CharSequence contentDescription = null;
1415 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001416 // Use PackageManager to load label
1417 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001418 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001419 mIPackageManager.getApplicationInfo(packageName, 0,
1420 mSettings.getCurrentUserId()));
1421 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001422 /* ignore */
1423 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001424 if (mStatusBar != null) {
1425 mStatusBar.setIcon("ime", packageName, iconId, 0,
1426 contentDescription != null
1427 ? contentDescription.toString() : null);
1428 mStatusBar.setIconVisibility("ime", true);
1429 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001430 }
1431 }
1432 } finally {
1433 Binder.restoreCallingIdentity(ident);
1434 }
1435 }
1436
satok7cfc0ed2011-06-20 21:29:36 +09001437 private boolean needsToShowImeSwitchOngoingNotification() {
1438 if (!mShowOngoingImeSwitcherForPhones) return false;
satok2c93efc2012-04-02 19:33:47 +09001439 if (isScreenLocked()) return false;
satok7cfc0ed2011-06-20 21:29:36 +09001440 synchronized (mMethodMap) {
1441 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1442 final int N = imis.size();
satokb6359412011-06-28 17:47:41 +09001443 if (N > 2) return true;
1444 if (N < 1) return false;
1445 int nonAuxCount = 0;
1446 int auxCount = 0;
1447 InputMethodSubtype nonAuxSubtype = null;
1448 InputMethodSubtype auxSubtype = null;
satok7cfc0ed2011-06-20 21:29:36 +09001449 for(int i = 0; i < N; ++i) {
1450 final InputMethodInfo imi = imis.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001451 final List<InputMethodSubtype> subtypes =
1452 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satok7cfc0ed2011-06-20 21:29:36 +09001453 final int subtypeCount = subtypes.size();
1454 if (subtypeCount == 0) {
satokb6359412011-06-28 17:47:41 +09001455 ++nonAuxCount;
satok7cfc0ed2011-06-20 21:29:36 +09001456 } else {
1457 for (int j = 0; j < subtypeCount; ++j) {
satokb6359412011-06-28 17:47:41 +09001458 final InputMethodSubtype subtype = subtypes.get(j);
1459 if (!subtype.isAuxiliary()) {
1460 ++nonAuxCount;
1461 nonAuxSubtype = subtype;
1462 } else {
1463 ++auxCount;
1464 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001465 }
1466 }
1467 }
satok7cfc0ed2011-06-20 21:29:36 +09001468 }
satokb6359412011-06-28 17:47:41 +09001469 if (nonAuxCount > 1 || auxCount > 1) {
1470 return true;
1471 } else if (nonAuxCount == 1 && auxCount == 1) {
1472 if (nonAuxSubtype != null && auxSubtype != null
satok9747f892011-09-12 15:56:40 +09001473 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1474 || auxSubtype.overridesImplicitlyEnabledSubtype()
1475 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
satokb6359412011-06-28 17:47:41 +09001476 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1477 return false;
1478 }
1479 return true;
1480 }
1481 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001482 }
satok7cfc0ed2011-06-20 21:29:36 +09001483 }
1484
John Spurlocke0980502013-10-25 11:59:29 -04001485 private boolean isKeyguardLocked() {
1486 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1487 }
1488
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001489 // Caution! This method is called in this class. Handle multi-user carefully
satokdbf29502011-08-25 15:28:23 +09001490 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001491 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001492 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001493 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001494 try {
1495 if (token == null || mCurToken != token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001496 int uid = Binder.getCallingUid();
Joe Onorato857fd9b2011-01-27 15:08:35 -08001497 Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token);
satok06487a52010-10-29 11:37:18 +09001498 return;
1499 }
satok06487a52010-10-29 11:37:18 +09001500 synchronized (mMethodMap) {
John Spurlocke0980502013-10-25 11:59:29 -04001501 // apply policy for binder calls
1502 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
1503 vis = 0;
1504 }
Joe Onorato857fd9b2011-01-27 15:08:35 -08001505 mImeWindowVis = vis;
1506 mBackDisposition = backDisposition;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001507 if (mStatusBar != null) {
1508 mStatusBar.setImeWindowStatus(token, vis, backDisposition);
1509 }
jungheang.lee217fd292013-02-26 16:53:22 +09001510 final boolean iconVisibility = ((vis & (InputMethodService.IME_ACTIVE)) != 0)
1511 && (mWindowManagerService.isHardKeyboardAvailable()
1512 || (vis & (InputMethodService.IME_VISIBLE)) != 0);
satok5bc8e732011-07-22 21:07:23 +09001513 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1514 if (imi != null && iconVisibility && needsToShowImeSwitchOngoingNotification()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001515 // Used to load label
satok7cfc0ed2011-06-20 21:29:36 +09001516 final CharSequence title = mRes.getText(
1517 com.android.internal.R.string.select_input_method);
Satoshi Kataokab2827262013-07-04 19:43:14 +09001518 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
1519 mContext, imi, mCurrentSubtype);
satok5bc8e732011-07-22 21:07:23 +09001520
satok7cfc0ed2011-06-20 21:29:36 +09001521 mImeSwitcherNotification.setLatestEventInfo(
satok5bc8e732011-07-22 21:07:23 +09001522 mContext, title, summary, mImeSwitchPendingIntent);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001523 if (mNotificationManager != null) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001524 if (DEBUG) {
Satoshi Kataokab2827262013-07-04 19:43:14 +09001525 Slog.d(TAG, "--- show notification: label = " + summary);
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001526 }
1527 mNotificationManager.notifyAsUser(null,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001528 com.android.internal.R.string.select_input_method,
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001529 mImeSwitcherNotification, UserHandle.ALL);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001530 mNotificationShown = true;
1531 }
satok7cfc0ed2011-06-20 21:29:36 +09001532 } else {
Dianne Hackborn661cd522011-08-22 00:26:20 -07001533 if (mNotificationShown && mNotificationManager != null) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001534 if (DEBUG) {
1535 Slog.d(TAG, "--- hide notification");
1536 }
1537 mNotificationManager.cancelAsUser(null,
1538 com.android.internal.R.string.select_input_method, UserHandle.ALL);
satok7cfc0ed2011-06-20 21:29:36 +09001539 mNotificationShown = false;
1540 }
1541 }
satok06487a52010-10-29 11:37:18 +09001542 }
1543 } finally {
1544 Binder.restoreCallingIdentity(ident);
1545 }
1546 }
1547
satoke7c6998e2011-06-03 17:57:59 +09001548 @Override
satokf9f01002011-05-19 21:31:50 +09001549 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001550 if (!calledFromValidUser()) {
1551 return;
1552 }
satokf9f01002011-05-19 21:31:50 +09001553 synchronized (mMethodMap) {
1554 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1555 for (int i = 0; i < spans.length; ++i) {
1556 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001557 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001558 mSecureSuggestionSpans.put(ss, currentImi);
1559 }
1560 }
1561 }
1562 }
1563
satoke7c6998e2011-06-03 17:57:59 +09001564 @Override
satokf9f01002011-05-19 21:31:50 +09001565 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001566 if (!calledFromValidUser()) {
1567 return false;
1568 }
satokf9f01002011-05-19 21:31:50 +09001569 synchronized (mMethodMap) {
1570 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1571 // TODO: Do not send the intent if the process of the targetImi is already dead.
1572 if (targetImi != null) {
1573 final String[] suggestions = span.getSuggestions();
1574 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001575 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001576 final Intent intent = new Intent();
1577 // Ensures that only a class in the original IME package will receive the
1578 // notification.
satok42c5a162011-05-26 16:46:14 +09001579 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001580 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1581 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1582 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1583 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001584 final long ident = Binder.clearCallingIdentity();
1585 try {
1586 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1587 } finally {
1588 Binder.restoreCallingIdentity(ident);
1589 }
satokf9f01002011-05-19 21:31:50 +09001590 return true;
1591 }
1592 }
1593 return false;
1594 }
1595
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001596 void updateFromSettingsLocked(boolean enabledMayChange) {
1597 if (enabledMayChange) {
1598 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1599 for (int i=0; i<enabled.size(); i++) {
1600 // We allow the user to select "disabled until used" apps, so if they
1601 // are enabling one of those here we now need to make it enabled.
1602 InputMethodInfo imm = enabled.get(i);
1603 try {
1604 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1605 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1606 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09001607 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001608 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001609 if (DEBUG) {
1610 Slog.d(TAG, "Update state(" + imm.getId()
1611 + "): DISABLED_UNTIL_USED -> DEFAULT");
1612 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001613 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1614 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001615 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
1616 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001617 }
1618 } catch (RemoteException e) {
1619 }
1620 }
1621 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001622 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1623 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1624 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1625 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001626 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001627 // There is no input method selected, try to choose new applicable input method.
1628 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001629 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001630 }
1631 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001632 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001633 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001634 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001635 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001636 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001637 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001638 }
satokf3db1af2010-11-23 13:34:33 +09001639 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001640 } else {
1641 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001642 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001643 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001644 }
1645 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001646
satokab751aa2010-09-14 19:17:36 +09001647 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001648 InputMethodInfo info = mMethodMap.get(id);
1649 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001650 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001651 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001652
satokd81e9502012-05-21 12:58:45 +09001653 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001654 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09001655 final int subtypeCount = info.getSubtypeCount();
1656 if (subtypeCount <= 0) {
1657 return;
satokcd7cd292010-11-20 15:46:23 +09001658 }
satokd81e9502012-05-21 12:58:45 +09001659 final InputMethodSubtype oldSubtype = mCurrentSubtype;
1660 final InputMethodSubtype newSubtype;
1661 if (subtypeId >= 0 && subtypeId < subtypeCount) {
1662 newSubtype = info.getSubtypeAt(subtypeId);
1663 } else {
1664 // If subtype is null, try to find the most applicable one from
1665 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001666 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09001667 }
1668 if (newSubtype == null || oldSubtype == null) {
1669 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
1670 + ", new subtype = " + newSubtype);
1671 return;
1672 }
1673 if (newSubtype != oldSubtype) {
1674 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1675 if (mCurMethod != null) {
1676 try {
1677 refreshImeWindowVisibilityLocked();
1678 mCurMethod.changeInputMethodSubtype(newSubtype);
1679 } catch (RemoteException e) {
1680 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
satokab751aa2010-09-14 19:17:36 +09001681 }
1682 }
1683 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001684 return;
1685 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001686
satokd81e9502012-05-21 12:58:45 +09001687 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001688 final long ident = Binder.clearCallingIdentity();
1689 try {
satokab751aa2010-09-14 19:17:36 +09001690 // Set a subtype to this input method.
1691 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001692 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1693 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1694 // because mCurMethodId is stored as a history in
1695 // setSelectedInputMethodAndSubtypeLocked().
1696 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001697
1698 if (ActivityManagerNative.isSystemReady()) {
1699 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001700 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001701 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07001702 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001703 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001704 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001705 } finally {
1706 Binder.restoreCallingIdentity(ident);
1707 }
1708 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001709
satok42c5a162011-05-26 16:46:14 +09001710 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001711 public boolean showSoftInput(IInputMethodClient client, int flags,
1712 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001713 if (!calledFromValidUser()) {
1714 return false;
1715 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001716 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001717 long ident = Binder.clearCallingIdentity();
1718 try {
1719 synchronized (mMethodMap) {
1720 if (mCurClient == null || client == null
1721 || mCurClient.client.asBinder() != client.asBinder()) {
1722 try {
1723 // We need to check if this is the current client with
1724 // focus in the window manager, to allow this call to
1725 // be made before input is started in it.
1726 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001727 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001728 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001729 }
1730 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001731 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001732 }
1733 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001734
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001735 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001736 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001737 }
1738 } finally {
1739 Binder.restoreCallingIdentity(ident);
1740 }
1741 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001742
The Android Open Source Project4df24232009-03-05 14:34:35 -08001743 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001744 mShowRequested = true;
1745 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1746 mShowExplicitlyRequested = true;
1747 }
1748 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1749 mShowExplicitlyRequested = true;
1750 mShowForced = true;
1751 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001752
Dianne Hackborncc278702009-09-02 23:07:23 -07001753 if (!mSystemReady) {
1754 return false;
1755 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001756
The Android Open Source Project4df24232009-03-05 14:34:35 -08001757 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001758 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001759 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001760 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1761 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1762 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001763 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001764 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001765 bindCurrentInputMethodService(
1766 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001767 mVisibleBound = true;
1768 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001769 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001770 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09001771 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001772 // The client has asked to have the input method shown, but
1773 // we have been sitting here too long with a connection to the
1774 // service and no interface received, so let's disconnect/connect
1775 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001776 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001777 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09001778 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001779 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001780 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001781 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001782 } else {
1783 if (DEBUG) {
1784 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
1785 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
1786 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001787 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001788
The Android Open Source Project4df24232009-03-05 14:34:35 -08001789 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001790 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001791
satok42c5a162011-05-26 16:46:14 +09001792 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001793 public boolean hideSoftInput(IInputMethodClient client, int flags,
1794 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001795 if (!calledFromValidUser()) {
1796 return false;
1797 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001798 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001799 long ident = Binder.clearCallingIdentity();
1800 try {
1801 synchronized (mMethodMap) {
1802 if (mCurClient == null || client == null
1803 || mCurClient.client.asBinder() != client.asBinder()) {
1804 try {
1805 // We need to check if this is the current client with
1806 // focus in the window manager, to allow this call to
1807 // be made before input is started in it.
1808 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001809 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1810 + uid + ": " + client);
satok15452a42011-10-28 17:58:28 +09001811 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001812 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001813 }
1814 } catch (RemoteException e) {
satok15452a42011-10-28 17:58:28 +09001815 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001816 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001817 }
1818 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001819
Joe Onorato8a9b2202010-02-26 18:56:32 -08001820 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001821 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001822 }
1823 } finally {
1824 Binder.restoreCallingIdentity(ident);
1825 }
1826 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001827
The Android Open Source Project4df24232009-03-05 14:34:35 -08001828 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001829 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1830 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001831 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 -08001832 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001833 }
1834 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001835 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 -08001836 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001837 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001838 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001839 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001840 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1841 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1842 res = true;
1843 } else {
1844 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001845 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001846 if (mHaveConnection && mVisibleBound) {
1847 mContext.unbindService(mVisibleConnection);
1848 mVisibleBound = false;
1849 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001850 mInputShown = false;
1851 mShowRequested = false;
1852 mShowExplicitlyRequested = false;
1853 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001854 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001855 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001856
satok42c5a162011-05-26 16:46:14 +09001857 @Override
Dianne Hackborn7663d802012-02-24 13:08:49 -08001858 public InputBindResult windowGainedFocus(IInputMethodClient client, IBinder windowToken,
1859 int controlFlags, int softInputMode, int windowFlags,
1860 EditorInfo attribute, IInputContext inputContext) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09001861 // Needs to check the validity before clearing calling identity
1862 final boolean calledFromValidUser = calledFromValidUser();
1863
Dianne Hackborn7663d802012-02-24 13:08:49 -08001864 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001865 long ident = Binder.clearCallingIdentity();
1866 try {
1867 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001868 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
Dianne Hackborn7663d802012-02-24 13:08:49 -08001869 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001870 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08001871 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001872
Dianne Hackborn7663d802012-02-24 13:08:49 -08001873 ClientState cs = mClients.get(client.asBinder());
1874 if (cs == null) {
1875 throw new IllegalArgumentException("unknown client "
1876 + client.asBinder());
1877 }
1878
1879 try {
1880 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1881 // Check with the window manager to make sure this client actually
1882 // has a window with focus. If not, reject. This is thread safe
1883 // because if the focus changes some time before or after, the
1884 // next client receiving focus that has any interest in input will
1885 // be calling through here after that change happens.
1886 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
1887 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1888 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001889 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001890 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001891 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001892
Satoshi Kataoka8d033052012-11-19 17:30:40 +09001893 if (!calledFromValidUser) {
1894 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
1895 Slog.w(TAG, "If you want to interect with IME, you need "
1896 + "android.permission.INTERACT_ACROSS_USERS_FULL");
1897 hideCurrentInputLocked(0, null);
1898 return null;
1899 }
1900
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001901 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07001902 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09001903 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001904 if (attribute != null) {
1905 return startInputUncheckedLocked(cs, inputContext, attribute,
1906 controlFlags);
1907 }
1908 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001909 }
1910 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001911
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001912 // Should we auto-show the IME even if the caller has not
1913 // specified what should be done with it?
1914 // We only do this automatically if the window can resize
1915 // to accommodate the IME (so what the user sees will give
1916 // them good context without input information being obscured
1917 // by the IME) or if running on a large screen where there
1918 // is more room for the target window + IME.
1919 final boolean doAutoShow =
1920 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1921 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1922 || mRes.getConfiguration().isLayoutSizeAtLeast(
1923 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001924 final boolean isTextEditor =
1925 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
1926
1927 // We want to start input before showing the IME, but after closing
1928 // it. We want to do this after closing it to help the IME disappear
1929 // more quickly (not get stuck behind it initializing itself for the
1930 // new focused input, even if its window wants to hide the IME).
1931 boolean didStart = false;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001932
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001933 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1934 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001935 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001936 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1937 // There is no focus view, and this window will
1938 // be behind any soft input window, so hide the
1939 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001940 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001941 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001942 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001943 } else if (isTextEditor && doAutoShow && (softInputMode &
1944 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001945 // There is a focus view, and we are navigating forward
1946 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001947 // We only do this automatically if the window can resize
1948 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001949 // them good context without input information being obscured
1950 // by the IME) or if running on a large screen where there
1951 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001952 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001953 if (attribute != null) {
1954 res = startInputUncheckedLocked(cs, inputContext, attribute,
1955 controlFlags);
1956 didStart = true;
1957 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001958 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001959 }
1960 break;
1961 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1962 // Do nothing.
1963 break;
1964 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1965 if ((softInputMode &
1966 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001967 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001968 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001969 }
1970 break;
1971 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001972 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001973 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001974 break;
1975 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1976 if ((softInputMode &
1977 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001978 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001979 if (attribute != null) {
1980 res = startInputUncheckedLocked(cs, inputContext, attribute,
1981 controlFlags);
1982 didStart = true;
1983 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001984 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001985 }
1986 break;
1987 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001988 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001989 if (attribute != null) {
1990 res = startInputUncheckedLocked(cs, inputContext, attribute,
1991 controlFlags);
1992 didStart = true;
1993 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001994 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001995 break;
1996 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001997
1998 if (!didStart && attribute != null) {
1999 res = startInputUncheckedLocked(cs, inputContext, attribute,
2000 controlFlags);
2001 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002002 }
2003 } finally {
2004 Binder.restoreCallingIdentity(ident);
2005 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002006
2007 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002008 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002009
satok42c5a162011-05-26 16:46:14 +09002010 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002011 public void showInputMethodPickerFromClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002012 if (!calledFromValidUser()) {
2013 return;
2014 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002015 synchronized (mMethodMap) {
2016 if (mCurClient == null || client == null
2017 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002018 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002019 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002020 }
2021
satok440aab52010-11-25 09:43:11 +09002022 // Always call subtype picker, because subtype picker is a superset of input method
2023 // picker.
satokab751aa2010-09-14 19:17:36 +09002024 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
2025 }
2026 }
2027
satok42c5a162011-05-26 16:46:14 +09002028 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002029 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002030 if (!calledFromValidUser()) {
2031 return;
2032 }
satok28203512010-11-24 11:06:49 +09002033 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2034 }
2035
satok42c5a162011-05-26 16:46:14 +09002036 @Override
satok28203512010-11-24 11:06:49 +09002037 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002038 if (!calledFromValidUser()) {
2039 return;
2040 }
satok28203512010-11-24 11:06:49 +09002041 synchronized (mMethodMap) {
2042 if (subtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002043 setInputMethodWithSubtypeId(token, id, InputMethodUtils.getSubtypeIdFromHashCode(
satok28203512010-11-24 11:06:49 +09002044 mMethodMap.get(id), subtype.hashCode()));
2045 } else {
2046 setInputMethod(token, id);
2047 }
2048 }
satokab751aa2010-09-14 19:17:36 +09002049 }
2050
satok42c5a162011-05-26 16:46:14 +09002051 @Override
satokb416a712010-11-25 20:42:14 +09002052 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002053 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002054 if (!calledFromValidUser()) {
2055 return;
2056 }
satokb416a712010-11-25 20:42:14 +09002057 synchronized (mMethodMap) {
2058 if (mCurClient == null || client == null
2059 || mCurClient.client.asBinder() != client.asBinder()) {
2060 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
2061 }
satok7fee71f2010-12-17 18:54:26 +09002062 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2063 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002064 }
2065 }
2066
satok4fc87d62011-05-20 16:13:43 +09002067 @Override
satok735cf382010-11-11 20:40:09 +09002068 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002069 if (!calledFromValidUser()) {
2070 return false;
2071 }
satok735cf382010-11-11 20:40:09 +09002072 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002073 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002074 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002075 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002076 lastImi = mMethodMap.get(lastIme.first);
2077 } else {
2078 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002079 }
satok4fc87d62011-05-20 16:13:43 +09002080 String targetLastImiId = null;
2081 int subtypeId = NOT_A_SUBTYPE_ID;
2082 if (lastIme != null && lastImi != null) {
2083 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
2084 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
2085 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2086 : mCurrentSubtype.hashCode();
2087 // If the last IME is the same as the current IME and the last subtype is not
2088 // defined, there is no need to switch to the last IME.
2089 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2090 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002091 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002092 }
2093 }
2094
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002095 if (TextUtils.isEmpty(targetLastImiId)
2096 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002097 // This is a safety net. If the currentSubtype can't be added to the history
2098 // and the framework couldn't find the last ime, we will make the last ime be
2099 // the most applicable enabled keyboard subtype of the system imes.
2100 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2101 if (enabled != null) {
2102 final int N = enabled.size();
2103 final String locale = mCurrentSubtype == null
2104 ? mRes.getConfiguration().locale.toString()
2105 : mCurrentSubtype.getLocale();
2106 for (int i = 0; i < N; ++i) {
2107 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002108 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002109 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002110 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2111 InputMethodUtils.getSubtypes(imi),
2112 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002113 if (keyboardSubtype != null) {
2114 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002115 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002116 imi, keyboardSubtype.hashCode());
2117 if(keyboardSubtype.getLocale().equals(locale)) {
2118 break;
2119 }
2120 }
2121 }
2122 }
2123 }
2124 }
2125
2126 if (!TextUtils.isEmpty(targetLastImiId)) {
2127 if (DEBUG) {
2128 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2129 + ", from: " + mCurMethodId + ", " + subtypeId);
2130 }
2131 setInputMethodWithSubtypeId(token, targetLastImiId, subtypeId);
2132 return true;
2133 } else {
2134 return false;
2135 }
satok735cf382010-11-11 20:40:09 +09002136 }
2137 }
2138
satoke7c6998e2011-06-03 17:57:59 +09002139 @Override
satok688bd472012-02-09 20:09:17 +09002140 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002141 if (!calledFromValidUser()) {
2142 return false;
2143 }
satok688bd472012-02-09 20:09:17 +09002144 synchronized (mMethodMap) {
Satoshi Kataokad787f692013-10-26 04:44:21 +09002145 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethod(
satok688bd472012-02-09 20:09:17 +09002146 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2147 if (nextSubtype == null) {
2148 return false;
2149 }
2150 setInputMethodWithSubtypeId(token, nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
2151 return true;
2152 }
2153 }
2154
2155 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002156 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2157 if (!calledFromValidUser()) {
2158 return false;
2159 }
2160 synchronized (mMethodMap) {
Satoshi Kataokad787f692013-10-26 04:44:21 +09002161 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethod(
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002162 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2163 if (nextSubtype == null) {
2164 return false;
2165 }
2166 return true;
2167 }
2168 }
2169
2170 @Override
satok68f1b782011-04-11 14:26:04 +09002171 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002172 if (!calledFromValidUser()) {
2173 return null;
2174 }
satok68f1b782011-04-11 14:26:04 +09002175 synchronized (mMethodMap) {
2176 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2177 // TODO: Handle the case of the last IME with no subtypes
2178 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2179 || TextUtils.isEmpty(lastIme.second)) return null;
2180 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2181 if (lastImi == null) return null;
2182 try {
2183 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002184 final int lastSubtypeId =
2185 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002186 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2187 return null;
2188 }
2189 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002190 } catch (NumberFormatException e) {
2191 return null;
2192 }
2193 }
2194 }
2195
satoke7c6998e2011-06-03 17:57:59 +09002196 @Override
satokee5e77c2011-09-02 18:50:15 +09002197 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002198 if (!calledFromValidUser()) {
2199 return;
2200 }
satok91e88122011-07-18 11:11:42 +09002201 // By this IPC call, only a process which shares the same uid with the IME can add
2202 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09002203 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09002204 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002205 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002206 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002207 final String[] packageInfos;
2208 try {
2209 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2210 } catch (RemoteException e) {
2211 Slog.e(TAG, "Failed to get package infos");
2212 return;
2213 }
satok91e88122011-07-18 11:11:42 +09002214 if (packageInfos != null) {
2215 final int packageNum = packageInfos.length;
2216 for (int i = 0; i < packageNum; ++i) {
2217 if (packageInfos[i].equals(imi.getPackageName())) {
2218 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002219 final long ident = Binder.clearCallingIdentity();
2220 try {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002221 buildInputMethodListLocked(mMethodList, mMethodMap,
2222 false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002223 } finally {
2224 Binder.restoreCallingIdentity(ident);
2225 }
satokee5e77c2011-09-02 18:50:15 +09002226 return;
satok91e88122011-07-18 11:11:42 +09002227 }
2228 }
2229 }
satoke7c6998e2011-06-03 17:57:59 +09002230 }
satokee5e77c2011-09-02 18:50:15 +09002231 return;
satoke7c6998e2011-06-03 17:57:59 +09002232 }
2233
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002234 @Override
2235 public int getInputMethodWindowVisibleHeight() {
2236 return mWindowManagerService.getInputMethodWindowVisibleHeight();
2237 }
2238
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002239 @Override
2240 public void notifyTextCommitted() {
2241 if (DEBUG) {
2242 Slog.d(TAG, "Got the notification of commitText");
2243 }
2244 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2245 if (imi != null) {
2246 mSwitchingController.onCommitText(imi, mCurrentSubtype);
2247 }
2248 }
2249
satok28203512010-11-24 11:06:49 +09002250 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002251 synchronized (mMethodMap) {
2252 if (token == null) {
2253 if (mContext.checkCallingOrSelfPermission(
2254 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2255 != PackageManager.PERMISSION_GRANTED) {
2256 throw new SecurityException(
2257 "Using null token requires permission "
2258 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2259 }
2260 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002261 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2262 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002263 return;
2264 }
2265
satokc5933802011-08-31 21:26:04 +09002266 final long ident = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002267 try {
satokab751aa2010-09-14 19:17:36 +09002268 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002269 } finally {
2270 Binder.restoreCallingIdentity(ident);
2271 }
2272 }
2273 }
2274
satok42c5a162011-05-26 16:46:14 +09002275 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002276 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002277 if (!calledFromValidUser()) {
2278 return;
2279 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002280 synchronized (mMethodMap) {
2281 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002282 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
2283 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002284 return;
2285 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002286 long ident = Binder.clearCallingIdentity();
2287 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002288 hideCurrentInputLocked(flags, null);
2289 } finally {
2290 Binder.restoreCallingIdentity(ident);
2291 }
2292 }
2293 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002294
satok42c5a162011-05-26 16:46:14 +09002295 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002296 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002297 if (!calledFromValidUser()) {
2298 return;
2299 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002300 synchronized (mMethodMap) {
2301 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002302 Slog.w(TAG, "Ignoring showMySoftInput of uid "
2303 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002304 return;
2305 }
2306 long ident = Binder.clearCallingIdentity();
2307 try {
2308 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002309 } finally {
2310 Binder.restoreCallingIdentity(ident);
2311 }
2312 }
2313 }
2314
2315 void setEnabledSessionInMainThread(SessionState session) {
2316 if (mEnabledSession != session) {
2317 if (mEnabledSession != null) {
2318 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002319 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002320 mEnabledSession.method.setSessionEnabled(
2321 mEnabledSession.session, false);
2322 } catch (RemoteException e) {
2323 }
2324 }
2325 mEnabledSession = session;
2326 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002327 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002328 session.method.setSessionEnabled(
2329 session.session, true);
2330 } catch (RemoteException e) {
2331 }
2332 }
2333 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002334
satok42c5a162011-05-26 16:46:14 +09002335 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002336 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002337 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002338 switch (msg.what) {
2339 case MSG_SHOW_IM_PICKER:
2340 showInputMethodMenu();
2341 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002342
satokab751aa2010-09-14 19:17:36 +09002343 case MSG_SHOW_IM_SUBTYPE_PICKER:
2344 showInputMethodSubtypeMenu();
2345 return true;
2346
satok47a44912010-10-06 16:03:58 +09002347 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002348 args = (SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09002349 showInputMethodAndSubtypeEnabler((String)args.arg1);
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002350 args.recycle();
satok217f5482010-12-15 05:19:19 +09002351 return true;
2352
2353 case MSG_SHOW_IM_CONFIG:
2354 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002355 return true;
2356
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002357 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002359 case MSG_UNBIND_INPUT:
2360 try {
2361 ((IInputMethod)msg.obj).unbindInput();
2362 } catch (RemoteException e) {
2363 // There is nothing interesting about the method dying.
2364 }
2365 return true;
2366 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002367 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002368 try {
2369 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2370 } catch (RemoteException e) {
2371 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002372 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002373 return true;
2374 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002375 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002376 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002377 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002378 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002379 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002380 } catch (RemoteException e) {
2381 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002382 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002383 return true;
2384 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002385 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002386 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002387 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002388 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002389 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002390 } catch (RemoteException e) {
2391 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002392 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002393 return true;
2394 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002395 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002396 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002397 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002398 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2399 } catch (RemoteException e) {
2400 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002401 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002402 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002403 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002404 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002405 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002406 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002407 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002408 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002409 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002410 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002411 // Dispose the channel if the input method is not local to this process
2412 // because the remote proxy will get its own copy when unparceled.
2413 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002414 channel.dispose();
2415 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002416 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002417 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002418 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002419 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002420 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002421
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002422 case MSG_START_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002423 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002424 try {
2425 SessionState session = (SessionState)args.arg1;
2426 setEnabledSessionInMainThread(session);
2427 session.method.startInput((IInputContext)args.arg2,
2428 (EditorInfo)args.arg3);
2429 } catch (RemoteException e) {
2430 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002431 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002432 return true;
2433 case MSG_RESTART_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002434 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002435 try {
2436 SessionState session = (SessionState)args.arg1;
2437 setEnabledSessionInMainThread(session);
2438 session.method.restartInput((IInputContext)args.arg2,
2439 (EditorInfo)args.arg3);
2440 } catch (RemoteException e) {
2441 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002442 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002443 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002444
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002445 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002446
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002447 case MSG_UNBIND_METHOD:
2448 try {
2449 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
2450 } catch (RemoteException e) {
2451 // There is nothing interesting about the last client dying.
2452 }
2453 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002454 case MSG_BIND_METHOD: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002455 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002456 IInputMethodClient client = (IInputMethodClient)args.arg1;
2457 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002458 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002459 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002460 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002461 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002462 } finally {
2463 // Dispose the channel if the input method is not local to this process
2464 // because the remote proxy will get its own copy when unparceled.
2465 if (res.channel != null && Binder.isProxy(client)) {
2466 res.channel.dispose();
2467 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002468 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002469 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002470 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002471 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002472 case MSG_SET_ACTIVE:
2473 try {
2474 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2475 } catch (RemoteException e) {
2476 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2477 + ((ClientState)msg.obj).pid + " uid "
2478 + ((ClientState)msg.obj).uid);
2479 }
2480 return true;
satok01038492012-04-09 21:08:27 +09002481
2482 // --------------------------------------------------------------
2483 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
2484 mHardKeyboardListener.handleHardKeyboardStatusChange(
2485 msg.arg1 == 1, msg.arg2 == 1);
2486 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002487 }
2488 return false;
2489 }
2490
satokdc9ddae2011-10-06 12:22:36 +09002491 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002492 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
2493 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09002494 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002495 if (DEBUG) {
2496 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2497 }
satok723a27e2010-11-11 14:58:11 +09002498 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002499 return true;
2500 }
2501
2502 return false;
2503 }
2504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002505 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002506 HashMap<String, InputMethodInfo> map, boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002507 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002508 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Satoshi Kataoka0766eb02013-07-31 18:30:13 +09002509 + " \n ------ \n" + InputMethodUtils.getStackTrace());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002510 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002511 list.clear();
2512 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002513
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002514 // Use for queryIntentServicesAsUser
2515 final PackageManager pm = mContext.getPackageManager();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002516 String disabledSysImes = mSettings.getDisabledSystemInputMethods();
Amith Yamasanie861ec12010-03-24 21:39:27 -07002517 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002518
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002519 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002520 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002521 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2522 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002523
satoke7c6998e2011-06-03 17:57:59 +09002524 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2525 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002526 for (int i = 0; i < services.size(); ++i) {
2527 ResolveInfo ri = services.get(i);
2528 ServiceInfo si = ri.serviceInfo;
2529 ComponentName compName = new ComponentName(si.packageName, si.name);
2530 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2531 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002532 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002533 + ": it does not require the permission "
2534 + android.Manifest.permission.BIND_INPUT_METHOD);
2535 continue;
2536 }
2537
Joe Onorato8a9b2202010-02-26 18:56:32 -08002538 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002539
2540 try {
satoke7c6998e2011-06-03 17:57:59 +09002541 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002542 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002543 final String id = p.getId();
2544 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002545
2546 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002547 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002548 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002549
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002550 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002551 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002552 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002553 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002554 }
2555 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002556
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002557 if (resetDefaultEnabledIme) {
2558 final ArrayList<InputMethodInfo> defaultEnabledIme =
2559 InputMethodUtils.getDefaultEnabledImes(mContext, mSystemReady, list);
2560 for (int i = 0; i < defaultEnabledIme.size(); ++i) {
2561 final InputMethodInfo imi = defaultEnabledIme.get(i);
2562 if (DEBUG) {
2563 Slog.d(TAG, "--- enable ime = " + imi);
2564 }
2565 setInputMethodEnabledLocked(imi.getId(), true);
2566 }
2567 }
2568
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002569 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09002570 if (!TextUtils.isEmpty(defaultImiId)) {
2571 if (!map.containsKey(defaultImiId)) {
2572 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
2573 if (chooseNewDefaultIMELocked()) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002574 updateFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09002575 }
2576 } else {
2577 // Double check that the default IME is certainly enabled.
2578 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002579 }
2580 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002581 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002582
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002583 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002584
satokab751aa2010-09-14 19:17:36 +09002585 private void showInputMethodMenu() {
2586 showInputMethodMenuInternal(false);
2587 }
2588
2589 private void showInputMethodSubtypeMenu() {
2590 showInputMethodMenuInternal(true);
2591 }
2592
satok217f5482010-12-15 05:19:19 +09002593 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002594 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002595 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002596 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2597 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002598 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002599 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002600 }
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002601 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok217f5482010-12-15 05:19:19 +09002602 }
2603
2604 private void showConfigureInputMethods() {
2605 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2606 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2607 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2608 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002609 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09002610 }
2611
satok2c93efc2012-04-02 19:33:47 +09002612 private boolean isScreenLocked() {
2613 return mKeyguardManager != null
2614 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
2615 }
satokab751aa2010-09-14 19:17:36 +09002616 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002617 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002618
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002619 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09002620 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002621
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002622 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002623 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002624 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002625
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002626 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002627 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09002628 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
2629 mContext);
satok7f35c8c2010-10-07 21:13:11 +09002630 if (immis == null || immis.size() == 0) {
2631 return;
2632 }
2633
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002634 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002635
satok688bd472012-02-09 20:09:17 +09002636 final List<ImeSubtypeListItem> imList =
Satoshi Kataokad787f692013-10-26 04:44:21 +09002637 mSwitchingController.getSortedInputMethodAndSubtypeList(
satok688bd472012-02-09 20:09:17 +09002638 showSubtypes, mInputShown, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09002639
satokc3690562012-01-10 20:14:43 +09002640 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002641 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09002642 if (currentSubtype != null) {
2643 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002644 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
2645 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09002646 }
2647 }
2648
Ken Wakasa761eb372011-03-04 19:06:18 +09002649 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09002650 mIms = new InputMethodInfo[N];
2651 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002652 int checkedItem = 0;
2653 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002654 final ImeSubtypeListItem item = imList.get(i);
2655 mIms[i] = item.mImi;
2656 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002657 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002658 int subtypeId = mSubtypeIds[i];
2659 if ((subtypeId == NOT_A_SUBTYPE_ID)
2660 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2661 || (subtypeId == lastInputMethodSubtypeId)) {
2662 checkedItem = i;
2663 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002664 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002665 }
Ken Wakasa05dbb652011-08-22 15:22:43 +09002666 final TypedArray a = context.obtainStyledAttributes(null,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002667 com.android.internal.R.styleable.DialogPreference,
2668 com.android.internal.R.attr.alertDialogStyle, 0);
2669 mDialogBuilder = new AlertDialog.Builder(context)
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002670 .setOnCancelListener(new OnCancelListener() {
satok42c5a162011-05-26 16:46:14 +09002671 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002672 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002673 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002674 }
2675 })
2676 .setIcon(a.getDrawable(
2677 com.android.internal.R.styleable.DialogPreference_dialogTitle));
2678 a.recycle();
satok01038492012-04-09 21:08:27 +09002679 final LayoutInflater inflater =
2680 (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2681 final View tv = inflater.inflate(
2682 com.android.internal.R.layout.input_method_switch_dialog_title, null);
2683 mDialogBuilder.setCustomTitle(tv);
2684
2685 // Setup layout for a toggle switch of the hardware keyboard
2686 mSwitchingDialogTitleView = tv;
2687 mSwitchingDialogTitleView.findViewById(
2688 com.android.internal.R.id.hard_keyboard_section).setVisibility(
2689 mWindowManagerService.isHardKeyboardAvailable() ?
2690 View.VISIBLE : View.GONE);
2691 final Switch hardKeySwitch = ((Switch)mSwitchingDialogTitleView.findViewById(
2692 com.android.internal.R.id.hard_keyboard_switch));
2693 hardKeySwitch.setChecked(mWindowManagerService.isHardKeyboardEnabled());
2694 hardKeySwitch.setOnCheckedChangeListener(
2695 new OnCheckedChangeListener() {
2696 @Override
2697 public void onCheckedChanged(
2698 CompoundButton buttonView, boolean isChecked) {
2699 mWindowManagerService.setHardKeyboardEnabled(isChecked);
Satoshi Kataoka04dd24d2013-01-18 13:44:37 +09002700 // Ensure that the input method dialog is dismissed when changing
2701 // the hardware keyboard state.
2702 hideInputMethodMenu();
satok01038492012-04-09 21:08:27 +09002703 }
2704 });
satokd87c2592010-09-29 11:52:06 +09002705
Ken Wakasa05dbb652011-08-22 15:22:43 +09002706 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(context,
2707 com.android.internal.R.layout.simple_list_item_2_single_choice, imList,
2708 checkedItem);
2709
2710 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002711 new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002712 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002713 public void onClick(DialogInterface dialog, int which) {
2714 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09002715 if (mIms == null || mIms.length <= which
2716 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002717 return;
2718 }
2719 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09002720 int subtypeId = mSubtypeIds[which];
Satoshi Kataokad2142962012-11-12 18:43:06 +09002721 adapter.mCheckedItem = which;
2722 adapter.notifyDataSetChanged();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002723 hideInputMethodMenu();
2724 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09002725 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09002726 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09002727 subtypeId = NOT_A_SUBTYPE_ID;
2728 }
2729 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002730 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08002731 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002732 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002733 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002734
satokbc81b692011-08-26 16:22:22 +09002735 if (showSubtypes && !isScreenLocked) {
satok82beadf2010-12-27 19:03:06 +09002736 mDialogBuilder.setPositiveButton(
2737 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09002738 new DialogInterface.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002739 @Override
satok7f35c8c2010-10-07 21:13:11 +09002740 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09002741 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09002742 }
2743 });
2744 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002745 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002746 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002747 mSwitchingDialog.getWindow().setType(
2748 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09002749 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
2750 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002751 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002752 mSwitchingDialog.show();
2753 }
2754 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002755
Ken Wakasa05dbb652011-08-22 15:22:43 +09002756 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
2757 private final LayoutInflater mInflater;
2758 private final int mTextViewResourceId;
2759 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09002760 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09002761 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
2762 List<ImeSubtypeListItem> itemsList, int checkedItem) {
2763 super(context, textViewResourceId, itemsList);
2764 mTextViewResourceId = textViewResourceId;
2765 mItemsList = itemsList;
2766 mCheckedItem = checkedItem;
2767 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2768 }
2769
2770 @Override
2771 public View getView(int position, View convertView, ViewGroup parent) {
2772 final View view = convertView != null ? convertView
2773 : mInflater.inflate(mTextViewResourceId, null);
2774 if (position < 0 || position >= mItemsList.size()) return view;
2775 final ImeSubtypeListItem item = mItemsList.get(position);
2776 final CharSequence imeName = item.mImeName;
2777 final CharSequence subtypeName = item.mSubtypeName;
2778 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
2779 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
2780 if (TextUtils.isEmpty(subtypeName)) {
2781 firstTextView.setText(imeName);
2782 secondTextView.setVisibility(View.GONE);
2783 } else {
2784 firstTextView.setText(subtypeName);
2785 secondTextView.setText(imeName);
2786 secondTextView.setVisibility(View.VISIBLE);
2787 }
2788 final RadioButton radioButton =
2789 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
2790 radioButton.setChecked(position == mCheckedItem);
2791 return view;
2792 }
2793 }
2794
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002795 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07002796 synchronized (mMethodMap) {
2797 hideInputMethodMenuLocked();
2798 }
2799 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002800
The Android Open Source Project10592532009-03-18 17:39:46 -07002801 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002802 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002803
The Android Open Source Project10592532009-03-18 17:39:46 -07002804 if (mSwitchingDialog != null) {
2805 mSwitchingDialog.dismiss();
2806 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002807 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002808
The Android Open Source Project10592532009-03-18 17:39:46 -07002809 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07002810 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002811 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002812
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002813 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002814
satok42c5a162011-05-26 16:46:14 +09002815 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002816 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002817 // TODO: Make this work even for non-current users?
2818 if (!calledFromValidUser()) {
2819 return false;
2820 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002821 synchronized (mMethodMap) {
2822 if (mContext.checkCallingOrSelfPermission(
2823 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2824 != PackageManager.PERMISSION_GRANTED) {
2825 throw new SecurityException(
2826 "Requires permission "
2827 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2828 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002829
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002830 long ident = Binder.clearCallingIdentity();
2831 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002832 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002833 } finally {
2834 Binder.restoreCallingIdentity(ident);
2835 }
2836 }
2837 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002838
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002839 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
2840 // Make sure this is a valid input method.
2841 InputMethodInfo imm = mMethodMap.get(id);
2842 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09002843 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002844 }
2845
satokd87c2592010-09-29 11:52:06 +09002846 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
2847 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002848
satokd87c2592010-09-29 11:52:06 +09002849 if (enabled) {
2850 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
2851 if (pair.first.equals(id)) {
2852 // We are enabling this input method, but it is already enabled.
2853 // Nothing to do. The previous state was enabled.
2854 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002855 }
2856 }
satokd87c2592010-09-29 11:52:06 +09002857 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
2858 // Previous state was disabled.
2859 return false;
2860 } else {
2861 StringBuilder builder = new StringBuilder();
2862 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2863 builder, enabledInputMethodsList, id)) {
2864 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002865 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002866 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
2867 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
2868 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09002869 }
2870 // Previous state was enabled.
2871 return true;
2872 } else {
2873 // We are disabling the input method but it is already disabled.
2874 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002875 return false;
2876 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002877 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002878 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002879
satok723a27e2010-11-11 14:58:11 +09002880 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
2881 boolean setSubtypeOnly) {
2882 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002883 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09002884
2885 // Set Subtype here
2886 if (imi == null || subtypeId < 0) {
2887 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08002888 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09002889 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09002890 if (subtypeId < imi.getSubtypeCount()) {
2891 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
2892 mSettings.putSelectedSubtype(subtype.hashCode());
2893 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09002894 } else {
2895 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09002896 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002897 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09002898 }
satokab751aa2010-09-14 19:17:36 +09002899 }
satok723a27e2010-11-11 14:58:11 +09002900
satok4c0e7152012-06-20 20:08:44 +09002901 // Workaround.
2902 // ASEC is not ready in the IMMS constructor. Accordingly, forward-locked
2903 // IMEs are not recognized and considered uninstalled.
2904 // Actually, we can't move everything after SystemReady because
2905 // IMMS needs to run in the encryption lock screen. So, we just skip changing
2906 // the default IME here and try cheking the default IME again in systemReady().
2907 // TODO: Do nothing before system ready and implement a separated logic for
2908 // the encryption lock screen.
2909 // TODO: ASEC should be ready before IMMS is instantiated.
2910 if (mSystemReady && !setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09002911 // Set InputMethod here
2912 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
2913 }
2914 }
2915
2916 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
2917 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
2918 int lastSubtypeId = NOT_A_SUBTYPE_ID;
2919 // newDefaultIme is empty when there is no candidate for the selected IME.
2920 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
2921 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
2922 if (subtypeHashCode != null) {
2923 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002924 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok723a27e2010-11-11 14:58:11 +09002925 imi, Integer.valueOf(subtypeHashCode));
2926 } catch (NumberFormatException e) {
2927 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
2928 }
2929 }
2930 }
2931 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09002932 }
2933
satok4e4569d2010-11-19 18:45:53 +09002934 // If there are no selected shortcuts, tries finding the most applicable ones.
2935 private Pair<InputMethodInfo, InputMethodSubtype>
2936 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
2937 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2938 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09002939 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09002940 boolean foundInSystemIME = false;
2941
2942 // Search applicable subtype for each InputMethodInfo
2943 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09002944 final String imiId = imi.getId();
2945 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
2946 continue;
2947 }
satokcd7cd292010-11-20 15:46:23 +09002948 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09002949 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002950 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09002951 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09002952 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002953 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002954 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09002955 }
satokdf31ae62011-01-15 06:19:44 +09002956 // 2. Search by the system locale from enabledSubtypes.
2957 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09002958 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002959 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002960 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002961 }
satoka86f5e42011-09-02 17:12:42 +09002962 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002963 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09002964 final ArrayList<InputMethodSubtype> subtypesForSearch =
2965 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002966 ? InputMethodUtils.getSubtypes(imi)
2967 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09002968 // 4. Search by the current subtype's locale from all subtypes.
2969 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002970 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09002971 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09002972 }
2973 // 5. Search by the system locale from all subtypes.
2974 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09002975 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002976 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09002977 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002978 }
satokcd7cd292010-11-20 15:46:23 +09002979 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09002980 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09002981 // The current input method is the most applicable IME.
2982 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002983 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09002984 break;
satok7599a7f2010-12-22 13:45:23 +09002985 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09002986 // The system input method is 2nd applicable IME.
2987 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002988 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09002989 if ((imi.getServiceInfo().applicationInfo.flags
2990 & ApplicationInfo.FLAG_SYSTEM) != 0) {
2991 foundInSystemIME = true;
2992 }
satok4e4569d2010-11-19 18:45:53 +09002993 }
2994 }
2995 }
2996 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09002997 if (mostApplicableIMI != null) {
2998 Slog.w(TAG, "Most applicable shortcut input method was:"
2999 + mostApplicableIMI.getId());
3000 if (mostApplicableSubtype != null) {
3001 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3002 + "," + mostApplicableSubtype.getMode() + ","
3003 + mostApplicableSubtype.getLocale());
3004 }
3005 }
satok4e4569d2010-11-19 18:45:53 +09003006 }
satokcd7cd292010-11-20 15:46:23 +09003007 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09003008 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09003009 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003010 } else {
3011 return null;
3012 }
3013 }
3014
satokab751aa2010-09-14 19:17:36 +09003015 /**
3016 * @return Return the current subtype of this input method.
3017 */
satok42c5a162011-05-26 16:46:14 +09003018 @Override
satokab751aa2010-09-14 19:17:36 +09003019 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003020 // TODO: Make this work even for non-current users?
3021 if (!calledFromValidUser()) {
3022 return null;
3023 }
3024 synchronized (mMethodMap) {
3025 return getCurrentInputMethodSubtypeLocked();
3026 }
3027 }
3028
3029 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003030 if (mCurMethodId == null) {
3031 return null;
3032 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003033 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003034 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3035 if (imi == null || imi.getSubtypeCount() == 0) {
3036 return null;
satok4e4569d2010-11-19 18:45:53 +09003037 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003038 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003039 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3040 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003041 if (subtypeId == NOT_A_SUBTYPE_ID) {
3042 // If there are no selected subtypes, the framework will try to find
3043 // the most applicable subtype from explicitly or implicitly enabled
3044 // subtypes.
3045 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003046 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003047 // If there is only one explicitly or implicitly enabled subtype,
3048 // just returns it.
3049 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3050 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3051 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003052 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003053 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003054 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003055 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003056 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003057 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3058 true);
satok4e4569d2010-11-19 18:45:53 +09003059 }
satok3ef8b292010-11-23 06:06:29 +09003060 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003061 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003062 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003063 }
3064 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003065 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003066 }
3067
satokf3db1af2010-11-23 13:34:33 +09003068 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
3069 InputMethodSubtype subtype) {
3070 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
3071 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
3072 } else {
3073 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3074 subtypes.add(subtype);
3075 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
3076 }
3077 }
3078
satok4e4569d2010-11-19 18:45:53 +09003079 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003080 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003081 @Override
satok4e4569d2010-11-19 18:45:53 +09003082 public List getShortcutInputMethodsAndSubtypes() {
3083 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09003084 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09003085 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003086 // If there are no selected shortcut subtypes, the framework will try to find
3087 // the most applicable subtype from all subtypes whose mode is
3088 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003089 Pair<InputMethodInfo, InputMethodSubtype> info =
3090 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003091 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003092 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003093 ret.add(info.first);
3094 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003095 }
satok3da92232011-01-11 22:46:30 +09003096 return ret;
satokf3db1af2010-11-23 13:34:33 +09003097 }
satokf3db1af2010-11-23 13:34:33 +09003098 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3099 ret.add(imi);
3100 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3101 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003102 }
3103 }
satokf3db1af2010-11-23 13:34:33 +09003104 return ret;
satok4e4569d2010-11-19 18:45:53 +09003105 }
3106 }
3107
satok42c5a162011-05-26 16:46:14 +09003108 @Override
satokb66d2872010-11-10 01:04:04 +09003109 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003110 // TODO: Make this work even for non-current users?
3111 if (!calledFromValidUser()) {
3112 return false;
3113 }
satokb66d2872010-11-10 01:04:04 +09003114 synchronized (mMethodMap) {
3115 if (subtype != null && mCurMethodId != null) {
3116 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003117 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003118 if (subtypeId != NOT_A_SUBTYPE_ID) {
3119 setInputMethodLocked(mCurMethodId, subtypeId);
3120 return true;
3121 }
3122 }
3123 return false;
3124 }
3125 }
3126
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003127 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003128 private static class InputMethodFileManager {
3129 private static final String SYSTEM_PATH = "system";
3130 private static final String INPUT_METHOD_PATH = "inputmethod";
3131 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3132 private static final String NODE_SUBTYPES = "subtypes";
3133 private static final String NODE_SUBTYPE = "subtype";
3134 private static final String NODE_IMI = "imi";
3135 private static final String ATTR_ID = "id";
3136 private static final String ATTR_LABEL = "label";
3137 private static final String ATTR_ICON = "icon";
3138 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3139 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3140 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3141 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3142 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3143 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003144 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
satoke7c6998e2011-06-03 17:57:59 +09003145 new HashMap<String, List<InputMethodSubtype>>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003146 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003147 if (methodMap == null) {
3148 throw new NullPointerException("methodMap is null");
3149 }
3150 mMethodMap = methodMap;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003151 final File systemDir = userId == UserHandle.USER_OWNER
3152 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3153 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003154 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3155 if (!inputMethodDir.mkdirs()) {
3156 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3157 }
3158 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3159 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3160 if (!subtypeFile.exists()) {
3161 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003162 writeAdditionalInputMethodSubtypes(
3163 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003164 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003165 readAdditionalInputMethodSubtypes(
3166 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003167 }
3168 }
3169
3170 private void deleteAllInputMethodSubtypes(String imiId) {
3171 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003172 mAdditionalSubtypesMap.remove(imiId);
3173 writeAdditionalInputMethodSubtypes(
3174 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003175 }
3176 }
3177
3178 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003179 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003180 synchronized (mMethodMap) {
3181 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3182 final int N = additionalSubtypes.length;
3183 for (int i = 0; i < N; ++i) {
3184 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003185 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003186 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003187 } else {
3188 Slog.w(TAG, "Duplicated subtype definition found: "
3189 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003190 }
3191 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003192 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3193 writeAdditionalInputMethodSubtypes(
3194 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003195 }
3196 }
3197
3198 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3199 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003200 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003201 }
3202 }
3203
3204 private static void writeAdditionalInputMethodSubtypes(
3205 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3206 HashMap<String, InputMethodInfo> methodMap) {
3207 // Safety net for the case that this function is called before methodMap is set.
3208 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3209 FileOutputStream fos = null;
3210 try {
3211 fos = subtypesFile.startWrite();
3212 final XmlSerializer out = new FastXmlSerializer();
3213 out.setOutput(fos, "utf-8");
3214 out.startDocument(null, true);
3215 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3216 out.startTag(null, NODE_SUBTYPES);
3217 for (String imiId : allSubtypes.keySet()) {
3218 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3219 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3220 continue;
3221 }
3222 out.startTag(null, NODE_IMI);
3223 out.attribute(null, ATTR_ID, imiId);
3224 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3225 final int N = subtypesList.size();
3226 for (int i = 0; i < N; ++i) {
3227 final InputMethodSubtype subtype = subtypesList.get(i);
3228 out.startTag(null, NODE_SUBTYPE);
3229 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3230 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3231 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3232 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3233 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3234 out.attribute(null, ATTR_IS_AUXILIARY,
3235 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3236 out.endTag(null, NODE_SUBTYPE);
3237 }
3238 out.endTag(null, NODE_IMI);
3239 }
3240 out.endTag(null, NODE_SUBTYPES);
3241 out.endDocument();
3242 subtypesFile.finishWrite(fos);
3243 } catch (java.io.IOException e) {
3244 Slog.w(TAG, "Error writing subtypes", e);
3245 if (fos != null) {
3246 subtypesFile.failWrite(fos);
3247 }
3248 }
3249 }
3250
3251 private static void readAdditionalInputMethodSubtypes(
3252 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3253 if (allSubtypes == null || subtypesFile == null) return;
3254 allSubtypes.clear();
3255 FileInputStream fis = null;
3256 try {
3257 fis = subtypesFile.openRead();
3258 final XmlPullParser parser = Xml.newPullParser();
3259 parser.setInput(fis, null);
3260 int type = parser.getEventType();
3261 // Skip parsing until START_TAG
3262 while ((type = parser.next()) != XmlPullParser.START_TAG
3263 && type != XmlPullParser.END_DOCUMENT) {}
3264 String firstNodeName = parser.getName();
3265 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3266 throw new XmlPullParserException("Xml doesn't start with subtypes");
3267 }
3268 final int depth =parser.getDepth();
3269 String currentImiId = null;
3270 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3271 while (((type = parser.next()) != XmlPullParser.END_TAG
3272 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3273 if (type != XmlPullParser.START_TAG)
3274 continue;
3275 final String nodeName = parser.getName();
3276 if (NODE_IMI.equals(nodeName)) {
3277 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3278 if (TextUtils.isEmpty(currentImiId)) {
3279 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3280 continue;
3281 }
3282 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
3283 allSubtypes.put(currentImiId, tempSubtypesArray);
3284 } else if (NODE_SUBTYPE.equals(nodeName)) {
3285 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3286 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3287 continue;
3288 }
3289 final int icon = Integer.valueOf(
3290 parser.getAttributeValue(null, ATTR_ICON));
3291 final int label = Integer.valueOf(
3292 parser.getAttributeValue(null, ATTR_LABEL));
3293 final String imeSubtypeLocale =
3294 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3295 final String imeSubtypeMode =
3296 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3297 final String imeSubtypeExtraValue =
3298 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003299 final boolean isAuxiliary = "1".equals(String.valueOf(
3300 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
satoke7c6998e2011-06-03 17:57:59 +09003301 final InputMethodSubtype subtype =
3302 new InputMethodSubtype(label, icon, imeSubtypeLocale,
3303 imeSubtypeMode, imeSubtypeExtraValue, isAuxiliary);
3304 tempSubtypesArray.add(subtype);
3305 }
3306 }
3307 } catch (XmlPullParserException e) {
3308 Slog.w(TAG, "Error reading subtypes: " + e);
3309 return;
3310 } catch (java.io.IOException e) {
3311 Slog.w(TAG, "Error reading subtypes: " + e);
3312 return;
3313 } catch (NumberFormatException e) {
3314 Slog.w(TAG, "Error reading subtypes: " + e);
3315 return;
3316 } finally {
3317 if (fis != null) {
3318 try {
3319 fis.close();
3320 } catch (java.io.IOException e1) {
3321 Slog.w(TAG, "Failed to close.");
3322 }
3323 }
3324 }
3325 }
3326 }
3327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003328 @Override
3329 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3330 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3331 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003333 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3334 + Binder.getCallingPid()
3335 + ", uid=" + Binder.getCallingUid());
3336 return;
3337 }
3338
3339 IInputMethod method;
3340 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003341
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003342 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003344 synchronized (mMethodMap) {
3345 p.println("Current Input Method Manager state:");
3346 int N = mMethodList.size();
3347 p.println(" Input Methods:");
3348 for (int i=0; i<N; i++) {
3349 InputMethodInfo info = mMethodList.get(i);
3350 p.println(" InputMethod #" + i + ":");
3351 info.dump(p, " ");
3352 }
3353 p.println(" Clients:");
3354 for (ClientState ci : mClients.values()) {
3355 p.println(" Client " + ci + ":");
3356 p.println(" client=" + ci.client);
3357 p.println(" inputContext=" + ci.inputContext);
3358 p.println(" sessionRequested=" + ci.sessionRequested);
3359 p.println(" curSession=" + ci.curSession);
3360 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003361 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003362 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003363 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3364 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003365 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3366 + " mBoundToMethod=" + mBoundToMethod);
3367 p.println(" mCurToken=" + mCurToken);
3368 p.println(" mCurIntent=" + mCurIntent);
3369 method = mCurMethod;
3370 p.println(" mCurMethod=" + mCurMethod);
3371 p.println(" mEnabledSession=" + mEnabledSession);
3372 p.println(" mShowRequested=" + mShowRequested
3373 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3374 + " mShowForced=" + mShowForced
3375 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07003376 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003377 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003378
Jeff Brownb88102f2010-09-08 11:49:43 -07003379 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003380 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003381 pw.flush();
3382 try {
3383 client.client.asBinder().dump(fd, args);
3384 } catch (RemoteException e) {
3385 p.println("Input method client dead: " + e);
3386 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003387 } else {
3388 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003389 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003390
Jeff Brownb88102f2010-09-08 11:49:43 -07003391 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003392 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003393 pw.flush();
3394 try {
3395 method.asBinder().dump(fd, args);
3396 } catch (RemoteException e) {
3397 p.println("Input method service dead: " + e);
3398 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003399 } else {
3400 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003401 }
3402 }
3403}