blob: d4f141d9b6ace2643cc6563dfa656e3d6022e2d5 [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 Yukawa3d1e8122014-06-06 19:12:47 +0900151 static final int MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER = 3040;
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
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900175 final InputBindResult mNoBinding = new InputBindResult(null, null, null, -1, -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
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900382 int mCurUserActionNotificationSequenceNumber = 0;
383
Joe Onorato857fd9b2011-01-27 15:08:35 -0800384 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
385 int mImeWindowVis;
386
Ken Wakasa05dbb652011-08-22 15:22:43 +0900387 private AlertDialog.Builder mDialogBuilder;
388 private AlertDialog mSwitchingDialog;
satok01038492012-04-09 21:08:27 +0900389 private View mSwitchingDialogTitleView;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900390 private InputMethodInfo[] mIms;
391 private int[] mSubtypeIds;
satok5b927c432012-05-01 20:09:34 +0900392 private Locale mLastSystemLocale;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900393 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
394 private final IPackageManager mIPackageManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 class SettingsObserver extends ContentObserver {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800397 String mLastEnabled = "";
398
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399 SettingsObserver(Handler handler) {
400 super(handler);
401 ContentResolver resolver = mContext.getContentResolver();
402 resolver.registerContentObserver(Settings.Secure.getUriFor(
403 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
satokab751aa2010-09-14 19:17:36 +0900404 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokb6109bb2011-02-03 22:24:54 +0900405 Settings.Secure.ENABLED_INPUT_METHODS), false, this);
406 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokab751aa2010-09-14 19:17:36 +0900407 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800409
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 @Override public void onChange(boolean selfChange) {
411 synchronized (mMethodMap) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800412 boolean enabledChanged = false;
413 String newEnabled = mSettings.getEnabledInputMethodsStr();
414 if (!mLastEnabled.equals(newEnabled)) {
415 mLastEnabled = newEnabled;
416 enabledChanged = true;
417 }
418 updateFromSettingsLocked(enabledChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 }
420 }
421 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800422
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900423 class ImmsBroadcastReceiver extends android.content.BroadcastReceiver {
424 private void updateActive() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 // Inform the current client of the change in active status
Dianne Hackborna6e41342012-05-22 16:30:34 -0700426 if (mCurClient != null && mCurClient.client != null) {
427 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
428 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 }
430 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900431
432 @Override
433 public void onReceive(Context context, Intent intent) {
434 final String action = intent.getAction();
435 if (Intent.ACTION_SCREEN_ON.equals(action)) {
436 mScreenOn = true;
437 refreshImeWindowVisibilityLocked();
438 updateActive();
439 return;
440 } else if (Intent.ACTION_SCREEN_OFF.equals(action)) {
441 mScreenOn = false;
442 setImeWindowVisibilityStatusHiddenLocked();
443 updateActive();
444 return;
445 } else if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
446 hideInputMethodMenu();
447 // No need to updateActive
448 return;
Amith Yamasani734983f2014-03-04 16:48:05 -0800449 } else if (Intent.ACTION_USER_ADDED.equals(action)
450 || Intent.ACTION_USER_REMOVED.equals(action)) {
Kenny Guy2a764942014-04-02 13:29:20 +0100451 updateCurrentProfileIds();
Amith Yamasani734983f2014-03-04 16:48:05 -0800452 return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900453 } else {
454 Slog.w(TAG, "Unexpected intent " + intent);
455 }
456 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800458
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800459 class MyPackageMonitor extends PackageMonitor {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900460 private boolean isChangingPackagesOfCurrentUser() {
461 final int userId = getChangingUserId();
462 final boolean retval = userId == mSettings.getCurrentUserId();
463 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900464 if (!retval) {
465 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
466 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900467 }
468 return retval;
469 }
470
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800472 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900473 if (!isChangingPackagesOfCurrentUser()) {
474 return false;
475 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900477 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 final int N = mMethodList.size();
479 if (curInputMethodId != null) {
480 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800481 InputMethodInfo imi = mMethodList.get(i);
482 if (imi.getId().equals(curInputMethodId)) {
483 for (String pkg : packages) {
484 if (imi.getPackageName().equals(pkg)) {
485 if (!doit) {
486 return true;
487 }
satok723a27e2010-11-11 14:58:11 +0900488 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800489 chooseNewDefaultIMELocked();
490 return true;
491 }
492 }
493 }
494 }
495 }
496 }
497 return false;
498 }
499
500 @Override
501 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900502 if (!isChangingPackagesOfCurrentUser()) {
503 return;
504 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800505 synchronized (mMethodMap) {
506 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900507 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800508 final int N = mMethodList.size();
509 if (curInputMethodId != null) {
510 for (int i=0; i<N; i++) {
511 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900512 final String imiId = imi.getId();
513 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800514 curIm = imi;
515 }
satoke7c6998e2011-06-03 17:57:59 +0900516
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800517 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900518 if (isPackageModified(imi.getPackageName())) {
519 mFileManager.deleteAllInputMethodSubtypes(imiId);
520 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800521 if (change == PACKAGE_TEMPORARY_CHANGE
522 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800523 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800524 + imi.getComponent());
525 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 }
527 }
528 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800529
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900530 buildInputMethodListLocked(
531 mMethodList, mMethodMap, false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800532
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800534
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800535 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800536 int change = isPackageDisappearing(curIm.getPackageName());
537 if (change == PACKAGE_TEMPORARY_CHANGE
538 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800539 ServiceInfo si = null;
540 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900541 si = mIPackageManager.getServiceInfo(
542 curIm.getComponent(), 0, mSettings.getCurrentUserId());
543 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800544 }
545 if (si == null) {
546 // Uh oh, current input method is no longer around!
547 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800548 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
satok15452a42011-10-28 17:58:28 +0900549 setImeWindowVisibilityStatusHiddenLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800550 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800551 changed = true;
552 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800553 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900554 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800555 }
556 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800557 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800558 }
satokab751aa2010-09-14 19:17:36 +0900559
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800560 if (curIm == null) {
561 // We currently don't have a default input method... is
562 // one now available?
563 changed = chooseNewDefaultIMELocked();
564 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800565
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800566 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800567 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 }
569 }
570 }
571 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800572
Jeff Brownc28867a2013-03-26 15:42:39 -0700573 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900574 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -0700575 private final IInputMethod mMethod;
576 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800577
Jeff Brownc28867a2013-03-26 15:42:39 -0700578 MethodCallback(InputMethodManagerService imms, IInputMethod method,
579 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900580 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -0700581 mMethod = method;
582 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800583 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800584
satoke7c6998e2011-06-03 17:57:59 +0900585 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -0700586 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -0700587 long ident = Binder.clearCallingIdentity();
588 try {
589 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
590 } finally {
591 Binder.restoreCallingIdentity(ident);
592 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593 }
594 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800595
satok01038492012-04-09 21:08:27 +0900596 private class HardKeyboardListener
597 implements WindowManagerService.OnHardKeyboardStatusChangeListener {
598 @Override
599 public void onHardKeyboardStatusChange(boolean available, boolean enabled) {
600 mHandler.sendMessage(mHandler.obtainMessage(
601 MSG_HARD_KEYBOARD_SWITCH_CHANGED, available ? 1 : 0, enabled ? 1 : 0));
602 }
603
604 public void handleHardKeyboardStatusChange(boolean available, boolean enabled) {
605 if (DEBUG) {
606 Slog.w(TAG, "HardKeyboardStatusChanged: available = " + available + ", enabled = "
607 + enabled);
608 }
609 synchronized(mMethodMap) {
610 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
611 && mSwitchingDialog.isShowing()) {
612 mSwitchingDialogTitleView.findViewById(
613 com.android.internal.R.id.hard_keyboard_section).setVisibility(
614 available ? View.VISIBLE : View.GONE);
615 }
616 }
617 }
618 }
619
620 public InputMethodManagerService(Context context, WindowManagerService windowManager) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900621 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800623 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624 mHandler = new Handler(this);
625 mIWindowManager = IWindowManager.Stub.asInterface(
626 ServiceManager.getService(Context.WINDOW_SERVICE));
Mita Yuned218c72012-12-06 17:18:25 -0800627 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900628 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800629 public void executeMessage(Message msg) {
630 handleMessage(msg);
631 }
Mita Yuned218c72012-12-06 17:18:25 -0800632 }, true /*asyncHandler*/);
satok01038492012-04-09 21:08:27 +0900633 mWindowManagerService = windowManager;
634 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700635 mHasFeature = context.getPackageManager().hasSystemFeature(
636 PackageManager.FEATURE_INPUT_METHODS);
satok7cfc0ed2011-06-20 21:29:36 +0900637
satok7cfc0ed2011-06-20 21:29:36 +0900638 mImeSwitcherNotification = new Notification();
639 mImeSwitcherNotification.icon = com.android.internal.R.drawable.ic_notification_ime_default;
640 mImeSwitcherNotification.when = 0;
641 mImeSwitcherNotification.flags = Notification.FLAG_ONGOING_EVENT;
642 mImeSwitcherNotification.tickerText = null;
643 mImeSwitcherNotification.defaults = 0; // please be quiet
644 mImeSwitcherNotification.sound = null;
645 mImeSwitcherNotification.vibrate = null;
Daniel Sandler590d5152012-06-14 16:10:13 -0400646
647 // Tag this notification specially so SystemUI knows it's important
John Spurlockfd7f1e02014-03-18 16:41:57 -0400648 mImeSwitcherNotification.extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
649 mImeSwitcherNotification.category = Notification.CATEGORY_SYSTEM;
Daniel Sandler590d5152012-06-14 16:10:13 -0400650
satok7cfc0ed2011-06-20 21:29:36 +0900651 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900652 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900653
654 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900655
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900656 final IntentFilter broadcastFilter = new IntentFilter();
657 broadcastFilter.addAction(Intent.ACTION_SCREEN_ON);
658 broadcastFilter.addAction(Intent.ACTION_SCREEN_OFF);
659 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Amith Yamasani734983f2014-03-04 16:48:05 -0800660 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
661 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900662 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800663
satok7cfc0ed2011-06-20 21:29:36 +0900664 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900665 int userId = 0;
666 try {
667 ActivityManagerNative.getDefault().registerUserSwitchObserver(
668 new IUserSwitchObserver.Stub() {
669 @Override
670 public void onUserSwitching(int newUserId, IRemoteCallback reply) {
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900671 synchronized(mMethodMap) {
672 switchUserLocked(newUserId);
673 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900674 if (reply != null) {
675 try {
676 reply.sendResult(null);
677 } catch (RemoteException e) {
678 }
679 }
680 }
681
682 @Override
683 public void onUserSwitchComplete(int newUserId) throws RemoteException {
684 }
685 });
686 userId = ActivityManagerNative.getDefault().getCurrentUser().id;
687 } catch (RemoteException e) {
688 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
689 }
satok81f8b7c2012-12-04 20:42:56 +0900690 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
satok913a8922010-08-26 21:53:41 +0900691
satokd87c2592010-09-29 11:52:06 +0900692 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900693 mSettings = new InputMethodSettings(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900694 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId);
Kenny Guy2a764942014-04-02 13:29:20 +0100695 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900696 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900697 synchronized (mMethodMap) {
698 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
699 mSettings, context);
700 }
satok0a1bcf42012-05-16 19:26:31 +0900701
702 // Just checking if defaultImiId is empty or not
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900703 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900704 if (DEBUG) {
705 Slog.d(TAG, "Initial default ime = " + defaultImiId);
706 }
satok0a1bcf42012-05-16 19:26:31 +0900707 mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
708
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900709 synchronized (mMethodMap) {
710 buildInputMethodListLocked(mMethodList, mMethodMap,
711 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
712 }
satokd87c2592010-09-29 11:52:06 +0900713 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800714
satok0a1bcf42012-05-16 19:26:31 +0900715 if (!mImeSelectedOnBoot) {
716 Slog.w(TAG, "No IME selected. Choose the most applicable IME.");
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900717 synchronized (mMethodMap) {
718 resetDefaultImeLocked(context);
719 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800720 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800721
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722 mSettingsObserver = new SettingsObserver(mHandler);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900723 synchronized (mMethodMap) {
724 updateFromSettingsLocked(true);
725 }
satok5b927c432012-05-01 20:09:34 +0900726
727 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME
728 // according to the new system locale.
729 final IntentFilter filter = new IntentFilter();
730 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
731 mContext.registerReceiver(
732 new BroadcastReceiver() {
733 @Override
734 public void onReceive(Context context, Intent intent) {
735 synchronized(mMethodMap) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900736 resetStateIfCurrentLocaleChangedLocked();
satok5b927c432012-05-01 20:09:34 +0900737 }
738 }
739 }, filter);
740 }
741
satok5b927c432012-05-01 20:09:34 +0900742 private void resetDefaultImeLocked(Context context) {
743 // Do not reset the default (current) IME when it is a 3rd-party IME
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900744 if (mCurMethodId != null
745 && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +0900746 return;
747 }
748
749 InputMethodInfo defIm = null;
750 for (InputMethodInfo imi : mMethodList) {
751 if (defIm == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900752 if (InputMethodUtils.isValidSystemDefaultIme(
753 mSystemReady, imi, context)) {
satok5b927c432012-05-01 20:09:34 +0900754 defIm = imi;
755 Slog.i(TAG, "Selected default: " + imi.getId());
756 }
757 }
758 }
759 if (defIm == null && mMethodList.size() > 0) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900760 defIm = InputMethodUtils.getMostApplicableDefaultIME(
761 mSettings.getEnabledInputMethodListLocked());
satok5b927c432012-05-01 20:09:34 +0900762 Slog.i(TAG, "No default found, using " + defIm.getId());
763 }
764 if (defIm != null) {
765 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
766 }
767 }
768
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900769 private void resetAllInternalStateLocked(final boolean updateOnlyWhenLocaleChanged,
770 final boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900771 if (!mSystemReady) {
772 // not system ready
773 return;
774 }
775 final Locale newLocale = mRes.getConfiguration().locale;
776 if (!updateOnlyWhenLocaleChanged
777 || (newLocale != null && !newLocale.equals(mLastSystemLocale))) {
778 if (!updateOnlyWhenLocaleChanged) {
779 hideCurrentInputLocked(0, null);
780 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -0700781 unbindCurrentMethodLocked(true, false);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900782 }
783 if (DEBUG) {
784 Slog.i(TAG, "Locale has been changed to " + newLocale);
785 }
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900786 buildInputMethodListLocked(mMethodList, mMethodMap, resetDefaultEnabledIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900787 if (!updateOnlyWhenLocaleChanged) {
788 final String selectedImiId = mSettings.getSelectedInputMethod();
789 if (TextUtils.isEmpty(selectedImiId)) {
790 // This is the first time of the user switch and
791 // set the current ime to the proper one.
792 resetDefaultImeLocked(mContext);
793 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +0900794 } else {
795 // If the locale is changed, needs to reset the default ime
796 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900797 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800798 updateFromSettingsLocked(true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900799 mLastSystemLocale = newLocale;
800 if (!updateOnlyWhenLocaleChanged) {
801 try {
802 startInputInnerLocked();
803 } catch (RuntimeException e) {
804 Slog.w(TAG, "Unexpected exception", e);
805 }
806 }
807 }
808 }
809
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900810 private void resetStateIfCurrentLocaleChangedLocked() {
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900811 resetAllInternalStateLocked(true /* updateOnlyWhenLocaleChanged */,
812 true /* resetDefaultImeLocked */);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900813 }
814
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900815 private void switchUserLocked(int newUserId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900816 mSettings.setCurrentUserId(newUserId);
Kenny Guy2a764942014-04-02 13:29:20 +0100817 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900818 // InputMethodFileManager should be reset when the user is changed
819 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900820 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +0900821 // For secondary users, the list of enabled IMEs may not have been updated since the
822 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
823 // not be empty even if the IME has been uninstalled by the primary user.
824 // Even in such cases, IMMS works fine because it will find the most applicable
825 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900826 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900827 if (DEBUG) {
828 Slog.d(TAG, "Switch user: " + newUserId + " current ime = " + defaultImiId);
829 }
830 resetAllInternalStateLocked(false /* updateOnlyWhenLocaleChanged */,
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900831 initialUserSwitch /* needsToResetDefaultIme */);
832 if (initialUserSwitch) {
833 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mContext.getPackageManager(),
834 mSettings.getEnabledInputMethodListLocked());
835 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900836 }
837
Kenny Guy2a764942014-04-02 13:29:20 +0100838 void updateCurrentProfileIds() {
839 List<UserInfo> profiles =
840 UserManager.get(mContext).getProfiles(mSettings.getCurrentUserId());
841 int[] currentProfileIds = new int[profiles.size()]; // profiles will not be null
842 for (int i = 0; i < currentProfileIds.length; i++) {
843 currentProfileIds[i] = profiles.get(i).id;
Amith Yamasani734983f2014-03-04 16:48:05 -0800844 }
Kenny Guy2a764942014-04-02 13:29:20 +0100845 mSettings.setCurrentProfileIds(currentProfileIds);
Amith Yamasani734983f2014-03-04 16:48:05 -0800846 }
847
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800848 @Override
849 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
850 throws RemoteException {
851 try {
852 return super.onTransact(code, data, reply, flags);
853 } catch (RuntimeException e) {
854 // The input method manager only throws security exceptions, so let's
855 // log all others.
856 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -0700857 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800858 }
859 throw e;
860 }
861 }
862
Svetoslav Ganova0027152013-06-25 14:59:53 -0700863 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700864 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900865 if (DEBUG) {
866 Slog.d(TAG, "--- systemReady");
867 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700868 if (!mSystemReady) {
869 mSystemReady = true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900870 mKeyguardManager =
871 (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700872 mNotificationManager = (NotificationManager)
873 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
874 mStatusBar = statusBar;
875 statusBar.setIconVisibility("ime", false);
876 updateImeWindowStatusLocked();
satokb858c732011-07-22 19:54:34 +0900877 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
878 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +0900879 if (mShowOngoingImeSwitcherForPhones) {
880 mWindowManagerService.setOnHardKeyboardStatusChangeListener(
881 mHardKeyboardListener);
882 }
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900883 buildInputMethodListLocked(mMethodList, mMethodMap,
884 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satok0a1bcf42012-05-16 19:26:31 +0900885 if (!mImeSelectedOnBoot) {
886 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900887 resetStateIfCurrentLocaleChangedLocked();
888 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(
889 mContext.getPackageManager(),
890 mSettings.getEnabledInputMethodListLocked());
satok0a1bcf42012-05-16 19:26:31 +0900891 }
892 mLastSystemLocale = mRes.getConfiguration().locale;
Dianne Hackborncc278702009-09-02 23:07:23 -0700893 try {
894 startInputInnerLocked();
895 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800896 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700897 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700898 }
899 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800900 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800901
satok15452a42011-10-28 17:58:28 +0900902 private void setImeWindowVisibilityStatusHiddenLocked() {
903 mImeWindowVis = 0;
904 updateImeWindowStatusLocked();
905 }
906
satok3afd6c02011-11-18 08:38:19 +0900907 private void refreshImeWindowVisibilityLocked() {
908 final Configuration conf = mRes.getConfiguration();
909 final boolean haveHardKeyboard = conf.keyboard
910 != Configuration.KEYBOARD_NOKEYS;
911 final boolean hardKeyShown = haveHardKeyboard
912 && conf.hardKeyboardHidden
913 != Configuration.HARDKEYBOARDHIDDEN_YES;
John Spurlocke0980502013-10-25 11:59:29 -0400914
915 final boolean isScreenLocked = isKeyguardLocked();
916 final boolean inputActive = !isScreenLocked && (mInputShown || hardKeyShown);
John Spurlock36439b42013-10-23 16:36:47 -0400917 // We assume the softkeyboard is shown when the input is active as long as the
918 // hard keyboard is not shown.
919 final boolean inputVisible = inputActive && !hardKeyShown;
920 mImeWindowVis = (inputActive ? InputMethodService.IME_ACTIVE : 0)
921 | (inputVisible ? InputMethodService.IME_VISIBLE : 0);
satok3afd6c02011-11-18 08:38:19 +0900922 updateImeWindowStatusLocked();
923 }
924
satok15452a42011-10-28 17:58:28 +0900925 private void updateImeWindowStatusLocked() {
satokdbf29502011-08-25 15:28:23 +0900926 setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700927 }
928
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900929 // ---------------------------------------------------------------------------------------
930 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
931 // 1) it comes from the system process
932 // 2) the calling process' user id is identical to the current user id IMMS thinks.
933 private boolean calledFromValidUser() {
934 final int uid = Binder.getCallingUid();
935 final int userId = UserHandle.getUserId(uid);
936 if (DEBUG) {
937 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
938 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
939 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +0900940 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
941 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900942 }
Kenny Guy2a764942014-04-02 13:29:20 +0100943 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900944 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900945 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +0900946
947 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
948 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
949 // must not manage background users' states in any functions.
950 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
951 // by a token.
952 if (mContext.checkCallingOrSelfPermission(
953 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
954 == PackageManager.PERMISSION_GRANTED) {
955 if (DEBUG) {
956 Slog.d(TAG, "--- Access granted because the calling process has "
957 + "the INTERACT_ACROSS_USERS_FULL permission");
958 }
959 return true;
960 }
Satoshi Kataoka0766eb02013-07-31 18:30:13 +0900961 Slog.w(TAG, "--- IPC called from background users. Ignore. \n"
962 + InputMethodUtils.getStackTrace());
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +0900963 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900964 }
965
Yohei Yukawa22c97be2014-06-04 19:43:36 +0900966
967 /**
968 * Returns true iff the caller is identified to be the current input method with the token.
969 * @param token The window token given to the input method when it was started.
970 * @return true if and only if non-null valid token is specified.
971 */
972 private boolean calledWithValidToken(IBinder token) {
973 if (token == null || mCurToken != token) {
974 return false;
975 }
976 return true;
977 }
978
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900979 private boolean bindCurrentInputMethodService(
980 Intent service, ServiceConnection conn, int flags) {
981 if (service == null || conn == null) {
982 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
983 return false;
984 }
Amith Yamasani27b89e62013-01-16 12:30:11 -0800985 return mContext.bindServiceAsUser(service, conn, flags,
986 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900987 }
988
satoke7c6998e2011-06-03 17:57:59 +0900989 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900991 // TODO: Make this work even for non-current users?
992 if (!calledFromValidUser()) {
993 return Collections.emptyList();
994 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800995 synchronized (mMethodMap) {
996 return new ArrayList<InputMethodInfo>(mMethodList);
997 }
998 }
999
satoke7c6998e2011-06-03 17:57:59 +09001000 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001002 // TODO: Make this work even for non-current users?
1003 if (!calledFromValidUser()) {
1004 return Collections.emptyList();
1005 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001006 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001007 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008 }
1009 }
1010
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001011 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001012 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001013 * @return enabled subtypes of the specified imi
1014 */
satoke7c6998e2011-06-03 17:57:59 +09001015 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001016 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001017 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001018 // TODO: Make this work even for non-current users?
1019 if (!calledFromValidUser()) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001020 return Collections.<InputMethodSubtype>emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001021 }
satok67ddf9c2010-11-17 09:45:54 +09001022 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001023 final InputMethodInfo imi;
1024 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001025 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001026 } else {
1027 imi = mMethodMap.get(imiId);
1028 }
1029 if (imi == null) {
1030 return Collections.<InputMethodSubtype>emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001031 }
1032 return mSettings.getEnabledInputMethodSubtypeListLocked(
1033 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001034 }
1035 }
1036
satoke7c6998e2011-06-03 17:57:59 +09001037 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001038 public void addClient(IInputMethodClient client,
1039 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001040 if (!calledFromValidUser()) {
1041 return;
1042 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001043 synchronized (mMethodMap) {
1044 mClients.put(client.asBinder(), new ClientState(client,
1045 inputContext, uid, pid));
1046 }
1047 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001048
satoke7c6998e2011-06-03 17:57:59 +09001049 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001050 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001051 if (!calledFromValidUser()) {
1052 return;
1053 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001054 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001055 ClientState cs = mClients.remove(client.asBinder());
1056 if (cs != null) {
1057 clearClientSessionLocked(cs);
1058 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001059 }
1060 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062 void executeOrSendMessage(IInterface target, Message msg) {
1063 if (target.asBinder() instanceof Binder) {
1064 mCaller.sendMessage(msg);
1065 } else {
1066 handleMessage(msg);
1067 msg.recycle();
1068 }
1069 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001070
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001071 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001072 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001073 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001074 + mCurClient.client.asBinder());
1075 if (mBoundToMethod) {
1076 mBoundToMethod = false;
1077 if (mCurMethod != null) {
1078 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1079 MSG_UNBIND_INPUT, mCurMethod));
1080 }
1081 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001082
1083 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1084 MSG_SET_ACTIVE, 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001085 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1086 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1087 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001088 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001089
The Android Open Source Project10592532009-03-18 17:39:46 -07001090 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 }
1092 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094 private int getImeShowFlags() {
1095 int flags = 0;
1096 if (mShowForced) {
1097 flags |= InputMethod.SHOW_FORCED
1098 | InputMethod.SHOW_EXPLICIT;
1099 } else if (mShowExplicitlyRequested) {
1100 flags |= InputMethod.SHOW_EXPLICIT;
1101 }
1102 return flags;
1103 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 private int getAppShowFlags() {
1106 int flags = 0;
1107 if (mShowForced) {
1108 flags |= InputMethodManager.SHOW_FORCED;
1109 } else if (!mShowExplicitlyRequested) {
1110 flags |= InputMethodManager.SHOW_IMPLICIT;
1111 }
1112 return flags;
1113 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001114
Dianne Hackborn7663d802012-02-24 13:08:49 -08001115 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001116 if (!mBoundToMethod) {
1117 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1118 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1119 mBoundToMethod = true;
1120 }
1121 final SessionState session = mCurClient.curSession;
1122 if (initial) {
1123 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1124 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
1125 } else {
1126 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1127 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
1128 }
1129 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001130 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001131 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001132 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001133 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001134 (session.channel != null ? session.channel.dup() : null),
1135 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001136 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001138 InputBindResult startInputLocked(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001139 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001140 // If no method is currently selected, do nothing.
1141 if (mCurMethodId == null) {
1142 return mNoBinding;
1143 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001145 ClientState cs = mClients.get(client.asBinder());
1146 if (cs == null) {
1147 throw new IllegalArgumentException("unknown client "
1148 + client.asBinder());
1149 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 try {
1152 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1153 // Check with the window manager to make sure this client actually
1154 // has a window with focus. If not, reject. This is thread safe
1155 // because if the focus changes some time before or after, the
1156 // next client receiving focus that has any interest in input will
1157 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001158 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1160 return null;
1161 }
1162 } catch (RemoteException e) {
1163 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001164
Dianne Hackborn7663d802012-02-24 13:08:49 -08001165 return startInputUncheckedLocked(cs, inputContext, attribute, controlFlags);
1166 }
1167
1168 InputBindResult startInputUncheckedLocked(ClientState cs,
1169 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
1170 // If no method is currently selected, do nothing.
1171 if (mCurMethodId == null) {
1172 return mNoBinding;
1173 }
1174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001175 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001176 // Was the keyguard locked when switching over to the new client?
1177 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001178 // If the client is changing, we need to switch over to the new
1179 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001180 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001181 if (DEBUG) Slog.v(TAG, "switching to client: client = "
John Spurlocke0980502013-10-25 11:59:29 -04001182 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001183
1184 // If the screen is on, inform the new client it is active
1185 if (mScreenOn) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001186 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
1187 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001188 }
1189 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 // Bump up the sequence for this client and attach it.
1192 mCurSeq++;
1193 if (mCurSeq <= 0) mCurSeq = 1;
1194 mCurClient = cs;
1195 mCurInputContext = inputContext;
1196 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 // Check if the input method is changing.
1199 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1200 if (cs.curSession != null) {
1201 // Fast case: if we are already connected to the input method,
1202 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001203 return attachNewInputLocked(
1204 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205 }
1206 if (mHaveConnection) {
1207 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001208 // Return to client, and we will get back with it when
1209 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001210 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001211 return new InputBindResult(null, null, mCurId, mCurSeq,
1212 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 } else if (SystemClock.uptimeMillis()
1214 < (mLastBindTime+TIME_TO_RECONNECT)) {
1215 // In this case we have connected to the service, but
1216 // don't yet have its interface. If it hasn't been too
1217 // long since we did the connection, we'll return to
1218 // the client and wait to get the service interface so
1219 // we can report back. If it has been too long, we want
1220 // to fall through so we can try a disconnect/reconnect
1221 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001222 return new InputBindResult(null, null, mCurId, mCurSeq,
1223 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001225 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1226 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 }
1228 }
1229 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001230
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001231 return startInputInnerLocked();
1232 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001233
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001234 InputBindResult startInputInnerLocked() {
1235 if (mCurMethodId == null) {
1236 return mNoBinding;
1237 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001238
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001239 if (!mSystemReady) {
1240 // If the system is not yet ready, we shouldn't be running third
1241 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001242 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1243 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001244 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001245
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1247 if (info == null) {
1248 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1249 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001250
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001251 unbindCurrentMethodLocked(false, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001252
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001253 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1254 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001255 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1256 com.android.internal.R.string.input_method_binding_label);
1257 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1258 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001259 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001260 | Context.BIND_NOT_VISIBLE | Context.BIND_NOT_FOREGROUND
1261 | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 mLastBindTime = SystemClock.uptimeMillis();
1263 mHaveConnection = true;
1264 mCurId = info.getId();
1265 mCurToken = new Binder();
1266 try {
Craig Mautnerca0ac712013-03-14 09:43:02 -07001267 if (true || DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001268 mIWindowManager.addWindowToken(mCurToken,
1269 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1270 } catch (RemoteException e) {
1271 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001272 return new InputBindResult(null, null, mCurId, mCurSeq,
1273 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001274 } else {
1275 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001276 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 + mCurIntent);
1278 }
1279 return null;
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 InputBindResult startInput(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001284 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001285 if (!calledFromValidUser()) {
1286 return null;
1287 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288 synchronized (mMethodMap) {
1289 final long ident = Binder.clearCallingIdentity();
1290 try {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001291 return startInputLocked(client, inputContext, attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 } finally {
1293 Binder.restoreCallingIdentity(ident);
1294 }
1295 }
1296 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001297
satoke7c6998e2011-06-03 17:57:59 +09001298 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001299 public void finishInput(IInputMethodClient client) {
1300 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001301
satoke7c6998e2011-06-03 17:57:59 +09001302 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001303 public void onServiceConnected(ComponentName name, IBinder service) {
1304 synchronized (mMethodMap) {
1305 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1306 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001307 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001308 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001309 unbindCurrentMethodLocked(false, false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001310 return;
1311 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001312 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001313 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1314 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001315 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001316 clearClientSessionLocked(mCurClient);
1317 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001318 }
1319 }
1320 }
1321 }
1322
Jeff Brownc28867a2013-03-26 15:42:39 -07001323 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1324 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001325 synchronized (mMethodMap) {
1326 if (mCurMethod != null && method != null
1327 && mCurMethod.asBinder() == method.asBinder()) {
1328 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001329 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001330 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001331 method, session, channel);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001332 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001333 if (res.method != null) {
1334 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
1335 MSG_BIND_METHOD, mCurClient.client, res));
1336 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001337 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001338 }
1339 }
1340 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001341
1342 // Session abandoned. Close its associated input channel.
1343 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001344 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001345
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001346 void unbindCurrentMethodLocked(boolean reportToClient, boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001347 if (mVisibleBound) {
1348 mContext.unbindService(mVisibleConnection);
1349 mVisibleBound = false;
1350 }
1351
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001352 if (mHaveConnection) {
1353 mContext.unbindService(this);
1354 mHaveConnection = false;
1355 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001356
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001357 if (mCurToken != null) {
1358 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001359 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001360 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001361 // The current IME is shown. Hence an IME switch (transition) is happening.
1362 mWindowManagerService.saveLastInputMethodWindowForTransition();
1363 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001364 mIWindowManager.removeWindowToken(mCurToken);
1365 } catch (RemoteException e) {
1366 }
1367 mCurToken = null;
1368 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001369
The Android Open Source Project10592532009-03-18 17:39:46 -07001370 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001371 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001372
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001373 if (reportToClient && mCurClient != null) {
1374 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1375 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1376 }
1377 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001378
1379 void requestClientSessionLocked(ClientState cs) {
1380 if (!cs.sessionRequested) {
1381 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1382 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1383 cs.sessionRequested = true;
1384 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1385 MSG_CREATE_SESSION, mCurMethod, channels[1],
1386 new MethodCallback(this, mCurMethod, channels[0])));
1387 }
1388 }
1389
1390 void clearClientSessionLocked(ClientState cs) {
1391 finishSessionLocked(cs.curSession);
1392 cs.curSession = null;
1393 cs.sessionRequested = false;
1394 }
1395
1396 private void finishSessionLocked(SessionState sessionState) {
1397 if (sessionState != null) {
1398 if (sessionState.session != null) {
1399 try {
1400 sessionState.session.finishSession();
1401 } catch (RemoteException e) {
1402 Slog.w(TAG, "Session failed to close due to remote exception", e);
1403 setImeWindowVisibilityStatusHiddenLocked();
1404 }
1405 sessionState.session = null;
1406 }
1407 if (sessionState.channel != null) {
1408 sessionState.channel.dispose();
1409 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001410 }
1411 }
1412 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001413
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001414 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001415 if (mCurMethod != null) {
1416 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001417 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001418 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001419
Jeff Brownc28867a2013-03-26 15:42:39 -07001420 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001421 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001422 mCurMethod = null;
1423 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001424 if (mStatusBar != null) {
1425 mStatusBar.setIconVisibility("ime", false);
1426 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001427 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001428
satoke7c6998e2011-06-03 17:57:59 +09001429 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001430 public void onServiceDisconnected(ComponentName name) {
1431 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001432 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001433 + " mCurIntent=" + mCurIntent);
1434 if (mCurMethod != null && mCurIntent != null
1435 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001436 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001437 // We consider this to be a new bind attempt, since the system
1438 // should now try to restart the service for us.
1439 mLastBindTime = SystemClock.uptimeMillis();
1440 mShowRequested = mInputShown;
1441 mInputShown = false;
1442 if (mCurClient != null) {
1443 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1444 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1445 }
1446 }
1447 }
1448 }
1449
satokf9f01002011-05-19 21:31:50 +09001450 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001451 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
1452 long ident = Binder.clearCallingIdentity();
1453 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001454 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09001455 if (!calledWithValidToken(token)) {
1456 final int uid = Binder.getCallingUid();
1457 Slog.e(TAG, "Ignoring updateStatusIcon due to an invalid token. uid:" + uid
1458 + " token:" + token);
1459 return;
1460 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001461 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001462 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001463 if (mStatusBar != null) {
1464 mStatusBar.setIconVisibility("ime", false);
1465 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001466 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001467 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001468 CharSequence contentDescription = null;
1469 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001470 // Use PackageManager to load label
1471 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001472 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001473 mIPackageManager.getApplicationInfo(packageName, 0,
1474 mSettings.getCurrentUserId()));
1475 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001476 /* ignore */
1477 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001478 if (mStatusBar != null) {
1479 mStatusBar.setIcon("ime", packageName, iconId, 0,
1480 contentDescription != null
1481 ? contentDescription.toString() : null);
1482 mStatusBar.setIconVisibility("ime", true);
1483 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001484 }
1485 }
1486 } finally {
1487 Binder.restoreCallingIdentity(ident);
1488 }
1489 }
1490
satok7cfc0ed2011-06-20 21:29:36 +09001491 private boolean needsToShowImeSwitchOngoingNotification() {
1492 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04001493 if (mSwitchingDialog != null) return false;
satok2c93efc2012-04-02 19:33:47 +09001494 if (isScreenLocked()) return false;
satok7cfc0ed2011-06-20 21:29:36 +09001495 synchronized (mMethodMap) {
1496 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1497 final int N = imis.size();
satokb6359412011-06-28 17:47:41 +09001498 if (N > 2) return true;
1499 if (N < 1) return false;
1500 int nonAuxCount = 0;
1501 int auxCount = 0;
1502 InputMethodSubtype nonAuxSubtype = null;
1503 InputMethodSubtype auxSubtype = null;
satok7cfc0ed2011-06-20 21:29:36 +09001504 for(int i = 0; i < N; ++i) {
1505 final InputMethodInfo imi = imis.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001506 final List<InputMethodSubtype> subtypes =
1507 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satok7cfc0ed2011-06-20 21:29:36 +09001508 final int subtypeCount = subtypes.size();
1509 if (subtypeCount == 0) {
satokb6359412011-06-28 17:47:41 +09001510 ++nonAuxCount;
satok7cfc0ed2011-06-20 21:29:36 +09001511 } else {
1512 for (int j = 0; j < subtypeCount; ++j) {
satokb6359412011-06-28 17:47:41 +09001513 final InputMethodSubtype subtype = subtypes.get(j);
1514 if (!subtype.isAuxiliary()) {
1515 ++nonAuxCount;
1516 nonAuxSubtype = subtype;
1517 } else {
1518 ++auxCount;
1519 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001520 }
1521 }
1522 }
satok7cfc0ed2011-06-20 21:29:36 +09001523 }
satokb6359412011-06-28 17:47:41 +09001524 if (nonAuxCount > 1 || auxCount > 1) {
1525 return true;
1526 } else if (nonAuxCount == 1 && auxCount == 1) {
1527 if (nonAuxSubtype != null && auxSubtype != null
satok9747f892011-09-12 15:56:40 +09001528 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1529 || auxSubtype.overridesImplicitlyEnabledSubtype()
1530 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
satokb6359412011-06-28 17:47:41 +09001531 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1532 return false;
1533 }
1534 return true;
1535 }
1536 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001537 }
satok7cfc0ed2011-06-20 21:29:36 +09001538 }
1539
John Spurlocke0980502013-10-25 11:59:29 -04001540 private boolean isKeyguardLocked() {
1541 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1542 }
1543
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001544 // Caution! This method is called in this class. Handle multi-user carefully
satokdbf29502011-08-25 15:28:23 +09001545 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001546 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001547 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001548 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001549 try {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001550 if (!calledWithValidToken(token)) {
1551 final int uid = Binder.getCallingUid();
1552 Slog.e(TAG, "Ignoring setImeWindowStatus due to an invalid token. uid:" + uid
1553 + " token:" + token);
satok06487a52010-10-29 11:37:18 +09001554 return;
1555 }
satok06487a52010-10-29 11:37:18 +09001556 synchronized (mMethodMap) {
John Spurlocke0980502013-10-25 11:59:29 -04001557 // apply policy for binder calls
1558 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
1559 vis = 0;
1560 }
Joe Onorato857fd9b2011-01-27 15:08:35 -08001561 mImeWindowVis = vis;
1562 mBackDisposition = backDisposition;
jungheang.lee217fd292013-02-26 16:53:22 +09001563 final boolean iconVisibility = ((vis & (InputMethodService.IME_ACTIVE)) != 0)
1564 && (mWindowManagerService.isHardKeyboardAvailable()
1565 || (vis & (InputMethodService.IME_VISIBLE)) != 0);
Jason Monkb605fec2014-05-02 17:04:10 -04001566 final boolean needsToShowImeSwitcher = iconVisibility
1567 && needsToShowImeSwitchOngoingNotification();
1568 if (mStatusBar != null) {
1569 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
1570 needsToShowImeSwitcher);
1571 }
satok5bc8e732011-07-22 21:07:23 +09001572 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Jason Monkb605fec2014-05-02 17:04:10 -04001573 if (imi != null && needsToShowImeSwitcher) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001574 // Used to load label
satok7cfc0ed2011-06-20 21:29:36 +09001575 final CharSequence title = mRes.getText(
1576 com.android.internal.R.string.select_input_method);
Satoshi Kataokab2827262013-07-04 19:43:14 +09001577 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
1578 mContext, imi, mCurrentSubtype);
satok5bc8e732011-07-22 21:07:23 +09001579
satok7cfc0ed2011-06-20 21:29:36 +09001580 mImeSwitcherNotification.setLatestEventInfo(
satok5bc8e732011-07-22 21:07:23 +09001581 mContext, title, summary, mImeSwitchPendingIntent);
Jason Monke463c742014-05-02 12:31:45 -04001582 if ((mNotificationManager != null)
1583 && !mWindowManagerService.hasNavigationBar()) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001584 if (DEBUG) {
Satoshi Kataokab2827262013-07-04 19:43:14 +09001585 Slog.d(TAG, "--- show notification: label = " + summary);
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001586 }
1587 mNotificationManager.notifyAsUser(null,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001588 com.android.internal.R.string.select_input_method,
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001589 mImeSwitcherNotification, UserHandle.ALL);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001590 mNotificationShown = true;
1591 }
satok7cfc0ed2011-06-20 21:29:36 +09001592 } else {
Dianne Hackborn661cd522011-08-22 00:26:20 -07001593 if (mNotificationShown && mNotificationManager != null) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001594 if (DEBUG) {
1595 Slog.d(TAG, "--- hide notification");
1596 }
1597 mNotificationManager.cancelAsUser(null,
1598 com.android.internal.R.string.select_input_method, UserHandle.ALL);
satok7cfc0ed2011-06-20 21:29:36 +09001599 mNotificationShown = false;
1600 }
1601 }
satok06487a52010-10-29 11:37:18 +09001602 }
1603 } finally {
1604 Binder.restoreCallingIdentity(ident);
1605 }
1606 }
1607
satoke7c6998e2011-06-03 17:57:59 +09001608 @Override
satokf9f01002011-05-19 21:31:50 +09001609 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001610 if (!calledFromValidUser()) {
1611 return;
1612 }
satokf9f01002011-05-19 21:31:50 +09001613 synchronized (mMethodMap) {
1614 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1615 for (int i = 0; i < spans.length; ++i) {
1616 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001617 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001618 mSecureSuggestionSpans.put(ss, currentImi);
1619 }
1620 }
1621 }
1622 }
1623
satoke7c6998e2011-06-03 17:57:59 +09001624 @Override
satokf9f01002011-05-19 21:31:50 +09001625 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001626 if (!calledFromValidUser()) {
1627 return false;
1628 }
satokf9f01002011-05-19 21:31:50 +09001629 synchronized (mMethodMap) {
1630 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1631 // TODO: Do not send the intent if the process of the targetImi is already dead.
1632 if (targetImi != null) {
1633 final String[] suggestions = span.getSuggestions();
1634 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001635 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001636 final Intent intent = new Intent();
1637 // Ensures that only a class in the original IME package will receive the
1638 // notification.
satok42c5a162011-05-26 16:46:14 +09001639 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001640 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1641 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1642 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1643 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001644 final long ident = Binder.clearCallingIdentity();
1645 try {
1646 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1647 } finally {
1648 Binder.restoreCallingIdentity(ident);
1649 }
satokf9f01002011-05-19 21:31:50 +09001650 return true;
1651 }
1652 }
1653 return false;
1654 }
1655
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001656 void updateFromSettingsLocked(boolean enabledMayChange) {
1657 if (enabledMayChange) {
1658 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1659 for (int i=0; i<enabled.size(); i++) {
1660 // We allow the user to select "disabled until used" apps, so if they
1661 // are enabling one of those here we now need to make it enabled.
1662 InputMethodInfo imm = enabled.get(i);
1663 try {
1664 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1665 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1666 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09001667 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001668 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001669 if (DEBUG) {
1670 Slog.d(TAG, "Update state(" + imm.getId()
1671 + "): DISABLED_UNTIL_USED -> DEFAULT");
1672 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001673 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1674 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001675 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
1676 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001677 }
1678 } catch (RemoteException e) {
1679 }
1680 }
1681 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001682 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1683 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1684 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1685 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001686 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001687 // There is no input method selected, try to choose new applicable input method.
1688 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001689 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001690 }
1691 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001692 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001693 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001694 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001695 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001696 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001697 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001698 }
satokf3db1af2010-11-23 13:34:33 +09001699 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001700 } else {
1701 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001702 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001703 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001704 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09001705 // Here is not the perfect place to reset the switching controller. Ideally
1706 // mSwitchingController and mSettings should be able to share the same state.
1707 // TODO: Make sure that mSwitchingController and mSettings are sharing the
1708 // the same enabled IMEs list.
1709 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001710 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001711
satokab751aa2010-09-14 19:17:36 +09001712 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001713 InputMethodInfo info = mMethodMap.get(id);
1714 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001715 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001716 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001717
satokd81e9502012-05-21 12:58:45 +09001718 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001719 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09001720 final int subtypeCount = info.getSubtypeCount();
1721 if (subtypeCount <= 0) {
1722 return;
satokcd7cd292010-11-20 15:46:23 +09001723 }
satokd81e9502012-05-21 12:58:45 +09001724 final InputMethodSubtype oldSubtype = mCurrentSubtype;
1725 final InputMethodSubtype newSubtype;
1726 if (subtypeId >= 0 && subtypeId < subtypeCount) {
1727 newSubtype = info.getSubtypeAt(subtypeId);
1728 } else {
1729 // If subtype is null, try to find the most applicable one from
1730 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001731 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09001732 }
1733 if (newSubtype == null || oldSubtype == null) {
1734 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
1735 + ", new subtype = " + newSubtype);
1736 return;
1737 }
1738 if (newSubtype != oldSubtype) {
1739 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1740 if (mCurMethod != null) {
1741 try {
1742 refreshImeWindowVisibilityLocked();
1743 mCurMethod.changeInputMethodSubtype(newSubtype);
1744 } catch (RemoteException e) {
1745 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
satokab751aa2010-09-14 19:17:36 +09001746 }
1747 }
1748 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001749 return;
1750 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001751
satokd81e9502012-05-21 12:58:45 +09001752 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001753 final long ident = Binder.clearCallingIdentity();
1754 try {
satokab751aa2010-09-14 19:17:36 +09001755 // Set a subtype to this input method.
1756 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001757 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1758 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1759 // because mCurMethodId is stored as a history in
1760 // setSelectedInputMethodAndSubtypeLocked().
1761 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001762
1763 if (ActivityManagerNative.isSystemReady()) {
1764 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001765 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001766 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07001767 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001768 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001769 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001770 } finally {
1771 Binder.restoreCallingIdentity(ident);
1772 }
1773 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001774
satok42c5a162011-05-26 16:46:14 +09001775 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001776 public boolean showSoftInput(IInputMethodClient client, int flags,
1777 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001778 if (!calledFromValidUser()) {
1779 return false;
1780 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001781 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001782 long ident = Binder.clearCallingIdentity();
1783 try {
1784 synchronized (mMethodMap) {
1785 if (mCurClient == null || client == null
1786 || mCurClient.client.asBinder() != client.asBinder()) {
1787 try {
1788 // We need to check if this is the current client with
1789 // focus in the window manager, to allow this call to
1790 // be made before input is started in it.
1791 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001792 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001793 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001794 }
1795 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001796 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001797 }
1798 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001799
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001800 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001801 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001802 }
1803 } finally {
1804 Binder.restoreCallingIdentity(ident);
1805 }
1806 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001807
The Android Open Source Project4df24232009-03-05 14:34:35 -08001808 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001809 mShowRequested = true;
1810 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1811 mShowExplicitlyRequested = true;
1812 }
1813 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1814 mShowExplicitlyRequested = true;
1815 mShowForced = true;
1816 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001817
Dianne Hackborncc278702009-09-02 23:07:23 -07001818 if (!mSystemReady) {
1819 return false;
1820 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001821
The Android Open Source Project4df24232009-03-05 14:34:35 -08001822 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001823 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001824 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001825 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1826 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1827 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001828 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001829 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001830 bindCurrentInputMethodService(
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001831 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE
1832 | Context.BIND_TREAT_LIKE_ACTIVITY);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001833 mVisibleBound = true;
1834 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001835 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001836 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09001837 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001838 // The client has asked to have the input method shown, but
1839 // we have been sitting here too long with a connection to the
1840 // service and no interface received, so let's disconnect/connect
1841 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001842 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001843 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09001844 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001845 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001846 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001847 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001848 } else {
1849 if (DEBUG) {
1850 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
1851 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
1852 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001853 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001854
The Android Open Source Project4df24232009-03-05 14:34:35 -08001855 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001856 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001857
satok42c5a162011-05-26 16:46:14 +09001858 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001859 public boolean hideSoftInput(IInputMethodClient client, int flags,
1860 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001861 if (!calledFromValidUser()) {
1862 return false;
1863 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001864 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001865 long ident = Binder.clearCallingIdentity();
1866 try {
1867 synchronized (mMethodMap) {
1868 if (mCurClient == null || client == null
1869 || mCurClient.client.asBinder() != client.asBinder()) {
1870 try {
1871 // We need to check if this is the current client with
1872 // focus in the window manager, to allow this call to
1873 // be made before input is started in it.
1874 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001875 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1876 + uid + ": " + client);
satok15452a42011-10-28 17:58:28 +09001877 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001878 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001879 }
1880 } catch (RemoteException e) {
satok15452a42011-10-28 17:58:28 +09001881 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001882 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001883 }
1884 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001885
Joe Onorato8a9b2202010-02-26 18:56:32 -08001886 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001887 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001888 }
1889 } finally {
1890 Binder.restoreCallingIdentity(ident);
1891 }
1892 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001893
The Android Open Source Project4df24232009-03-05 14:34:35 -08001894 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001895 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1896 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001897 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 -08001898 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001899 }
1900 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001901 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 -08001902 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001903 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001904 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001905 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001906 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1907 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1908 res = true;
1909 } else {
1910 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001911 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001912 if (mHaveConnection && mVisibleBound) {
1913 mContext.unbindService(mVisibleConnection);
1914 mVisibleBound = false;
1915 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001916 mInputShown = false;
1917 mShowRequested = false;
1918 mShowExplicitlyRequested = false;
1919 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001920 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001921 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001922
satok42c5a162011-05-26 16:46:14 +09001923 @Override
Dianne Hackborn7663d802012-02-24 13:08:49 -08001924 public InputBindResult windowGainedFocus(IInputMethodClient client, IBinder windowToken,
1925 int controlFlags, int softInputMode, int windowFlags,
1926 EditorInfo attribute, IInputContext inputContext) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09001927 // Needs to check the validity before clearing calling identity
1928 final boolean calledFromValidUser = calledFromValidUser();
1929
Dianne Hackborn7663d802012-02-24 13:08:49 -08001930 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001931 long ident = Binder.clearCallingIdentity();
1932 try {
1933 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001934 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
Dianne Hackborn7663d802012-02-24 13:08:49 -08001935 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001936 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08001937 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001938
Dianne Hackborn7663d802012-02-24 13:08:49 -08001939 ClientState cs = mClients.get(client.asBinder());
1940 if (cs == null) {
1941 throw new IllegalArgumentException("unknown client "
1942 + client.asBinder());
1943 }
1944
1945 try {
1946 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1947 // Check with the window manager to make sure this client actually
1948 // has a window with focus. If not, reject. This is thread safe
1949 // because if the focus changes some time before or after, the
1950 // next client receiving focus that has any interest in input will
1951 // be calling through here after that change happens.
1952 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
1953 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1954 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001955 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001956 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001957 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001958
Satoshi Kataoka8d033052012-11-19 17:30:40 +09001959 if (!calledFromValidUser) {
1960 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
1961 Slog.w(TAG, "If you want to interect with IME, you need "
1962 + "android.permission.INTERACT_ACROSS_USERS_FULL");
1963 hideCurrentInputLocked(0, null);
1964 return null;
1965 }
1966
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001967 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07001968 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09001969 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001970 if (attribute != null) {
1971 return startInputUncheckedLocked(cs, inputContext, attribute,
1972 controlFlags);
1973 }
1974 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001975 }
1976 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001977
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001978 // Should we auto-show the IME even if the caller has not
1979 // specified what should be done with it?
1980 // We only do this automatically if the window can resize
1981 // to accommodate the IME (so what the user sees will give
1982 // them good context without input information being obscured
1983 // by the IME) or if running on a large screen where there
1984 // is more room for the target window + IME.
1985 final boolean doAutoShow =
1986 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1987 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1988 || mRes.getConfiguration().isLayoutSizeAtLeast(
1989 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001990 final boolean isTextEditor =
1991 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
1992
1993 // We want to start input before showing the IME, but after closing
1994 // it. We want to do this after closing it to help the IME disappear
1995 // more quickly (not get stuck behind it initializing itself for the
1996 // new focused input, even if its window wants to hide the IME).
1997 boolean didStart = false;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001998
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001999 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2000 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002001 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002002 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2003 // There is no focus view, and this window will
2004 // be behind any soft input window, so hide the
2005 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002006 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002007 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002008 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002009 } else if (isTextEditor && doAutoShow && (softInputMode &
2010 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002011 // There is a focus view, and we are navigating forward
2012 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002013 // We only do this automatically if the window can resize
2014 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002015 // them good context without input information being obscured
2016 // by the IME) or if running on a large screen where there
2017 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002018 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002019 if (attribute != null) {
2020 res = startInputUncheckedLocked(cs, inputContext, attribute,
2021 controlFlags);
2022 didStart = true;
2023 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002024 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002025 }
2026 break;
2027 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2028 // Do nothing.
2029 break;
2030 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2031 if ((softInputMode &
2032 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002033 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002034 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002035 }
2036 break;
2037 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002038 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002039 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002040 break;
2041 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2042 if ((softInputMode &
2043 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002044 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002045 if (attribute != null) {
2046 res = startInputUncheckedLocked(cs, inputContext, attribute,
2047 controlFlags);
2048 didStart = true;
2049 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002050 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002051 }
2052 break;
2053 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002054 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002055 if (attribute != null) {
2056 res = startInputUncheckedLocked(cs, inputContext, attribute,
2057 controlFlags);
2058 didStart = true;
2059 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002060 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002061 break;
2062 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002063
2064 if (!didStart && attribute != null) {
2065 res = startInputUncheckedLocked(cs, inputContext, attribute,
2066 controlFlags);
2067 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002068 }
2069 } finally {
2070 Binder.restoreCallingIdentity(ident);
2071 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002072
2073 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002074 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002075
satok42c5a162011-05-26 16:46:14 +09002076 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002077 public void showInputMethodPickerFromClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002078 if (!calledFromValidUser()) {
2079 return;
2080 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002081 synchronized (mMethodMap) {
2082 if (mCurClient == null || client == null
2083 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002084 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002085 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002086 }
2087
satok440aab52010-11-25 09:43:11 +09002088 // Always call subtype picker, because subtype picker is a superset of input method
2089 // picker.
satokab751aa2010-09-14 19:17:36 +09002090 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
2091 }
2092 }
2093
satok42c5a162011-05-26 16:46:14 +09002094 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002095 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002096 if (!calledFromValidUser()) {
2097 return;
2098 }
satok28203512010-11-24 11:06:49 +09002099 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2100 }
2101
satok42c5a162011-05-26 16:46:14 +09002102 @Override
satok28203512010-11-24 11:06:49 +09002103 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002104 if (!calledFromValidUser()) {
2105 return;
2106 }
satok28203512010-11-24 11:06:49 +09002107 synchronized (mMethodMap) {
2108 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002109 setInputMethodWithSubtypeIdLocked(token, id,
2110 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2111 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002112 } else {
2113 setInputMethod(token, id);
2114 }
2115 }
satokab751aa2010-09-14 19:17:36 +09002116 }
2117
satok42c5a162011-05-26 16:46:14 +09002118 @Override
satokb416a712010-11-25 20:42:14 +09002119 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002120 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002121 if (!calledFromValidUser()) {
2122 return;
2123 }
satokb416a712010-11-25 20:42:14 +09002124 synchronized (mMethodMap) {
2125 if (mCurClient == null || client == null
2126 || mCurClient.client.asBinder() != client.asBinder()) {
2127 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
2128 }
satok7fee71f2010-12-17 18:54:26 +09002129 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2130 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002131 }
2132 }
2133
satok4fc87d62011-05-20 16:13:43 +09002134 @Override
satok735cf382010-11-11 20:40:09 +09002135 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002136 if (!calledFromValidUser()) {
2137 return false;
2138 }
satok735cf382010-11-11 20:40:09 +09002139 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002140 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002141 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002142 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002143 lastImi = mMethodMap.get(lastIme.first);
2144 } else {
2145 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002146 }
satok4fc87d62011-05-20 16:13:43 +09002147 String targetLastImiId = null;
2148 int subtypeId = NOT_A_SUBTYPE_ID;
2149 if (lastIme != null && lastImi != null) {
2150 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
2151 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
2152 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2153 : mCurrentSubtype.hashCode();
2154 // If the last IME is the same as the current IME and the last subtype is not
2155 // defined, there is no need to switch to the last IME.
2156 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2157 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002158 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002159 }
2160 }
2161
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002162 if (TextUtils.isEmpty(targetLastImiId)
2163 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002164 // This is a safety net. If the currentSubtype can't be added to the history
2165 // and the framework couldn't find the last ime, we will make the last ime be
2166 // the most applicable enabled keyboard subtype of the system imes.
2167 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2168 if (enabled != null) {
2169 final int N = enabled.size();
2170 final String locale = mCurrentSubtype == null
2171 ? mRes.getConfiguration().locale.toString()
2172 : mCurrentSubtype.getLocale();
2173 for (int i = 0; i < N; ++i) {
2174 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002175 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002176 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002177 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2178 InputMethodUtils.getSubtypes(imi),
2179 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002180 if (keyboardSubtype != null) {
2181 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002182 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002183 imi, keyboardSubtype.hashCode());
2184 if(keyboardSubtype.getLocale().equals(locale)) {
2185 break;
2186 }
2187 }
2188 }
2189 }
2190 }
2191 }
2192
2193 if (!TextUtils.isEmpty(targetLastImiId)) {
2194 if (DEBUG) {
2195 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2196 + ", from: " + mCurMethodId + ", " + subtypeId);
2197 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002198 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002199 return true;
2200 } else {
2201 return false;
2202 }
satok735cf382010-11-11 20:40:09 +09002203 }
2204 }
2205
satoke7c6998e2011-06-03 17:57:59 +09002206 @Override
satok688bd472012-02-09 20:09:17 +09002207 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002208 if (!calledFromValidUser()) {
2209 return false;
2210 }
satok688bd472012-02-09 20:09:17 +09002211 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002212 if (!calledWithValidToken(token)) {
2213 final int uid = Binder.getCallingUid();
2214 Slog.e(TAG, "Ignoring switchToNextInputMethod due to an invalid token. uid:" + uid
2215 + " token:" + token);
2216 return false;
2217 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002218 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
satok688bd472012-02-09 20:09:17 +09002219 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2220 if (nextSubtype == null) {
2221 return false;
2222 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002223 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2224 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002225 return true;
2226 }
2227 }
2228
2229 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002230 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2231 if (!calledFromValidUser()) {
2232 return false;
2233 }
2234 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002235 if (!calledWithValidToken(token)) {
2236 final int uid = Binder.getCallingUid();
2237 Slog.e(TAG, "Ignoring shouldOfferSwitchingToNextInputMethod due to an invalid "
2238 + "token. uid:" + uid + " token:" + token);
2239 return false;
2240 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002241 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002242 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2243 if (nextSubtype == null) {
2244 return false;
2245 }
2246 return true;
2247 }
2248 }
2249
2250 @Override
satok68f1b782011-04-11 14:26:04 +09002251 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002252 if (!calledFromValidUser()) {
2253 return null;
2254 }
satok68f1b782011-04-11 14:26:04 +09002255 synchronized (mMethodMap) {
2256 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2257 // TODO: Handle the case of the last IME with no subtypes
2258 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2259 || TextUtils.isEmpty(lastIme.second)) return null;
2260 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2261 if (lastImi == null) return null;
2262 try {
2263 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002264 final int lastSubtypeId =
2265 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002266 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2267 return null;
2268 }
2269 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002270 } catch (NumberFormatException e) {
2271 return null;
2272 }
2273 }
2274 }
2275
satoke7c6998e2011-06-03 17:57:59 +09002276 @Override
satokee5e77c2011-09-02 18:50:15 +09002277 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002278 if (!calledFromValidUser()) {
2279 return;
2280 }
satok91e88122011-07-18 11:11:42 +09002281 // By this IPC call, only a process which shares the same uid with the IME can add
2282 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09002283 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09002284 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002285 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002286 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002287 final String[] packageInfos;
2288 try {
2289 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2290 } catch (RemoteException e) {
2291 Slog.e(TAG, "Failed to get package infos");
2292 return;
2293 }
satok91e88122011-07-18 11:11:42 +09002294 if (packageInfos != null) {
2295 final int packageNum = packageInfos.length;
2296 for (int i = 0; i < packageNum; ++i) {
2297 if (packageInfos[i].equals(imi.getPackageName())) {
2298 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002299 final long ident = Binder.clearCallingIdentity();
2300 try {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002301 buildInputMethodListLocked(mMethodList, mMethodMap,
2302 false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002303 } finally {
2304 Binder.restoreCallingIdentity(ident);
2305 }
satokee5e77c2011-09-02 18:50:15 +09002306 return;
satok91e88122011-07-18 11:11:42 +09002307 }
2308 }
2309 }
satoke7c6998e2011-06-03 17:57:59 +09002310 }
satokee5e77c2011-09-02 18:50:15 +09002311 return;
satoke7c6998e2011-06-03 17:57:59 +09002312 }
2313
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002314 @Override
2315 public int getInputMethodWindowVisibleHeight() {
2316 return mWindowManagerService.getInputMethodWindowVisibleHeight();
2317 }
2318
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002319 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002320 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002321 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002322 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002323 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002324 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002325 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
2326 if (DEBUG) {
2327 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
2328 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
2329 + " actual: " + sequenceNumber);
2330 }
2331 return;
2332 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002333 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2334 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09002335 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002336 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002337 }
2338 }
2339
satok28203512010-11-24 11:06:49 +09002340 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002341 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002342 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
2343 }
2344 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002345
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002346 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
2347 if (token == null) {
2348 if (mContext.checkCallingOrSelfPermission(
2349 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2350 != PackageManager.PERMISSION_GRANTED) {
2351 throw new SecurityException(
2352 "Using null token requires permission "
2353 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002354 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002355 } else if (mCurToken != token) {
2356 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2357 + " token: " + token);
2358 return;
2359 }
2360
2361 final long ident = Binder.clearCallingIdentity();
2362 try {
2363 setInputMethodLocked(id, subtypeId);
2364 } finally {
2365 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002366 }
2367 }
2368
satok42c5a162011-05-26 16:46:14 +09002369 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002370 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002371 if (!calledFromValidUser()) {
2372 return;
2373 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002374 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002375 if (!calledWithValidToken(token)) {
2376 final int uid = Binder.getCallingUid();
2377 Slog.e(TAG, "Ignoring hideInputMethod due to an invalid token. uid:"
2378 + uid + " token:" + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002379 return;
2380 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002381 long ident = Binder.clearCallingIdentity();
2382 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002383 hideCurrentInputLocked(flags, null);
2384 } finally {
2385 Binder.restoreCallingIdentity(ident);
2386 }
2387 }
2388 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002389
satok42c5a162011-05-26 16:46:14 +09002390 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002391 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002392 if (!calledFromValidUser()) {
2393 return;
2394 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002395 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002396 if (!calledWithValidToken(token)) {
2397 final int uid = Binder.getCallingUid();
2398 Slog.e(TAG, "Ignoring showMySoftInput due to an invalid token. uid:"
2399 + uid + " token:" + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002400 return;
2401 }
2402 long ident = Binder.clearCallingIdentity();
2403 try {
2404 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002405 } finally {
2406 Binder.restoreCallingIdentity(ident);
2407 }
2408 }
2409 }
2410
2411 void setEnabledSessionInMainThread(SessionState session) {
2412 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002413 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002414 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002415 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002416 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002417 } catch (RemoteException e) {
2418 }
2419 }
2420 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002421 if (mEnabledSession != null && mEnabledSession.session != null) {
2422 try {
2423 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
2424 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
2425 } catch (RemoteException e) {
2426 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002427 }
2428 }
2429 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002430
satok42c5a162011-05-26 16:46:14 +09002431 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002432 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002433 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002434 switch (msg.what) {
2435 case MSG_SHOW_IM_PICKER:
2436 showInputMethodMenu();
2437 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002438
satokab751aa2010-09-14 19:17:36 +09002439 case MSG_SHOW_IM_SUBTYPE_PICKER:
2440 showInputMethodSubtypeMenu();
2441 return true;
2442
satok47a44912010-10-06 16:03:58 +09002443 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002444 args = (SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09002445 showInputMethodAndSubtypeEnabler((String)args.arg1);
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002446 args.recycle();
satok217f5482010-12-15 05:19:19 +09002447 return true;
2448
2449 case MSG_SHOW_IM_CONFIG:
2450 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002451 return true;
2452
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002453 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002455 case MSG_UNBIND_INPUT:
2456 try {
2457 ((IInputMethod)msg.obj).unbindInput();
2458 } catch (RemoteException e) {
2459 // There is nothing interesting about the method dying.
2460 }
2461 return true;
2462 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002463 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002464 try {
2465 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2466 } catch (RemoteException e) {
2467 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002468 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002469 return true;
2470 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002471 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002472 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002473 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002474 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002475 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002476 } catch (RemoteException e) {
2477 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002478 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002479 return true;
2480 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002481 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002482 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002483 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002484 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002485 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002486 } catch (RemoteException e) {
2487 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002488 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002489 return true;
2490 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002491 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002492 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002493 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002494 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2495 } catch (RemoteException e) {
2496 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002497 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002498 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002499 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002500 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002501 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002502 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002503 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002504 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002505 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002506 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002507 // Dispose the channel if the input method is not local to this process
2508 // because the remote proxy will get its own copy when unparceled.
2509 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002510 channel.dispose();
2511 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002512 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002513 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002514 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002515 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002516 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002517
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002518 case MSG_START_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002519 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002520 try {
2521 SessionState session = (SessionState)args.arg1;
2522 setEnabledSessionInMainThread(session);
2523 session.method.startInput((IInputContext)args.arg2,
2524 (EditorInfo)args.arg3);
2525 } catch (RemoteException e) {
2526 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002527 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002528 return true;
2529 case MSG_RESTART_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002530 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002531 try {
2532 SessionState session = (SessionState)args.arg1;
2533 setEnabledSessionInMainThread(session);
2534 session.method.restartInput((IInputContext)args.arg2,
2535 (EditorInfo)args.arg3);
2536 } catch (RemoteException e) {
2537 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002538 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002539 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002540
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002541 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002542
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002543 case MSG_UNBIND_METHOD:
2544 try {
2545 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
2546 } catch (RemoteException e) {
2547 // There is nothing interesting about the last client dying.
2548 }
2549 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002550 case MSG_BIND_METHOD: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002551 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002552 IInputMethodClient client = (IInputMethodClient)args.arg1;
2553 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002554 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002555 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002556 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002557 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002558 } finally {
2559 // Dispose the channel if the input method is not local to this process
2560 // because the remote proxy will get its own copy when unparceled.
2561 if (res.channel != null && Binder.isProxy(client)) {
2562 res.channel.dispose();
2563 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002564 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002565 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002566 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002567 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002568 case MSG_SET_ACTIVE:
2569 try {
2570 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2571 } catch (RemoteException e) {
2572 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2573 + ((ClientState)msg.obj).pid + " uid "
2574 + ((ClientState)msg.obj).uid);
2575 }
2576 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002577 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
2578 final int sequenceNumber = msg.arg1;
2579 final IInputMethodClient client = (IInputMethodClient)msg.obj;
2580 try {
2581 client.setUserActionNotificationSequenceNumber(sequenceNumber);
2582 } catch (RemoteException e) {
2583 Slog.w(TAG, "Got RemoteException sending "
2584 + "setUserActionNotificationSequenceNumber("
2585 + sequenceNumber + ") notification to pid "
2586 + ((ClientState)msg.obj).pid + " uid "
2587 + ((ClientState)msg.obj).uid);
2588 }
2589 return true;
2590 }
satok01038492012-04-09 21:08:27 +09002591
2592 // --------------------------------------------------------------
2593 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
2594 mHardKeyboardListener.handleHardKeyboardStatusChange(
2595 msg.arg1 == 1, msg.arg2 == 1);
2596 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002597 }
2598 return false;
2599 }
2600
satokdc9ddae2011-10-06 12:22:36 +09002601 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002602 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
2603 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09002604 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002605 if (DEBUG) {
2606 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2607 }
satok723a27e2010-11-11 14:58:11 +09002608 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002609 return true;
2610 }
2611
2612 return false;
2613 }
2614
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002615 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002616 HashMap<String, InputMethodInfo> map, boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002617 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002618 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Satoshi Kataoka0766eb02013-07-31 18:30:13 +09002619 + " \n ------ \n" + InputMethodUtils.getStackTrace());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002620 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002621 list.clear();
2622 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002623
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002624 // Use for queryIntentServicesAsUser
2625 final PackageManager pm = mContext.getPackageManager();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002626 String disabledSysImes = mSettings.getDisabledSystemInputMethods();
Amith Yamasanie861ec12010-03-24 21:39:27 -07002627 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002628
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002629 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002630 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002631 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2632 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002633
satoke7c6998e2011-06-03 17:57:59 +09002634 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2635 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002636 for (int i = 0; i < services.size(); ++i) {
2637 ResolveInfo ri = services.get(i);
2638 ServiceInfo si = ri.serviceInfo;
2639 ComponentName compName = new ComponentName(si.packageName, si.name);
2640 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2641 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002642 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002643 + ": it does not require the permission "
2644 + android.Manifest.permission.BIND_INPUT_METHOD);
2645 continue;
2646 }
2647
Joe Onorato8a9b2202010-02-26 18:56:32 -08002648 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002649
2650 try {
satoke7c6998e2011-06-03 17:57:59 +09002651 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002652 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002653 final String id = p.getId();
2654 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002655
2656 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002657 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002658 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002659
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002660 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002661 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002662 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002663 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002664 }
2665 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002666
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002667 if (resetDefaultEnabledIme) {
2668 final ArrayList<InputMethodInfo> defaultEnabledIme =
2669 InputMethodUtils.getDefaultEnabledImes(mContext, mSystemReady, list);
2670 for (int i = 0; i < defaultEnabledIme.size(); ++i) {
2671 final InputMethodInfo imi = defaultEnabledIme.get(i);
2672 if (DEBUG) {
2673 Slog.d(TAG, "--- enable ime = " + imi);
2674 }
2675 setInputMethodEnabledLocked(imi.getId(), true);
2676 }
2677 }
2678
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002679 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09002680 if (!TextUtils.isEmpty(defaultImiId)) {
2681 if (!map.containsKey(defaultImiId)) {
2682 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
2683 if (chooseNewDefaultIMELocked()) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002684 updateFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09002685 }
2686 } else {
2687 // Double check that the default IME is certainly enabled.
2688 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002689 }
2690 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09002691 // Here is not the perfect place to reset the switching controller. Ideally
2692 // mSwitchingController and mSettings should be able to share the same state.
2693 // TODO: Make sure that mSwitchingController and mSettings are sharing the
2694 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09002695 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002696 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002697
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002698 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002699
satokab751aa2010-09-14 19:17:36 +09002700 private void showInputMethodMenu() {
2701 showInputMethodMenuInternal(false);
2702 }
2703
2704 private void showInputMethodSubtypeMenu() {
2705 showInputMethodMenuInternal(true);
2706 }
2707
satok217f5482010-12-15 05:19:19 +09002708 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002709 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002710 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002711 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2712 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002713 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002714 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002715 }
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002716 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok217f5482010-12-15 05:19:19 +09002717 }
2718
2719 private void showConfigureInputMethods() {
2720 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2721 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2722 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2723 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002724 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09002725 }
2726
satok2c93efc2012-04-02 19:33:47 +09002727 private boolean isScreenLocked() {
2728 return mKeyguardManager != null
2729 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
2730 }
satokab751aa2010-09-14 19:17:36 +09002731 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002732 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002733
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002734 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09002735 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002736
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002737 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002738 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002739 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002740
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002741 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002742 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09002743 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
2744 mContext);
satok7f35c8c2010-10-07 21:13:11 +09002745 if (immis == null || immis.size() == 0) {
2746 return;
2747 }
2748
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002749 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002750
satok688bd472012-02-09 20:09:17 +09002751 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002752 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
satok688bd472012-02-09 20:09:17 +09002753 showSubtypes, mInputShown, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09002754
satokc3690562012-01-10 20:14:43 +09002755 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002756 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09002757 if (currentSubtype != null) {
2758 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002759 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
2760 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09002761 }
2762 }
2763
Ken Wakasa761eb372011-03-04 19:06:18 +09002764 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09002765 mIms = new InputMethodInfo[N];
2766 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002767 int checkedItem = 0;
2768 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002769 final ImeSubtypeListItem item = imList.get(i);
2770 mIms[i] = item.mImi;
2771 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002772 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002773 int subtypeId = mSubtypeIds[i];
2774 if ((subtypeId == NOT_A_SUBTYPE_ID)
2775 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2776 || (subtypeId == lastInputMethodSubtypeId)) {
2777 checkedItem = i;
2778 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002779 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002780 }
Ken Wakasa05dbb652011-08-22 15:22:43 +09002781 final TypedArray a = context.obtainStyledAttributes(null,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002782 com.android.internal.R.styleable.DialogPreference,
2783 com.android.internal.R.attr.alertDialogStyle, 0);
2784 mDialogBuilder = new AlertDialog.Builder(context)
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002785 .setOnCancelListener(new OnCancelListener() {
satok42c5a162011-05-26 16:46:14 +09002786 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002787 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002788 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002789 }
2790 })
2791 .setIcon(a.getDrawable(
2792 com.android.internal.R.styleable.DialogPreference_dialogTitle));
2793 a.recycle();
satok01038492012-04-09 21:08:27 +09002794 final LayoutInflater inflater =
2795 (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2796 final View tv = inflater.inflate(
2797 com.android.internal.R.layout.input_method_switch_dialog_title, null);
2798 mDialogBuilder.setCustomTitle(tv);
2799
2800 // Setup layout for a toggle switch of the hardware keyboard
2801 mSwitchingDialogTitleView = tv;
2802 mSwitchingDialogTitleView.findViewById(
2803 com.android.internal.R.id.hard_keyboard_section).setVisibility(
2804 mWindowManagerService.isHardKeyboardAvailable() ?
2805 View.VISIBLE : View.GONE);
2806 final Switch hardKeySwitch = ((Switch)mSwitchingDialogTitleView.findViewById(
2807 com.android.internal.R.id.hard_keyboard_switch));
2808 hardKeySwitch.setChecked(mWindowManagerService.isHardKeyboardEnabled());
2809 hardKeySwitch.setOnCheckedChangeListener(
2810 new OnCheckedChangeListener() {
2811 @Override
2812 public void onCheckedChanged(
2813 CompoundButton buttonView, boolean isChecked) {
2814 mWindowManagerService.setHardKeyboardEnabled(isChecked);
Satoshi Kataoka04dd24d2013-01-18 13:44:37 +09002815 // Ensure that the input method dialog is dismissed when changing
2816 // the hardware keyboard state.
2817 hideInputMethodMenu();
satok01038492012-04-09 21:08:27 +09002818 }
2819 });
satokd87c2592010-09-29 11:52:06 +09002820
Ken Wakasa05dbb652011-08-22 15:22:43 +09002821 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(context,
2822 com.android.internal.R.layout.simple_list_item_2_single_choice, imList,
2823 checkedItem);
2824
2825 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002826 new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002827 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002828 public void onClick(DialogInterface dialog, int which) {
2829 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09002830 if (mIms == null || mIms.length <= which
2831 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002832 return;
2833 }
2834 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09002835 int subtypeId = mSubtypeIds[which];
Satoshi Kataokad2142962012-11-12 18:43:06 +09002836 adapter.mCheckedItem = which;
2837 adapter.notifyDataSetChanged();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002838 hideInputMethodMenu();
2839 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09002840 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09002841 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09002842 subtypeId = NOT_A_SUBTYPE_ID;
2843 }
2844 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002845 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08002846 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002847 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002848 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002849
satokbc81b692011-08-26 16:22:22 +09002850 if (showSubtypes && !isScreenLocked) {
satok82beadf2010-12-27 19:03:06 +09002851 mDialogBuilder.setPositiveButton(
2852 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09002853 new DialogInterface.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002854 @Override
satok7f35c8c2010-10-07 21:13:11 +09002855 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09002856 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09002857 }
2858 });
2859 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002860 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002861 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002862 mSwitchingDialog.getWindow().setType(
2863 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09002864 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
2865 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002866 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
Jason Monk807ef762014-05-08 15:47:46 -04002867 updateImeWindowStatusLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002868 mSwitchingDialog.show();
2869 }
2870 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002871
Ken Wakasa05dbb652011-08-22 15:22:43 +09002872 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
2873 private final LayoutInflater mInflater;
2874 private final int mTextViewResourceId;
2875 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09002876 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09002877 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
2878 List<ImeSubtypeListItem> itemsList, int checkedItem) {
2879 super(context, textViewResourceId, itemsList);
2880 mTextViewResourceId = textViewResourceId;
2881 mItemsList = itemsList;
2882 mCheckedItem = checkedItem;
2883 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2884 }
2885
2886 @Override
2887 public View getView(int position, View convertView, ViewGroup parent) {
2888 final View view = convertView != null ? convertView
2889 : mInflater.inflate(mTextViewResourceId, null);
2890 if (position < 0 || position >= mItemsList.size()) return view;
2891 final ImeSubtypeListItem item = mItemsList.get(position);
2892 final CharSequence imeName = item.mImeName;
2893 final CharSequence subtypeName = item.mSubtypeName;
2894 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
2895 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
2896 if (TextUtils.isEmpty(subtypeName)) {
2897 firstTextView.setText(imeName);
2898 secondTextView.setVisibility(View.GONE);
2899 } else {
2900 firstTextView.setText(subtypeName);
2901 secondTextView.setText(imeName);
2902 secondTextView.setVisibility(View.VISIBLE);
2903 }
2904 final RadioButton radioButton =
2905 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
2906 radioButton.setChecked(position == mCheckedItem);
2907 return view;
2908 }
2909 }
2910
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002911 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07002912 synchronized (mMethodMap) {
2913 hideInputMethodMenuLocked();
2914 }
2915 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002916
The Android Open Source Project10592532009-03-18 17:39:46 -07002917 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002918 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002919
The Android Open Source Project10592532009-03-18 17:39:46 -07002920 if (mSwitchingDialog != null) {
2921 mSwitchingDialog.dismiss();
2922 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002923 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002924
Jason Monk807ef762014-05-08 15:47:46 -04002925 updateImeWindowStatusLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -07002926 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07002927 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002928 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002929
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002930 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002931
satok42c5a162011-05-26 16:46:14 +09002932 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002933 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002934 // TODO: Make this work even for non-current users?
2935 if (!calledFromValidUser()) {
2936 return false;
2937 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002938 synchronized (mMethodMap) {
2939 if (mContext.checkCallingOrSelfPermission(
2940 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2941 != PackageManager.PERMISSION_GRANTED) {
2942 throw new SecurityException(
2943 "Requires permission "
2944 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2945 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002946
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002947 long ident = Binder.clearCallingIdentity();
2948 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002949 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002950 } finally {
2951 Binder.restoreCallingIdentity(ident);
2952 }
2953 }
2954 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002955
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002956 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
2957 // Make sure this is a valid input method.
2958 InputMethodInfo imm = mMethodMap.get(id);
2959 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09002960 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002961 }
2962
satokd87c2592010-09-29 11:52:06 +09002963 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
2964 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002965
satokd87c2592010-09-29 11:52:06 +09002966 if (enabled) {
2967 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
2968 if (pair.first.equals(id)) {
2969 // We are enabling this input method, but it is already enabled.
2970 // Nothing to do. The previous state was enabled.
2971 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002972 }
2973 }
satokd87c2592010-09-29 11:52:06 +09002974 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
2975 // Previous state was disabled.
2976 return false;
2977 } else {
2978 StringBuilder builder = new StringBuilder();
2979 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2980 builder, enabledInputMethodsList, id)) {
2981 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002982 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002983 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
2984 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
2985 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09002986 }
2987 // Previous state was enabled.
2988 return true;
2989 } else {
2990 // We are disabling the input method but it is already disabled.
2991 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002992 return false;
2993 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002994 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002995 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002996
satok723a27e2010-11-11 14:58:11 +09002997 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
2998 boolean setSubtypeOnly) {
2999 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003000 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003001
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003002 mCurUserActionNotificationSequenceNumber =
3003 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3004 if (DEBUG) {
3005 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3006 + mCurUserActionNotificationSequenceNumber);
3007 }
3008
3009 if (mCurClient != null && mCurClient.client != null) {
3010 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3011 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
3012 mCurUserActionNotificationSequenceNumber, mCurClient.client));
3013 }
3014
satok723a27e2010-11-11 14:58:11 +09003015 // Set Subtype here
3016 if (imi == null || subtypeId < 0) {
3017 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003018 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003019 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003020 if (subtypeId < imi.getSubtypeCount()) {
3021 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3022 mSettings.putSelectedSubtype(subtype.hashCode());
3023 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003024 } else {
3025 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003026 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003027 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003028 }
satokab751aa2010-09-14 19:17:36 +09003029 }
satok723a27e2010-11-11 14:58:11 +09003030
satok4c0e7152012-06-20 20:08:44 +09003031 // Workaround.
3032 // ASEC is not ready in the IMMS constructor. Accordingly, forward-locked
3033 // IMEs are not recognized and considered uninstalled.
3034 // Actually, we can't move everything after SystemReady because
3035 // IMMS needs to run in the encryption lock screen. So, we just skip changing
3036 // the default IME here and try cheking the default IME again in systemReady().
3037 // TODO: Do nothing before system ready and implement a separated logic for
3038 // the encryption lock screen.
3039 // TODO: ASEC should be ready before IMMS is instantiated.
3040 if (mSystemReady && !setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003041 // Set InputMethod here
3042 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3043 }
3044 }
3045
3046 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3047 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3048 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3049 // newDefaultIme is empty when there is no candidate for the selected IME.
3050 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3051 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3052 if (subtypeHashCode != null) {
3053 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003054 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok723a27e2010-11-11 14:58:11 +09003055 imi, Integer.valueOf(subtypeHashCode));
3056 } catch (NumberFormatException e) {
3057 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3058 }
3059 }
3060 }
3061 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003062 }
3063
satok4e4569d2010-11-19 18:45:53 +09003064 // If there are no selected shortcuts, tries finding the most applicable ones.
3065 private Pair<InputMethodInfo, InputMethodSubtype>
3066 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3067 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3068 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003069 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003070 boolean foundInSystemIME = false;
3071
3072 // Search applicable subtype for each InputMethodInfo
3073 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003074 final String imiId = imi.getId();
3075 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3076 continue;
3077 }
satokcd7cd292010-11-20 15:46:23 +09003078 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003079 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003080 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003081 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003082 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003083 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003084 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003085 }
satokdf31ae62011-01-15 06:19:44 +09003086 // 2. Search by the system locale from enabledSubtypes.
3087 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003088 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003089 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003090 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003091 }
satoka86f5e42011-09-02 17:12:42 +09003092 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003093 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003094 final ArrayList<InputMethodSubtype> subtypesForSearch =
3095 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003096 ? InputMethodUtils.getSubtypes(imi)
3097 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003098 // 4. Search by the current subtype's locale from all subtypes.
3099 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003100 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003101 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003102 }
3103 // 5. Search by the system locale from all subtypes.
3104 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003105 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003106 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003107 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003108 }
satokcd7cd292010-11-20 15:46:23 +09003109 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003110 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003111 // The current input method is the most applicable IME.
3112 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003113 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003114 break;
satok7599a7f2010-12-22 13:45:23 +09003115 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003116 // The system input method is 2nd applicable IME.
3117 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003118 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003119 if ((imi.getServiceInfo().applicationInfo.flags
3120 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3121 foundInSystemIME = true;
3122 }
satok4e4569d2010-11-19 18:45:53 +09003123 }
3124 }
3125 }
3126 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003127 if (mostApplicableIMI != null) {
3128 Slog.w(TAG, "Most applicable shortcut input method was:"
3129 + mostApplicableIMI.getId());
3130 if (mostApplicableSubtype != null) {
3131 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3132 + "," + mostApplicableSubtype.getMode() + ","
3133 + mostApplicableSubtype.getLocale());
3134 }
3135 }
satok4e4569d2010-11-19 18:45:53 +09003136 }
satokcd7cd292010-11-20 15:46:23 +09003137 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09003138 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09003139 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003140 } else {
3141 return null;
3142 }
3143 }
3144
satokab751aa2010-09-14 19:17:36 +09003145 /**
3146 * @return Return the current subtype of this input method.
3147 */
satok42c5a162011-05-26 16:46:14 +09003148 @Override
satokab751aa2010-09-14 19:17:36 +09003149 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003150 // TODO: Make this work even for non-current users?
3151 if (!calledFromValidUser()) {
3152 return null;
3153 }
3154 synchronized (mMethodMap) {
3155 return getCurrentInputMethodSubtypeLocked();
3156 }
3157 }
3158
3159 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003160 if (mCurMethodId == null) {
3161 return null;
3162 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003163 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003164 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3165 if (imi == null || imi.getSubtypeCount() == 0) {
3166 return null;
satok4e4569d2010-11-19 18:45:53 +09003167 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003168 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003169 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3170 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003171 if (subtypeId == NOT_A_SUBTYPE_ID) {
3172 // If there are no selected subtypes, the framework will try to find
3173 // the most applicable subtype from explicitly or implicitly enabled
3174 // subtypes.
3175 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003176 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003177 // If there is only one explicitly or implicitly enabled subtype,
3178 // just returns it.
3179 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3180 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3181 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003182 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003183 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003184 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003185 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003186 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003187 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3188 true);
satok4e4569d2010-11-19 18:45:53 +09003189 }
satok3ef8b292010-11-23 06:06:29 +09003190 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003191 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003192 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003193 }
3194 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003195 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003196 }
3197
satokf3db1af2010-11-23 13:34:33 +09003198 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
3199 InputMethodSubtype subtype) {
3200 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
3201 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
3202 } else {
3203 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3204 subtypes.add(subtype);
3205 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
3206 }
3207 }
3208
satok4e4569d2010-11-19 18:45:53 +09003209 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003210 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003211 @Override
satok4e4569d2010-11-19 18:45:53 +09003212 public List getShortcutInputMethodsAndSubtypes() {
3213 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09003214 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09003215 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003216 // If there are no selected shortcut subtypes, the framework will try to find
3217 // the most applicable subtype from all subtypes whose mode is
3218 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003219 Pair<InputMethodInfo, InputMethodSubtype> info =
3220 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003221 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003222 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003223 ret.add(info.first);
3224 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003225 }
satok3da92232011-01-11 22:46:30 +09003226 return ret;
satokf3db1af2010-11-23 13:34:33 +09003227 }
satokf3db1af2010-11-23 13:34:33 +09003228 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3229 ret.add(imi);
3230 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3231 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003232 }
3233 }
satokf3db1af2010-11-23 13:34:33 +09003234 return ret;
satok4e4569d2010-11-19 18:45:53 +09003235 }
3236 }
3237
satok42c5a162011-05-26 16:46:14 +09003238 @Override
satokb66d2872010-11-10 01:04:04 +09003239 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003240 // TODO: Make this work even for non-current users?
3241 if (!calledFromValidUser()) {
3242 return false;
3243 }
satokb66d2872010-11-10 01:04:04 +09003244 synchronized (mMethodMap) {
3245 if (subtype != null && mCurMethodId != null) {
3246 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003247 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003248 if (subtypeId != NOT_A_SUBTYPE_ID) {
3249 setInputMethodLocked(mCurMethodId, subtypeId);
3250 return true;
3251 }
3252 }
3253 return false;
3254 }
3255 }
3256
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003257 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003258 private static class InputMethodFileManager {
3259 private static final String SYSTEM_PATH = "system";
3260 private static final String INPUT_METHOD_PATH = "inputmethod";
3261 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3262 private static final String NODE_SUBTYPES = "subtypes";
3263 private static final String NODE_SUBTYPE = "subtype";
3264 private static final String NODE_IMI = "imi";
3265 private static final String ATTR_ID = "id";
3266 private static final String ATTR_LABEL = "label";
3267 private static final String ATTR_ICON = "icon";
3268 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3269 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3270 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3271 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3272 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3273 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003274 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
satoke7c6998e2011-06-03 17:57:59 +09003275 new HashMap<String, List<InputMethodSubtype>>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003276 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003277 if (methodMap == null) {
3278 throw new NullPointerException("methodMap is null");
3279 }
3280 mMethodMap = methodMap;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003281 final File systemDir = userId == UserHandle.USER_OWNER
3282 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3283 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003284 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3285 if (!inputMethodDir.mkdirs()) {
3286 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3287 }
3288 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3289 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3290 if (!subtypeFile.exists()) {
3291 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003292 writeAdditionalInputMethodSubtypes(
3293 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003294 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003295 readAdditionalInputMethodSubtypes(
3296 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003297 }
3298 }
3299
3300 private void deleteAllInputMethodSubtypes(String imiId) {
3301 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003302 mAdditionalSubtypesMap.remove(imiId);
3303 writeAdditionalInputMethodSubtypes(
3304 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003305 }
3306 }
3307
3308 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003309 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003310 synchronized (mMethodMap) {
3311 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3312 final int N = additionalSubtypes.length;
3313 for (int i = 0; i < N; ++i) {
3314 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003315 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003316 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003317 } else {
3318 Slog.w(TAG, "Duplicated subtype definition found: "
3319 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003320 }
3321 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003322 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3323 writeAdditionalInputMethodSubtypes(
3324 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003325 }
3326 }
3327
3328 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3329 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003330 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003331 }
3332 }
3333
3334 private static void writeAdditionalInputMethodSubtypes(
3335 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3336 HashMap<String, InputMethodInfo> methodMap) {
3337 // Safety net for the case that this function is called before methodMap is set.
3338 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3339 FileOutputStream fos = null;
3340 try {
3341 fos = subtypesFile.startWrite();
3342 final XmlSerializer out = new FastXmlSerializer();
3343 out.setOutput(fos, "utf-8");
3344 out.startDocument(null, true);
3345 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3346 out.startTag(null, NODE_SUBTYPES);
3347 for (String imiId : allSubtypes.keySet()) {
3348 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3349 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3350 continue;
3351 }
3352 out.startTag(null, NODE_IMI);
3353 out.attribute(null, ATTR_ID, imiId);
3354 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3355 final int N = subtypesList.size();
3356 for (int i = 0; i < N; ++i) {
3357 final InputMethodSubtype subtype = subtypesList.get(i);
3358 out.startTag(null, NODE_SUBTYPE);
3359 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3360 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3361 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3362 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3363 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3364 out.attribute(null, ATTR_IS_AUXILIARY,
3365 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3366 out.endTag(null, NODE_SUBTYPE);
3367 }
3368 out.endTag(null, NODE_IMI);
3369 }
3370 out.endTag(null, NODE_SUBTYPES);
3371 out.endDocument();
3372 subtypesFile.finishWrite(fos);
3373 } catch (java.io.IOException e) {
3374 Slog.w(TAG, "Error writing subtypes", e);
3375 if (fos != null) {
3376 subtypesFile.failWrite(fos);
3377 }
3378 }
3379 }
3380
3381 private static void readAdditionalInputMethodSubtypes(
3382 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3383 if (allSubtypes == null || subtypesFile == null) return;
3384 allSubtypes.clear();
3385 FileInputStream fis = null;
3386 try {
3387 fis = subtypesFile.openRead();
3388 final XmlPullParser parser = Xml.newPullParser();
3389 parser.setInput(fis, null);
3390 int type = parser.getEventType();
3391 // Skip parsing until START_TAG
3392 while ((type = parser.next()) != XmlPullParser.START_TAG
3393 && type != XmlPullParser.END_DOCUMENT) {}
3394 String firstNodeName = parser.getName();
3395 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3396 throw new XmlPullParserException("Xml doesn't start with subtypes");
3397 }
3398 final int depth =parser.getDepth();
3399 String currentImiId = null;
3400 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3401 while (((type = parser.next()) != XmlPullParser.END_TAG
3402 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3403 if (type != XmlPullParser.START_TAG)
3404 continue;
3405 final String nodeName = parser.getName();
3406 if (NODE_IMI.equals(nodeName)) {
3407 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3408 if (TextUtils.isEmpty(currentImiId)) {
3409 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3410 continue;
3411 }
3412 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
3413 allSubtypes.put(currentImiId, tempSubtypesArray);
3414 } else if (NODE_SUBTYPE.equals(nodeName)) {
3415 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3416 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3417 continue;
3418 }
3419 final int icon = Integer.valueOf(
3420 parser.getAttributeValue(null, ATTR_ICON));
3421 final int label = Integer.valueOf(
3422 parser.getAttributeValue(null, ATTR_LABEL));
3423 final String imeSubtypeLocale =
3424 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3425 final String imeSubtypeMode =
3426 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3427 final String imeSubtypeExtraValue =
3428 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003429 final boolean isAuxiliary = "1".equals(String.valueOf(
3430 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
satoke7c6998e2011-06-03 17:57:59 +09003431 final InputMethodSubtype subtype =
3432 new InputMethodSubtype(label, icon, imeSubtypeLocale,
3433 imeSubtypeMode, imeSubtypeExtraValue, isAuxiliary);
3434 tempSubtypesArray.add(subtype);
3435 }
3436 }
3437 } catch (XmlPullParserException e) {
3438 Slog.w(TAG, "Error reading subtypes: " + e);
3439 return;
3440 } catch (java.io.IOException e) {
3441 Slog.w(TAG, "Error reading subtypes: " + e);
3442 return;
3443 } catch (NumberFormatException e) {
3444 Slog.w(TAG, "Error reading subtypes: " + e);
3445 return;
3446 } finally {
3447 if (fis != null) {
3448 try {
3449 fis.close();
3450 } catch (java.io.IOException e1) {
3451 Slog.w(TAG, "Failed to close.");
3452 }
3453 }
3454 }
3455 }
3456 }
3457
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003458 @Override
3459 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3460 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3461 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003463 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3464 + Binder.getCallingPid()
3465 + ", uid=" + Binder.getCallingUid());
3466 return;
3467 }
3468
3469 IInputMethod method;
3470 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003472 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003474 synchronized (mMethodMap) {
3475 p.println("Current Input Method Manager state:");
3476 int N = mMethodList.size();
3477 p.println(" Input Methods:");
3478 for (int i=0; i<N; i++) {
3479 InputMethodInfo info = mMethodList.get(i);
3480 p.println(" InputMethod #" + i + ":");
3481 info.dump(p, " ");
3482 }
3483 p.println(" Clients:");
3484 for (ClientState ci : mClients.values()) {
3485 p.println(" Client " + ci + ":");
3486 p.println(" client=" + ci.client);
3487 p.println(" inputContext=" + ci.inputContext);
3488 p.println(" sessionRequested=" + ci.sessionRequested);
3489 p.println(" curSession=" + ci.curSession);
3490 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003491 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003492 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003493 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3494 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003495 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3496 + " mBoundToMethod=" + mBoundToMethod);
3497 p.println(" mCurToken=" + mCurToken);
3498 p.println(" mCurIntent=" + mCurIntent);
3499 method = mCurMethod;
3500 p.println(" mCurMethod=" + mCurMethod);
3501 p.println(" mEnabledSession=" + mEnabledSession);
3502 p.println(" mShowRequested=" + mShowRequested
3503 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3504 + " mShowForced=" + mShowForced
3505 + " mInputShown=" + mInputShown);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003506 p.println(" mCurUserActionNotificationSequenceNumber="
3507 + mCurUserActionNotificationSequenceNumber);
Dianne Hackbornd4e6d462014-05-16 16:32:37 -07003508 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003509 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003510
Jeff Brownb88102f2010-09-08 11:49:43 -07003511 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003512 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003513 pw.flush();
3514 try {
3515 client.client.asBinder().dump(fd, args);
3516 } catch (RemoteException e) {
3517 p.println("Input method client dead: " + e);
3518 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003519 } else {
3520 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003521 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003522
Jeff Brownb88102f2010-09-08 11:49:43 -07003523 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003524 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003525 pw.flush();
3526 try {
3527 method.asBinder().dump(fd, args);
3528 } catch (RemoteException e) {
3529 p.println("Input method service dead: " + e);
3530 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003531 } else {
3532 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003533 }
3534 }
3535}