blob: 9e0ff1976b2a26e5e424d28ae31d25f164e403b6 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 * use this file except in compliance with the License. You may obtain a copy of
5 * the License at
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007 * http://www.apache.org/licenses/LICENSE-2.0
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 * License for the specific language governing permissions and limitations under
13 * the License.
14 */
15
16package com.android.server;
17
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080018import com.android.internal.content.PackageMonitor;
Satoshi Kataokad7443c82013-10-15 17:45:43 +090019import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController;
Satoshi Kataokad787f692013-10-26 04:44:21 +090020import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController.ImeSubtypeListItem;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +090021import com.android.internal.inputmethod.InputMethodUtils;
22import com.android.internal.inputmethod.InputMethodUtils.InputMethodSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import com.android.internal.os.HandlerCaller;
Svetoslav Ganov758143e2012-08-06 16:40:27 -070024import com.android.internal.os.SomeArgs;
satoke7c6998e2011-06-03 17:57:59 +090025import com.android.internal.util.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import com.android.internal.view.IInputContext;
27import com.android.internal.view.IInputMethod;
Michael Wright52a53522013-03-14 10:59:38 -070028import com.android.internal.view.IInputSessionCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import com.android.internal.view.IInputMethodClient;
30import com.android.internal.view.IInputMethodManager;
31import com.android.internal.view.IInputMethodSession;
32import com.android.internal.view.InputBindResult;
Adam Lesinskief2ea1f2013-12-05 16:48:06 -080033import com.android.server.statusbar.StatusBarManagerService;
satok01038492012-04-09 21:08:27 +090034import com.android.server.wm.WindowManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035
satoke7c6998e2011-06-03 17:57:59 +090036import org.xmlpull.v1.XmlPullParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import org.xmlpull.v1.XmlPullParserException;
satoke7c6998e2011-06-03 17:57:59 +090038import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039
40import android.app.ActivityManagerNative;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090041import android.app.AppGlobals;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.app.AlertDialog;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090043import android.app.IUserSwitchObserver;
satokf90a33e2011-07-19 11:55:52 +090044import android.app.KeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +090045import android.app.Notification;
46import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070047import android.app.PendingIntent;
satok5b927c432012-05-01 20:09:34 +090048import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.content.ComponentName;
50import android.content.ContentResolver;
51import android.content.Context;
52import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.content.DialogInterface.OnCancelListener;
54import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090055import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070057import android.content.pm.ApplicationInfo;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090058import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.content.pm.PackageManager;
60import android.content.pm.ResolveInfo;
61import android.content.pm.ServiceInfo;
Amith Yamasani734983f2014-03-04 16:48:05 -080062import android.content.pm.UserInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070063import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.content.res.Resources;
65import android.content.res.TypedArray;
66import android.database.ContentObserver;
Joe Onorato857fd9b2011-01-27 15:08:35 -080067import android.inputmethodservice.InputMethodService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.os.Binder;
satoke7c6998e2011-06-03 17:57:59 +090069import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.os.Handler;
71import android.os.IBinder;
72import android.os.IInterface;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090073import android.os.IRemoteCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074import android.os.Message;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090075import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076import android.os.Parcel;
77import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080078import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079import android.os.ServiceManager;
80import android.os.SystemClock;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090081import android.os.UserHandle;
Amith Yamasani734983f2014-03-04 16:48:05 -080082import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083import android.provider.Settings;
84import android.text.TextUtils;
satokf9f01002011-05-19 21:31:50 +090085import android.text.style.SuggestionSpan;
Dianne Hackborn39606a02012-07-31 17:54:35 -070086import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +090088import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +090089import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090import android.util.PrintWriterPrinter;
91import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +090092import android.util.Slog;
93import android.util.Xml;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094import android.view.IWindowManager;
Jeff Brownc28867a2013-03-26 15:42:39 -070095import android.view.InputChannel;
Ken Wakasa05dbb652011-08-22 15:22:43 +090096import android.view.LayoutInflater;
97import android.view.View;
98import android.view.ViewGroup;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099import android.view.WindowManager;
satokab751aa2010-09-14 19:17:36 +0900100import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101import android.view.inputmethod.InputBinding;
102import android.view.inputmethod.InputMethod;
103import android.view.inputmethod.InputMethodInfo;
104import android.view.inputmethod.InputMethodManager;
satokab751aa2010-09-14 19:17:36 +0900105import android.view.inputmethod.InputMethodSubtype;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900106import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +0900107import android.widget.CompoundButton;
108import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900109import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +0900110import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900111import android.widget.TextView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112
satoke7c6998e2011-06-03 17:57:59 +0900113import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900115import java.io.FileInputStream;
116import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117import java.io.IOException;
118import java.io.PrintWriter;
119import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900120import java.util.Collections;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121import java.util.HashMap;
122import java.util.List;
satok5b927c432012-05-01 20:09:34 +0900123import java.util.Locale;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124
125/**
126 * This class provides a system service that manages input methods.
127 */
128public class InputMethodManagerService extends IInputMethodManager.Stub
129 implements ServiceConnection, Handler.Callback {
130 static final boolean DEBUG = false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700131 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132
133 static final int MSG_SHOW_IM_PICKER = 1;
satokab751aa2010-09-14 19:17:36 +0900134 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 2;
satok47a44912010-10-06 16:03:58 +0900135 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 3;
satok217f5482010-12-15 05:19:19 +0900136 static final int MSG_SHOW_IM_CONFIG = 4;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 static final int MSG_UNBIND_INPUT = 1000;
139 static final int MSG_BIND_INPUT = 1010;
140 static final int MSG_SHOW_SOFT_INPUT = 1020;
141 static final int MSG_HIDE_SOFT_INPUT = 1030;
142 static final int MSG_ATTACH_TOKEN = 1040;
143 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 static final int MSG_START_INPUT = 2000;
146 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 static final int MSG_UNBIND_METHOD = 3000;
149 static final int MSG_BIND_METHOD = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700150 static final int MSG_SET_ACTIVE = 3020;
Yohei Yukawab7b79072014-03-25 11:02:00 +0900151 static final int MSG_SET_CURSOR_ANCHOR_MONITOR_MODE = 3030;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800152
satok01038492012-04-09 21:08:27 +0900153 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
154
Satoshi Kataokabcacc322013-10-21 17:57:27 -0700155 static final long TIME_TO_RECONNECT = 3 * 1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800156
satokf9f01002011-05-19 21:31:50 +0900157 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
158
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900159 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
satokb6359412011-06-28 17:47:41 +0900160 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900161
satok4e4569d2010-11-19 18:45:53 +0900162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800164 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900166 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 final IWindowManager mIWindowManager;
169 final HandlerCaller mCaller;
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700170 final boolean mHasFeature;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900171 private InputMethodFileManager mFileManager;
satok01038492012-04-09 21:08:27 +0900172 private final HardKeyboardListener mHardKeyboardListener;
173 private final WindowManagerService mWindowManagerService;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800174
Jeff Brownc28867a2013-03-26 15:42:39 -0700175 final InputBindResult mNoBinding = new InputBindResult(null, null, null, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800176
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 // All known input methods. mMethodMap also serves as the global
178 // lock for this class.
satokd87c2592010-09-29 11:52:06 +0900179 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<InputMethodInfo>();
180 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<String, InputMethodInfo>();
satokf9f01002011-05-19 21:31:50 +0900181 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
182 new LruCache<SuggestionSpan, InputMethodInfo>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Satoshi Kataokad787f692013-10-26 04:44:21 +0900183 private final InputMethodSubtypeSwitchingController mSwitchingController;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800184
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700185 // Used to bring IME service up to visible adjustment while it is being shown.
186 final ServiceConnection mVisibleConnection = new ServiceConnection() {
187 @Override public void onServiceConnected(ComponentName name, IBinder service) {
188 }
189
190 @Override public void onServiceDisconnected(ComponentName name) {
191 }
192 };
193 boolean mVisibleBound = false;
194
satok7cfc0ed2011-06-20 21:29:36 +0900195 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700196 private NotificationManager mNotificationManager;
197 private KeyguardManager mKeyguardManager;
198 private StatusBarManagerService mStatusBar;
199 private Notification mImeSwitcherNotification;
200 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900201 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900202 private boolean mNotificationShown;
satok0a1bcf42012-05-16 19:26:31 +0900203 private final boolean mImeSelectedOnBoot;
satok7cfc0ed2011-06-20 21:29:36 +0900204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 class SessionState {
206 final ClientState client;
207 final IInputMethod method;
Jeff Brownc28867a2013-03-26 15:42:39 -0700208
209 IInputMethodSession session;
210 InputChannel channel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 @Override
213 public String toString() {
214 return "SessionState{uid " + client.uid + " pid " + client.pid
215 + " method " + Integer.toHexString(
216 System.identityHashCode(method))
217 + " session " + Integer.toHexString(
218 System.identityHashCode(session))
Jeff Brownc28867a2013-03-26 15:42:39 -0700219 + " channel " + channel
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 + "}";
221 }
222
223 SessionState(ClientState _client, IInputMethod _method,
Jeff Brownc28867a2013-03-26 15:42:39 -0700224 IInputMethodSession _session, InputChannel _channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 client = _client;
226 method = _method;
227 session = _session;
Jeff Brownc28867a2013-03-26 15:42:39 -0700228 channel = _channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 }
230 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800231
Jeff Brownc28867a2013-03-26 15:42:39 -0700232 static final class ClientState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 final IInputMethodClient client;
234 final IInputContext inputContext;
235 final int uid;
236 final int pid;
237 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800238
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 boolean sessionRequested;
240 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 @Override
243 public String toString() {
244 return "ClientState{" + Integer.toHexString(
245 System.identityHashCode(this)) + " uid " + uid
246 + " pid " + pid + "}";
247 }
248
249 ClientState(IInputMethodClient _client, IInputContext _inputContext,
250 int _uid, int _pid) {
251 client = _client;
252 inputContext = _inputContext;
253 uid = _uid;
254 pid = _pid;
255 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
256 }
257 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 final HashMap<IBinder, ClientState> mClients
260 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700263 * Set once the system is ready to run third party code.
264 */
265 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800266
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700267 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 * Id of the currently selected input method.
269 */
270 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 /**
273 * The current binding sequence number, incremented every time there is
274 * a new bind performed.
275 */
276 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 /**
279 * The client that is currently bound to an input method.
280 */
281 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700284 * The last window token that gained focus.
285 */
286 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800287
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700288 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 * The input context last provided by the current client.
290 */
291 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 /**
294 * The attributes last provided by the current client.
295 */
296 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800297
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298 /**
299 * The input method ID of the input method service that we are currently
300 * connected to or in the process of connecting to.
301 */
302 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800303
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 /**
satokab751aa2010-09-14 19:17:36 +0900305 * The current subtype of the current input method.
306 */
307 private InputMethodSubtype mCurrentSubtype;
308
satok4e4569d2010-11-19 18:45:53 +0900309 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900310 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
311 mShortcutInputMethodsAndSubtypes =
312 new HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>();
satokab751aa2010-09-14 19:17:36 +0900313
John Spurlocke0980502013-10-25 11:59:29 -0400314 // Was the keyguard locked when this client became current?
315 private boolean mCurClientInKeyguard;
316
satokab751aa2010-09-14 19:17:36 +0900317 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 * Set to true if our ServiceConnection is currently actively bound to
319 * a service (whether or not we have gotten its IBinder back yet).
320 */
321 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 /**
324 * Set if the client has asked for the input method to be shown.
325 */
326 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328 /**
329 * Set if we were explicitly told to show the input method.
330 */
331 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 /**
334 * Set if we were forced to be shown.
335 */
336 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 /**
339 * Set if we last told the input method to show itself.
340 */
341 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 /**
344 * The Intent used to connect to the current input method.
345 */
346 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800347
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 /**
349 * The token we have made for the currently active input method, to
350 * identify it in the future.
351 */
352 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 /**
355 * If non-null, this is the input method service we are currently connected
356 * to.
357 */
358 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 /**
361 * Time that we last initiated a bind to the input method, to determine
362 * if we should try to disconnect and reconnect to it.
363 */
364 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 /**
367 * Have we called mCurMethod.bindInput()?
368 */
369 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 /**
372 * Currently enabled session. Only touched by service thread, not
373 * protected by a lock.
374 */
375 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800376
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377 /**
378 * True if the screen is on. The value is true initially.
379 */
380 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800381
Joe Onorato857fd9b2011-01-27 15:08:35 -0800382 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
383 int mImeWindowVis;
384
Ken Wakasa05dbb652011-08-22 15:22:43 +0900385 private AlertDialog.Builder mDialogBuilder;
386 private AlertDialog mSwitchingDialog;
satok01038492012-04-09 21:08:27 +0900387 private View mSwitchingDialogTitleView;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900388 private InputMethodInfo[] mIms;
389 private int[] mSubtypeIds;
satok5b927c432012-05-01 20:09:34 +0900390 private Locale mLastSystemLocale;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900391 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
392 private final IPackageManager mIPackageManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 class SettingsObserver extends ContentObserver {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800395 String mLastEnabled = "";
396
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 SettingsObserver(Handler handler) {
398 super(handler);
399 ContentResolver resolver = mContext.getContentResolver();
400 resolver.registerContentObserver(Settings.Secure.getUriFor(
401 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
satokab751aa2010-09-14 19:17:36 +0900402 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokb6109bb2011-02-03 22:24:54 +0900403 Settings.Secure.ENABLED_INPUT_METHODS), false, this);
404 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokab751aa2010-09-14 19:17:36 +0900405 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 @Override public void onChange(boolean selfChange) {
409 synchronized (mMethodMap) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800410 boolean enabledChanged = false;
411 String newEnabled = mSettings.getEnabledInputMethodsStr();
412 if (!mLastEnabled.equals(newEnabled)) {
413 mLastEnabled = newEnabled;
414 enabledChanged = true;
415 }
416 updateFromSettingsLocked(enabledChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 }
418 }
419 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800420
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900421 class ImmsBroadcastReceiver extends android.content.BroadcastReceiver {
422 private void updateActive() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423 // Inform the current client of the change in active status
Dianne Hackborna6e41342012-05-22 16:30:34 -0700424 if (mCurClient != null && mCurClient.client != null) {
425 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
426 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800427 }
428 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900429
430 @Override
431 public void onReceive(Context context, Intent intent) {
432 final String action = intent.getAction();
433 if (Intent.ACTION_SCREEN_ON.equals(action)) {
434 mScreenOn = true;
435 refreshImeWindowVisibilityLocked();
436 updateActive();
437 return;
438 } else if (Intent.ACTION_SCREEN_OFF.equals(action)) {
439 mScreenOn = false;
440 setImeWindowVisibilityStatusHiddenLocked();
441 updateActive();
442 return;
443 } else if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
444 hideInputMethodMenu();
445 // No need to updateActive
446 return;
Amith Yamasani734983f2014-03-04 16:48:05 -0800447 } else if (Intent.ACTION_USER_ADDED.equals(action)
448 || Intent.ACTION_USER_REMOVED.equals(action)) {
Kenny Guy2a764942014-04-02 13:29:20 +0100449 updateCurrentProfileIds();
Amith Yamasani734983f2014-03-04 16:48:05 -0800450 return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900451 } else {
452 Slog.w(TAG, "Unexpected intent " + intent);
453 }
454 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800456
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800457 class MyPackageMonitor extends PackageMonitor {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900458 private boolean isChangingPackagesOfCurrentUser() {
459 final int userId = getChangingUserId();
460 final boolean retval = userId == mSettings.getCurrentUserId();
461 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900462 if (!retval) {
463 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
464 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900465 }
466 return retval;
467 }
468
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800470 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900471 if (!isChangingPackagesOfCurrentUser()) {
472 return false;
473 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900475 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 final int N = mMethodList.size();
477 if (curInputMethodId != null) {
478 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800479 InputMethodInfo imi = mMethodList.get(i);
480 if (imi.getId().equals(curInputMethodId)) {
481 for (String pkg : packages) {
482 if (imi.getPackageName().equals(pkg)) {
483 if (!doit) {
484 return true;
485 }
satok723a27e2010-11-11 14:58:11 +0900486 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800487 chooseNewDefaultIMELocked();
488 return true;
489 }
490 }
491 }
492 }
493 }
494 }
495 return false;
496 }
497
498 @Override
499 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900500 if (!isChangingPackagesOfCurrentUser()) {
501 return;
502 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800503 synchronized (mMethodMap) {
504 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900505 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800506 final int N = mMethodList.size();
507 if (curInputMethodId != null) {
508 for (int i=0; i<N; i++) {
509 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900510 final String imiId = imi.getId();
511 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800512 curIm = imi;
513 }
satoke7c6998e2011-06-03 17:57:59 +0900514
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800515 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900516 if (isPackageModified(imi.getPackageName())) {
517 mFileManager.deleteAllInputMethodSubtypes(imiId);
518 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800519 if (change == PACKAGE_TEMPORARY_CHANGE
520 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800521 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800522 + imi.getComponent());
523 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524 }
525 }
526 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800527
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900528 buildInputMethodListLocked(
529 mMethodList, mMethodMap, false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800530
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800532
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800533 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800534 int change = isPackageDisappearing(curIm.getPackageName());
535 if (change == PACKAGE_TEMPORARY_CHANGE
536 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800537 ServiceInfo si = null;
538 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900539 si = mIPackageManager.getServiceInfo(
540 curIm.getComponent(), 0, mSettings.getCurrentUserId());
541 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800542 }
543 if (si == null) {
544 // Uh oh, current input method is no longer around!
545 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800546 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
satok15452a42011-10-28 17:58:28 +0900547 setImeWindowVisibilityStatusHiddenLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800548 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800549 changed = true;
550 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800551 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900552 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800553 }
554 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800555 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800556 }
satokab751aa2010-09-14 19:17:36 +0900557
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800558 if (curIm == null) {
559 // We currently don't have a default input method... is
560 // one now available?
561 changed = chooseNewDefaultIMELocked();
562 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800563
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800564 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800565 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566 }
567 }
568 }
569 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800570
Jeff Brownc28867a2013-03-26 15:42:39 -0700571 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900572 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -0700573 private final IInputMethod mMethod;
574 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800575
Jeff Brownc28867a2013-03-26 15:42:39 -0700576 MethodCallback(InputMethodManagerService imms, IInputMethod method,
577 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900578 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -0700579 mMethod = method;
580 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800581 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800582
satoke7c6998e2011-06-03 17:57:59 +0900583 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -0700584 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -0700585 long ident = Binder.clearCallingIdentity();
586 try {
587 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
588 } finally {
589 Binder.restoreCallingIdentity(ident);
590 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591 }
592 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800593
satok01038492012-04-09 21:08:27 +0900594 private class HardKeyboardListener
595 implements WindowManagerService.OnHardKeyboardStatusChangeListener {
596 @Override
597 public void onHardKeyboardStatusChange(boolean available, boolean enabled) {
598 mHandler.sendMessage(mHandler.obtainMessage(
599 MSG_HARD_KEYBOARD_SWITCH_CHANGED, available ? 1 : 0, enabled ? 1 : 0));
600 }
601
602 public void handleHardKeyboardStatusChange(boolean available, boolean enabled) {
603 if (DEBUG) {
604 Slog.w(TAG, "HardKeyboardStatusChanged: available = " + available + ", enabled = "
605 + enabled);
606 }
607 synchronized(mMethodMap) {
608 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
609 && mSwitchingDialog.isShowing()) {
610 mSwitchingDialogTitleView.findViewById(
611 com.android.internal.R.id.hard_keyboard_section).setVisibility(
612 available ? View.VISIBLE : View.GONE);
613 }
614 }
615 }
616 }
617
618 public InputMethodManagerService(Context context, WindowManagerService windowManager) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900619 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800621 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 mHandler = new Handler(this);
623 mIWindowManager = IWindowManager.Stub.asInterface(
624 ServiceManager.getService(Context.WINDOW_SERVICE));
Mita Yuned218c72012-12-06 17:18:25 -0800625 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900626 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627 public void executeMessage(Message msg) {
628 handleMessage(msg);
629 }
Mita Yuned218c72012-12-06 17:18:25 -0800630 }, true /*asyncHandler*/);
satok01038492012-04-09 21:08:27 +0900631 mWindowManagerService = windowManager;
632 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700633 mHasFeature = context.getPackageManager().hasSystemFeature(
634 PackageManager.FEATURE_INPUT_METHODS);
satok7cfc0ed2011-06-20 21:29:36 +0900635
satok7cfc0ed2011-06-20 21:29:36 +0900636 mImeSwitcherNotification = new Notification();
637 mImeSwitcherNotification.icon = com.android.internal.R.drawable.ic_notification_ime_default;
638 mImeSwitcherNotification.when = 0;
639 mImeSwitcherNotification.flags = Notification.FLAG_ONGOING_EVENT;
640 mImeSwitcherNotification.tickerText = null;
641 mImeSwitcherNotification.defaults = 0; // please be quiet
642 mImeSwitcherNotification.sound = null;
643 mImeSwitcherNotification.vibrate = null;
Daniel Sandler590d5152012-06-14 16:10:13 -0400644
645 // Tag this notification specially so SystemUI knows it's important
John Spurlockfd7f1e02014-03-18 16:41:57 -0400646 mImeSwitcherNotification.extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
647 mImeSwitcherNotification.category = Notification.CATEGORY_SYSTEM;
Daniel Sandler590d5152012-06-14 16:10:13 -0400648
satok7cfc0ed2011-06-20 21:29:36 +0900649 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900650 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900651
652 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900653
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900654 final IntentFilter broadcastFilter = new IntentFilter();
655 broadcastFilter.addAction(Intent.ACTION_SCREEN_ON);
656 broadcastFilter.addAction(Intent.ACTION_SCREEN_OFF);
657 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Amith Yamasani734983f2014-03-04 16:48:05 -0800658 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
659 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900660 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800661
satok7cfc0ed2011-06-20 21:29:36 +0900662 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900663 int userId = 0;
664 try {
665 ActivityManagerNative.getDefault().registerUserSwitchObserver(
666 new IUserSwitchObserver.Stub() {
667 @Override
668 public void onUserSwitching(int newUserId, IRemoteCallback reply) {
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900669 synchronized(mMethodMap) {
670 switchUserLocked(newUserId);
671 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900672 if (reply != null) {
673 try {
674 reply.sendResult(null);
675 } catch (RemoteException e) {
676 }
677 }
678 }
679
680 @Override
681 public void onUserSwitchComplete(int newUserId) throws RemoteException {
682 }
683 });
684 userId = ActivityManagerNative.getDefault().getCurrentUser().id;
685 } catch (RemoteException e) {
686 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
687 }
satok81f8b7c2012-12-04 20:42:56 +0900688 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
satok913a8922010-08-26 21:53:41 +0900689
satokd87c2592010-09-29 11:52:06 +0900690 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900691 mSettings = new InputMethodSettings(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900692 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId);
Kenny Guy2a764942014-04-02 13:29:20 +0100693 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900694 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900695 synchronized (mMethodMap) {
696 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
697 mSettings, context);
698 }
satok0a1bcf42012-05-16 19:26:31 +0900699
700 // Just checking if defaultImiId is empty or not
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900701 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900702 if (DEBUG) {
703 Slog.d(TAG, "Initial default ime = " + defaultImiId);
704 }
satok0a1bcf42012-05-16 19:26:31 +0900705 mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
706
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900707 synchronized (mMethodMap) {
708 buildInputMethodListLocked(mMethodList, mMethodMap,
709 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
710 }
satokd87c2592010-09-29 11:52:06 +0900711 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712
satok0a1bcf42012-05-16 19:26:31 +0900713 if (!mImeSelectedOnBoot) {
714 Slog.w(TAG, "No IME selected. Choose the most applicable IME.");
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900715 synchronized (mMethodMap) {
716 resetDefaultImeLocked(context);
717 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800718 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800719
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800720 mSettingsObserver = new SettingsObserver(mHandler);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900721 synchronized (mMethodMap) {
722 updateFromSettingsLocked(true);
723 }
satok5b927c432012-05-01 20:09:34 +0900724
725 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME
726 // according to the new system locale.
727 final IntentFilter filter = new IntentFilter();
728 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
729 mContext.registerReceiver(
730 new BroadcastReceiver() {
731 @Override
732 public void onReceive(Context context, Intent intent) {
733 synchronized(mMethodMap) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900734 resetStateIfCurrentLocaleChangedLocked();
satok5b927c432012-05-01 20:09:34 +0900735 }
736 }
737 }, filter);
738 }
739
satok5b927c432012-05-01 20:09:34 +0900740 private void resetDefaultImeLocked(Context context) {
741 // Do not reset the default (current) IME when it is a 3rd-party IME
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900742 if (mCurMethodId != null
743 && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +0900744 return;
745 }
746
747 InputMethodInfo defIm = null;
748 for (InputMethodInfo imi : mMethodList) {
749 if (defIm == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900750 if (InputMethodUtils.isValidSystemDefaultIme(
751 mSystemReady, imi, context)) {
satok5b927c432012-05-01 20:09:34 +0900752 defIm = imi;
753 Slog.i(TAG, "Selected default: " + imi.getId());
754 }
755 }
756 }
757 if (defIm == null && mMethodList.size() > 0) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900758 defIm = InputMethodUtils.getMostApplicableDefaultIME(
759 mSettings.getEnabledInputMethodListLocked());
satok5b927c432012-05-01 20:09:34 +0900760 Slog.i(TAG, "No default found, using " + defIm.getId());
761 }
762 if (defIm != null) {
763 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
764 }
765 }
766
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900767 private void resetAllInternalStateLocked(final boolean updateOnlyWhenLocaleChanged,
768 final boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900769 if (!mSystemReady) {
770 // not system ready
771 return;
772 }
773 final Locale newLocale = mRes.getConfiguration().locale;
774 if (!updateOnlyWhenLocaleChanged
775 || (newLocale != null && !newLocale.equals(mLastSystemLocale))) {
776 if (!updateOnlyWhenLocaleChanged) {
777 hideCurrentInputLocked(0, null);
778 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -0700779 unbindCurrentMethodLocked(true, false);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900780 }
781 if (DEBUG) {
782 Slog.i(TAG, "Locale has been changed to " + newLocale);
783 }
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900784 buildInputMethodListLocked(mMethodList, mMethodMap, resetDefaultEnabledIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900785 if (!updateOnlyWhenLocaleChanged) {
786 final String selectedImiId = mSettings.getSelectedInputMethod();
787 if (TextUtils.isEmpty(selectedImiId)) {
788 // This is the first time of the user switch and
789 // set the current ime to the proper one.
790 resetDefaultImeLocked(mContext);
791 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +0900792 } else {
793 // If the locale is changed, needs to reset the default ime
794 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900795 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800796 updateFromSettingsLocked(true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900797 mLastSystemLocale = newLocale;
798 if (!updateOnlyWhenLocaleChanged) {
799 try {
800 startInputInnerLocked();
801 } catch (RuntimeException e) {
802 Slog.w(TAG, "Unexpected exception", e);
803 }
804 }
805 }
806 }
807
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900808 private void resetStateIfCurrentLocaleChangedLocked() {
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900809 resetAllInternalStateLocked(true /* updateOnlyWhenLocaleChanged */,
810 true /* resetDefaultImeLocked */);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900811 }
812
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900813 private void switchUserLocked(int newUserId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900814 mSettings.setCurrentUserId(newUserId);
Kenny Guy2a764942014-04-02 13:29:20 +0100815 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900816 // InputMethodFileManager should be reset when the user is changed
817 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900818 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +0900819 // For secondary users, the list of enabled IMEs may not have been updated since the
820 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
821 // not be empty even if the IME has been uninstalled by the primary user.
822 // Even in such cases, IMMS works fine because it will find the most applicable
823 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900824 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900825 if (DEBUG) {
826 Slog.d(TAG, "Switch user: " + newUserId + " current ime = " + defaultImiId);
827 }
828 resetAllInternalStateLocked(false /* updateOnlyWhenLocaleChanged */,
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900829 initialUserSwitch /* needsToResetDefaultIme */);
830 if (initialUserSwitch) {
831 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mContext.getPackageManager(),
832 mSettings.getEnabledInputMethodListLocked());
833 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900834 }
835
Kenny Guy2a764942014-04-02 13:29:20 +0100836 void updateCurrentProfileIds() {
837 List<UserInfo> profiles =
838 UserManager.get(mContext).getProfiles(mSettings.getCurrentUserId());
839 int[] currentProfileIds = new int[profiles.size()]; // profiles will not be null
840 for (int i = 0; i < currentProfileIds.length; i++) {
841 currentProfileIds[i] = profiles.get(i).id;
Amith Yamasani734983f2014-03-04 16:48:05 -0800842 }
Kenny Guy2a764942014-04-02 13:29:20 +0100843 mSettings.setCurrentProfileIds(currentProfileIds);
Amith Yamasani734983f2014-03-04 16:48:05 -0800844 }
845
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800846 @Override
847 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
848 throws RemoteException {
849 try {
850 return super.onTransact(code, data, reply, flags);
851 } catch (RuntimeException e) {
852 // The input method manager only throws security exceptions, so let's
853 // log all others.
854 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -0700855 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800856 }
857 throw e;
858 }
859 }
860
Svetoslav Ganova0027152013-06-25 14:59:53 -0700861 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700862 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900863 if (DEBUG) {
864 Slog.d(TAG, "--- systemReady");
865 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700866 if (!mSystemReady) {
867 mSystemReady = true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900868 mKeyguardManager =
869 (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700870 mNotificationManager = (NotificationManager)
871 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
872 mStatusBar = statusBar;
873 statusBar.setIconVisibility("ime", false);
874 updateImeWindowStatusLocked();
satokb858c732011-07-22 19:54:34 +0900875 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
876 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +0900877 if (mShowOngoingImeSwitcherForPhones) {
878 mWindowManagerService.setOnHardKeyboardStatusChangeListener(
879 mHardKeyboardListener);
880 }
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900881 buildInputMethodListLocked(mMethodList, mMethodMap,
882 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satok0a1bcf42012-05-16 19:26:31 +0900883 if (!mImeSelectedOnBoot) {
884 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900885 resetStateIfCurrentLocaleChangedLocked();
886 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(
887 mContext.getPackageManager(),
888 mSettings.getEnabledInputMethodListLocked());
satok0a1bcf42012-05-16 19:26:31 +0900889 }
890 mLastSystemLocale = mRes.getConfiguration().locale;
Dianne Hackborncc278702009-09-02 23:07:23 -0700891 try {
892 startInputInnerLocked();
893 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800894 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700895 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700896 }
897 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800898 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800899
satok15452a42011-10-28 17:58:28 +0900900 private void setImeWindowVisibilityStatusHiddenLocked() {
901 mImeWindowVis = 0;
902 updateImeWindowStatusLocked();
903 }
904
satok3afd6c02011-11-18 08:38:19 +0900905 private void refreshImeWindowVisibilityLocked() {
906 final Configuration conf = mRes.getConfiguration();
907 final boolean haveHardKeyboard = conf.keyboard
908 != Configuration.KEYBOARD_NOKEYS;
909 final boolean hardKeyShown = haveHardKeyboard
910 && conf.hardKeyboardHidden
911 != Configuration.HARDKEYBOARDHIDDEN_YES;
John Spurlocke0980502013-10-25 11:59:29 -0400912
913 final boolean isScreenLocked = isKeyguardLocked();
914 final boolean inputActive = !isScreenLocked && (mInputShown || hardKeyShown);
John Spurlock36439b42013-10-23 16:36:47 -0400915 // We assume the softkeyboard is shown when the input is active as long as the
916 // hard keyboard is not shown.
917 final boolean inputVisible = inputActive && !hardKeyShown;
918 mImeWindowVis = (inputActive ? InputMethodService.IME_ACTIVE : 0)
919 | (inputVisible ? InputMethodService.IME_VISIBLE : 0);
satok3afd6c02011-11-18 08:38:19 +0900920 updateImeWindowStatusLocked();
921 }
922
satok15452a42011-10-28 17:58:28 +0900923 private void updateImeWindowStatusLocked() {
satokdbf29502011-08-25 15:28:23 +0900924 setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700925 }
926
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900927 // ---------------------------------------------------------------------------------------
928 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
929 // 1) it comes from the system process
930 // 2) the calling process' user id is identical to the current user id IMMS thinks.
931 private boolean calledFromValidUser() {
932 final int uid = Binder.getCallingUid();
933 final int userId = UserHandle.getUserId(uid);
934 if (DEBUG) {
935 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
936 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
937 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +0900938 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
939 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900940 }
Kenny Guy2a764942014-04-02 13:29:20 +0100941 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900942 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900943 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +0900944
945 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
946 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
947 // must not manage background users' states in any functions.
948 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
949 // by a token.
950 if (mContext.checkCallingOrSelfPermission(
951 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
952 == PackageManager.PERMISSION_GRANTED) {
953 if (DEBUG) {
954 Slog.d(TAG, "--- Access granted because the calling process has "
955 + "the INTERACT_ACROSS_USERS_FULL permission");
956 }
957 return true;
958 }
Satoshi Kataoka0766eb02013-07-31 18:30:13 +0900959 Slog.w(TAG, "--- IPC called from background users. Ignore. \n"
960 + InputMethodUtils.getStackTrace());
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +0900961 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900962 }
963
964 private boolean bindCurrentInputMethodService(
965 Intent service, ServiceConnection conn, int flags) {
966 if (service == null || conn == null) {
967 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
968 return false;
969 }
Amith Yamasani27b89e62013-01-16 12:30:11 -0800970 return mContext.bindServiceAsUser(service, conn, flags,
971 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900972 }
973
satoke7c6998e2011-06-03 17:57:59 +0900974 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800975 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900976 // TODO: Make this work even for non-current users?
977 if (!calledFromValidUser()) {
978 return Collections.emptyList();
979 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800980 synchronized (mMethodMap) {
981 return new ArrayList<InputMethodInfo>(mMethodList);
982 }
983 }
984
satoke7c6998e2011-06-03 17:57:59 +0900985 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800986 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900987 // TODO: Make this work even for non-current users?
988 if (!calledFromValidUser()) {
989 return Collections.emptyList();
990 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800991 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900992 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800993 }
994 }
995
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900996 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +0900997 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900998 * @return enabled subtypes of the specified imi
999 */
satoke7c6998e2011-06-03 17:57:59 +09001000 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001001 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001002 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001003 // TODO: Make this work even for non-current users?
1004 if (!calledFromValidUser()) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001005 return Collections.<InputMethodSubtype>emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001006 }
satok67ddf9c2010-11-17 09:45:54 +09001007 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001008 final InputMethodInfo imi;
1009 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001010 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001011 } else {
1012 imi = mMethodMap.get(imiId);
1013 }
1014 if (imi == null) {
1015 return Collections.<InputMethodSubtype>emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001016 }
1017 return mSettings.getEnabledInputMethodSubtypeListLocked(
1018 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001019 }
1020 }
1021
satoke7c6998e2011-06-03 17:57:59 +09001022 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001023 public void addClient(IInputMethodClient client,
1024 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001025 if (!calledFromValidUser()) {
1026 return;
1027 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001028 synchronized (mMethodMap) {
1029 mClients.put(client.asBinder(), new ClientState(client,
1030 inputContext, uid, pid));
1031 }
1032 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001033
satoke7c6998e2011-06-03 17:57:59 +09001034 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001036 if (!calledFromValidUser()) {
1037 return;
1038 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001039 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001040 ClientState cs = mClients.remove(client.asBinder());
1041 if (cs != null) {
1042 clearClientSessionLocked(cs);
1043 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001044 }
1045 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001046
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001047 void executeOrSendMessage(IInterface target, Message msg) {
1048 if (target.asBinder() instanceof Binder) {
1049 mCaller.sendMessage(msg);
1050 } else {
1051 handleMessage(msg);
1052 msg.recycle();
1053 }
1054 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001055
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001056 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001057 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001058 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001059 + mCurClient.client.asBinder());
1060 if (mBoundToMethod) {
1061 mBoundToMethod = false;
1062 if (mCurMethod != null) {
1063 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1064 MSG_UNBIND_INPUT, mCurMethod));
1065 }
1066 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001067
1068 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1069 MSG_SET_ACTIVE, 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001070 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1071 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1072 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001074
The Android Open Source Project10592532009-03-18 17:39:46 -07001075 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001076 }
1077 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001078
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001079 private int getImeShowFlags() {
1080 int flags = 0;
1081 if (mShowForced) {
1082 flags |= InputMethod.SHOW_FORCED
1083 | InputMethod.SHOW_EXPLICIT;
1084 } else if (mShowExplicitlyRequested) {
1085 flags |= InputMethod.SHOW_EXPLICIT;
1086 }
1087 return flags;
1088 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001089
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001090 private int getAppShowFlags() {
1091 int flags = 0;
1092 if (mShowForced) {
1093 flags |= InputMethodManager.SHOW_FORCED;
1094 } else if (!mShowExplicitlyRequested) {
1095 flags |= InputMethodManager.SHOW_IMPLICIT;
1096 }
1097 return flags;
1098 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001099
Dianne Hackborn7663d802012-02-24 13:08:49 -08001100 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001101 if (!mBoundToMethod) {
1102 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1103 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1104 mBoundToMethod = true;
1105 }
1106 final SessionState session = mCurClient.curSession;
1107 if (initial) {
1108 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1109 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
1110 } else {
1111 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1112 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
1113 }
1114 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001115 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001116 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001117 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001118 return new InputBindResult(session.session,
1119 session.channel != null ? session.channel.dup() : null, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001122 InputBindResult startInputLocked(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001123 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001124 // If no method is currently selected, do nothing.
1125 if (mCurMethodId == null) {
1126 return mNoBinding;
1127 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001128
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001129 ClientState cs = mClients.get(client.asBinder());
1130 if (cs == null) {
1131 throw new IllegalArgumentException("unknown client "
1132 + client.asBinder());
1133 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135 try {
1136 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1137 // Check with the window manager to make sure this client actually
1138 // has a window with focus. If not, reject. This is thread safe
1139 // because if the focus changes some time before or after, the
1140 // next client receiving focus that has any interest in input will
1141 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001142 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1144 return null;
1145 }
1146 } catch (RemoteException e) {
1147 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001148
Dianne Hackborn7663d802012-02-24 13:08:49 -08001149 return startInputUncheckedLocked(cs, inputContext, attribute, controlFlags);
1150 }
1151
1152 InputBindResult startInputUncheckedLocked(ClientState cs,
1153 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
1154 // If no method is currently selected, do nothing.
1155 if (mCurMethodId == null) {
1156 return mNoBinding;
1157 }
1158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001160 // Was the keyguard locked when switching over to the new client?
1161 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001162 // If the client is changing, we need to switch over to the new
1163 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001164 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001165 if (DEBUG) Slog.v(TAG, "switching to client: client = "
John Spurlocke0980502013-10-25 11:59:29 -04001166 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001167
1168 // If the screen is on, inform the new client it is active
1169 if (mScreenOn) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001170 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
1171 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001172 }
1173 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001175 // Bump up the sequence for this client and attach it.
1176 mCurSeq++;
1177 if (mCurSeq <= 0) mCurSeq = 1;
1178 mCurClient = cs;
1179 mCurInputContext = inputContext;
1180 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001182 // Check if the input method is changing.
1183 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1184 if (cs.curSession != null) {
1185 // Fast case: if we are already connected to the input method,
1186 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001187 return attachNewInputLocked(
1188 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 }
1190 if (mHaveConnection) {
1191 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 // Return to client, and we will get back with it when
1193 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001194 requestClientSessionLocked(cs);
1195 return new InputBindResult(null, null, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001196 } else if (SystemClock.uptimeMillis()
1197 < (mLastBindTime+TIME_TO_RECONNECT)) {
1198 // In this case we have connected to the service, but
1199 // don't yet have its interface. If it hasn't been too
1200 // long since we did the connection, we'll return to
1201 // the client and wait to get the service interface so
1202 // we can report back. If it has been too long, we want
1203 // to fall through so we can try a disconnect/reconnect
1204 // to see if we can get back in touch with the service.
Jeff Brownc28867a2013-03-26 15:42:39 -07001205 return new InputBindResult(null, null, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001207 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1208 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209 }
1210 }
1211 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001212
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001213 return startInputInnerLocked();
1214 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001215
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001216 InputBindResult startInputInnerLocked() {
1217 if (mCurMethodId == null) {
1218 return mNoBinding;
1219 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001220
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001221 if (!mSystemReady) {
1222 // If the system is not yet ready, we shouldn't be running third
1223 // party code.
Jeff Brownc28867a2013-03-26 15:42:39 -07001224 return new InputBindResult(null, null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001225 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001226
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1228 if (info == null) {
1229 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1230 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001231
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001232 unbindCurrentMethodLocked(false, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001234 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1235 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001236 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1237 com.android.internal.R.string.input_method_binding_label);
1238 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1239 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001240 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001241 | Context.BIND_NOT_VISIBLE | Context.BIND_NOT_FOREGROUND
1242 | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 mLastBindTime = SystemClock.uptimeMillis();
1244 mHaveConnection = true;
1245 mCurId = info.getId();
1246 mCurToken = new Binder();
1247 try {
Craig Mautnerca0ac712013-03-14 09:43:02 -07001248 if (true || DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001249 mIWindowManager.addWindowToken(mCurToken,
1250 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1251 } catch (RemoteException e) {
1252 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001253 return new InputBindResult(null, null, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001254 } else {
1255 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001256 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001257 + mCurIntent);
1258 }
1259 return null;
1260 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001261
satoke7c6998e2011-06-03 17:57:59 +09001262 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 public InputBindResult startInput(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001264 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001265 if (!calledFromValidUser()) {
1266 return null;
1267 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001268 synchronized (mMethodMap) {
1269 final long ident = Binder.clearCallingIdentity();
1270 try {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001271 return startInputLocked(client, inputContext, attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001272 } finally {
1273 Binder.restoreCallingIdentity(ident);
1274 }
1275 }
1276 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001277
satoke7c6998e2011-06-03 17:57:59 +09001278 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001279 public void finishInput(IInputMethodClient client) {
1280 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001281
satoke7c6998e2011-06-03 17:57:59 +09001282 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001283 public void onServiceConnected(ComponentName name, IBinder service) {
1284 synchronized (mMethodMap) {
1285 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1286 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001287 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001288 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001289 unbindCurrentMethodLocked(false, false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001290 return;
1291 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001292 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001293 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1294 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001295 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001296 clearClientSessionLocked(mCurClient);
1297 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001298 }
1299 }
1300 }
1301 }
1302
Jeff Brownc28867a2013-03-26 15:42:39 -07001303 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1304 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001305 synchronized (mMethodMap) {
1306 if (mCurMethod != null && method != null
1307 && mCurMethod.asBinder() == method.asBinder()) {
1308 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001309 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001311 method, session, channel);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001312 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001313 if (res.method != null) {
1314 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
1315 MSG_BIND_METHOD, mCurClient.client, res));
1316 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001317 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001318 }
1319 }
1320 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001321
1322 // Session abandoned. Close its associated input channel.
1323 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001324 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001325
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001326 void unbindCurrentMethodLocked(boolean reportToClient, boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001327 if (mVisibleBound) {
1328 mContext.unbindService(mVisibleConnection);
1329 mVisibleBound = false;
1330 }
1331
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001332 if (mHaveConnection) {
1333 mContext.unbindService(this);
1334 mHaveConnection = false;
1335 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001336
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001337 if (mCurToken != null) {
1338 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001339 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001340 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001341 // The current IME is shown. Hence an IME switch (transition) is happening.
1342 mWindowManagerService.saveLastInputMethodWindowForTransition();
1343 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001344 mIWindowManager.removeWindowToken(mCurToken);
1345 } catch (RemoteException e) {
1346 }
1347 mCurToken = null;
1348 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001349
The Android Open Source Project10592532009-03-18 17:39:46 -07001350 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001351 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001352
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001353 if (reportToClient && mCurClient != null) {
1354 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1355 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1356 }
1357 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001358
1359 void requestClientSessionLocked(ClientState cs) {
1360 if (!cs.sessionRequested) {
1361 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1362 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1363 cs.sessionRequested = true;
1364 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1365 MSG_CREATE_SESSION, mCurMethod, channels[1],
1366 new MethodCallback(this, mCurMethod, channels[0])));
1367 }
1368 }
1369
1370 void clearClientSessionLocked(ClientState cs) {
1371 finishSessionLocked(cs.curSession);
1372 cs.curSession = null;
1373 cs.sessionRequested = false;
1374 }
1375
1376 private void finishSessionLocked(SessionState sessionState) {
1377 if (sessionState != null) {
1378 if (sessionState.session != null) {
1379 try {
1380 sessionState.session.finishSession();
1381 } catch (RemoteException e) {
1382 Slog.w(TAG, "Session failed to close due to remote exception", e);
1383 setImeWindowVisibilityStatusHiddenLocked();
1384 }
1385 sessionState.session = null;
1386 }
1387 if (sessionState.channel != null) {
1388 sessionState.channel.dispose();
1389 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001390 }
1391 }
1392 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001393
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001394 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001395 if (mCurMethod != null) {
1396 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001397 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001398 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001399
Jeff Brownc28867a2013-03-26 15:42:39 -07001400 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001401 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001402 mCurMethod = null;
1403 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001404 if (mStatusBar != null) {
1405 mStatusBar.setIconVisibility("ime", false);
1406 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001407 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001408
satoke7c6998e2011-06-03 17:57:59 +09001409 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001410 public void onServiceDisconnected(ComponentName name) {
1411 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001412 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001413 + " mCurIntent=" + mCurIntent);
1414 if (mCurMethod != null && mCurIntent != null
1415 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001416 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001417 // We consider this to be a new bind attempt, since the system
1418 // should now try to restart the service for us.
1419 mLastBindTime = SystemClock.uptimeMillis();
1420 mShowRequested = mInputShown;
1421 mInputShown = false;
1422 if (mCurClient != null) {
1423 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1424 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1425 }
1426 }
1427 }
1428 }
1429
satokf9f01002011-05-19 21:31:50 +09001430 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001431 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001432 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001433 long ident = Binder.clearCallingIdentity();
1434 try {
1435 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001436 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001437 return;
1438 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001440 synchronized (mMethodMap) {
1441 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001442 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001443 if (mStatusBar != null) {
1444 mStatusBar.setIconVisibility("ime", false);
1445 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001446 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001447 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001448 CharSequence contentDescription = null;
1449 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001450 // Use PackageManager to load label
1451 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001452 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001453 mIPackageManager.getApplicationInfo(packageName, 0,
1454 mSettings.getCurrentUserId()));
1455 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001456 /* ignore */
1457 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001458 if (mStatusBar != null) {
1459 mStatusBar.setIcon("ime", packageName, iconId, 0,
1460 contentDescription != null
1461 ? contentDescription.toString() : null);
1462 mStatusBar.setIconVisibility("ime", true);
1463 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001464 }
1465 }
1466 } finally {
1467 Binder.restoreCallingIdentity(ident);
1468 }
1469 }
1470
satok7cfc0ed2011-06-20 21:29:36 +09001471 private boolean needsToShowImeSwitchOngoingNotification() {
1472 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04001473 if (mSwitchingDialog != null) return false;
satok2c93efc2012-04-02 19:33:47 +09001474 if (isScreenLocked()) return false;
satok7cfc0ed2011-06-20 21:29:36 +09001475 synchronized (mMethodMap) {
1476 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1477 final int N = imis.size();
satokb6359412011-06-28 17:47:41 +09001478 if (N > 2) return true;
1479 if (N < 1) return false;
1480 int nonAuxCount = 0;
1481 int auxCount = 0;
1482 InputMethodSubtype nonAuxSubtype = null;
1483 InputMethodSubtype auxSubtype = null;
satok7cfc0ed2011-06-20 21:29:36 +09001484 for(int i = 0; i < N; ++i) {
1485 final InputMethodInfo imi = imis.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001486 final List<InputMethodSubtype> subtypes =
1487 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satok7cfc0ed2011-06-20 21:29:36 +09001488 final int subtypeCount = subtypes.size();
1489 if (subtypeCount == 0) {
satokb6359412011-06-28 17:47:41 +09001490 ++nonAuxCount;
satok7cfc0ed2011-06-20 21:29:36 +09001491 } else {
1492 for (int j = 0; j < subtypeCount; ++j) {
satokb6359412011-06-28 17:47:41 +09001493 final InputMethodSubtype subtype = subtypes.get(j);
1494 if (!subtype.isAuxiliary()) {
1495 ++nonAuxCount;
1496 nonAuxSubtype = subtype;
1497 } else {
1498 ++auxCount;
1499 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001500 }
1501 }
1502 }
satok7cfc0ed2011-06-20 21:29:36 +09001503 }
satokb6359412011-06-28 17:47:41 +09001504 if (nonAuxCount > 1 || auxCount > 1) {
1505 return true;
1506 } else if (nonAuxCount == 1 && auxCount == 1) {
1507 if (nonAuxSubtype != null && auxSubtype != null
satok9747f892011-09-12 15:56:40 +09001508 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1509 || auxSubtype.overridesImplicitlyEnabledSubtype()
1510 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
satokb6359412011-06-28 17:47:41 +09001511 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1512 return false;
1513 }
1514 return true;
1515 }
1516 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001517 }
satok7cfc0ed2011-06-20 21:29:36 +09001518 }
1519
John Spurlocke0980502013-10-25 11:59:29 -04001520 private boolean isKeyguardLocked() {
1521 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1522 }
1523
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001524 // Caution! This method is called in this class. Handle multi-user carefully
satokdbf29502011-08-25 15:28:23 +09001525 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001526 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001527 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001528 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001529 try {
1530 if (token == null || mCurToken != token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001531 int uid = Binder.getCallingUid();
Joe Onorato857fd9b2011-01-27 15:08:35 -08001532 Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token);
satok06487a52010-10-29 11:37:18 +09001533 return;
1534 }
satok06487a52010-10-29 11:37:18 +09001535 synchronized (mMethodMap) {
John Spurlocke0980502013-10-25 11:59:29 -04001536 // apply policy for binder calls
1537 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
1538 vis = 0;
1539 }
Joe Onorato857fd9b2011-01-27 15:08:35 -08001540 mImeWindowVis = vis;
1541 mBackDisposition = backDisposition;
jungheang.lee217fd292013-02-26 16:53:22 +09001542 final boolean iconVisibility = ((vis & (InputMethodService.IME_ACTIVE)) != 0)
1543 && (mWindowManagerService.isHardKeyboardAvailable()
1544 || (vis & (InputMethodService.IME_VISIBLE)) != 0);
Jason Monkb605fec2014-05-02 17:04:10 -04001545 final boolean needsToShowImeSwitcher = iconVisibility
1546 && needsToShowImeSwitchOngoingNotification();
1547 if (mStatusBar != null) {
1548 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
1549 needsToShowImeSwitcher);
1550 }
satok5bc8e732011-07-22 21:07:23 +09001551 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Jason Monkb605fec2014-05-02 17:04:10 -04001552 if (imi != null && needsToShowImeSwitcher) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001553 // Used to load label
satok7cfc0ed2011-06-20 21:29:36 +09001554 final CharSequence title = mRes.getText(
1555 com.android.internal.R.string.select_input_method);
Satoshi Kataokab2827262013-07-04 19:43:14 +09001556 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
1557 mContext, imi, mCurrentSubtype);
satok5bc8e732011-07-22 21:07:23 +09001558
satok7cfc0ed2011-06-20 21:29:36 +09001559 mImeSwitcherNotification.setLatestEventInfo(
satok5bc8e732011-07-22 21:07:23 +09001560 mContext, title, summary, mImeSwitchPendingIntent);
Jason Monke463c742014-05-02 12:31:45 -04001561 if ((mNotificationManager != null)
1562 && !mWindowManagerService.hasNavigationBar()) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001563 if (DEBUG) {
Satoshi Kataokab2827262013-07-04 19:43:14 +09001564 Slog.d(TAG, "--- show notification: label = " + summary);
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001565 }
1566 mNotificationManager.notifyAsUser(null,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001567 com.android.internal.R.string.select_input_method,
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001568 mImeSwitcherNotification, UserHandle.ALL);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001569 mNotificationShown = true;
1570 }
satok7cfc0ed2011-06-20 21:29:36 +09001571 } else {
Dianne Hackborn661cd522011-08-22 00:26:20 -07001572 if (mNotificationShown && mNotificationManager != null) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001573 if (DEBUG) {
1574 Slog.d(TAG, "--- hide notification");
1575 }
1576 mNotificationManager.cancelAsUser(null,
1577 com.android.internal.R.string.select_input_method, UserHandle.ALL);
satok7cfc0ed2011-06-20 21:29:36 +09001578 mNotificationShown = false;
1579 }
1580 }
satok06487a52010-10-29 11:37:18 +09001581 }
1582 } finally {
1583 Binder.restoreCallingIdentity(ident);
1584 }
1585 }
1586
satoke7c6998e2011-06-03 17:57:59 +09001587 @Override
satokf9f01002011-05-19 21:31:50 +09001588 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001589 if (!calledFromValidUser()) {
1590 return;
1591 }
satokf9f01002011-05-19 21:31:50 +09001592 synchronized (mMethodMap) {
1593 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1594 for (int i = 0; i < spans.length; ++i) {
1595 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001596 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001597 mSecureSuggestionSpans.put(ss, currentImi);
1598 }
1599 }
1600 }
1601 }
1602
satoke7c6998e2011-06-03 17:57:59 +09001603 @Override
satokf9f01002011-05-19 21:31:50 +09001604 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001605 if (!calledFromValidUser()) {
1606 return false;
1607 }
satokf9f01002011-05-19 21:31:50 +09001608 synchronized (mMethodMap) {
1609 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1610 // TODO: Do not send the intent if the process of the targetImi is already dead.
1611 if (targetImi != null) {
1612 final String[] suggestions = span.getSuggestions();
1613 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001614 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001615 final Intent intent = new Intent();
1616 // Ensures that only a class in the original IME package will receive the
1617 // notification.
satok42c5a162011-05-26 16:46:14 +09001618 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001619 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1620 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1621 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1622 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001623 final long ident = Binder.clearCallingIdentity();
1624 try {
1625 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1626 } finally {
1627 Binder.restoreCallingIdentity(ident);
1628 }
satokf9f01002011-05-19 21:31:50 +09001629 return true;
1630 }
1631 }
1632 return false;
1633 }
1634
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001635 void updateFromSettingsLocked(boolean enabledMayChange) {
1636 if (enabledMayChange) {
1637 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1638 for (int i=0; i<enabled.size(); i++) {
1639 // We allow the user to select "disabled until used" apps, so if they
1640 // are enabling one of those here we now need to make it enabled.
1641 InputMethodInfo imm = enabled.get(i);
1642 try {
1643 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1644 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1645 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09001646 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001647 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001648 if (DEBUG) {
1649 Slog.d(TAG, "Update state(" + imm.getId()
1650 + "): DISABLED_UNTIL_USED -> DEFAULT");
1651 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001652 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1653 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001654 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
1655 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001656 }
1657 } catch (RemoteException e) {
1658 }
1659 }
1660 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001661 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1662 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1663 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1664 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001665 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001666 // There is no input method selected, try to choose new applicable input method.
1667 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001668 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001669 }
1670 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001671 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001672 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001673 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001674 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001675 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001676 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001677 }
satokf3db1af2010-11-23 13:34:33 +09001678 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001679 } else {
1680 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001681 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001682 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001683 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09001684 // Here is not the perfect place to reset the switching controller. Ideally
1685 // mSwitchingController and mSettings should be able to share the same state.
1686 // TODO: Make sure that mSwitchingController and mSettings are sharing the
1687 // the same enabled IMEs list.
1688 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001689 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001690
satokab751aa2010-09-14 19:17:36 +09001691 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001692 InputMethodInfo info = mMethodMap.get(id);
1693 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001694 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001695 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001696
satokd81e9502012-05-21 12:58:45 +09001697 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001698 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09001699 final int subtypeCount = info.getSubtypeCount();
1700 if (subtypeCount <= 0) {
1701 return;
satokcd7cd292010-11-20 15:46:23 +09001702 }
satokd81e9502012-05-21 12:58:45 +09001703 final InputMethodSubtype oldSubtype = mCurrentSubtype;
1704 final InputMethodSubtype newSubtype;
1705 if (subtypeId >= 0 && subtypeId < subtypeCount) {
1706 newSubtype = info.getSubtypeAt(subtypeId);
1707 } else {
1708 // If subtype is null, try to find the most applicable one from
1709 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001710 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09001711 }
1712 if (newSubtype == null || oldSubtype == null) {
1713 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
1714 + ", new subtype = " + newSubtype);
1715 return;
1716 }
1717 if (newSubtype != oldSubtype) {
1718 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1719 if (mCurMethod != null) {
1720 try {
1721 refreshImeWindowVisibilityLocked();
1722 mCurMethod.changeInputMethodSubtype(newSubtype);
1723 } catch (RemoteException e) {
1724 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
satokab751aa2010-09-14 19:17:36 +09001725 }
1726 }
1727 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001728 return;
1729 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001730
satokd81e9502012-05-21 12:58:45 +09001731 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001732 final long ident = Binder.clearCallingIdentity();
1733 try {
satokab751aa2010-09-14 19:17:36 +09001734 // Set a subtype to this input method.
1735 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001736 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1737 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1738 // because mCurMethodId is stored as a history in
1739 // setSelectedInputMethodAndSubtypeLocked().
1740 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001741
1742 if (ActivityManagerNative.isSystemReady()) {
1743 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001744 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001745 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07001746 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001747 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001748 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001749 } finally {
1750 Binder.restoreCallingIdentity(ident);
1751 }
1752 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001753
satok42c5a162011-05-26 16:46:14 +09001754 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001755 public boolean showSoftInput(IInputMethodClient client, int flags,
1756 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001757 if (!calledFromValidUser()) {
1758 return false;
1759 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001760 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001761 long ident = Binder.clearCallingIdentity();
1762 try {
1763 synchronized (mMethodMap) {
1764 if (mCurClient == null || client == null
1765 || mCurClient.client.asBinder() != client.asBinder()) {
1766 try {
1767 // We need to check if this is the current client with
1768 // focus in the window manager, to allow this call to
1769 // be made before input is started in it.
1770 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001771 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001772 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001773 }
1774 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001775 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001776 }
1777 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001778
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001779 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001780 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001781 }
1782 } finally {
1783 Binder.restoreCallingIdentity(ident);
1784 }
1785 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001786
The Android Open Source Project4df24232009-03-05 14:34:35 -08001787 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001788 mShowRequested = true;
1789 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1790 mShowExplicitlyRequested = true;
1791 }
1792 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1793 mShowExplicitlyRequested = true;
1794 mShowForced = true;
1795 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001796
Dianne Hackborncc278702009-09-02 23:07:23 -07001797 if (!mSystemReady) {
1798 return false;
1799 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001800
The Android Open Source Project4df24232009-03-05 14:34:35 -08001801 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001802 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001803 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001804 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1805 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1806 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001807 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001808 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001809 bindCurrentInputMethodService(
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001810 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE
1811 | Context.BIND_TREAT_LIKE_ACTIVITY);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001812 mVisibleBound = true;
1813 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001814 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001815 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09001816 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001817 // The client has asked to have the input method shown, but
1818 // we have been sitting here too long with a connection to the
1819 // service and no interface received, so let's disconnect/connect
1820 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001821 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001822 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09001823 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001824 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001825 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001826 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001827 } else {
1828 if (DEBUG) {
1829 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
1830 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
1831 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001832 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001833
The Android Open Source Project4df24232009-03-05 14:34:35 -08001834 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001835 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001836
satok42c5a162011-05-26 16:46:14 +09001837 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001838 public boolean hideSoftInput(IInputMethodClient client, int flags,
1839 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001840 if (!calledFromValidUser()) {
1841 return false;
1842 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001843 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001844 long ident = Binder.clearCallingIdentity();
1845 try {
1846 synchronized (mMethodMap) {
1847 if (mCurClient == null || client == null
1848 || mCurClient.client.asBinder() != client.asBinder()) {
1849 try {
1850 // We need to check if this is the current client with
1851 // focus in the window manager, to allow this call to
1852 // be made before input is started in it.
1853 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001854 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1855 + uid + ": " + client);
satok15452a42011-10-28 17:58:28 +09001856 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001857 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001858 }
1859 } catch (RemoteException e) {
satok15452a42011-10-28 17:58:28 +09001860 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001861 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001862 }
1863 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001864
Joe Onorato8a9b2202010-02-26 18:56:32 -08001865 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001866 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001867 }
1868 } finally {
1869 Binder.restoreCallingIdentity(ident);
1870 }
1871 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001872
The Android Open Source Project4df24232009-03-05 14:34:35 -08001873 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001874 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1875 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001876 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 -08001877 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001878 }
1879 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001880 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 -08001881 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001882 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001883 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001884 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001885 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1886 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1887 res = true;
1888 } else {
1889 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001890 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001891 if (mHaveConnection && mVisibleBound) {
1892 mContext.unbindService(mVisibleConnection);
1893 mVisibleBound = false;
1894 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001895 mInputShown = false;
1896 mShowRequested = false;
1897 mShowExplicitlyRequested = false;
1898 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001899 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001900 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001901
satok42c5a162011-05-26 16:46:14 +09001902 @Override
Dianne Hackborn7663d802012-02-24 13:08:49 -08001903 public InputBindResult windowGainedFocus(IInputMethodClient client, IBinder windowToken,
1904 int controlFlags, int softInputMode, int windowFlags,
1905 EditorInfo attribute, IInputContext inputContext) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09001906 // Needs to check the validity before clearing calling identity
1907 final boolean calledFromValidUser = calledFromValidUser();
1908
Dianne Hackborn7663d802012-02-24 13:08:49 -08001909 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001910 long ident = Binder.clearCallingIdentity();
1911 try {
1912 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001913 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
Dianne Hackborn7663d802012-02-24 13:08:49 -08001914 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001915 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08001916 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001917
Dianne Hackborn7663d802012-02-24 13:08:49 -08001918 ClientState cs = mClients.get(client.asBinder());
1919 if (cs == null) {
1920 throw new IllegalArgumentException("unknown client "
1921 + client.asBinder());
1922 }
1923
1924 try {
1925 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1926 // Check with the window manager to make sure this client actually
1927 // has a window with focus. If not, reject. This is thread safe
1928 // because if the focus changes some time before or after, the
1929 // next client receiving focus that has any interest in input will
1930 // be calling through here after that change happens.
1931 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
1932 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1933 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001934 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001935 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001936 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001937
Satoshi Kataoka8d033052012-11-19 17:30:40 +09001938 if (!calledFromValidUser) {
1939 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
1940 Slog.w(TAG, "If you want to interect with IME, you need "
1941 + "android.permission.INTERACT_ACROSS_USERS_FULL");
1942 hideCurrentInputLocked(0, null);
1943 return null;
1944 }
1945
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001946 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07001947 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09001948 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001949 if (attribute != null) {
1950 return startInputUncheckedLocked(cs, inputContext, attribute,
1951 controlFlags);
1952 }
1953 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001954 }
1955 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001956
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001957 // Should we auto-show the IME even if the caller has not
1958 // specified what should be done with it?
1959 // We only do this automatically if the window can resize
1960 // to accommodate the IME (so what the user sees will give
1961 // them good context without input information being obscured
1962 // by the IME) or if running on a large screen where there
1963 // is more room for the target window + IME.
1964 final boolean doAutoShow =
1965 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1966 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1967 || mRes.getConfiguration().isLayoutSizeAtLeast(
1968 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001969 final boolean isTextEditor =
1970 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
1971
1972 // We want to start input before showing the IME, but after closing
1973 // it. We want to do this after closing it to help the IME disappear
1974 // more quickly (not get stuck behind it initializing itself for the
1975 // new focused input, even if its window wants to hide the IME).
1976 boolean didStart = false;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001977
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001978 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1979 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001980 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001981 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1982 // There is no focus view, and this window will
1983 // be behind any soft input window, so hide the
1984 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001985 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001986 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001987 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001988 } else if (isTextEditor && doAutoShow && (softInputMode &
1989 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001990 // There is a focus view, and we are navigating forward
1991 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001992 // We only do this automatically if the window can resize
1993 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001994 // them good context without input information being obscured
1995 // by the IME) or if running on a large screen where there
1996 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001997 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001998 if (attribute != null) {
1999 res = startInputUncheckedLocked(cs, inputContext, attribute,
2000 controlFlags);
2001 didStart = true;
2002 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002003 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002004 }
2005 break;
2006 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2007 // Do nothing.
2008 break;
2009 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2010 if ((softInputMode &
2011 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002012 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002013 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002014 }
2015 break;
2016 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002017 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002018 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002019 break;
2020 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2021 if ((softInputMode &
2022 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002023 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002024 if (attribute != null) {
2025 res = startInputUncheckedLocked(cs, inputContext, attribute,
2026 controlFlags);
2027 didStart = true;
2028 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002029 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002030 }
2031 break;
2032 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002033 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002034 if (attribute != null) {
2035 res = startInputUncheckedLocked(cs, inputContext, attribute,
2036 controlFlags);
2037 didStart = true;
2038 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002039 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002040 break;
2041 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002042
2043 if (!didStart && attribute != null) {
2044 res = startInputUncheckedLocked(cs, inputContext, attribute,
2045 controlFlags);
2046 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002047 }
2048 } finally {
2049 Binder.restoreCallingIdentity(ident);
2050 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002051
2052 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002053 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002054
satok42c5a162011-05-26 16:46:14 +09002055 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002056 public void showInputMethodPickerFromClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002057 if (!calledFromValidUser()) {
2058 return;
2059 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002060 synchronized (mMethodMap) {
2061 if (mCurClient == null || client == null
2062 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002063 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002064 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002065 }
2066
satok440aab52010-11-25 09:43:11 +09002067 // Always call subtype picker, because subtype picker is a superset of input method
2068 // picker.
satokab751aa2010-09-14 19:17:36 +09002069 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
2070 }
2071 }
2072
satok42c5a162011-05-26 16:46:14 +09002073 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002074 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002075 if (!calledFromValidUser()) {
2076 return;
2077 }
satok28203512010-11-24 11:06:49 +09002078 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2079 }
2080
satok42c5a162011-05-26 16:46:14 +09002081 @Override
satok28203512010-11-24 11:06:49 +09002082 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002083 if (!calledFromValidUser()) {
2084 return;
2085 }
satok28203512010-11-24 11:06:49 +09002086 synchronized (mMethodMap) {
2087 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002088 setInputMethodWithSubtypeIdLocked(token, id,
2089 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2090 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002091 } else {
2092 setInputMethod(token, id);
2093 }
2094 }
satokab751aa2010-09-14 19:17:36 +09002095 }
2096
satok42c5a162011-05-26 16:46:14 +09002097 @Override
satokb416a712010-11-25 20:42:14 +09002098 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002099 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002100 if (!calledFromValidUser()) {
2101 return;
2102 }
satokb416a712010-11-25 20:42:14 +09002103 synchronized (mMethodMap) {
2104 if (mCurClient == null || client == null
2105 || mCurClient.client.asBinder() != client.asBinder()) {
2106 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
2107 }
satok7fee71f2010-12-17 18:54:26 +09002108 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2109 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002110 }
2111 }
2112
satok4fc87d62011-05-20 16:13:43 +09002113 @Override
satok735cf382010-11-11 20:40:09 +09002114 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002115 if (!calledFromValidUser()) {
2116 return false;
2117 }
satok735cf382010-11-11 20:40:09 +09002118 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002119 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002120 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002121 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002122 lastImi = mMethodMap.get(lastIme.first);
2123 } else {
2124 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002125 }
satok4fc87d62011-05-20 16:13:43 +09002126 String targetLastImiId = null;
2127 int subtypeId = NOT_A_SUBTYPE_ID;
2128 if (lastIme != null && lastImi != null) {
2129 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
2130 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
2131 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2132 : mCurrentSubtype.hashCode();
2133 // If the last IME is the same as the current IME and the last subtype is not
2134 // defined, there is no need to switch to the last IME.
2135 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2136 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002137 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002138 }
2139 }
2140
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002141 if (TextUtils.isEmpty(targetLastImiId)
2142 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002143 // This is a safety net. If the currentSubtype can't be added to the history
2144 // and the framework couldn't find the last ime, we will make the last ime be
2145 // the most applicable enabled keyboard subtype of the system imes.
2146 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2147 if (enabled != null) {
2148 final int N = enabled.size();
2149 final String locale = mCurrentSubtype == null
2150 ? mRes.getConfiguration().locale.toString()
2151 : mCurrentSubtype.getLocale();
2152 for (int i = 0; i < N; ++i) {
2153 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002154 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002155 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002156 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2157 InputMethodUtils.getSubtypes(imi),
2158 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002159 if (keyboardSubtype != null) {
2160 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002161 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002162 imi, keyboardSubtype.hashCode());
2163 if(keyboardSubtype.getLocale().equals(locale)) {
2164 break;
2165 }
2166 }
2167 }
2168 }
2169 }
2170 }
2171
2172 if (!TextUtils.isEmpty(targetLastImiId)) {
2173 if (DEBUG) {
2174 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2175 + ", from: " + mCurMethodId + ", " + subtypeId);
2176 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002177 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002178 return true;
2179 } else {
2180 return false;
2181 }
satok735cf382010-11-11 20:40:09 +09002182 }
2183 }
2184
satoke7c6998e2011-06-03 17:57:59 +09002185 @Override
satok688bd472012-02-09 20:09:17 +09002186 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002187 if (!calledFromValidUser()) {
2188 return false;
2189 }
satok688bd472012-02-09 20:09:17 +09002190 synchronized (mMethodMap) {
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002191 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
satok688bd472012-02-09 20:09:17 +09002192 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2193 if (nextSubtype == null) {
2194 return false;
2195 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002196 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2197 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002198 return true;
2199 }
2200 }
2201
2202 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002203 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2204 if (!calledFromValidUser()) {
2205 return false;
2206 }
2207 synchronized (mMethodMap) {
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002208 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002209 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2210 if (nextSubtype == null) {
2211 return false;
2212 }
2213 return true;
2214 }
2215 }
2216
2217 @Override
satok68f1b782011-04-11 14:26:04 +09002218 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002219 if (!calledFromValidUser()) {
2220 return null;
2221 }
satok68f1b782011-04-11 14:26:04 +09002222 synchronized (mMethodMap) {
2223 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2224 // TODO: Handle the case of the last IME with no subtypes
2225 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2226 || TextUtils.isEmpty(lastIme.second)) return null;
2227 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2228 if (lastImi == null) return null;
2229 try {
2230 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002231 final int lastSubtypeId =
2232 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002233 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2234 return null;
2235 }
2236 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002237 } catch (NumberFormatException e) {
2238 return null;
2239 }
2240 }
2241 }
2242
satoke7c6998e2011-06-03 17:57:59 +09002243 @Override
satokee5e77c2011-09-02 18:50:15 +09002244 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002245 if (!calledFromValidUser()) {
2246 return;
2247 }
satok91e88122011-07-18 11:11:42 +09002248 // By this IPC call, only a process which shares the same uid with the IME can add
2249 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09002250 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09002251 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002252 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002253 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002254 final String[] packageInfos;
2255 try {
2256 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2257 } catch (RemoteException e) {
2258 Slog.e(TAG, "Failed to get package infos");
2259 return;
2260 }
satok91e88122011-07-18 11:11:42 +09002261 if (packageInfos != null) {
2262 final int packageNum = packageInfos.length;
2263 for (int i = 0; i < packageNum; ++i) {
2264 if (packageInfos[i].equals(imi.getPackageName())) {
2265 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002266 final long ident = Binder.clearCallingIdentity();
2267 try {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002268 buildInputMethodListLocked(mMethodList, mMethodMap,
2269 false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002270 } finally {
2271 Binder.restoreCallingIdentity(ident);
2272 }
satokee5e77c2011-09-02 18:50:15 +09002273 return;
satok91e88122011-07-18 11:11:42 +09002274 }
2275 }
2276 }
satoke7c6998e2011-06-03 17:57:59 +09002277 }
satokee5e77c2011-09-02 18:50:15 +09002278 return;
satoke7c6998e2011-06-03 17:57:59 +09002279 }
2280
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002281 @Override
2282 public int getInputMethodWindowVisibleHeight() {
2283 return mWindowManagerService.getInputMethodWindowVisibleHeight();
2284 }
2285
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002286 @Override
2287 public void notifyTextCommitted() {
2288 if (DEBUG) {
2289 Slog.d(TAG, "Got the notification of commitText");
2290 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002291 synchronized (mMethodMap) {
2292 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2293 if (imi != null) {
2294 mSwitchingController.onCommitTextLocked(imi, mCurrentSubtype);
2295 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002296 }
2297 }
2298
Yohei Yukawab7b79072014-03-25 11:02:00 +09002299 @Override
2300 public void setCursorAnchorMonitorMode(IBinder token, int monitorMode) {
2301 if (DEBUG) {
2302 Slog.d(TAG, "setCursorAnchorMonitorMode: monitorMode=" + monitorMode);
2303 }
2304 if (!calledFromValidUser()) {
2305 return;
2306 }
2307 synchronized (mMethodMap) {
2308 if (token == null || mCurToken != token) {
2309 if (DEBUG) {
2310 Slog.w(TAG, "Ignoring setCursorAnchorMonitorMode from uid "
2311 + Binder.getCallingUid() + " token: " + token);
2312 }
2313 return;
2314 }
2315 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIO(
2316 MSG_SET_CURSOR_ANCHOR_MONITOR_MODE, monitorMode, mCurClient));
2317 }
2318 }
2319
satok28203512010-11-24 11:06:49 +09002320 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002321 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002322 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
2323 }
2324 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002325
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002326 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
2327 if (token == null) {
2328 if (mContext.checkCallingOrSelfPermission(
2329 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2330 != PackageManager.PERMISSION_GRANTED) {
2331 throw new SecurityException(
2332 "Using null token requires permission "
2333 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002334 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002335 } else if (mCurToken != token) {
2336 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2337 + " token: " + token);
2338 return;
2339 }
2340
2341 final long ident = Binder.clearCallingIdentity();
2342 try {
2343 setInputMethodLocked(id, subtypeId);
2344 } finally {
2345 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002346 }
2347 }
2348
satok42c5a162011-05-26 16:46:14 +09002349 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002350 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002351 if (!calledFromValidUser()) {
2352 return;
2353 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002354 synchronized (mMethodMap) {
2355 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002356 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
2357 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002358 return;
2359 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002360 long ident = Binder.clearCallingIdentity();
2361 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002362 hideCurrentInputLocked(flags, null);
2363 } finally {
2364 Binder.restoreCallingIdentity(ident);
2365 }
2366 }
2367 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002368
satok42c5a162011-05-26 16:46:14 +09002369 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002370 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002371 if (!calledFromValidUser()) {
2372 return;
2373 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002374 synchronized (mMethodMap) {
2375 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002376 Slog.w(TAG, "Ignoring showMySoftInput of uid "
2377 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002378 return;
2379 }
2380 long ident = Binder.clearCallingIdentity();
2381 try {
2382 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002383 } finally {
2384 Binder.restoreCallingIdentity(ident);
2385 }
2386 }
2387 }
2388
2389 void setEnabledSessionInMainThread(SessionState session) {
2390 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002391 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002392 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002393 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002394 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002395 } catch (RemoteException e) {
2396 }
2397 }
2398 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002399 if (mEnabledSession != null && mEnabledSession.session != null) {
2400 try {
2401 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
2402 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
2403 } catch (RemoteException e) {
2404 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002405 }
2406 }
2407 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002408
satok42c5a162011-05-26 16:46:14 +09002409 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002410 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002411 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002412 switch (msg.what) {
2413 case MSG_SHOW_IM_PICKER:
2414 showInputMethodMenu();
2415 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002416
satokab751aa2010-09-14 19:17:36 +09002417 case MSG_SHOW_IM_SUBTYPE_PICKER:
2418 showInputMethodSubtypeMenu();
2419 return true;
2420
satok47a44912010-10-06 16:03:58 +09002421 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002422 args = (SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09002423 showInputMethodAndSubtypeEnabler((String)args.arg1);
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002424 args.recycle();
satok217f5482010-12-15 05:19:19 +09002425 return true;
2426
2427 case MSG_SHOW_IM_CONFIG:
2428 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002429 return true;
2430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002431 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002433 case MSG_UNBIND_INPUT:
2434 try {
2435 ((IInputMethod)msg.obj).unbindInput();
2436 } catch (RemoteException e) {
2437 // There is nothing interesting about the method dying.
2438 }
2439 return true;
2440 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002441 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002442 try {
2443 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2444 } catch (RemoteException e) {
2445 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002446 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002447 return true;
2448 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002449 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002450 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002451 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002452 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002453 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002454 } catch (RemoteException e) {
2455 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002456 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002457 return true;
2458 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002459 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002460 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002461 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002462 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002463 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002464 } catch (RemoteException e) {
2465 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002466 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002467 return true;
2468 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002469 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002470 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002471 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002472 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2473 } catch (RemoteException e) {
2474 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002475 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002476 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002477 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002478 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002479 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002480 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002481 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002482 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002483 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002484 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002485 // Dispose the channel if the input method is not local to this process
2486 // because the remote proxy will get its own copy when unparceled.
2487 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002488 channel.dispose();
2489 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002490 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002491 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002492 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002493 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002494 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002496 case MSG_START_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002497 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002498 try {
2499 SessionState session = (SessionState)args.arg1;
2500 setEnabledSessionInMainThread(session);
2501 session.method.startInput((IInputContext)args.arg2,
2502 (EditorInfo)args.arg3);
2503 } catch (RemoteException e) {
2504 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002505 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002506 return true;
2507 case MSG_RESTART_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002508 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002509 try {
2510 SessionState session = (SessionState)args.arg1;
2511 setEnabledSessionInMainThread(session);
2512 session.method.restartInput((IInputContext)args.arg2,
2513 (EditorInfo)args.arg3);
2514 } catch (RemoteException e) {
2515 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002516 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002517 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002518
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002519 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002520
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002521 case MSG_UNBIND_METHOD:
2522 try {
2523 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
2524 } catch (RemoteException e) {
2525 // There is nothing interesting about the last client dying.
2526 }
2527 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002528 case MSG_BIND_METHOD: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002529 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002530 IInputMethodClient client = (IInputMethodClient)args.arg1;
2531 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002532 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002533 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002534 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002535 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002536 } finally {
2537 // Dispose the channel if the input method is not local to this process
2538 // because the remote proxy will get its own copy when unparceled.
2539 if (res.channel != null && Binder.isProxy(client)) {
2540 res.channel.dispose();
2541 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002542 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002543 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002544 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002545 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002546 case MSG_SET_ACTIVE:
2547 try {
2548 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2549 } catch (RemoteException e) {
2550 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2551 + ((ClientState)msg.obj).pid + " uid "
2552 + ((ClientState)msg.obj).uid);
2553 }
2554 return true;
Yohei Yukawab7b79072014-03-25 11:02:00 +09002555 case MSG_SET_CURSOR_ANCHOR_MONITOR_MODE:
2556 try {
2557 ((ClientState)msg.obj).client.setCursorAnchorMonitorMode(msg.arg1);
2558 } catch (RemoteException e) {
2559 Slog.w(TAG, "Got RemoteException sending setCursorAnchorMonitorMode "
2560 + "notification to pid " + ((ClientState)msg.obj).pid
2561 + " uid " + ((ClientState)msg.obj).uid);
2562 }
2563 return true;
satok01038492012-04-09 21:08:27 +09002564
2565 // --------------------------------------------------------------
2566 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
2567 mHardKeyboardListener.handleHardKeyboardStatusChange(
2568 msg.arg1 == 1, msg.arg2 == 1);
2569 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002570 }
2571 return false;
2572 }
2573
satokdc9ddae2011-10-06 12:22:36 +09002574 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002575 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
2576 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09002577 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002578 if (DEBUG) {
2579 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2580 }
satok723a27e2010-11-11 14:58:11 +09002581 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002582 return true;
2583 }
2584
2585 return false;
2586 }
2587
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002588 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002589 HashMap<String, InputMethodInfo> map, boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002590 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002591 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Satoshi Kataoka0766eb02013-07-31 18:30:13 +09002592 + " \n ------ \n" + InputMethodUtils.getStackTrace());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002593 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002594 list.clear();
2595 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002596
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002597 // Use for queryIntentServicesAsUser
2598 final PackageManager pm = mContext.getPackageManager();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002599 String disabledSysImes = mSettings.getDisabledSystemInputMethods();
Amith Yamasanie861ec12010-03-24 21:39:27 -07002600 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002601
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002602 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002603 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002604 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2605 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002606
satoke7c6998e2011-06-03 17:57:59 +09002607 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2608 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002609 for (int i = 0; i < services.size(); ++i) {
2610 ResolveInfo ri = services.get(i);
2611 ServiceInfo si = ri.serviceInfo;
2612 ComponentName compName = new ComponentName(si.packageName, si.name);
2613 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2614 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002615 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002616 + ": it does not require the permission "
2617 + android.Manifest.permission.BIND_INPUT_METHOD);
2618 continue;
2619 }
2620
Joe Onorato8a9b2202010-02-26 18:56:32 -08002621 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002622
2623 try {
satoke7c6998e2011-06-03 17:57:59 +09002624 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002625 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002626 final String id = p.getId();
2627 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002628
2629 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002630 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002631 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002632
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002633 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002634 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002635 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002636 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002637 }
2638 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002639
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002640 if (resetDefaultEnabledIme) {
2641 final ArrayList<InputMethodInfo> defaultEnabledIme =
2642 InputMethodUtils.getDefaultEnabledImes(mContext, mSystemReady, list);
2643 for (int i = 0; i < defaultEnabledIme.size(); ++i) {
2644 final InputMethodInfo imi = defaultEnabledIme.get(i);
2645 if (DEBUG) {
2646 Slog.d(TAG, "--- enable ime = " + imi);
2647 }
2648 setInputMethodEnabledLocked(imi.getId(), true);
2649 }
2650 }
2651
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002652 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09002653 if (!TextUtils.isEmpty(defaultImiId)) {
2654 if (!map.containsKey(defaultImiId)) {
2655 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
2656 if (chooseNewDefaultIMELocked()) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002657 updateFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09002658 }
2659 } else {
2660 // Double check that the default IME is certainly enabled.
2661 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002662 }
2663 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09002664 // Here is not the perfect place to reset the switching controller. Ideally
2665 // mSwitchingController and mSettings should be able to share the same state.
2666 // TODO: Make sure that mSwitchingController and mSettings are sharing the
2667 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09002668 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002669 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002670
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002671 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002672
satokab751aa2010-09-14 19:17:36 +09002673 private void showInputMethodMenu() {
2674 showInputMethodMenuInternal(false);
2675 }
2676
2677 private void showInputMethodSubtypeMenu() {
2678 showInputMethodMenuInternal(true);
2679 }
2680
satok217f5482010-12-15 05:19:19 +09002681 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002682 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002683 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002684 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2685 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002686 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002687 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002688 }
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002689 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok217f5482010-12-15 05:19:19 +09002690 }
2691
2692 private void showConfigureInputMethods() {
2693 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2694 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2695 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2696 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002697 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09002698 }
2699
satok2c93efc2012-04-02 19:33:47 +09002700 private boolean isScreenLocked() {
2701 return mKeyguardManager != null
2702 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
2703 }
satokab751aa2010-09-14 19:17:36 +09002704 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002705 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002707 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09002708 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002709
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002710 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002711 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002712 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002713
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002714 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002715 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09002716 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
2717 mContext);
satok7f35c8c2010-10-07 21:13:11 +09002718 if (immis == null || immis.size() == 0) {
2719 return;
2720 }
2721
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002722 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002723
satok688bd472012-02-09 20:09:17 +09002724 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002725 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
satok688bd472012-02-09 20:09:17 +09002726 showSubtypes, mInputShown, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09002727
satokc3690562012-01-10 20:14:43 +09002728 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002729 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09002730 if (currentSubtype != null) {
2731 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002732 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
2733 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09002734 }
2735 }
2736
Ken Wakasa761eb372011-03-04 19:06:18 +09002737 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09002738 mIms = new InputMethodInfo[N];
2739 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002740 int checkedItem = 0;
2741 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002742 final ImeSubtypeListItem item = imList.get(i);
2743 mIms[i] = item.mImi;
2744 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002745 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002746 int subtypeId = mSubtypeIds[i];
2747 if ((subtypeId == NOT_A_SUBTYPE_ID)
2748 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2749 || (subtypeId == lastInputMethodSubtypeId)) {
2750 checkedItem = i;
2751 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002752 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002753 }
Ken Wakasa05dbb652011-08-22 15:22:43 +09002754 final TypedArray a = context.obtainStyledAttributes(null,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002755 com.android.internal.R.styleable.DialogPreference,
2756 com.android.internal.R.attr.alertDialogStyle, 0);
2757 mDialogBuilder = new AlertDialog.Builder(context)
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002758 .setOnCancelListener(new OnCancelListener() {
satok42c5a162011-05-26 16:46:14 +09002759 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002760 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002761 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002762 }
2763 })
2764 .setIcon(a.getDrawable(
2765 com.android.internal.R.styleable.DialogPreference_dialogTitle));
2766 a.recycle();
satok01038492012-04-09 21:08:27 +09002767 final LayoutInflater inflater =
2768 (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2769 final View tv = inflater.inflate(
2770 com.android.internal.R.layout.input_method_switch_dialog_title, null);
2771 mDialogBuilder.setCustomTitle(tv);
2772
2773 // Setup layout for a toggle switch of the hardware keyboard
2774 mSwitchingDialogTitleView = tv;
2775 mSwitchingDialogTitleView.findViewById(
2776 com.android.internal.R.id.hard_keyboard_section).setVisibility(
2777 mWindowManagerService.isHardKeyboardAvailable() ?
2778 View.VISIBLE : View.GONE);
2779 final Switch hardKeySwitch = ((Switch)mSwitchingDialogTitleView.findViewById(
2780 com.android.internal.R.id.hard_keyboard_switch));
2781 hardKeySwitch.setChecked(mWindowManagerService.isHardKeyboardEnabled());
2782 hardKeySwitch.setOnCheckedChangeListener(
2783 new OnCheckedChangeListener() {
2784 @Override
2785 public void onCheckedChanged(
2786 CompoundButton buttonView, boolean isChecked) {
2787 mWindowManagerService.setHardKeyboardEnabled(isChecked);
Satoshi Kataoka04dd24d2013-01-18 13:44:37 +09002788 // Ensure that the input method dialog is dismissed when changing
2789 // the hardware keyboard state.
2790 hideInputMethodMenu();
satok01038492012-04-09 21:08:27 +09002791 }
2792 });
satokd87c2592010-09-29 11:52:06 +09002793
Ken Wakasa05dbb652011-08-22 15:22:43 +09002794 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(context,
2795 com.android.internal.R.layout.simple_list_item_2_single_choice, imList,
2796 checkedItem);
2797
2798 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002799 new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002800 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002801 public void onClick(DialogInterface dialog, int which) {
2802 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09002803 if (mIms == null || mIms.length <= which
2804 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002805 return;
2806 }
2807 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09002808 int subtypeId = mSubtypeIds[which];
Satoshi Kataokad2142962012-11-12 18:43:06 +09002809 adapter.mCheckedItem = which;
2810 adapter.notifyDataSetChanged();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002811 hideInputMethodMenu();
2812 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09002813 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09002814 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09002815 subtypeId = NOT_A_SUBTYPE_ID;
2816 }
2817 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002818 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08002819 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002820 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002821 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002822
satokbc81b692011-08-26 16:22:22 +09002823 if (showSubtypes && !isScreenLocked) {
satok82beadf2010-12-27 19:03:06 +09002824 mDialogBuilder.setPositiveButton(
2825 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09002826 new DialogInterface.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002827 @Override
satok7f35c8c2010-10-07 21:13:11 +09002828 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09002829 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09002830 }
2831 });
2832 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002833 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002834 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002835 mSwitchingDialog.getWindow().setType(
2836 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09002837 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
2838 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002839 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
Jason Monk807ef762014-05-08 15:47:46 -04002840 updateImeWindowStatusLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002841 mSwitchingDialog.show();
2842 }
2843 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002844
Ken Wakasa05dbb652011-08-22 15:22:43 +09002845 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
2846 private final LayoutInflater mInflater;
2847 private final int mTextViewResourceId;
2848 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09002849 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09002850 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
2851 List<ImeSubtypeListItem> itemsList, int checkedItem) {
2852 super(context, textViewResourceId, itemsList);
2853 mTextViewResourceId = textViewResourceId;
2854 mItemsList = itemsList;
2855 mCheckedItem = checkedItem;
2856 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2857 }
2858
2859 @Override
2860 public View getView(int position, View convertView, ViewGroup parent) {
2861 final View view = convertView != null ? convertView
2862 : mInflater.inflate(mTextViewResourceId, null);
2863 if (position < 0 || position >= mItemsList.size()) return view;
2864 final ImeSubtypeListItem item = mItemsList.get(position);
2865 final CharSequence imeName = item.mImeName;
2866 final CharSequence subtypeName = item.mSubtypeName;
2867 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
2868 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
2869 if (TextUtils.isEmpty(subtypeName)) {
2870 firstTextView.setText(imeName);
2871 secondTextView.setVisibility(View.GONE);
2872 } else {
2873 firstTextView.setText(subtypeName);
2874 secondTextView.setText(imeName);
2875 secondTextView.setVisibility(View.VISIBLE);
2876 }
2877 final RadioButton radioButton =
2878 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
2879 radioButton.setChecked(position == mCheckedItem);
2880 return view;
2881 }
2882 }
2883
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002884 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07002885 synchronized (mMethodMap) {
2886 hideInputMethodMenuLocked();
2887 }
2888 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002889
The Android Open Source Project10592532009-03-18 17:39:46 -07002890 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002891 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002892
The Android Open Source Project10592532009-03-18 17:39:46 -07002893 if (mSwitchingDialog != null) {
2894 mSwitchingDialog.dismiss();
2895 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002896 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002897
Jason Monk807ef762014-05-08 15:47:46 -04002898 updateImeWindowStatusLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -07002899 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07002900 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002901 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002902
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002903 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002904
satok42c5a162011-05-26 16:46:14 +09002905 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002906 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002907 // TODO: Make this work even for non-current users?
2908 if (!calledFromValidUser()) {
2909 return false;
2910 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002911 synchronized (mMethodMap) {
2912 if (mContext.checkCallingOrSelfPermission(
2913 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2914 != PackageManager.PERMISSION_GRANTED) {
2915 throw new SecurityException(
2916 "Requires permission "
2917 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2918 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002920 long ident = Binder.clearCallingIdentity();
2921 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002922 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002923 } finally {
2924 Binder.restoreCallingIdentity(ident);
2925 }
2926 }
2927 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002928
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002929 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
2930 // Make sure this is a valid input method.
2931 InputMethodInfo imm = mMethodMap.get(id);
2932 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09002933 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002934 }
2935
satokd87c2592010-09-29 11:52:06 +09002936 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
2937 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002938
satokd87c2592010-09-29 11:52:06 +09002939 if (enabled) {
2940 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
2941 if (pair.first.equals(id)) {
2942 // We are enabling this input method, but it is already enabled.
2943 // Nothing to do. The previous state was enabled.
2944 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002945 }
2946 }
satokd87c2592010-09-29 11:52:06 +09002947 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
2948 // Previous state was disabled.
2949 return false;
2950 } else {
2951 StringBuilder builder = new StringBuilder();
2952 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2953 builder, enabledInputMethodsList, id)) {
2954 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002955 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002956 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
2957 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
2958 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09002959 }
2960 // Previous state was enabled.
2961 return true;
2962 } else {
2963 // We are disabling the input method but it is already disabled.
2964 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002965 return false;
2966 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002967 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002968 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002969
satok723a27e2010-11-11 14:58:11 +09002970 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
2971 boolean setSubtypeOnly) {
2972 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002973 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09002974
2975 // Set Subtype here
2976 if (imi == null || subtypeId < 0) {
2977 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08002978 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09002979 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09002980 if (subtypeId < imi.getSubtypeCount()) {
2981 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
2982 mSettings.putSelectedSubtype(subtype.hashCode());
2983 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09002984 } else {
2985 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09002986 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002987 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09002988 }
satokab751aa2010-09-14 19:17:36 +09002989 }
satok723a27e2010-11-11 14:58:11 +09002990
satok4c0e7152012-06-20 20:08:44 +09002991 // Workaround.
2992 // ASEC is not ready in the IMMS constructor. Accordingly, forward-locked
2993 // IMEs are not recognized and considered uninstalled.
2994 // Actually, we can't move everything after SystemReady because
2995 // IMMS needs to run in the encryption lock screen. So, we just skip changing
2996 // the default IME here and try cheking the default IME again in systemReady().
2997 // TODO: Do nothing before system ready and implement a separated logic for
2998 // the encryption lock screen.
2999 // TODO: ASEC should be ready before IMMS is instantiated.
3000 if (mSystemReady && !setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003001 // Set InputMethod here
3002 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3003 }
3004 }
3005
3006 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3007 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3008 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3009 // newDefaultIme is empty when there is no candidate for the selected IME.
3010 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3011 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3012 if (subtypeHashCode != null) {
3013 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003014 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok723a27e2010-11-11 14:58:11 +09003015 imi, Integer.valueOf(subtypeHashCode));
3016 } catch (NumberFormatException e) {
3017 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3018 }
3019 }
3020 }
3021 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003022 }
3023
satok4e4569d2010-11-19 18:45:53 +09003024 // If there are no selected shortcuts, tries finding the most applicable ones.
3025 private Pair<InputMethodInfo, InputMethodSubtype>
3026 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3027 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3028 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003029 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003030 boolean foundInSystemIME = false;
3031
3032 // Search applicable subtype for each InputMethodInfo
3033 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003034 final String imiId = imi.getId();
3035 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3036 continue;
3037 }
satokcd7cd292010-11-20 15:46:23 +09003038 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003039 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003040 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003041 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003042 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003043 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003044 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003045 }
satokdf31ae62011-01-15 06:19:44 +09003046 // 2. Search by the system locale from enabledSubtypes.
3047 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003048 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003049 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003050 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003051 }
satoka86f5e42011-09-02 17:12:42 +09003052 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003053 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003054 final ArrayList<InputMethodSubtype> subtypesForSearch =
3055 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003056 ? InputMethodUtils.getSubtypes(imi)
3057 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003058 // 4. Search by the current subtype's locale from all subtypes.
3059 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003060 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003061 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003062 }
3063 // 5. Search by the system locale from all subtypes.
3064 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003065 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003066 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003067 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003068 }
satokcd7cd292010-11-20 15:46:23 +09003069 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003070 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003071 // The current input method is the most applicable IME.
3072 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003073 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003074 break;
satok7599a7f2010-12-22 13:45:23 +09003075 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003076 // The system input method is 2nd applicable IME.
3077 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003078 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003079 if ((imi.getServiceInfo().applicationInfo.flags
3080 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3081 foundInSystemIME = true;
3082 }
satok4e4569d2010-11-19 18:45:53 +09003083 }
3084 }
3085 }
3086 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003087 if (mostApplicableIMI != null) {
3088 Slog.w(TAG, "Most applicable shortcut input method was:"
3089 + mostApplicableIMI.getId());
3090 if (mostApplicableSubtype != null) {
3091 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3092 + "," + mostApplicableSubtype.getMode() + ","
3093 + mostApplicableSubtype.getLocale());
3094 }
3095 }
satok4e4569d2010-11-19 18:45:53 +09003096 }
satokcd7cd292010-11-20 15:46:23 +09003097 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09003098 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09003099 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003100 } else {
3101 return null;
3102 }
3103 }
3104
satokab751aa2010-09-14 19:17:36 +09003105 /**
3106 * @return Return the current subtype of this input method.
3107 */
satok42c5a162011-05-26 16:46:14 +09003108 @Override
satokab751aa2010-09-14 19:17:36 +09003109 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003110 // TODO: Make this work even for non-current users?
3111 if (!calledFromValidUser()) {
3112 return null;
3113 }
3114 synchronized (mMethodMap) {
3115 return getCurrentInputMethodSubtypeLocked();
3116 }
3117 }
3118
3119 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003120 if (mCurMethodId == null) {
3121 return null;
3122 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003123 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003124 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3125 if (imi == null || imi.getSubtypeCount() == 0) {
3126 return null;
satok4e4569d2010-11-19 18:45:53 +09003127 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003128 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003129 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3130 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003131 if (subtypeId == NOT_A_SUBTYPE_ID) {
3132 // If there are no selected subtypes, the framework will try to find
3133 // the most applicable subtype from explicitly or implicitly enabled
3134 // subtypes.
3135 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003136 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003137 // If there is only one explicitly or implicitly enabled subtype,
3138 // just returns it.
3139 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3140 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3141 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003142 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003143 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003144 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003145 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003146 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003147 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3148 true);
satok4e4569d2010-11-19 18:45:53 +09003149 }
satok3ef8b292010-11-23 06:06:29 +09003150 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003151 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003152 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003153 }
3154 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003155 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003156 }
3157
satokf3db1af2010-11-23 13:34:33 +09003158 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
3159 InputMethodSubtype subtype) {
3160 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
3161 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
3162 } else {
3163 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3164 subtypes.add(subtype);
3165 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
3166 }
3167 }
3168
satok4e4569d2010-11-19 18:45:53 +09003169 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003170 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003171 @Override
satok4e4569d2010-11-19 18:45:53 +09003172 public List getShortcutInputMethodsAndSubtypes() {
3173 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09003174 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09003175 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003176 // If there are no selected shortcut subtypes, the framework will try to find
3177 // the most applicable subtype from all subtypes whose mode is
3178 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003179 Pair<InputMethodInfo, InputMethodSubtype> info =
3180 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003181 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003182 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003183 ret.add(info.first);
3184 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003185 }
satok3da92232011-01-11 22:46:30 +09003186 return ret;
satokf3db1af2010-11-23 13:34:33 +09003187 }
satokf3db1af2010-11-23 13:34:33 +09003188 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3189 ret.add(imi);
3190 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3191 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003192 }
3193 }
satokf3db1af2010-11-23 13:34:33 +09003194 return ret;
satok4e4569d2010-11-19 18:45:53 +09003195 }
3196 }
3197
satok42c5a162011-05-26 16:46:14 +09003198 @Override
satokb66d2872010-11-10 01:04:04 +09003199 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003200 // TODO: Make this work even for non-current users?
3201 if (!calledFromValidUser()) {
3202 return false;
3203 }
satokb66d2872010-11-10 01:04:04 +09003204 synchronized (mMethodMap) {
3205 if (subtype != null && mCurMethodId != null) {
3206 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003207 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003208 if (subtypeId != NOT_A_SUBTYPE_ID) {
3209 setInputMethodLocked(mCurMethodId, subtypeId);
3210 return true;
3211 }
3212 }
3213 return false;
3214 }
3215 }
3216
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003217 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003218 private static class InputMethodFileManager {
3219 private static final String SYSTEM_PATH = "system";
3220 private static final String INPUT_METHOD_PATH = "inputmethod";
3221 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3222 private static final String NODE_SUBTYPES = "subtypes";
3223 private static final String NODE_SUBTYPE = "subtype";
3224 private static final String NODE_IMI = "imi";
3225 private static final String ATTR_ID = "id";
3226 private static final String ATTR_LABEL = "label";
3227 private static final String ATTR_ICON = "icon";
3228 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3229 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3230 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3231 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3232 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3233 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003234 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
satoke7c6998e2011-06-03 17:57:59 +09003235 new HashMap<String, List<InputMethodSubtype>>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003236 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003237 if (methodMap == null) {
3238 throw new NullPointerException("methodMap is null");
3239 }
3240 mMethodMap = methodMap;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003241 final File systemDir = userId == UserHandle.USER_OWNER
3242 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3243 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003244 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3245 if (!inputMethodDir.mkdirs()) {
3246 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3247 }
3248 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3249 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3250 if (!subtypeFile.exists()) {
3251 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003252 writeAdditionalInputMethodSubtypes(
3253 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003254 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003255 readAdditionalInputMethodSubtypes(
3256 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003257 }
3258 }
3259
3260 private void deleteAllInputMethodSubtypes(String imiId) {
3261 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003262 mAdditionalSubtypesMap.remove(imiId);
3263 writeAdditionalInputMethodSubtypes(
3264 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003265 }
3266 }
3267
3268 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003269 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003270 synchronized (mMethodMap) {
3271 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3272 final int N = additionalSubtypes.length;
3273 for (int i = 0; i < N; ++i) {
3274 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003275 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003276 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003277 } else {
3278 Slog.w(TAG, "Duplicated subtype definition found: "
3279 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003280 }
3281 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003282 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3283 writeAdditionalInputMethodSubtypes(
3284 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003285 }
3286 }
3287
3288 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3289 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003290 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003291 }
3292 }
3293
3294 private static void writeAdditionalInputMethodSubtypes(
3295 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3296 HashMap<String, InputMethodInfo> methodMap) {
3297 // Safety net for the case that this function is called before methodMap is set.
3298 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3299 FileOutputStream fos = null;
3300 try {
3301 fos = subtypesFile.startWrite();
3302 final XmlSerializer out = new FastXmlSerializer();
3303 out.setOutput(fos, "utf-8");
3304 out.startDocument(null, true);
3305 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3306 out.startTag(null, NODE_SUBTYPES);
3307 for (String imiId : allSubtypes.keySet()) {
3308 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3309 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3310 continue;
3311 }
3312 out.startTag(null, NODE_IMI);
3313 out.attribute(null, ATTR_ID, imiId);
3314 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3315 final int N = subtypesList.size();
3316 for (int i = 0; i < N; ++i) {
3317 final InputMethodSubtype subtype = subtypesList.get(i);
3318 out.startTag(null, NODE_SUBTYPE);
3319 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3320 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3321 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3322 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3323 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3324 out.attribute(null, ATTR_IS_AUXILIARY,
3325 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3326 out.endTag(null, NODE_SUBTYPE);
3327 }
3328 out.endTag(null, NODE_IMI);
3329 }
3330 out.endTag(null, NODE_SUBTYPES);
3331 out.endDocument();
3332 subtypesFile.finishWrite(fos);
3333 } catch (java.io.IOException e) {
3334 Slog.w(TAG, "Error writing subtypes", e);
3335 if (fos != null) {
3336 subtypesFile.failWrite(fos);
3337 }
3338 }
3339 }
3340
3341 private static void readAdditionalInputMethodSubtypes(
3342 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3343 if (allSubtypes == null || subtypesFile == null) return;
3344 allSubtypes.clear();
3345 FileInputStream fis = null;
3346 try {
3347 fis = subtypesFile.openRead();
3348 final XmlPullParser parser = Xml.newPullParser();
3349 parser.setInput(fis, null);
3350 int type = parser.getEventType();
3351 // Skip parsing until START_TAG
3352 while ((type = parser.next()) != XmlPullParser.START_TAG
3353 && type != XmlPullParser.END_DOCUMENT) {}
3354 String firstNodeName = parser.getName();
3355 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3356 throw new XmlPullParserException("Xml doesn't start with subtypes");
3357 }
3358 final int depth =parser.getDepth();
3359 String currentImiId = null;
3360 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3361 while (((type = parser.next()) != XmlPullParser.END_TAG
3362 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3363 if (type != XmlPullParser.START_TAG)
3364 continue;
3365 final String nodeName = parser.getName();
3366 if (NODE_IMI.equals(nodeName)) {
3367 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3368 if (TextUtils.isEmpty(currentImiId)) {
3369 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3370 continue;
3371 }
3372 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
3373 allSubtypes.put(currentImiId, tempSubtypesArray);
3374 } else if (NODE_SUBTYPE.equals(nodeName)) {
3375 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3376 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3377 continue;
3378 }
3379 final int icon = Integer.valueOf(
3380 parser.getAttributeValue(null, ATTR_ICON));
3381 final int label = Integer.valueOf(
3382 parser.getAttributeValue(null, ATTR_LABEL));
3383 final String imeSubtypeLocale =
3384 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3385 final String imeSubtypeMode =
3386 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3387 final String imeSubtypeExtraValue =
3388 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003389 final boolean isAuxiliary = "1".equals(String.valueOf(
3390 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
satoke7c6998e2011-06-03 17:57:59 +09003391 final InputMethodSubtype subtype =
3392 new InputMethodSubtype(label, icon, imeSubtypeLocale,
3393 imeSubtypeMode, imeSubtypeExtraValue, isAuxiliary);
3394 tempSubtypesArray.add(subtype);
3395 }
3396 }
3397 } catch (XmlPullParserException e) {
3398 Slog.w(TAG, "Error reading subtypes: " + e);
3399 return;
3400 } catch (java.io.IOException e) {
3401 Slog.w(TAG, "Error reading subtypes: " + e);
3402 return;
3403 } catch (NumberFormatException e) {
3404 Slog.w(TAG, "Error reading subtypes: " + e);
3405 return;
3406 } finally {
3407 if (fis != null) {
3408 try {
3409 fis.close();
3410 } catch (java.io.IOException e1) {
3411 Slog.w(TAG, "Failed to close.");
3412 }
3413 }
3414 }
3415 }
3416 }
3417
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003418 @Override
3419 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3420 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3421 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003423 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3424 + Binder.getCallingPid()
3425 + ", uid=" + Binder.getCallingUid());
3426 return;
3427 }
3428
3429 IInputMethod method;
3430 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003431
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003432 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003434 synchronized (mMethodMap) {
3435 p.println("Current Input Method Manager state:");
3436 int N = mMethodList.size();
3437 p.println(" Input Methods:");
3438 for (int i=0; i<N; i++) {
3439 InputMethodInfo info = mMethodList.get(i);
3440 p.println(" InputMethod #" + i + ":");
3441 info.dump(p, " ");
3442 }
3443 p.println(" Clients:");
3444 for (ClientState ci : mClients.values()) {
3445 p.println(" Client " + ci + ":");
3446 p.println(" client=" + ci.client);
3447 p.println(" inputContext=" + ci.inputContext);
3448 p.println(" sessionRequested=" + ci.sessionRequested);
3449 p.println(" curSession=" + ci.curSession);
3450 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003451 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003452 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003453 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3454 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003455 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3456 + " mBoundToMethod=" + mBoundToMethod);
3457 p.println(" mCurToken=" + mCurToken);
3458 p.println(" mCurIntent=" + mCurIntent);
3459 method = mCurMethod;
3460 p.println(" mCurMethod=" + mCurMethod);
3461 p.println(" mEnabledSession=" + mEnabledSession);
3462 p.println(" mShowRequested=" + mShowRequested
3463 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3464 + " mShowForced=" + mShowForced
3465 + " mInputShown=" + mInputShown);
Dianne Hackbornd4e6d462014-05-16 16:32:37 -07003466 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003467 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003468
Jeff Brownb88102f2010-09-08 11:49:43 -07003469 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003470 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003471 pw.flush();
3472 try {
3473 client.client.asBinder().dump(fd, args);
3474 } catch (RemoteException e) {
3475 p.println("Input method client dead: " + e);
3476 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003477 } else {
3478 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003479 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003480
Jeff Brownb88102f2010-09-08 11:49:43 -07003481 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003482 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003483 pw.flush();
3484 try {
3485 method.asBinder().dump(fd, args);
3486 } catch (RemoteException e) {
3487 p.println("Input method service dead: " + e);
3488 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003489 } else {
3490 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003491 }
3492 }
3493}