blob: 9a9a4aeacd24af465b295fedab756149c554f47c [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
Yohei Yukawa0f3ad12015-04-06 16:48:24 -070018import android.annotation.NonNull;
Svet Ganovadc1cf42015-06-15 16:36:24 -070019import android.content.pm.PackageManagerInternal;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080020import com.android.internal.content.PackageMonitor;
Satoshi Kataokad7443c82013-10-15 17:45:43 +090021import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController;
Satoshi Kataokad787f692013-10-26 04:44:21 +090022import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController.ImeSubtypeListItem;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +090023import com.android.internal.inputmethod.InputMethodUtils;
24import com.android.internal.inputmethod.InputMethodUtils.InputMethodSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import com.android.internal.os.HandlerCaller;
Svetoslav Ganov758143e2012-08-06 16:40:27 -070026import com.android.internal.os.SomeArgs;
satoke7c6998e2011-06-03 17:57:59 +090027import com.android.internal.util.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import com.android.internal.view.IInputContext;
29import com.android.internal.view.IInputMethod;
Michael Wright52a53522013-03-14 10:59:38 -070030import com.android.internal.view.IInputSessionCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import com.android.internal.view.IInputMethodClient;
32import com.android.internal.view.IInputMethodManager;
33import com.android.internal.view.IInputMethodSession;
34import com.android.internal.view.InputBindResult;
Svet Ganovadc1cf42015-06-15 16:36:24 -070035import com.android.server.pm.UserManagerService;
Adam Lesinskief2ea1f2013-12-05 16:48:06 -080036import com.android.server.statusbar.StatusBarManagerService;
satok01038492012-04-09 21:08:27 +090037import com.android.server.wm.WindowManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038
satoke7c6998e2011-06-03 17:57:59 +090039import org.xmlpull.v1.XmlPullParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import org.xmlpull.v1.XmlPullParserException;
satoke7c6998e2011-06-03 17:57:59 +090041import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042
43import android.app.ActivityManagerNative;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090044import android.app.AppGlobals;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.app.AlertDialog;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +090046import android.app.AppOpsManager;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090047import android.app.IUserSwitchObserver;
satokf90a33e2011-07-19 11:55:52 +090048import android.app.KeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +090049import android.app.Notification;
50import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070051import android.app.PendingIntent;
satok5b927c432012-05-01 20:09:34 +090052import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.content.ComponentName;
54import android.content.ContentResolver;
55import android.content.Context;
56import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057import android.content.DialogInterface.OnCancelListener;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +090058import android.content.DialogInterface.OnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090060import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070062import android.content.pm.ApplicationInfo;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090063import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.content.pm.PackageManager;
65import android.content.pm.ResolveInfo;
66import android.content.pm.ServiceInfo;
Amith Yamasani734983f2014-03-04 16:48:05 -080067import android.content.pm.UserInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070068import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.content.res.Resources;
70import android.content.res.TypedArray;
71import android.database.ContentObserver;
Alan Viverette505e3ab2014-11-24 15:22:11 -080072import android.graphics.drawable.Drawable;
Joe Onorato857fd9b2011-01-27 15:08:35 -080073import android.inputmethodservice.InputMethodService;
Michael Wright7b5a96b2014-08-09 19:28:42 -070074import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.os.Binder;
Chris Wren1ce4b6d2015-06-11 10:19:43 -040076import android.os.Bundle;
satoke7c6998e2011-06-03 17:57:59 +090077import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078import android.os.Handler;
79import android.os.IBinder;
80import android.os.IInterface;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090081import android.os.IRemoteCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082import android.os.Message;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090083import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084import android.os.Parcel;
85import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080086import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087import android.os.ServiceManager;
88import android.os.SystemClock;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090089import android.os.UserHandle;
Amith Yamasani734983f2014-03-04 16:48:05 -080090import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091import android.provider.Settings;
92import android.text.TextUtils;
Christopher Tate7b9a28c2015-03-18 13:06:16 -070093import android.text.TextUtils.SimpleStringSplitter;
satokf9f01002011-05-19 21:31:50 +090094import android.text.style.SuggestionSpan;
Christopher Tate7b9a28c2015-03-18 13:06:16 -070095import android.util.ArrayMap;
96import android.util.ArraySet;
Dianne Hackborn39606a02012-07-31 17:54:35 -070097import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +090099import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +0900100import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101import android.util.PrintWriterPrinter;
102import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +0900103import android.util.Slog;
104import android.util.Xml;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +0900105import android.view.ContextThemeWrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106import android.view.IWindowManager;
Jeff Brownc28867a2013-03-26 15:42:39 -0700107import android.view.InputChannel;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900108import android.view.LayoutInflater;
109import android.view.View;
110import android.view.ViewGroup;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111import android.view.WindowManager;
satokab751aa2010-09-14 19:17:36 +0900112import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113import android.view.inputmethod.InputBinding;
114import android.view.inputmethod.InputMethod;
115import android.view.inputmethod.InputMethodInfo;
116import android.view.inputmethod.InputMethodManager;
satokab751aa2010-09-14 19:17:36 +0900117import android.view.inputmethod.InputMethodSubtype;
Yohei Yukawa443c2ba2014-09-10 14:10:30 +0900118import android.view.inputmethod.InputMethodSubtype.InputMethodSubtypeBuilder;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900119import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +0900120import android.widget.CompoundButton;
121import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900122import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +0900123import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900124import android.widget.TextView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125
satoke7c6998e2011-06-03 17:57:59 +0900126import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900128import java.io.FileInputStream;
129import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130import java.io.IOException;
131import java.io.PrintWriter;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100132import java.nio.charset.StandardCharsets;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900134import java.util.Collections;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135import java.util.HashMap;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700136import java.util.HashSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137import java.util.List;
satok5b927c432012-05-01 20:09:34 +0900138import java.util.Locale;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139
140/**
141 * This class provides a system service that manages input methods.
142 */
143public class InputMethodManagerService extends IInputMethodManager.Stub
144 implements ServiceConnection, Handler.Callback {
145 static final boolean DEBUG = false;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700146 static final boolean DEBUG_RESTORE = DEBUG || false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700147 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700149 private static final char INPUT_METHOD_SEPARATOR = ':';
150 private static final char INPUT_METHOD_SUBTYPE_SEPARATOR = ';';
151
Seigo Nonakad4474cb2015-05-04 16:53:24 -0700152 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 1;
153 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 2;
154 static final int MSG_SHOW_IM_CONFIG = 3;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800155
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 static final int MSG_UNBIND_INPUT = 1000;
157 static final int MSG_BIND_INPUT = 1010;
158 static final int MSG_SHOW_SOFT_INPUT = 1020;
159 static final int MSG_HIDE_SOFT_INPUT = 1030;
160 static final int MSG_ATTACH_TOKEN = 1040;
161 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 static final int MSG_START_INPUT = 2000;
164 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 static final int MSG_UNBIND_METHOD = 3000;
167 static final int MSG_BIND_METHOD = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700168 static final int MSG_SET_ACTIVE = 3020;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900169 static final int MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER = 3040;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800170
satok01038492012-04-09 21:08:27 +0900171 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
172
Satoshi Kataokabcacc322013-10-21 17:57:27 -0700173 static final long TIME_TO_RECONNECT = 3 * 1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800174
satokf9f01002011-05-19 21:31:50 +0900175 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
176
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900177 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
satokb6359412011-06-28 17:47:41 +0900178 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900179
satok4e4569d2010-11-19 18:45:53 +0900180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800182 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900184 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 final IWindowManager mIWindowManager;
187 final HandlerCaller mCaller;
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700188 final boolean mHasFeature;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900189 private InputMethodFileManager mFileManager;
satok01038492012-04-09 21:08:27 +0900190 private final HardKeyboardListener mHardKeyboardListener;
191 private final WindowManagerService mWindowManagerService;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900192 private final AppOpsManager mAppOpsManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800193
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900194 final InputBindResult mNoBinding = new InputBindResult(null, null, null, -1, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 // All known input methods. mMethodMap also serves as the global
197 // lock for this class.
satokd87c2592010-09-29 11:52:06 +0900198 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<InputMethodInfo>();
199 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<String, InputMethodInfo>();
satokf9f01002011-05-19 21:31:50 +0900200 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
201 new LruCache<SuggestionSpan, InputMethodInfo>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Satoshi Kataokad787f692013-10-26 04:44:21 +0900202 private final InputMethodSubtypeSwitchingController mSwitchingController;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800203
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700204 // Used to bring IME service up to visible adjustment while it is being shown.
205 final ServiceConnection mVisibleConnection = new ServiceConnection() {
206 @Override public void onServiceConnected(ComponentName name, IBinder service) {
207 }
208
209 @Override public void onServiceDisconnected(ComponentName name) {
210 }
211 };
212 boolean mVisibleBound = false;
213
satok7cfc0ed2011-06-20 21:29:36 +0900214 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700215 private NotificationManager mNotificationManager;
216 private KeyguardManager mKeyguardManager;
217 private StatusBarManagerService mStatusBar;
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400218 private Notification.Builder mImeSwitcherNotification;
Dianne Hackborn661cd522011-08-22 00:26:20 -0700219 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900220 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900221 private boolean mNotificationShown;
satok0a1bcf42012-05-16 19:26:31 +0900222 private final boolean mImeSelectedOnBoot;
satok7cfc0ed2011-06-20 21:29:36 +0900223
Tadashi G. Takaoka8c6d4772014-08-05 15:29:17 +0900224 static class SessionState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 final ClientState client;
226 final IInputMethod method;
Jeff Brownc28867a2013-03-26 15:42:39 -0700227
228 IInputMethodSession session;
229 InputChannel channel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 @Override
232 public String toString() {
233 return "SessionState{uid " + client.uid + " pid " + client.pid
234 + " method " + Integer.toHexString(
235 System.identityHashCode(method))
236 + " session " + Integer.toHexString(
237 System.identityHashCode(session))
Jeff Brownc28867a2013-03-26 15:42:39 -0700238 + " channel " + channel
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 + "}";
240 }
241
242 SessionState(ClientState _client, IInputMethod _method,
Jeff Brownc28867a2013-03-26 15:42:39 -0700243 IInputMethodSession _session, InputChannel _channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244 client = _client;
245 method = _method;
246 session = _session;
Jeff Brownc28867a2013-03-26 15:42:39 -0700247 channel = _channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 }
249 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800250
Jeff Brownc28867a2013-03-26 15:42:39 -0700251 static final class ClientState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 final IInputMethodClient client;
253 final IInputContext inputContext;
254 final int uid;
255 final int pid;
256 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 boolean sessionRequested;
259 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 @Override
262 public String toString() {
263 return "ClientState{" + Integer.toHexString(
264 System.identityHashCode(this)) + " uid " + uid
265 + " pid " + pid + "}";
266 }
267
268 ClientState(IInputMethodClient _client, IInputContext _inputContext,
269 int _uid, int _pid) {
270 client = _client;
271 inputContext = _inputContext;
272 uid = _uid;
273 pid = _pid;
274 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
275 }
276 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 final HashMap<IBinder, ClientState> mClients
279 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700282 * Set once the system is ready to run third party code.
283 */
284 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800285
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700286 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287 * Id of the currently selected input method.
288 */
289 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291 /**
292 * The current binding sequence number, incremented every time there is
293 * a new bind performed.
294 */
295 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297 /**
298 * The client that is currently bound to an input method.
299 */
300 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700303 * The last window token that gained focus.
304 */
305 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800306
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700307 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308 * The input context last provided by the current client.
309 */
310 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 /**
313 * The attributes last provided by the current client.
314 */
315 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800316
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 /**
318 * The input method ID of the input method service that we are currently
319 * connected to or in the process of connecting to.
320 */
321 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 /**
satokab751aa2010-09-14 19:17:36 +0900324 * The current subtype of the current input method.
325 */
326 private InputMethodSubtype mCurrentSubtype;
327
satok4e4569d2010-11-19 18:45:53 +0900328 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900329 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
330 mShortcutInputMethodsAndSubtypes =
331 new HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>();
satokab751aa2010-09-14 19:17:36 +0900332
John Spurlocke0980502013-10-25 11:59:29 -0400333 // Was the keyguard locked when this client became current?
334 private boolean mCurClientInKeyguard;
335
satokab751aa2010-09-14 19:17:36 +0900336 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 * Set to true if our ServiceConnection is currently actively bound to
338 * a service (whether or not we have gotten its IBinder back yet).
339 */
340 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800341
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 /**
343 * Set if the client has asked for the input method to be shown.
344 */
345 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 /**
348 * Set if we were explicitly told to show the input method.
349 */
350 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800351
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 /**
353 * Set if we were forced to be shown.
354 */
355 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800356
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 /**
358 * Set if we last told the input method to show itself.
359 */
360 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 /**
363 * The Intent used to connect to the current input method.
364 */
365 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800366
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367 /**
368 * The token we have made for the currently active input method, to
369 * identify it in the future.
370 */
371 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 /**
374 * If non-null, this is the input method service we are currently connected
375 * to.
376 */
377 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 /**
380 * Time that we last initiated a bind to the input method, to determine
381 * if we should try to disconnect and reconnect to it.
382 */
383 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800384
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385 /**
386 * Have we called mCurMethod.bindInput()?
387 */
388 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 /**
391 * Currently enabled session. Only touched by service thread, not
392 * protected by a lock.
393 */
394 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 /**
397 * True if the screen is on. The value is true initially.
398 */
399 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800400
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900401 int mCurUserActionNotificationSequenceNumber = 0;
402
Joe Onorato857fd9b2011-01-27 15:08:35 -0800403 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900404
405 /**
406 * A set of status bits regarding the active IME.
407 *
408 * <p>This value is a combination of following two bits:</p>
409 * <dl>
410 * <dt>{@link InputMethodService#IME_ACTIVE}</dt>
411 * <dd>
412 * If this bit is ON, connected IME is ready to accept touch/key events.
413 * </dd>
414 * <dt>{@link InputMethodService#IME_VISIBLE}</dt>
415 * <dd>
416 * If this bit is ON, some of IME view, e.g. software input, candidate view, is visible.
417 * </dd>
418 * </dl>
419 * <em>Do not update this value outside of setImeWindowStatus.</em>
420 */
Joe Onorato857fd9b2011-01-27 15:08:35 -0800421 int mImeWindowVis;
422
Ken Wakasa05dbb652011-08-22 15:22:43 +0900423 private AlertDialog.Builder mDialogBuilder;
424 private AlertDialog mSwitchingDialog;
satok01038492012-04-09 21:08:27 +0900425 private View mSwitchingDialogTitleView;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900426 private InputMethodInfo[] mIms;
427 private int[] mSubtypeIds;
satok5b927c432012-05-01 20:09:34 +0900428 private Locale mLastSystemLocale;
Michael Wright7b5a96b2014-08-09 19:28:42 -0700429 private boolean mShowImeWithHardKeyboard;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900430 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
431 private final IPackageManager mIPackageManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 class SettingsObserver extends ContentObserver {
Yohei Yukawa81482972015-06-04 00:58:59 -0700434 int mUserId;
435 boolean mRegistered = false;
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800436 String mLastEnabled = "";
437
Yohei Yukawa81482972015-06-04 00:58:59 -0700438 /**
439 * <em>This constructor must be called within the lock.</em>
440 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441 SettingsObserver(Handler handler) {
442 super(handler);
Yohei Yukawa81482972015-06-04 00:58:59 -0700443 }
444
445 public void registerContentObserverLocked(int userId) {
446 if (mRegistered && mUserId == userId) {
447 return;
448 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 ContentResolver resolver = mContext.getContentResolver();
Yohei Yukawa81482972015-06-04 00:58:59 -0700450 if (mRegistered) {
451 mContext.getContentResolver().unregisterContentObserver(this);
452 mRegistered = false;
453 }
454 if (mUserId != userId) {
455 mLastEnabled = "";
456 mUserId = userId;
457 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700459 Settings.Secure.DEFAULT_INPUT_METHOD), false, this, userId);
satokab751aa2010-09-14 19:17:36 +0900460 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700461 Settings.Secure.ENABLED_INPUT_METHODS), false, this, userId);
satokb6109bb2011-02-03 22:24:54 +0900462 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700463 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this, userId);
Michael Wright7b5a96b2014-08-09 19:28:42 -0700464 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700465 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD), false, this, userId);
466 mRegistered = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800468
Michael Wright7b5a96b2014-08-09 19:28:42 -0700469 @Override public void onChange(boolean selfChange, Uri uri) {
470 final Uri showImeUri =
471 Settings.Secure.getUriFor(Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 synchronized (mMethodMap) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700473 if (showImeUri.equals(uri)) {
474 updateKeyboardFromSettingsLocked();
475 } else {
476 boolean enabledChanged = false;
477 String newEnabled = mSettings.getEnabledInputMethodsStr();
478 if (!mLastEnabled.equals(newEnabled)) {
479 mLastEnabled = newEnabled;
480 enabledChanged = true;
481 }
482 updateInputMethodsFromSettingsLocked(enabledChanged);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800483 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800484 }
485 }
Yohei Yukawa81482972015-06-04 00:58:59 -0700486
487 @Override
488 public String toString() {
489 return "SettingsObserver{mUserId=" + mUserId + " mRegistered=" + mRegistered
490 + " mLastEnabled=" + mLastEnabled + "}";
491 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800493
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900494 class ImmsBroadcastReceiver extends android.content.BroadcastReceiver {
495 private void updateActive() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496 // Inform the current client of the change in active status
Dianne Hackborna6e41342012-05-22 16:30:34 -0700497 if (mCurClient != null && mCurClient.client != null) {
498 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
499 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 }
501 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900502
503 @Override
504 public void onReceive(Context context, Intent intent) {
505 final String action = intent.getAction();
506 if (Intent.ACTION_SCREEN_ON.equals(action)) {
507 mScreenOn = true;
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900508 updateSystemUi(mCurToken, mImeWindowVis, mBackDisposition);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900509 updateActive();
510 return;
511 } else if (Intent.ACTION_SCREEN_OFF.equals(action)) {
512 mScreenOn = false;
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900513 updateSystemUi(mCurToken, 0 /* vis */, mBackDisposition);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900514 updateActive();
515 return;
516 } else if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
517 hideInputMethodMenu();
518 // No need to updateActive
519 return;
Amith Yamasani734983f2014-03-04 16:48:05 -0800520 } else if (Intent.ACTION_USER_ADDED.equals(action)
521 || Intent.ACTION_USER_REMOVED.equals(action)) {
Kenny Guy2a764942014-04-02 13:29:20 +0100522 updateCurrentProfileIds();
Amith Yamasani734983f2014-03-04 16:48:05 -0800523 return;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700524 } else if (Intent.ACTION_SETTING_RESTORED.equals(action)) {
525 final String name = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
526 if (Settings.Secure.ENABLED_INPUT_METHODS.equals(name)) {
527 final String prevValue = intent.getStringExtra(
528 Intent.EXTRA_SETTING_PREVIOUS_VALUE);
529 final String newValue = intent.getStringExtra(
530 Intent.EXTRA_SETTING_NEW_VALUE);
531 restoreEnabledInputMethods(mContext, prevValue, newValue);
532 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900533 } else {
534 Slog.w(TAG, "Unexpected intent " + intent);
535 }
536 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800537 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800538
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700539 // Apply the results of a restore operation to the set of enabled IMEs. Note that this
540 // does not attempt to validate on the fly with any installed device policy, so must only
541 // be run in the context of initial device setup.
542 //
543 // TODO: Move this method to InputMethodUtils with adding unit tests.
544 static void restoreEnabledInputMethods(Context context, String prevValue, String newValue) {
545 if (DEBUG_RESTORE) {
546 Slog.i(TAG, "Restoring enabled input methods:");
547 Slog.i(TAG, "prev=" + prevValue);
548 Slog.i(TAG, " new=" + newValue);
549 }
550 // 'new' is the just-restored state, 'prev' is what was in settings prior to the restore
551 ArrayMap<String, ArraySet<String>> prevMap = parseInputMethodsAndSubtypesString(prevValue);
552 ArrayMap<String, ArraySet<String>> newMap = parseInputMethodsAndSubtypesString(newValue);
553
554 // Merge the restored ime+subtype enabled states into the live state
555 for (ArrayMap.Entry<String, ArraySet<String>> entry : newMap.entrySet()) {
556 final String imeId = entry.getKey();
557 ArraySet<String> prevSubtypes = prevMap.get(imeId);
558 if (prevSubtypes == null) {
559 prevSubtypes = new ArraySet<String>(2);
560 prevMap.put(imeId, prevSubtypes);
561 }
562 prevSubtypes.addAll(entry.getValue());
563 }
564
565 final String mergedImesAndSubtypesString = buildInputMethodsAndSubtypesString(prevMap);
566 if (DEBUG_RESTORE) {
567 Slog.i(TAG, "Merged IME string:");
568 Slog.i(TAG, " " + mergedImesAndSubtypesString);
569 }
570 Settings.Secure.putString(context.getContentResolver(),
571 Settings.Secure.ENABLED_INPUT_METHODS, mergedImesAndSubtypesString);
572 }
573
574 // TODO: Move this method to InputMethodUtils with adding unit tests.
575 static String buildInputMethodsAndSubtypesString(ArrayMap<String, ArraySet<String>> map) {
576 // we want to use the canonical InputMethodSettings implementation,
577 // so we convert data structures first.
578 List<Pair<String, ArrayList<String>>> imeMap =
579 new ArrayList<Pair<String, ArrayList<String>>>(4);
580 for (ArrayMap.Entry<String, ArraySet<String>> entry : map.entrySet()) {
581 final String imeName = entry.getKey();
582 final ArraySet<String> subtypeSet = entry.getValue();
583 final ArrayList<String> subtypes = new ArrayList<String>(2);
584 if (subtypeSet != null) {
585 subtypes.addAll(subtypeSet);
586 }
587 imeMap.add(new Pair<String, ArrayList<String>>(imeName, subtypes));
588 }
589 return InputMethodSettings.buildInputMethodsSettingString(imeMap);
590 }
591
592 // TODO: Move this method to InputMethodUtils with adding unit tests.
593 static ArrayMap<String, ArraySet<String>> parseInputMethodsAndSubtypesString(
594 final String inputMethodsAndSubtypesString) {
595 final ArrayMap<String, ArraySet<String>> imeMap =
596 new ArrayMap<String, ArraySet<String>>();
597 if (TextUtils.isEmpty(inputMethodsAndSubtypesString)) {
598 return imeMap;
599 }
600
601 final SimpleStringSplitter typeSplitter =
602 new SimpleStringSplitter(INPUT_METHOD_SEPARATOR);
603 final SimpleStringSplitter subtypeSplitter =
604 new SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATOR);
605 List<Pair<String, ArrayList<String>>> allImeSettings =
606 InputMethodSettings.buildInputMethodsAndSubtypeList(inputMethodsAndSubtypesString,
607 typeSplitter,
608 subtypeSplitter);
609 for (Pair<String, ArrayList<String>> ime : allImeSettings) {
610 ArraySet<String> subtypes = new ArraySet<String>();
611 if (ime.second != null) {
612 subtypes.addAll(ime.second);
613 }
614 imeMap.put(ime.first, subtypes);
615 }
616 return imeMap;
617 }
618
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800619 class MyPackageMonitor extends PackageMonitor {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900620 private boolean isChangingPackagesOfCurrentUser() {
621 final int userId = getChangingUserId();
622 final boolean retval = userId == mSettings.getCurrentUserId();
623 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900624 if (!retval) {
625 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
626 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900627 }
628 return retval;
629 }
630
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800632 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900633 if (!isChangingPackagesOfCurrentUser()) {
634 return false;
635 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900637 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 final int N = mMethodList.size();
639 if (curInputMethodId != null) {
640 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800641 InputMethodInfo imi = mMethodList.get(i);
642 if (imi.getId().equals(curInputMethodId)) {
643 for (String pkg : packages) {
644 if (imi.getPackageName().equals(pkg)) {
645 if (!doit) {
646 return true;
647 }
satok723a27e2010-11-11 14:58:11 +0900648 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800649 chooseNewDefaultIMELocked();
650 return true;
651 }
652 }
653 }
654 }
655 }
656 }
657 return false;
658 }
659
660 @Override
661 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900662 if (!isChangingPackagesOfCurrentUser()) {
663 return;
664 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800665 synchronized (mMethodMap) {
666 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900667 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800668 final int N = mMethodList.size();
669 if (curInputMethodId != null) {
670 for (int i=0; i<N; i++) {
671 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900672 final String imiId = imi.getId();
673 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800674 curIm = imi;
675 }
satoke7c6998e2011-06-03 17:57:59 +0900676
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800677 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900678 if (isPackageModified(imi.getPackageName())) {
679 mFileManager.deleteAllInputMethodSubtypes(imiId);
680 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800681 if (change == PACKAGE_TEMPORARY_CHANGE
682 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800683 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800684 + imi.getComponent());
685 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800686 }
687 }
688 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800689
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900690 buildInputMethodListLocked(
691 mMethodList, mMethodMap, false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800692
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800694
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800695 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800696 int change = isPackageDisappearing(curIm.getPackageName());
697 if (change == PACKAGE_TEMPORARY_CHANGE
698 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800699 ServiceInfo si = null;
700 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900701 si = mIPackageManager.getServiceInfo(
702 curIm.getComponent(), 0, mSettings.getCurrentUserId());
703 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800704 }
705 if (si == null) {
706 // Uh oh, current input method is no longer around!
707 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800708 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900709 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800710 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800711 changed = true;
712 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800713 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900714 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800715 }
716 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800717 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800718 }
satokab751aa2010-09-14 19:17:36 +0900719
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800720 if (curIm == null) {
721 // We currently don't have a default input method... is
722 // one now available?
723 changed = chooseNewDefaultIMELocked();
Yohei Yukawa54d512c2015-05-19 22:15:02 -0700724 } else if (!changed && isPackageModified(curIm.getPackageName())) {
725 // Even if the current input method is still available, mCurrentSubtype could
726 // be obsolete when the package is modified in practice.
727 changed = true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800728 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800729
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800730 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800731 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800732 }
733 }
734 }
735 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800736
Jeff Brownc28867a2013-03-26 15:42:39 -0700737 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900738 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -0700739 private final IInputMethod mMethod;
740 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800741
Jeff Brownc28867a2013-03-26 15:42:39 -0700742 MethodCallback(InputMethodManagerService imms, IInputMethod method,
743 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900744 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -0700745 mMethod = method;
746 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800747 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800748
satoke7c6998e2011-06-03 17:57:59 +0900749 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -0700750 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -0700751 long ident = Binder.clearCallingIdentity();
752 try {
753 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
754 } finally {
755 Binder.restoreCallingIdentity(ident);
756 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800757 }
758 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800759
satok01038492012-04-09 21:08:27 +0900760 private class HardKeyboardListener
761 implements WindowManagerService.OnHardKeyboardStatusChangeListener {
762 @Override
Michael Wright7b5a96b2014-08-09 19:28:42 -0700763 public void onHardKeyboardStatusChange(boolean available) {
764 mHandler.sendMessage(mHandler.obtainMessage(MSG_HARD_KEYBOARD_SWITCH_CHANGED,
765 available ? 1 : 0));
satok01038492012-04-09 21:08:27 +0900766 }
767
Michael Wright7b5a96b2014-08-09 19:28:42 -0700768 public void handleHardKeyboardStatusChange(boolean available) {
satok01038492012-04-09 21:08:27 +0900769 if (DEBUG) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700770 Slog.w(TAG, "HardKeyboardStatusChanged: available=" + available);
satok01038492012-04-09 21:08:27 +0900771 }
772 synchronized(mMethodMap) {
773 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
774 && mSwitchingDialog.isShowing()) {
775 mSwitchingDialogTitleView.findViewById(
776 com.android.internal.R.id.hard_keyboard_section).setVisibility(
777 available ? View.VISIBLE : View.GONE);
778 }
779 }
780 }
781 }
782
783 public InputMethodManagerService(Context context, WindowManagerService windowManager) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900784 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800785 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800786 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800787 mHandler = new Handler(this);
Yohei Yukawa81482972015-06-04 00:58:59 -0700788 // Note: SettingsObserver doesn't register observers in its constructor.
789 mSettingsObserver = new SettingsObserver(mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800790 mIWindowManager = IWindowManager.Stub.asInterface(
791 ServiceManager.getService(Context.WINDOW_SERVICE));
Mita Yuned218c72012-12-06 17:18:25 -0800792 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900793 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800794 public void executeMessage(Message msg) {
795 handleMessage(msg);
796 }
Mita Yuned218c72012-12-06 17:18:25 -0800797 }, true /*asyncHandler*/);
satok01038492012-04-09 21:08:27 +0900798 mWindowManagerService = windowManager;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900799 mAppOpsManager = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE);
satok01038492012-04-09 21:08:27 +0900800 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700801 mHasFeature = context.getPackageManager().hasSystemFeature(
802 PackageManager.FEATURE_INPUT_METHODS);
satok7cfc0ed2011-06-20 21:29:36 +0900803
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400804 Bundle extras = new Bundle();
805 extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
806 mImeSwitcherNotification = new Notification.Builder(mContext)
807 .setSmallIcon(com.android.internal.R.drawable.ic_notification_ime_default)
808 .setWhen(0)
809 .setOngoing(true)
810 .addExtras(extras)
811 .setCategory(Notification.CATEGORY_SYSTEM)
812 .setColor(com.android.internal.R.color.system_notification_accent_color);
Daniel Sandler590d5152012-06-14 16:10:13 -0400813
satok7cfc0ed2011-06-20 21:29:36 +0900814 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900815 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900816
817 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900818
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900819 final IntentFilter broadcastFilter = new IntentFilter();
820 broadcastFilter.addAction(Intent.ACTION_SCREEN_ON);
821 broadcastFilter.addAction(Intent.ACTION_SCREEN_OFF);
822 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Amith Yamasani734983f2014-03-04 16:48:05 -0800823 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
824 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700825 broadcastFilter.addAction(Intent.ACTION_SETTING_RESTORED);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900826 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800827
satok7cfc0ed2011-06-20 21:29:36 +0900828 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900829 int userId = 0;
830 try {
831 ActivityManagerNative.getDefault().registerUserSwitchObserver(
832 new IUserSwitchObserver.Stub() {
833 @Override
834 public void onUserSwitching(int newUserId, IRemoteCallback reply) {
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900835 synchronized(mMethodMap) {
836 switchUserLocked(newUserId);
837 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900838 if (reply != null) {
839 try {
840 reply.sendResult(null);
841 } catch (RemoteException e) {
842 }
843 }
844 }
845
846 @Override
847 public void onUserSwitchComplete(int newUserId) throws RemoteException {
848 }
Kenny Guy42979622015-04-13 18:03:05 +0000849
850 @Override
851 public void onForegroundProfileSwitch(int newProfileId) {
852 // Ignore.
853 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900854 });
855 userId = ActivityManagerNative.getDefault().getCurrentUser().id;
856 } catch (RemoteException e) {
857 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
858 }
satok81f8b7c2012-12-04 20:42:56 +0900859 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
satok913a8922010-08-26 21:53:41 +0900860
satokd87c2592010-09-29 11:52:06 +0900861 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900862 mSettings = new InputMethodSettings(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900863 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId);
Svet Ganovadc1cf42015-06-15 16:36:24 -0700864
865 // Let the package manager query which are the default imes
866 // as they get certain permissions granted by default.
867 PackageManagerInternal packageManagerInternal = LocalServices.getService(
868 PackageManagerInternal.class);
869 packageManagerInternal.setImePackagesProvider(
870 new PackageManagerInternal.PackagesProvider() {
871 @Override
872 public String[] getPackages(int userId) {
873 synchronized (mMethodMap) {
874 final int currentUserId = mSettings.getCurrentUserId();
875 // TODO: We are switching the current user id in the settings
876 // object to query it and then revert the user id. Ideally, we
877 // should call a API in settings with the user id as an argument.
878 mSettings.setCurrentUserId(userId);
879 List<InputMethodInfo> imes = mSettings
880 .getEnabledInputMethodListLocked();
881 String[] packageNames = null;
882 if (imes != null) {
883 final int imeCount = imes.size();
884 packageNames = new String[imeCount];
885 for (int i = 0; i < imeCount; i++) {
886 InputMethodInfo ime = imes.get(i);
887 packageNames[i] = ime.getPackageName();
888 }
889 }
890 mSettings.setCurrentUserId(currentUserId);
891 return packageNames;
892 }
893 }
894 });
895
Kenny Guy2a764942014-04-02 13:29:20 +0100896 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900897 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900898 synchronized (mMethodMap) {
899 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
900 mSettings, context);
901 }
satok0a1bcf42012-05-16 19:26:31 +0900902
903 // Just checking if defaultImiId is empty or not
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900904 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900905 if (DEBUG) {
906 Slog.d(TAG, "Initial default ime = " + defaultImiId);
907 }
satok0a1bcf42012-05-16 19:26:31 +0900908 mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
909
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900910 synchronized (mMethodMap) {
911 buildInputMethodListLocked(mMethodList, mMethodMap,
912 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
913 }
satokd87c2592010-09-29 11:52:06 +0900914 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800915
satok0a1bcf42012-05-16 19:26:31 +0900916 if (!mImeSelectedOnBoot) {
917 Slog.w(TAG, "No IME selected. Choose the most applicable IME.");
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900918 synchronized (mMethodMap) {
919 resetDefaultImeLocked(context);
920 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800921 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800922
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900923 synchronized (mMethodMap) {
Yohei Yukawa81482972015-06-04 00:58:59 -0700924 mSettingsObserver.registerContentObserverLocked(userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900925 updateFromSettingsLocked(true);
926 }
satok5b927c432012-05-01 20:09:34 +0900927
928 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME
929 // according to the new system locale.
930 final IntentFilter filter = new IntentFilter();
931 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
932 mContext.registerReceiver(
933 new BroadcastReceiver() {
934 @Override
935 public void onReceive(Context context, Intent intent) {
936 synchronized(mMethodMap) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900937 resetStateIfCurrentLocaleChangedLocked();
satok5b927c432012-05-01 20:09:34 +0900938 }
939 }
940 }, filter);
941 }
942
satok5b927c432012-05-01 20:09:34 +0900943 private void resetDefaultImeLocked(Context context) {
944 // Do not reset the default (current) IME when it is a 3rd-party IME
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900945 if (mCurMethodId != null
946 && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +0900947 return;
948 }
949
950 InputMethodInfo defIm = null;
951 for (InputMethodInfo imi : mMethodList) {
Yohei Yukawa6aa03782015-02-21 03:00:22 +0900952 if (defIm == null && mSystemReady) {
953 final Locale systemLocale = context.getResources().getConfiguration().locale;
954 if (InputMethodUtils.isSystemImeThatHasSubtypeOf(imi, context,
955 true /* checkDefaultAttribute */, systemLocale, false /* checkCountry */,
956 InputMethodUtils.SUBTYPE_MODE_ANY)) {
satok5b927c432012-05-01 20:09:34 +0900957 defIm = imi;
958 Slog.i(TAG, "Selected default: " + imi.getId());
959 }
960 }
961 }
962 if (defIm == null && mMethodList.size() > 0) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900963 defIm = InputMethodUtils.getMostApplicableDefaultIME(
964 mSettings.getEnabledInputMethodListLocked());
Hyejin Kim5baaaac2014-10-27 17:17:06 +0900965 if (defIm != null) {
966 Slog.i(TAG, "Default found, using " + defIm.getId());
967 } else {
968 Slog.i(TAG, "No default found");
969 }
satok5b927c432012-05-01 20:09:34 +0900970 }
971 if (defIm != null) {
972 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
973 }
974 }
975
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900976 private void resetAllInternalStateLocked(final boolean updateOnlyWhenLocaleChanged,
977 final boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900978 if (!mSystemReady) {
979 // not system ready
980 return;
981 }
982 final Locale newLocale = mRes.getConfiguration().locale;
983 if (!updateOnlyWhenLocaleChanged
984 || (newLocale != null && !newLocale.equals(mLastSystemLocale))) {
985 if (!updateOnlyWhenLocaleChanged) {
986 hideCurrentInputLocked(0, null);
987 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -0700988 unbindCurrentMethodLocked(true, false);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900989 }
990 if (DEBUG) {
991 Slog.i(TAG, "Locale has been changed to " + newLocale);
992 }
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900993 buildInputMethodListLocked(mMethodList, mMethodMap, resetDefaultEnabledIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900994 if (!updateOnlyWhenLocaleChanged) {
995 final String selectedImiId = mSettings.getSelectedInputMethod();
996 if (TextUtils.isEmpty(selectedImiId)) {
997 // This is the first time of the user switch and
998 // set the current ime to the proper one.
999 resetDefaultImeLocked(mContext);
1000 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +09001001 } else {
1002 // If the locale is changed, needs to reset the default ime
1003 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001004 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001005 updateFromSettingsLocked(true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001006 mLastSystemLocale = newLocale;
1007 if (!updateOnlyWhenLocaleChanged) {
1008 try {
1009 startInputInnerLocked();
1010 } catch (RuntimeException e) {
1011 Slog.w(TAG, "Unexpected exception", e);
1012 }
1013 }
1014 }
1015 }
1016
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001017 private void resetStateIfCurrentLocaleChangedLocked() {
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001018 resetAllInternalStateLocked(true /* updateOnlyWhenLocaleChanged */,
1019 true /* resetDefaultImeLocked */);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001020 }
1021
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001022 private void switchUserLocked(int newUserId) {
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001023 if (DEBUG) Slog.d(TAG, "Switching user stage 1/3. newUserId=" + newUserId
1024 + " currentUserId=" + mSettings.getCurrentUserId());
1025
Yohei Yukawa81482972015-06-04 00:58:59 -07001026 // ContentObserver should be registered again when the user is changed
1027 mSettingsObserver.registerContentObserverLocked(newUserId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001028 mSettings.setCurrentUserId(newUserId);
Kenny Guy2a764942014-04-02 13:29:20 +01001029 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001030 // InputMethodFileManager should be reset when the user is changed
1031 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001032 final String defaultImiId = mSettings.getSelectedInputMethod();
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001033
1034 if (DEBUG) Slog.d(TAG, "Switching user stage 2/3. newUserId=" + newUserId
1035 + " defaultImiId=" + defaultImiId);
1036
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +09001037 // For secondary users, the list of enabled IMEs may not have been updated since the
1038 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
1039 // not be empty even if the IME has been uninstalled by the primary user.
1040 // Even in such cases, IMMS works fine because it will find the most applicable
1041 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001042 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001043 resetAllInternalStateLocked(false /* updateOnlyWhenLocaleChanged */,
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001044 initialUserSwitch /* needsToResetDefaultIme */);
1045 if (initialUserSwitch) {
1046 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mContext.getPackageManager(),
1047 mSettings.getEnabledInputMethodListLocked());
1048 }
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001049
1050 if (DEBUG) Slog.d(TAG, "Switching user stage 3/3. newUserId=" + newUserId
1051 + " selectedIme=" + mSettings.getSelectedInputMethod());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001052 }
1053
Kenny Guy2a764942014-04-02 13:29:20 +01001054 void updateCurrentProfileIds() {
1055 List<UserInfo> profiles =
1056 UserManager.get(mContext).getProfiles(mSettings.getCurrentUserId());
1057 int[] currentProfileIds = new int[profiles.size()]; // profiles will not be null
1058 for (int i = 0; i < currentProfileIds.length; i++) {
1059 currentProfileIds[i] = profiles.get(i).id;
Amith Yamasani734983f2014-03-04 16:48:05 -08001060 }
Kenny Guy2a764942014-04-02 13:29:20 +01001061 mSettings.setCurrentProfileIds(currentProfileIds);
Amith Yamasani734983f2014-03-04 16:48:05 -08001062 }
1063
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064 @Override
1065 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
1066 throws RemoteException {
1067 try {
1068 return super.onTransact(code, data, reply, flags);
1069 } catch (RuntimeException e) {
1070 // The input method manager only throws security exceptions, so let's
1071 // log all others.
1072 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -07001073 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001074 }
1075 throw e;
1076 }
1077 }
1078
Svetoslav Ganova0027152013-06-25 14:59:53 -07001079 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001080 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001081 if (DEBUG) {
1082 Slog.d(TAG, "--- systemReady");
1083 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001084 if (!mSystemReady) {
1085 mSystemReady = true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001086 mKeyguardManager =
1087 (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001088 mNotificationManager = (NotificationManager)
1089 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
1090 mStatusBar = statusBar;
1091 statusBar.setIconVisibility("ime", false);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001092 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokb858c732011-07-22 19:54:34 +09001093 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
1094 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +09001095 if (mShowOngoingImeSwitcherForPhones) {
1096 mWindowManagerService.setOnHardKeyboardStatusChangeListener(
1097 mHardKeyboardListener);
1098 }
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09001099 buildInputMethodListLocked(mMethodList, mMethodMap,
1100 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satok0a1bcf42012-05-16 19:26:31 +09001101 if (!mImeSelectedOnBoot) {
1102 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001103 resetStateIfCurrentLocaleChangedLocked();
1104 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(
1105 mContext.getPackageManager(),
1106 mSettings.getEnabledInputMethodListLocked());
satok0a1bcf42012-05-16 19:26:31 +09001107 }
1108 mLastSystemLocale = mRes.getConfiguration().locale;
Dianne Hackborncc278702009-09-02 23:07:23 -07001109 try {
1110 startInputInnerLocked();
1111 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001112 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -07001113 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001114 }
1115 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001116 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001117
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001118 // ---------------------------------------------------------------------------------------
1119 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
1120 // 1) it comes from the system process
1121 // 2) the calling process' user id is identical to the current user id IMMS thinks.
1122 private boolean calledFromValidUser() {
1123 final int uid = Binder.getCallingUid();
1124 final int userId = UserHandle.getUserId(uid);
1125 if (DEBUG) {
1126 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
1127 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
1128 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +09001129 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
1130 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001131 }
Kenny Guy2a764942014-04-02 13:29:20 +01001132 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001133 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001134 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001135
1136 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
1137 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
1138 // must not manage background users' states in any functions.
1139 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
1140 // by a token.
1141 if (mContext.checkCallingOrSelfPermission(
1142 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1143 == PackageManager.PERMISSION_GRANTED) {
1144 if (DEBUG) {
1145 Slog.d(TAG, "--- Access granted because the calling process has "
1146 + "the INTERACT_ACROSS_USERS_FULL permission");
1147 }
1148 return true;
1149 }
Satoshi Kataoka0766eb02013-07-31 18:30:13 +09001150 Slog.w(TAG, "--- IPC called from background users. Ignore. \n"
1151 + InputMethodUtils.getStackTrace());
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001152 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001153 }
1154
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001155
1156 /**
1157 * Returns true iff the caller is identified to be the current input method with the token.
1158 * @param token The window token given to the input method when it was started.
1159 * @return true if and only if non-null valid token is specified.
1160 */
1161 private boolean calledWithValidToken(IBinder token) {
1162 if (token == null || mCurToken != token) {
1163 return false;
1164 }
1165 return true;
1166 }
1167
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001168 private boolean bindCurrentInputMethodService(
1169 Intent service, ServiceConnection conn, int flags) {
1170 if (service == null || conn == null) {
1171 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
1172 return false;
1173 }
Amith Yamasani27b89e62013-01-16 12:30:11 -08001174 return mContext.bindServiceAsUser(service, conn, flags,
1175 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001176 }
1177
satoke7c6998e2011-06-03 17:57:59 +09001178 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001179 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001180 // TODO: Make this work even for non-current users?
1181 if (!calledFromValidUser()) {
1182 return Collections.emptyList();
1183 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184 synchronized (mMethodMap) {
1185 return new ArrayList<InputMethodInfo>(mMethodList);
1186 }
1187 }
1188
satoke7c6998e2011-06-03 17:57:59 +09001189 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001190 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001191 // TODO: Make this work even for non-current users?
1192 if (!calledFromValidUser()) {
1193 return Collections.emptyList();
1194 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001195 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001196 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197 }
1198 }
1199
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001200 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001201 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001202 * @return enabled subtypes of the specified imi
1203 */
satoke7c6998e2011-06-03 17:57:59 +09001204 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001205 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001206 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001207 // TODO: Make this work even for non-current users?
1208 if (!calledFromValidUser()) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001209 return Collections.<InputMethodSubtype>emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001210 }
satok67ddf9c2010-11-17 09:45:54 +09001211 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001212 final InputMethodInfo imi;
1213 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001214 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001215 } else {
1216 imi = mMethodMap.get(imiId);
1217 }
1218 if (imi == null) {
1219 return Collections.<InputMethodSubtype>emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001220 }
1221 return mSettings.getEnabledInputMethodSubtypeListLocked(
1222 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001223 }
1224 }
1225
satoke7c6998e2011-06-03 17:57:59 +09001226 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 public void addClient(IInputMethodClient client,
1228 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001229 if (!calledFromValidUser()) {
1230 return;
1231 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001232 synchronized (mMethodMap) {
1233 mClients.put(client.asBinder(), new ClientState(client,
1234 inputContext, uid, pid));
1235 }
1236 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001237
satoke7c6998e2011-06-03 17:57:59 +09001238 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001239 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001240 if (!calledFromValidUser()) {
1241 return;
1242 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001244 ClientState cs = mClients.remove(client.asBinder());
1245 if (cs != null) {
1246 clearClientSessionLocked(cs);
1247 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001248 }
1249 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001251 void executeOrSendMessage(IInterface target, Message msg) {
1252 if (target.asBinder() instanceof Binder) {
1253 mCaller.sendMessage(msg);
1254 } else {
1255 handleMessage(msg);
1256 msg.recycle();
1257 }
1258 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001259
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001260 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001261 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001262 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 + mCurClient.client.asBinder());
1264 if (mBoundToMethod) {
1265 mBoundToMethod = false;
1266 if (mCurMethod != null) {
1267 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1268 MSG_UNBIND_INPUT, mCurMethod));
1269 }
1270 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001271
1272 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1273 MSG_SET_ACTIVE, 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001274 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1275 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1276 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001278
The Android Open Source Project10592532009-03-18 17:39:46 -07001279 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001280 }
1281 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001283 private int getImeShowFlags() {
1284 int flags = 0;
1285 if (mShowForced) {
1286 flags |= InputMethod.SHOW_FORCED
1287 | InputMethod.SHOW_EXPLICIT;
1288 } else if (mShowExplicitlyRequested) {
1289 flags |= InputMethod.SHOW_EXPLICIT;
1290 }
1291 return flags;
1292 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001293
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294 private int getAppShowFlags() {
1295 int flags = 0;
1296 if (mShowForced) {
1297 flags |= InputMethodManager.SHOW_FORCED;
1298 } else if (!mShowExplicitlyRequested) {
1299 flags |= InputMethodManager.SHOW_IMPLICIT;
1300 }
1301 return flags;
1302 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001303
Dianne Hackborn7663d802012-02-24 13:08:49 -08001304 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001305 if (!mBoundToMethod) {
1306 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1307 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1308 mBoundToMethod = true;
1309 }
1310 final SessionState session = mCurClient.curSession;
1311 if (initial) {
1312 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1313 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
1314 } else {
1315 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1316 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
1317 }
1318 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001319 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001320 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001321 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001322 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001323 (session.channel != null ? session.channel.dup() : null),
1324 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001325 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001327 InputBindResult startInputLocked(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001328 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001329 // If no method is currently selected, do nothing.
1330 if (mCurMethodId == null) {
1331 return mNoBinding;
1332 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001333
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001334 ClientState cs = mClients.get(client.asBinder());
1335 if (cs == null) {
1336 throw new IllegalArgumentException("unknown client "
1337 + client.asBinder());
1338 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001340 try {
1341 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1342 // Check with the window manager to make sure this client actually
1343 // has a window with focus. If not, reject. This is thread safe
1344 // because if the focus changes some time before or after, the
1345 // next client receiving focus that has any interest in input will
1346 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001347 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001348 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1349 return null;
1350 }
1351 } catch (RemoteException e) {
1352 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001353
Dianne Hackborn7663d802012-02-24 13:08:49 -08001354 return startInputUncheckedLocked(cs, inputContext, attribute, controlFlags);
1355 }
1356
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001357 InputBindResult startInputUncheckedLocked(@NonNull ClientState cs,
Yohei Yukawad57ba672015-06-08 16:39:46 -07001358 IInputContext inputContext, @NonNull EditorInfo attribute, int controlFlags) {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001359 // If no method is currently selected, do nothing.
1360 if (mCurMethodId == null) {
1361 return mNoBinding;
1362 }
1363
Yohei Yukawad57ba672015-06-08 16:39:46 -07001364 if (!InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.uid,
1365 attribute.packageName)) {
1366 Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
1367 + " uid=" + cs.uid + " package=" + attribute.packageName);
1368 return mNoBinding;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001369 }
1370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001371 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001372 // Was the keyguard locked when switching over to the new client?
1373 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001374 // If the client is changing, we need to switch over to the new
1375 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001376 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001377 if (DEBUG) Slog.v(TAG, "switching to client: client = "
John Spurlocke0980502013-10-25 11:59:29 -04001378 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001379
1380 // If the screen is on, inform the new client it is active
1381 if (mScreenOn) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001382 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
1383 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001384 }
1385 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001386
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001387 // Bump up the sequence for this client and attach it.
1388 mCurSeq++;
1389 if (mCurSeq <= 0) mCurSeq = 1;
1390 mCurClient = cs;
1391 mCurInputContext = inputContext;
1392 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001394 // Check if the input method is changing.
1395 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1396 if (cs.curSession != null) {
1397 // Fast case: if we are already connected to the input method,
1398 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001399 return attachNewInputLocked(
1400 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001401 }
1402 if (mHaveConnection) {
1403 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001404 // Return to client, and we will get back with it when
1405 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001406 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001407 return new InputBindResult(null, null, mCurId, mCurSeq,
1408 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001409 } else if (SystemClock.uptimeMillis()
1410 < (mLastBindTime+TIME_TO_RECONNECT)) {
1411 // In this case we have connected to the service, but
1412 // don't yet have its interface. If it hasn't been too
1413 // long since we did the connection, we'll return to
1414 // the client and wait to get the service interface so
1415 // we can report back. If it has been too long, we want
1416 // to fall through so we can try a disconnect/reconnect
1417 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001418 return new InputBindResult(null, null, mCurId, mCurSeq,
1419 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001420 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001421 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1422 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001423 }
1424 }
1425 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001426
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001427 return startInputInnerLocked();
1428 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001429
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001430 InputBindResult startInputInnerLocked() {
1431 if (mCurMethodId == null) {
1432 return mNoBinding;
1433 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001434
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001435 if (!mSystemReady) {
1436 // If the system is not yet ready, we shouldn't be running third
1437 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001438 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1439 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001440 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001442 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1443 if (info == null) {
1444 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1445 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001446
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001447 unbindCurrentMethodLocked(false, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001448
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001449 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1450 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001451 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1452 com.android.internal.R.string.input_method_binding_label);
1453 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1454 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001455 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001456 | Context.BIND_NOT_VISIBLE | Context.BIND_NOT_FOREGROUND
1457 | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001458 mLastBindTime = SystemClock.uptimeMillis();
1459 mHaveConnection = true;
1460 mCurId = info.getId();
1461 mCurToken = new Binder();
1462 try {
Craig Mautnerca0ac712013-03-14 09:43:02 -07001463 if (true || DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001464 mIWindowManager.addWindowToken(mCurToken,
1465 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1466 } catch (RemoteException e) {
1467 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001468 return new InputBindResult(null, null, mCurId, mCurSeq,
1469 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001470 } else {
1471 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001472 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001473 + mCurIntent);
1474 }
1475 return null;
1476 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001477
satoke7c6998e2011-06-03 17:57:59 +09001478 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001479 public InputBindResult startInput(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001480 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001481 if (!calledFromValidUser()) {
1482 return null;
1483 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001484 synchronized (mMethodMap) {
1485 final long ident = Binder.clearCallingIdentity();
1486 try {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001487 return startInputLocked(client, inputContext, attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001488 } finally {
1489 Binder.restoreCallingIdentity(ident);
1490 }
1491 }
1492 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001493
satoke7c6998e2011-06-03 17:57:59 +09001494 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001495 public void finishInput(IInputMethodClient client) {
1496 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001497
satoke7c6998e2011-06-03 17:57:59 +09001498 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001499 public void onServiceConnected(ComponentName name, IBinder service) {
1500 synchronized (mMethodMap) {
1501 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1502 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001503 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001504 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001505 unbindCurrentMethodLocked(false, false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001506 return;
1507 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001508 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001509 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1510 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001511 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001512 clearClientSessionLocked(mCurClient);
1513 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001514 }
1515 }
1516 }
1517 }
1518
Jeff Brownc28867a2013-03-26 15:42:39 -07001519 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1520 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001521 synchronized (mMethodMap) {
1522 if (mCurMethod != null && method != null
1523 && mCurMethod.asBinder() == method.asBinder()) {
1524 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001525 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001526 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001527 method, session, channel);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001528 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001529 if (res.method != null) {
1530 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
1531 MSG_BIND_METHOD, mCurClient.client, res));
1532 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001533 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001534 }
1535 }
1536 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001537
1538 // Session abandoned. Close its associated input channel.
1539 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001540 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001541
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001542 void unbindCurrentMethodLocked(boolean reportToClient, boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001543 if (mVisibleBound) {
1544 mContext.unbindService(mVisibleConnection);
1545 mVisibleBound = false;
1546 }
1547
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001548 if (mHaveConnection) {
1549 mContext.unbindService(this);
1550 mHaveConnection = false;
1551 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001552
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001553 if (mCurToken != null) {
1554 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001555 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001556 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001557 // The current IME is shown. Hence an IME switch (transition) is happening.
1558 mWindowManagerService.saveLastInputMethodWindowForTransition();
1559 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001560 mIWindowManager.removeWindowToken(mCurToken);
1561 } catch (RemoteException e) {
1562 }
1563 mCurToken = null;
1564 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001565
The Android Open Source Project10592532009-03-18 17:39:46 -07001566 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001567 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001568
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001569 if (reportToClient && mCurClient != null) {
1570 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1571 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1572 }
1573 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001574
1575 void requestClientSessionLocked(ClientState cs) {
1576 if (!cs.sessionRequested) {
1577 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1578 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1579 cs.sessionRequested = true;
1580 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1581 MSG_CREATE_SESSION, mCurMethod, channels[1],
1582 new MethodCallback(this, mCurMethod, channels[0])));
1583 }
1584 }
1585
1586 void clearClientSessionLocked(ClientState cs) {
1587 finishSessionLocked(cs.curSession);
1588 cs.curSession = null;
1589 cs.sessionRequested = false;
1590 }
1591
1592 private void finishSessionLocked(SessionState sessionState) {
1593 if (sessionState != null) {
1594 if (sessionState.session != null) {
1595 try {
1596 sessionState.session.finishSession();
1597 } catch (RemoteException e) {
1598 Slog.w(TAG, "Session failed to close due to remote exception", e);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001599 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Jeff Brownc28867a2013-03-26 15:42:39 -07001600 }
1601 sessionState.session = null;
1602 }
1603 if (sessionState.channel != null) {
1604 sessionState.channel.dispose();
1605 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001606 }
1607 }
1608 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001609
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001610 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001611 if (mCurMethod != null) {
1612 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001613 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001614 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001615
Jeff Brownc28867a2013-03-26 15:42:39 -07001616 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001617 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001618 mCurMethod = null;
1619 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001620 if (mStatusBar != null) {
1621 mStatusBar.setIconVisibility("ime", false);
1622 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001623 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001624
satoke7c6998e2011-06-03 17:57:59 +09001625 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001626 public void onServiceDisconnected(ComponentName name) {
1627 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001628 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001629 + " mCurIntent=" + mCurIntent);
1630 if (mCurMethod != null && mCurIntent != null
1631 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001632 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001633 // We consider this to be a new bind attempt, since the system
1634 // should now try to restart the service for us.
1635 mLastBindTime = SystemClock.uptimeMillis();
1636 mShowRequested = mInputShown;
1637 mInputShown = false;
1638 if (mCurClient != null) {
1639 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1640 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1641 }
1642 }
1643 }
1644 }
1645
satokf9f01002011-05-19 21:31:50 +09001646 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001647 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
1648 long ident = Binder.clearCallingIdentity();
1649 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001650 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09001651 if (!calledWithValidToken(token)) {
1652 final int uid = Binder.getCallingUid();
1653 Slog.e(TAG, "Ignoring updateStatusIcon due to an invalid token. uid:" + uid
1654 + " token:" + token);
1655 return;
1656 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001657 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001658 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001659 if (mStatusBar != null) {
1660 mStatusBar.setIconVisibility("ime", false);
1661 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001662 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001663 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001664 CharSequence contentDescription = null;
1665 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001666 // Use PackageManager to load label
1667 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001668 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001669 mIPackageManager.getApplicationInfo(packageName, 0,
1670 mSettings.getCurrentUserId()));
1671 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001672 /* ignore */
1673 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001674 if (mStatusBar != null) {
1675 mStatusBar.setIcon("ime", packageName, iconId, 0,
1676 contentDescription != null
1677 ? contentDescription.toString() : null);
1678 mStatusBar.setIconVisibility("ime", true);
1679 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001680 }
1681 }
1682 } finally {
1683 Binder.restoreCallingIdentity(ident);
1684 }
1685 }
1686
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001687 private boolean shouldShowImeSwitcherLocked(int visibility) {
satok7cfc0ed2011-06-20 21:29:36 +09001688 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04001689 if (mSwitchingDialog != null) return false;
satok2c93efc2012-04-02 19:33:47 +09001690 if (isScreenLocked()) return false;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001691 if ((visibility & InputMethodService.IME_ACTIVE) == 0) return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001692 if (mWindowManagerService.isHardKeyboardAvailable()) {
1693 // When physical keyboard is attached, we show the ime switcher (or notification if
1694 // NavBar is not available) because SHOW_IME_WITH_HARD_KEYBOARD settings currently
1695 // exists in the IME switcher dialog. Might be OK to remove this condition once
1696 // SHOW_IME_WITH_HARD_KEYBOARD settings finds a good place to live.
1697 return true;
1698 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001699 if ((visibility & InputMethodService.IME_VISIBLE) == 0) return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001700
1701 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1702 final int N = imis.size();
1703 if (N > 2) return true;
1704 if (N < 1) return false;
1705 int nonAuxCount = 0;
1706 int auxCount = 0;
1707 InputMethodSubtype nonAuxSubtype = null;
1708 InputMethodSubtype auxSubtype = null;
1709 for(int i = 0; i < N; ++i) {
1710 final InputMethodInfo imi = imis.get(i);
1711 final List<InputMethodSubtype> subtypes =
1712 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
1713 final int subtypeCount = subtypes.size();
1714 if (subtypeCount == 0) {
1715 ++nonAuxCount;
1716 } else {
1717 for (int j = 0; j < subtypeCount; ++j) {
1718 final InputMethodSubtype subtype = subtypes.get(j);
1719 if (!subtype.isAuxiliary()) {
1720 ++nonAuxCount;
1721 nonAuxSubtype = subtype;
1722 } else {
1723 ++auxCount;
1724 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001725 }
1726 }
satok7cfc0ed2011-06-20 21:29:36 +09001727 }
1728 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001729 if (nonAuxCount > 1 || auxCount > 1) {
1730 return true;
1731 } else if (nonAuxCount == 1 && auxCount == 1) {
1732 if (nonAuxSubtype != null && auxSubtype != null
1733 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1734 || auxSubtype.overridesImplicitlyEnabledSubtype()
1735 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
1736 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1737 return false;
1738 }
1739 return true;
1740 }
1741 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001742 }
1743
John Spurlocke0980502013-10-25 11:59:29 -04001744 private boolean isKeyguardLocked() {
1745 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1746 }
1747
satokdbf29502011-08-25 15:28:23 +09001748 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001749 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001750 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001751 if (!calledWithValidToken(token)) {
1752 final int uid = Binder.getCallingUid();
1753 Slog.e(TAG, "Ignoring setImeWindowStatus due to an invalid token. uid:" + uid
1754 + " token:" + token);
1755 return;
1756 }
1757
1758 synchronized (mMethodMap) {
1759 mImeWindowVis = vis;
1760 mBackDisposition = backDisposition;
1761 updateSystemUiLocked(token, vis, backDisposition);
1762 }
1763 }
1764
1765 private void updateSystemUi(IBinder token, int vis, int backDisposition) {
1766 synchronized (mMethodMap) {
1767 updateSystemUiLocked(token, vis, backDisposition);
1768 }
1769 }
1770
1771 // Caution! This method is called in this class. Handle multi-user carefully
1772 private void updateSystemUiLocked(IBinder token, int vis, int backDisposition) {
1773 if (!calledWithValidToken(token)) {
1774 final int uid = Binder.getCallingUid();
1775 Slog.e(TAG, "Ignoring updateSystemUiLocked due to an invalid token. uid:" + uid
1776 + " token:" + token);
1777 return;
1778 }
1779
1780 // TODO: Move this clearing calling identity block to setImeWindowStatus after making sure
1781 // all updateSystemUi happens on system previlege.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001782 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001783 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001784 // apply policy for binder calls
1785 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
1786 vis = 0;
satok06487a52010-10-29 11:37:18 +09001787 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001788 // mImeWindowVis should be updated before calling shouldShowImeSwitcherLocked().
1789 final boolean needsToShowImeSwitcher = shouldShowImeSwitcherLocked(vis);
1790 if (mStatusBar != null) {
1791 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
1792 needsToShowImeSwitcher);
1793 }
1794 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1795 if (imi != null && needsToShowImeSwitcher) {
1796 // Used to load label
1797 final CharSequence title = mRes.getText(
1798 com.android.internal.R.string.select_input_method);
1799 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
1800 mContext, imi, mCurrentSubtype);
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001801 mImeSwitcherNotification.setContentTitle(title)
1802 .setContentText(summary)
1803 .setContentIntent(mImeSwitchPendingIntent);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001804 if ((mNotificationManager != null)
1805 && !mWindowManagerService.hasNavigationBar()) {
1806 if (DEBUG) {
1807 Slog.d(TAG, "--- show notification: label = " + summary);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001808 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001809 mNotificationManager.notifyAsUser(null,
1810 com.android.internal.R.string.select_input_method,
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001811 mImeSwitcherNotification.build(), UserHandle.ALL);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001812 mNotificationShown = true;
1813 }
1814 } else {
1815 if (mNotificationShown && mNotificationManager != null) {
1816 if (DEBUG) {
1817 Slog.d(TAG, "--- hide notification");
satok7cfc0ed2011-06-20 21:29:36 +09001818 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001819 mNotificationManager.cancelAsUser(null,
1820 com.android.internal.R.string.select_input_method, UserHandle.ALL);
1821 mNotificationShown = false;
satok7cfc0ed2011-06-20 21:29:36 +09001822 }
satok06487a52010-10-29 11:37:18 +09001823 }
1824 } finally {
1825 Binder.restoreCallingIdentity(ident);
1826 }
1827 }
1828
satoke7c6998e2011-06-03 17:57:59 +09001829 @Override
satokf9f01002011-05-19 21:31:50 +09001830 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001831 if (!calledFromValidUser()) {
1832 return;
1833 }
satokf9f01002011-05-19 21:31:50 +09001834 synchronized (mMethodMap) {
1835 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1836 for (int i = 0; i < spans.length; ++i) {
1837 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001838 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001839 mSecureSuggestionSpans.put(ss, currentImi);
1840 }
1841 }
1842 }
1843 }
1844
satoke7c6998e2011-06-03 17:57:59 +09001845 @Override
satokf9f01002011-05-19 21:31:50 +09001846 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001847 if (!calledFromValidUser()) {
1848 return false;
1849 }
satokf9f01002011-05-19 21:31:50 +09001850 synchronized (mMethodMap) {
1851 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1852 // TODO: Do not send the intent if the process of the targetImi is already dead.
1853 if (targetImi != null) {
1854 final String[] suggestions = span.getSuggestions();
1855 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001856 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001857 final Intent intent = new Intent();
1858 // Ensures that only a class in the original IME package will receive the
1859 // notification.
satok42c5a162011-05-26 16:46:14 +09001860 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001861 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1862 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1863 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1864 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001865 final long ident = Binder.clearCallingIdentity();
1866 try {
1867 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1868 } finally {
1869 Binder.restoreCallingIdentity(ident);
1870 }
satokf9f01002011-05-19 21:31:50 +09001871 return true;
1872 }
1873 }
1874 return false;
1875 }
1876
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001877 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001878 updateInputMethodsFromSettingsLocked(enabledMayChange);
1879 updateKeyboardFromSettingsLocked();
1880 }
1881
1882 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001883 if (enabledMayChange) {
1884 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1885 for (int i=0; i<enabled.size(); i++) {
1886 // We allow the user to select "disabled until used" apps, so if they
1887 // are enabling one of those here we now need to make it enabled.
1888 InputMethodInfo imm = enabled.get(i);
1889 try {
1890 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1891 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1892 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09001893 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001894 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001895 if (DEBUG) {
1896 Slog.d(TAG, "Update state(" + imm.getId()
1897 + "): DISABLED_UNTIL_USED -> DEFAULT");
1898 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001899 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1900 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001901 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
1902 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001903 }
1904 } catch (RemoteException e) {
1905 }
1906 }
1907 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001908 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1909 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1910 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1911 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001912 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001913 // There is no input method selected, try to choose new applicable input method.
1914 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001915 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001916 }
1917 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001918 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001919 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001920 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001921 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001922 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001923 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001924 }
satokf3db1af2010-11-23 13:34:33 +09001925 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001926 } else {
1927 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001928 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001929 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001930 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09001931 // Here is not the perfect place to reset the switching controller. Ideally
1932 // mSwitchingController and mSettings should be able to share the same state.
1933 // TODO: Make sure that mSwitchingController and mSettings are sharing the
1934 // the same enabled IMEs list.
1935 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07001936
1937 }
1938
1939 public void updateKeyboardFromSettingsLocked() {
1940 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
1941 if (mSwitchingDialog != null
1942 && mSwitchingDialogTitleView != null
1943 && mSwitchingDialog.isShowing()) {
1944 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
1945 com.android.internal.R.id.hard_keyboard_switch);
1946 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
1947 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001948 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001949
satokab751aa2010-09-14 19:17:36 +09001950 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001951 InputMethodInfo info = mMethodMap.get(id);
1952 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001953 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001954 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001955
Yohei Yukawae63b5fa2014-09-19 13:14:55 +09001956 if (mCurClient != null && mCurAttribute != null) {
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001957 // We have already made sure that the package name belongs to the application's UID.
1958 // No further UID check is required.
1959 if (SystemConfig.getInstance().getFixedImeApps().contains(mCurAttribute.packageName)) {
1960 return;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +09001961 }
1962 }
1963
satokd81e9502012-05-21 12:58:45 +09001964 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001965 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09001966 final int subtypeCount = info.getSubtypeCount();
1967 if (subtypeCount <= 0) {
1968 return;
satokcd7cd292010-11-20 15:46:23 +09001969 }
satokd81e9502012-05-21 12:58:45 +09001970 final InputMethodSubtype oldSubtype = mCurrentSubtype;
1971 final InputMethodSubtype newSubtype;
1972 if (subtypeId >= 0 && subtypeId < subtypeCount) {
1973 newSubtype = info.getSubtypeAt(subtypeId);
1974 } else {
1975 // If subtype is null, try to find the most applicable one from
1976 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001977 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09001978 }
1979 if (newSubtype == null || oldSubtype == null) {
1980 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
1981 + ", new subtype = " + newSubtype);
1982 return;
1983 }
1984 if (newSubtype != oldSubtype) {
1985 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1986 if (mCurMethod != null) {
1987 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001988 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokd81e9502012-05-21 12:58:45 +09001989 mCurMethod.changeInputMethodSubtype(newSubtype);
1990 } catch (RemoteException e) {
1991 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
satokab751aa2010-09-14 19:17:36 +09001992 }
1993 }
1994 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001995 return;
1996 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001997
satokd81e9502012-05-21 12:58:45 +09001998 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001999 final long ident = Binder.clearCallingIdentity();
2000 try {
satokab751aa2010-09-14 19:17:36 +09002001 // Set a subtype to this input method.
2002 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09002003 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
2004 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
2005 // because mCurMethodId is stored as a history in
2006 // setSelectedInputMethodAndSubtypeLocked().
2007 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002008
2009 if (ActivityManagerNative.isSystemReady()) {
2010 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08002011 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002012 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07002013 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002014 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002015 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002016 } finally {
2017 Binder.restoreCallingIdentity(ident);
2018 }
2019 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002020
satok42c5a162011-05-26 16:46:14 +09002021 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002022 public boolean showSoftInput(IInputMethodClient client, int flags,
2023 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002024 if (!calledFromValidUser()) {
2025 return false;
2026 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002027 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002028 long ident = Binder.clearCallingIdentity();
2029 try {
2030 synchronized (mMethodMap) {
2031 if (mCurClient == null || client == null
2032 || mCurClient.client.asBinder() != client.asBinder()) {
2033 try {
2034 // We need to check if this is the current client with
2035 // focus in the window manager, to allow this call to
2036 // be made before input is started in it.
2037 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002038 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002039 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002040 }
2041 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002042 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002043 }
2044 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002045
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002046 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002047 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002048 }
2049 } finally {
2050 Binder.restoreCallingIdentity(ident);
2051 }
2052 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002053
The Android Open Source Project4df24232009-03-05 14:34:35 -08002054 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002055 mShowRequested = true;
2056 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
2057 mShowExplicitlyRequested = true;
2058 }
2059 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
2060 mShowExplicitlyRequested = true;
2061 mShowForced = true;
2062 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002063
Dianne Hackborncc278702009-09-02 23:07:23 -07002064 if (!mSystemReady) {
2065 return false;
2066 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002067
The Android Open Source Project4df24232009-03-05 14:34:35 -08002068 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002069 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002070 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002071 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
2072 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
2073 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002074 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002075 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002076 bindCurrentInputMethodService(
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07002077 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE
Dianne Hackbornd69e4c12015-04-24 09:54:54 -07002078 | Context.BIND_TREAT_LIKE_ACTIVITY
2079 | Context.BIND_FOREGROUND_SERVICE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002080 mVisibleBound = true;
2081 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002082 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002083 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09002084 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002085 // The client has asked to have the input method shown, but
2086 // we have been sitting here too long with a connection to the
2087 // service and no interface received, so let's disconnect/connect
2088 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002089 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002090 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09002091 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002092 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002093 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002094 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002095 } else {
2096 if (DEBUG) {
2097 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
2098 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
2099 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002100 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002101
The Android Open Source Project4df24232009-03-05 14:34:35 -08002102 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002103 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002104
satok42c5a162011-05-26 16:46:14 +09002105 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002106 public boolean hideSoftInput(IInputMethodClient client, int flags,
2107 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002108 if (!calledFromValidUser()) {
2109 return false;
2110 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002111 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002112 long ident = Binder.clearCallingIdentity();
2113 try {
2114 synchronized (mMethodMap) {
2115 if (mCurClient == null || client == null
2116 || mCurClient.client.asBinder() != client.asBinder()) {
2117 try {
2118 // We need to check if this is the current client with
2119 // focus in the window manager, to allow this call to
2120 // be made before input is started in it.
2121 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002122 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
2123 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002124 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002125 }
2126 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002127 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002128 }
2129 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002130
Joe Onorato8a9b2202010-02-26 18:56:32 -08002131 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002132 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002133 }
2134 } finally {
2135 Binder.restoreCallingIdentity(ident);
2136 }
2137 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002138
The Android Open Source Project4df24232009-03-05 14:34:35 -08002139 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002140 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
2141 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002142 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 -08002143 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002144 }
2145 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002146 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 -08002147 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002148 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002149 boolean res;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002150 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002151 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2152 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
2153 res = true;
2154 } else {
2155 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002156 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002157 if (mHaveConnection && mVisibleBound) {
2158 mContext.unbindService(mVisibleConnection);
2159 mVisibleBound = false;
2160 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002161 mInputShown = false;
2162 mShowRequested = false;
2163 mShowExplicitlyRequested = false;
2164 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002165 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002166 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002167
satok42c5a162011-05-26 16:46:14 +09002168 @Override
Dianne Hackborn7663d802012-02-24 13:08:49 -08002169 public InputBindResult windowGainedFocus(IInputMethodClient client, IBinder windowToken,
2170 int controlFlags, int softInputMode, int windowFlags,
2171 EditorInfo attribute, IInputContext inputContext) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002172 // Needs to check the validity before clearing calling identity
2173 final boolean calledFromValidUser = calledFromValidUser();
2174
Dianne Hackborn7663d802012-02-24 13:08:49 -08002175 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002176 long ident = Binder.clearCallingIdentity();
2177 try {
2178 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002179 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
Dianne Hackborn7663d802012-02-24 13:08:49 -08002180 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002181 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08002182 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002183
Dianne Hackborn7663d802012-02-24 13:08:49 -08002184 ClientState cs = mClients.get(client.asBinder());
2185 if (cs == null) {
2186 throw new IllegalArgumentException("unknown client "
2187 + client.asBinder());
2188 }
2189
2190 try {
2191 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2192 // Check with the window manager to make sure this client actually
2193 // has a window with focus. If not, reject. This is thread safe
2194 // because if the focus changes some time before or after, the
2195 // next client receiving focus that has any interest in input will
2196 // be calling through here after that change happens.
2197 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2198 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2199 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002200 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002201 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002202 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002203
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002204 if (!calledFromValidUser) {
2205 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2206 Slog.w(TAG, "If you want to interect with IME, you need "
2207 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2208 hideCurrentInputLocked(0, null);
2209 return null;
2210 }
2211
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002212 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07002213 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09002214 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002215 if (attribute != null) {
2216 return startInputUncheckedLocked(cs, inputContext, attribute,
2217 controlFlags);
2218 }
2219 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002220 }
2221 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002222
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002223 // Should we auto-show the IME even if the caller has not
2224 // specified what should be done with it?
2225 // We only do this automatically if the window can resize
2226 // to accommodate the IME (so what the user sees will give
2227 // them good context without input information being obscured
2228 // by the IME) or if running on a large screen where there
2229 // is more room for the target window + IME.
2230 final boolean doAutoShow =
2231 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2232 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2233 || mRes.getConfiguration().isLayoutSizeAtLeast(
2234 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002235 final boolean isTextEditor =
2236 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2237
2238 // We want to start input before showing the IME, but after closing
2239 // it. We want to do this after closing it to help the IME disappear
2240 // more quickly (not get stuck behind it initializing itself for the
2241 // new focused input, even if its window wants to hide the IME).
2242 boolean didStart = false;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002243
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002244 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2245 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002246 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002247 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2248 // There is no focus view, and this window will
2249 // be behind any soft input window, so hide the
2250 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002251 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002252 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002253 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002254 } else if (isTextEditor && doAutoShow && (softInputMode &
2255 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002256 // There is a focus view, and we are navigating forward
2257 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002258 // We only do this automatically if the window can resize
2259 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002260 // them good context without input information being obscured
2261 // by the IME) or if running on a large screen where there
2262 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002263 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002264 if (attribute != null) {
2265 res = startInputUncheckedLocked(cs, inputContext, attribute,
2266 controlFlags);
2267 didStart = true;
2268 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002269 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002270 }
2271 break;
2272 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2273 // Do nothing.
2274 break;
2275 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2276 if ((softInputMode &
2277 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002278 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002279 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002280 }
2281 break;
2282 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002283 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002284 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002285 break;
2286 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2287 if ((softInputMode &
2288 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002289 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002290 if (attribute != null) {
2291 res = startInputUncheckedLocked(cs, inputContext, attribute,
2292 controlFlags);
2293 didStart = true;
2294 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002295 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002296 }
2297 break;
2298 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002299 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002300 if (attribute != null) {
2301 res = startInputUncheckedLocked(cs, inputContext, attribute,
2302 controlFlags);
2303 didStart = true;
2304 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002305 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002306 break;
2307 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002308
2309 if (!didStart && attribute != null) {
2310 res = startInputUncheckedLocked(cs, inputContext, attribute,
2311 controlFlags);
2312 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002313 }
2314 } finally {
2315 Binder.restoreCallingIdentity(ident);
2316 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002317
2318 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002319 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002320
satok42c5a162011-05-26 16:46:14 +09002321 @Override
Seigo Nonaka14e13912015-05-06 21:04:13 -07002322 public void showInputMethodPickerFromClient(
2323 IInputMethodClient client, int auxiliarySubtypeMode) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002324 if (!calledFromValidUser()) {
2325 return;
2326 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002327 synchronized (mMethodMap) {
2328 if (mCurClient == null || client == null
2329 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002330 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002331 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002332 }
2333
satok440aab52010-11-25 09:43:11 +09002334 // Always call subtype picker, because subtype picker is a superset of input method
2335 // picker.
Seigo Nonaka14e13912015-05-06 21:04:13 -07002336 mHandler.sendMessage(mCaller.obtainMessageI(
2337 MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode));
satokab751aa2010-09-14 19:17:36 +09002338 }
2339 }
2340
satok42c5a162011-05-26 16:46:14 +09002341 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002342 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002343 if (!calledFromValidUser()) {
2344 return;
2345 }
satok28203512010-11-24 11:06:49 +09002346 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2347 }
2348
satok42c5a162011-05-26 16:46:14 +09002349 @Override
satok28203512010-11-24 11:06:49 +09002350 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002351 if (!calledFromValidUser()) {
2352 return;
2353 }
satok28203512010-11-24 11:06:49 +09002354 synchronized (mMethodMap) {
2355 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002356 setInputMethodWithSubtypeIdLocked(token, id,
2357 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2358 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002359 } else {
2360 setInputMethod(token, id);
2361 }
2362 }
satokab751aa2010-09-14 19:17:36 +09002363 }
2364
satok42c5a162011-05-26 16:46:14 +09002365 @Override
satokb416a712010-11-25 20:42:14 +09002366 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002367 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002368 if (!calledFromValidUser()) {
2369 return;
2370 }
satokb416a712010-11-25 20:42:14 +09002371 synchronized (mMethodMap) {
2372 if (mCurClient == null || client == null
2373 || mCurClient.client.asBinder() != client.asBinder()) {
2374 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
2375 }
satok7fee71f2010-12-17 18:54:26 +09002376 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2377 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002378 }
2379 }
2380
satok4fc87d62011-05-20 16:13:43 +09002381 @Override
satok735cf382010-11-11 20:40:09 +09002382 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002383 if (!calledFromValidUser()) {
2384 return false;
2385 }
satok735cf382010-11-11 20:40:09 +09002386 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002387 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002388 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002389 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002390 lastImi = mMethodMap.get(lastIme.first);
2391 } else {
2392 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002393 }
satok4fc87d62011-05-20 16:13:43 +09002394 String targetLastImiId = null;
2395 int subtypeId = NOT_A_SUBTYPE_ID;
2396 if (lastIme != null && lastImi != null) {
2397 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
2398 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
2399 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2400 : mCurrentSubtype.hashCode();
2401 // If the last IME is the same as the current IME and the last subtype is not
2402 // defined, there is no need to switch to the last IME.
2403 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2404 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002405 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002406 }
2407 }
2408
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002409 if (TextUtils.isEmpty(targetLastImiId)
2410 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002411 // This is a safety net. If the currentSubtype can't be added to the history
2412 // and the framework couldn't find the last ime, we will make the last ime be
2413 // the most applicable enabled keyboard subtype of the system imes.
2414 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2415 if (enabled != null) {
2416 final int N = enabled.size();
2417 final String locale = mCurrentSubtype == null
2418 ? mRes.getConfiguration().locale.toString()
2419 : mCurrentSubtype.getLocale();
2420 for (int i = 0; i < N; ++i) {
2421 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002422 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002423 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002424 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2425 InputMethodUtils.getSubtypes(imi),
2426 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002427 if (keyboardSubtype != null) {
2428 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002429 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002430 imi, keyboardSubtype.hashCode());
2431 if(keyboardSubtype.getLocale().equals(locale)) {
2432 break;
2433 }
2434 }
2435 }
2436 }
2437 }
2438 }
2439
2440 if (!TextUtils.isEmpty(targetLastImiId)) {
2441 if (DEBUG) {
2442 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2443 + ", from: " + mCurMethodId + ", " + subtypeId);
2444 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002445 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002446 return true;
2447 } else {
2448 return false;
2449 }
satok735cf382010-11-11 20:40:09 +09002450 }
2451 }
2452
satoke7c6998e2011-06-03 17:57:59 +09002453 @Override
satok688bd472012-02-09 20:09:17 +09002454 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002455 if (!calledFromValidUser()) {
2456 return false;
2457 }
satok688bd472012-02-09 20:09:17 +09002458 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002459 if (!calledWithValidToken(token)) {
2460 final int uid = Binder.getCallingUid();
2461 Slog.e(TAG, "Ignoring switchToNextInputMethod due to an invalid token. uid:" + uid
2462 + " token:" + token);
2463 return false;
2464 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002465 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
satok688bd472012-02-09 20:09:17 +09002466 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2467 if (nextSubtype == null) {
2468 return false;
2469 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002470 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2471 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002472 return true;
2473 }
2474 }
2475
2476 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002477 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2478 if (!calledFromValidUser()) {
2479 return false;
2480 }
2481 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002482 if (!calledWithValidToken(token)) {
2483 final int uid = Binder.getCallingUid();
2484 Slog.e(TAG, "Ignoring shouldOfferSwitchingToNextInputMethod due to an invalid "
2485 + "token. uid:" + uid + " token:" + token);
2486 return false;
2487 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002488 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002489 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2490 if (nextSubtype == null) {
2491 return false;
2492 }
2493 return true;
2494 }
2495 }
2496
2497 @Override
satok68f1b782011-04-11 14:26:04 +09002498 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002499 if (!calledFromValidUser()) {
2500 return null;
2501 }
satok68f1b782011-04-11 14:26:04 +09002502 synchronized (mMethodMap) {
2503 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2504 // TODO: Handle the case of the last IME with no subtypes
2505 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2506 || TextUtils.isEmpty(lastIme.second)) return null;
2507 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2508 if (lastImi == null) return null;
2509 try {
2510 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002511 final int lastSubtypeId =
2512 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002513 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2514 return null;
2515 }
2516 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002517 } catch (NumberFormatException e) {
2518 return null;
2519 }
2520 }
2521 }
2522
satoke7c6998e2011-06-03 17:57:59 +09002523 @Override
satokee5e77c2011-09-02 18:50:15 +09002524 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002525 if (!calledFromValidUser()) {
2526 return;
2527 }
satok91e88122011-07-18 11:11:42 +09002528 // By this IPC call, only a process which shares the same uid with the IME can add
2529 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09002530 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09002531 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002532 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002533 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002534 final String[] packageInfos;
2535 try {
2536 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2537 } catch (RemoteException e) {
2538 Slog.e(TAG, "Failed to get package infos");
2539 return;
2540 }
satok91e88122011-07-18 11:11:42 +09002541 if (packageInfos != null) {
2542 final int packageNum = packageInfos.length;
2543 for (int i = 0; i < packageNum; ++i) {
2544 if (packageInfos[i].equals(imi.getPackageName())) {
2545 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002546 final long ident = Binder.clearCallingIdentity();
2547 try {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002548 buildInputMethodListLocked(mMethodList, mMethodMap,
2549 false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002550 } finally {
2551 Binder.restoreCallingIdentity(ident);
2552 }
satokee5e77c2011-09-02 18:50:15 +09002553 return;
satok91e88122011-07-18 11:11:42 +09002554 }
2555 }
2556 }
satoke7c6998e2011-06-03 17:57:59 +09002557 }
satokee5e77c2011-09-02 18:50:15 +09002558 return;
satoke7c6998e2011-06-03 17:57:59 +09002559 }
2560
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002561 @Override
2562 public int getInputMethodWindowVisibleHeight() {
2563 return mWindowManagerService.getInputMethodWindowVisibleHeight();
2564 }
2565
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002566 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002567 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002568 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002569 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002570 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002571 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002572 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
2573 if (DEBUG) {
2574 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
2575 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
2576 + " actual: " + sequenceNumber);
2577 }
2578 return;
2579 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002580 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2581 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09002582 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002583 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002584 }
2585 }
2586
satok28203512010-11-24 11:06:49 +09002587 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002588 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002589 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
2590 }
2591 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002592
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002593 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
2594 if (token == null) {
2595 if (mContext.checkCallingOrSelfPermission(
2596 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2597 != PackageManager.PERMISSION_GRANTED) {
2598 throw new SecurityException(
2599 "Using null token requires permission "
2600 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002601 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002602 } else if (mCurToken != token) {
2603 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2604 + " token: " + token);
2605 return;
2606 }
2607
2608 final long ident = Binder.clearCallingIdentity();
2609 try {
2610 setInputMethodLocked(id, subtypeId);
2611 } finally {
2612 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002613 }
2614 }
2615
satok42c5a162011-05-26 16:46:14 +09002616 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002617 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002618 if (!calledFromValidUser()) {
2619 return;
2620 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002621 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002622 if (!calledWithValidToken(token)) {
2623 final int uid = Binder.getCallingUid();
2624 Slog.e(TAG, "Ignoring hideInputMethod due to an invalid token. uid:"
2625 + uid + " token:" + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002626 return;
2627 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002628 long ident = Binder.clearCallingIdentity();
2629 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002630 hideCurrentInputLocked(flags, null);
2631 } finally {
2632 Binder.restoreCallingIdentity(ident);
2633 }
2634 }
2635 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002636
satok42c5a162011-05-26 16:46:14 +09002637 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002638 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002639 if (!calledFromValidUser()) {
2640 return;
2641 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002642 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002643 if (!calledWithValidToken(token)) {
2644 final int uid = Binder.getCallingUid();
2645 Slog.e(TAG, "Ignoring showMySoftInput due to an invalid token. uid:"
2646 + uid + " token:" + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002647 return;
2648 }
2649 long ident = Binder.clearCallingIdentity();
2650 try {
2651 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002652 } finally {
2653 Binder.restoreCallingIdentity(ident);
2654 }
2655 }
2656 }
2657
2658 void setEnabledSessionInMainThread(SessionState session) {
2659 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002660 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002661 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002662 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002663 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002664 } catch (RemoteException e) {
2665 }
2666 }
2667 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002668 if (mEnabledSession != null && mEnabledSession.session != null) {
2669 try {
2670 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
2671 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
2672 } catch (RemoteException e) {
2673 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002674 }
2675 }
2676 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002677
satok42c5a162011-05-26 16:46:14 +09002678 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002679 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002680 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002681 switch (msg.what) {
satokab751aa2010-09-14 19:17:36 +09002682 case MSG_SHOW_IM_SUBTYPE_PICKER:
Seigo Nonaka14e13912015-05-06 21:04:13 -07002683 final boolean showAuxSubtypes;
2684 switch (msg.arg1) {
2685 case InputMethodManager.SHOW_IM_PICKER_MODE_AUTO:
2686 // This is undocumented so far, but IMM#showInputMethodPicker() has been
2687 // implemented so that auxiliary subtypes will be excluded when the soft
2688 // keyboard is invisible.
2689 showAuxSubtypes = mInputShown;
2690 break;
2691 case InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES:
2692 showAuxSubtypes = true;
2693 break;
2694 case InputMethodManager.SHOW_IM_PICKER_MODE_EXCLUDE_AUXILIARY_SUBTYPES:
2695 showAuxSubtypes = false;
2696 break;
2697 default:
2698 Slog.e(TAG, "Unknown subtype picker mode = " + msg.arg1);
2699 return false;
2700 }
2701 showInputMethodMenu(showAuxSubtypes);
satokab751aa2010-09-14 19:17:36 +09002702 return true;
2703
satok47a44912010-10-06 16:03:58 +09002704 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002705 args = (SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09002706 showInputMethodAndSubtypeEnabler((String)args.arg1);
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002707 args.recycle();
satok217f5482010-12-15 05:19:19 +09002708 return true;
2709
2710 case MSG_SHOW_IM_CONFIG:
2711 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002712 return true;
2713
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002714 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002715
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002716 case MSG_UNBIND_INPUT:
2717 try {
2718 ((IInputMethod)msg.obj).unbindInput();
2719 } catch (RemoteException e) {
2720 // There is nothing interesting about the method dying.
2721 }
2722 return true;
2723 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002724 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002725 try {
2726 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2727 } catch (RemoteException e) {
2728 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002729 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002730 return true;
2731 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002732 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002733 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002734 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002735 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002736 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002737 } catch (RemoteException e) {
2738 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002739 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002740 return true;
2741 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002742 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002743 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002744 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002745 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002746 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002747 } catch (RemoteException e) {
2748 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002749 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002750 return true;
2751 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002752 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002753 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002754 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002755 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2756 } catch (RemoteException e) {
2757 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002758 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002759 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002760 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002761 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002762 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002763 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002764 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002765 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002766 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002767 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002768 // Dispose the channel if the input method is not local to this process
2769 // because the remote proxy will get its own copy when unparceled.
2770 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002771 channel.dispose();
2772 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002773 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002774 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002775 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002776 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002777 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002778
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002779 case MSG_START_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002780 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002781 try {
2782 SessionState session = (SessionState)args.arg1;
2783 setEnabledSessionInMainThread(session);
2784 session.method.startInput((IInputContext)args.arg2,
2785 (EditorInfo)args.arg3);
2786 } catch (RemoteException e) {
2787 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002788 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002789 return true;
2790 case MSG_RESTART_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002791 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002792 try {
2793 SessionState session = (SessionState)args.arg1;
2794 setEnabledSessionInMainThread(session);
2795 session.method.restartInput((IInputContext)args.arg2,
2796 (EditorInfo)args.arg3);
2797 } catch (RemoteException e) {
2798 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002799 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002800 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002801
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002802 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002803
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002804 case MSG_UNBIND_METHOD:
2805 try {
2806 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
2807 } catch (RemoteException e) {
2808 // There is nothing interesting about the last client dying.
2809 }
2810 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002811 case MSG_BIND_METHOD: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002812 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002813 IInputMethodClient client = (IInputMethodClient)args.arg1;
2814 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002815 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002816 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002817 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002818 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002819 } finally {
2820 // Dispose the channel if the input method is not local to this process
2821 // because the remote proxy will get its own copy when unparceled.
2822 if (res.channel != null && Binder.isProxy(client)) {
2823 res.channel.dispose();
2824 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002825 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002826 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002827 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002828 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002829 case MSG_SET_ACTIVE:
2830 try {
2831 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2832 } catch (RemoteException e) {
2833 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2834 + ((ClientState)msg.obj).pid + " uid "
2835 + ((ClientState)msg.obj).uid);
2836 }
2837 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002838 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
2839 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07002840 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002841 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07002842 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002843 } catch (RemoteException e) {
2844 Slog.w(TAG, "Got RemoteException sending "
2845 + "setUserActionNotificationSequenceNumber("
2846 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07002847 + clientState.pid + " uid "
2848 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002849 }
2850 return true;
2851 }
satok01038492012-04-09 21:08:27 +09002852
2853 // --------------------------------------------------------------
2854 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07002855 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09002856 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002857 }
2858 return false;
2859 }
2860
satokdc9ddae2011-10-06 12:22:36 +09002861 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002862 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
2863 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09002864 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002865 if (DEBUG) {
2866 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2867 }
satok723a27e2010-11-11 14:58:11 +09002868 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002869 return true;
2870 }
2871
2872 return false;
2873 }
2874
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002875 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002876 HashMap<String, InputMethodInfo> map, boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002877 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002878 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Satoshi Kataoka0766eb02013-07-31 18:30:13 +09002879 + " \n ------ \n" + InputMethodUtils.getStackTrace());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002880 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002881 list.clear();
2882 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002883
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002884 // Use for queryIntentServicesAsUser
2885 final PackageManager pm = mContext.getPackageManager();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002886 String disabledSysImes = mSettings.getDisabledSystemInputMethods();
Amith Yamasanie861ec12010-03-24 21:39:27 -07002887 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002888
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002889 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002890 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002891 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2892 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002893
satoke7c6998e2011-06-03 17:57:59 +09002894 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2895 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002896 for (int i = 0; i < services.size(); ++i) {
2897 ResolveInfo ri = services.get(i);
2898 ServiceInfo si = ri.serviceInfo;
2899 ComponentName compName = new ComponentName(si.packageName, si.name);
2900 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2901 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002902 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002903 + ": it does not require the permission "
2904 + android.Manifest.permission.BIND_INPUT_METHOD);
2905 continue;
2906 }
2907
Joe Onorato8a9b2202010-02-26 18:56:32 -08002908 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002909
2910 try {
satoke7c6998e2011-06-03 17:57:59 +09002911 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002912 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002913 final String id = p.getId();
2914 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002915
2916 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002917 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002918 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002920 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002921 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002922 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002923 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002924 }
2925 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002926
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002927 if (resetDefaultEnabledIme) {
2928 final ArrayList<InputMethodInfo> defaultEnabledIme =
2929 InputMethodUtils.getDefaultEnabledImes(mContext, mSystemReady, list);
2930 for (int i = 0; i < defaultEnabledIme.size(); ++i) {
2931 final InputMethodInfo imi = defaultEnabledIme.get(i);
2932 if (DEBUG) {
2933 Slog.d(TAG, "--- enable ime = " + imi);
2934 }
2935 setInputMethodEnabledLocked(imi.getId(), true);
2936 }
2937 }
2938
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002939 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09002940 if (!TextUtils.isEmpty(defaultImiId)) {
2941 if (!map.containsKey(defaultImiId)) {
2942 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
2943 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07002944 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09002945 }
2946 } else {
2947 // Double check that the default IME is certainly enabled.
2948 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002949 }
2950 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09002951 // Here is not the perfect place to reset the switching controller. Ideally
2952 // mSwitchingController and mSettings should be able to share the same state.
2953 // TODO: Make sure that mSwitchingController and mSettings are sharing the
2954 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09002955 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002956 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002957
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002958 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002959
satok217f5482010-12-15 05:19:19 +09002960 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002961 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002962 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002963 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2964 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002965 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002966 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002967 }
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002968 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok217f5482010-12-15 05:19:19 +09002969 }
2970
2971 private void showConfigureInputMethods() {
2972 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2973 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2974 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2975 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002976 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09002977 }
2978
satok2c93efc2012-04-02 19:33:47 +09002979 private boolean isScreenLocked() {
2980 return mKeyguardManager != null
2981 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
2982 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002983
Seigo Nonaka14e13912015-05-06 21:04:13 -07002984 private void showInputMethodMenu(boolean showAuxSubtypes) {
2985 if (DEBUG) Slog.v(TAG, "Show switching menu. showAuxSubtypes=" + showAuxSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002986
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002987 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09002988 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002989
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002990 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002991 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002992 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002993
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002994 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002995 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09002996 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
2997 mContext);
satok7f35c8c2010-10-07 21:13:11 +09002998 if (immis == null || immis.size() == 0) {
2999 return;
3000 }
3001
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003002 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003003
satok688bd472012-02-09 20:09:17 +09003004 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003005 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
Seigo Nonaka14e13912015-05-06 21:04:13 -07003006 true /* showSubtypes */, showAuxSubtypes, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09003007
satokc3690562012-01-10 20:14:43 +09003008 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003009 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09003010 if (currentSubtype != null) {
3011 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003012 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
3013 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09003014 }
3015 }
3016
Ken Wakasa761eb372011-03-04 19:06:18 +09003017 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09003018 mIms = new InputMethodInfo[N];
3019 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003020 int checkedItem = 0;
3021 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09003022 final ImeSubtypeListItem item = imList.get(i);
3023 mIms[i] = item.mImi;
3024 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003025 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09003026 int subtypeId = mSubtypeIds[i];
3027 if ((subtypeId == NOT_A_SUBTYPE_ID)
3028 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
3029 || (subtypeId == lastInputMethodSubtypeId)) {
3030 checkedItem = i;
3031 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003032 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003033 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08003034
3035 final Context settingsContext = new ContextThemeWrapper(context,
3036 com.android.internal.R.style.Theme_DeviceDefault_Settings);
3037
3038 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003039 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
3040 @Override
3041 public void onCancel(DialogInterface dialog) {
3042 hideInputMethodMenu();
3043 }
3044 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08003045
3046 final Context dialogContext = mDialogBuilder.getContext();
3047 final TypedArray a = dialogContext.obtainStyledAttributes(null,
3048 com.android.internal.R.styleable.DialogPreference,
3049 com.android.internal.R.attr.alertDialogStyle, 0);
3050 final Drawable dialogIcon = a.getDrawable(
3051 com.android.internal.R.styleable.DialogPreference_dialogIcon);
3052 a.recycle();
3053
3054 mDialogBuilder.setIcon(dialogIcon);
3055
3056 final LayoutInflater inflater = (LayoutInflater) dialogContext.getSystemService(
3057 Context.LAYOUT_INFLATER_SERVICE);
satok01038492012-04-09 21:08:27 +09003058 final View tv = inflater.inflate(
3059 com.android.internal.R.layout.input_method_switch_dialog_title, null);
3060 mDialogBuilder.setCustomTitle(tv);
3061
3062 // Setup layout for a toggle switch of the hardware keyboard
3063 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003064 mSwitchingDialogTitleView
3065 .findViewById(com.android.internal.R.id.hard_keyboard_section)
3066 .setVisibility(mWindowManagerService.isHardKeyboardAvailable()
3067 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003068 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003069 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07003070 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003071 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
3072 @Override
3073 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003074 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003075 // Ensure that the input method dialog is dismissed when changing
3076 // the hardware keyboard state.
3077 hideInputMethodMenu();
3078 }
3079 });
3080
Alan Viverette505e3ab2014-11-24 15:22:11 -08003081 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003082 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
3083 final OnClickListener choiceListener = new OnClickListener() {
3084 @Override
3085 public void onClick(final DialogInterface dialog, final int which) {
3086 synchronized (mMethodMap) {
3087 if (mIms == null || mIms.length <= which || mSubtypeIds == null
3088 || mSubtypeIds.length <= which) {
3089 return;
satok01038492012-04-09 21:08:27 +09003090 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003091 final InputMethodInfo im = mIms[which];
3092 int subtypeId = mSubtypeIds[which];
3093 adapter.mCheckedItem = which;
3094 adapter.notifyDataSetChanged();
3095 hideInputMethodMenu();
3096 if (im != null) {
3097 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
3098 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003099 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003100 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003101 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003102 }
3103 }
3104 };
3105 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003106
Seigo Nonakad4474cb2015-05-04 16:53:24 -07003107 if (!isScreenLocked) {
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003108 final OnClickListener positiveListener = new OnClickListener() {
3109 @Override
3110 public void onClick(DialogInterface dialog, int whichButton) {
3111 showConfigureInputMethods();
3112 }
3113 };
satok82beadf2010-12-27 19:03:06 +09003114 mDialogBuilder.setPositiveButton(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003115 com.android.internal.R.string.configure_input_methods, positiveListener);
satok7f35c8c2010-10-07 21:13:11 +09003116 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003117 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003118 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003119 mSwitchingDialog.getWindow().setType(
3120 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09003121 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
3122 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003123 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003124 updateSystemUi(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003125 mSwitchingDialog.show();
3126 }
3127 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003128
Ken Wakasa05dbb652011-08-22 15:22:43 +09003129 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
3130 private final LayoutInflater mInflater;
3131 private final int mTextViewResourceId;
3132 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09003133 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09003134 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
3135 List<ImeSubtypeListItem> itemsList, int checkedItem) {
3136 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003137
Ken Wakasa05dbb652011-08-22 15:22:43 +09003138 mTextViewResourceId = textViewResourceId;
3139 mItemsList = itemsList;
3140 mCheckedItem = checkedItem;
Alan Viverette505e3ab2014-11-24 15:22:11 -08003141 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Ken Wakasa05dbb652011-08-22 15:22:43 +09003142 }
3143
3144 @Override
3145 public View getView(int position, View convertView, ViewGroup parent) {
3146 final View view = convertView != null ? convertView
3147 : mInflater.inflate(mTextViewResourceId, null);
3148 if (position < 0 || position >= mItemsList.size()) return view;
3149 final ImeSubtypeListItem item = mItemsList.get(position);
3150 final CharSequence imeName = item.mImeName;
3151 final CharSequence subtypeName = item.mSubtypeName;
3152 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
3153 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
3154 if (TextUtils.isEmpty(subtypeName)) {
3155 firstTextView.setText(imeName);
3156 secondTextView.setVisibility(View.GONE);
3157 } else {
3158 firstTextView.setText(subtypeName);
3159 secondTextView.setText(imeName);
3160 secondTextView.setVisibility(View.VISIBLE);
3161 }
3162 final RadioButton radioButton =
3163 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
3164 radioButton.setChecked(position == mCheckedItem);
3165 return view;
3166 }
3167 }
3168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003169 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003170 synchronized (mMethodMap) {
3171 hideInputMethodMenuLocked();
3172 }
3173 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003174
The Android Open Source Project10592532009-03-18 17:39:46 -07003175 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003176 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003177
The Android Open Source Project10592532009-03-18 17:39:46 -07003178 if (mSwitchingDialog != null) {
3179 mSwitchingDialog.dismiss();
3180 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003181 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003182
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003183 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project10592532009-03-18 17:39:46 -07003184 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003185 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003186 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003188 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003189
satok42c5a162011-05-26 16:46:14 +09003190 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003191 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003192 // TODO: Make this work even for non-current users?
3193 if (!calledFromValidUser()) {
3194 return false;
3195 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003196 synchronized (mMethodMap) {
3197 if (mContext.checkCallingOrSelfPermission(
3198 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3199 != PackageManager.PERMISSION_GRANTED) {
3200 throw new SecurityException(
3201 "Requires permission "
3202 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3203 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003205 long ident = Binder.clearCallingIdentity();
3206 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003207 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003208 } finally {
3209 Binder.restoreCallingIdentity(ident);
3210 }
3211 }
3212 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003213
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003214 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3215 // Make sure this is a valid input method.
3216 InputMethodInfo imm = mMethodMap.get(id);
3217 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003218 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003219 }
3220
satokd87c2592010-09-29 11:52:06 +09003221 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3222 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003223
satokd87c2592010-09-29 11:52:06 +09003224 if (enabled) {
3225 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3226 if (pair.first.equals(id)) {
3227 // We are enabling this input method, but it is already enabled.
3228 // Nothing to do. The previous state was enabled.
3229 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003230 }
3231 }
satokd87c2592010-09-29 11:52:06 +09003232 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3233 // Previous state was disabled.
3234 return false;
3235 } else {
3236 StringBuilder builder = new StringBuilder();
3237 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3238 builder, enabledInputMethodsList, id)) {
3239 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003240 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003241 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3242 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3243 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003244 }
3245 // Previous state was enabled.
3246 return true;
3247 } else {
3248 // We are disabling the input method but it is already disabled.
3249 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003250 return false;
3251 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003252 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003253 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003254
satok723a27e2010-11-11 14:58:11 +09003255 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3256 boolean setSubtypeOnly) {
3257 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003258 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003259
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003260 mCurUserActionNotificationSequenceNumber =
3261 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3262 if (DEBUG) {
3263 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3264 + mCurUserActionNotificationSequenceNumber);
3265 }
3266
3267 if (mCurClient != null && mCurClient.client != null) {
3268 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3269 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003270 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003271 }
3272
satok723a27e2010-11-11 14:58:11 +09003273 // Set Subtype here
3274 if (imi == null || subtypeId < 0) {
3275 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003276 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003277 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003278 if (subtypeId < imi.getSubtypeCount()) {
3279 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3280 mSettings.putSelectedSubtype(subtype.hashCode());
3281 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003282 } else {
3283 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003284 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003285 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003286 }
satokab751aa2010-09-14 19:17:36 +09003287 }
satok723a27e2010-11-11 14:58:11 +09003288
satok4c0e7152012-06-20 20:08:44 +09003289 // Workaround.
3290 // ASEC is not ready in the IMMS constructor. Accordingly, forward-locked
3291 // IMEs are not recognized and considered uninstalled.
3292 // Actually, we can't move everything after SystemReady because
3293 // IMMS needs to run in the encryption lock screen. So, we just skip changing
3294 // the default IME here and try cheking the default IME again in systemReady().
3295 // TODO: Do nothing before system ready and implement a separated logic for
3296 // the encryption lock screen.
3297 // TODO: ASEC should be ready before IMMS is instantiated.
3298 if (mSystemReady && !setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003299 // Set InputMethod here
3300 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3301 }
3302 }
3303
3304 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3305 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3306 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3307 // newDefaultIme is empty when there is no candidate for the selected IME.
3308 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3309 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3310 if (subtypeHashCode != null) {
3311 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003312 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok723a27e2010-11-11 14:58:11 +09003313 imi, Integer.valueOf(subtypeHashCode));
3314 } catch (NumberFormatException e) {
3315 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3316 }
3317 }
3318 }
3319 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003320 }
3321
satok4e4569d2010-11-19 18:45:53 +09003322 // If there are no selected shortcuts, tries finding the most applicable ones.
3323 private Pair<InputMethodInfo, InputMethodSubtype>
3324 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3325 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3326 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003327 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003328 boolean foundInSystemIME = false;
3329
3330 // Search applicable subtype for each InputMethodInfo
3331 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003332 final String imiId = imi.getId();
3333 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3334 continue;
3335 }
satokcd7cd292010-11-20 15:46:23 +09003336 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003337 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003338 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003339 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003340 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003341 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003342 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003343 }
satokdf31ae62011-01-15 06:19:44 +09003344 // 2. Search by the system locale from enabledSubtypes.
3345 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003346 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003347 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003348 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003349 }
satoka86f5e42011-09-02 17:12:42 +09003350 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003351 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003352 final ArrayList<InputMethodSubtype> subtypesForSearch =
3353 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003354 ? InputMethodUtils.getSubtypes(imi)
3355 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003356 // 4. Search by the current subtype's locale from all subtypes.
3357 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003358 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003359 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003360 }
3361 // 5. Search by the system locale from all subtypes.
3362 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003363 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003364 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003365 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003366 }
satokcd7cd292010-11-20 15:46:23 +09003367 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003368 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003369 // The current input method is the most applicable IME.
3370 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003371 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003372 break;
satok7599a7f2010-12-22 13:45:23 +09003373 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003374 // The system input method is 2nd applicable IME.
3375 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003376 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003377 if ((imi.getServiceInfo().applicationInfo.flags
3378 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3379 foundInSystemIME = true;
3380 }
satok4e4569d2010-11-19 18:45:53 +09003381 }
3382 }
3383 }
3384 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003385 if (mostApplicableIMI != null) {
3386 Slog.w(TAG, "Most applicable shortcut input method was:"
3387 + mostApplicableIMI.getId());
3388 if (mostApplicableSubtype != null) {
3389 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3390 + "," + mostApplicableSubtype.getMode() + ","
3391 + mostApplicableSubtype.getLocale());
3392 }
3393 }
satok4e4569d2010-11-19 18:45:53 +09003394 }
satokcd7cd292010-11-20 15:46:23 +09003395 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09003396 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09003397 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003398 } else {
3399 return null;
3400 }
3401 }
3402
satokab751aa2010-09-14 19:17:36 +09003403 /**
3404 * @return Return the current subtype of this input method.
3405 */
satok42c5a162011-05-26 16:46:14 +09003406 @Override
satokab751aa2010-09-14 19:17:36 +09003407 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003408 // TODO: Make this work even for non-current users?
3409 if (!calledFromValidUser()) {
3410 return null;
3411 }
3412 synchronized (mMethodMap) {
3413 return getCurrentInputMethodSubtypeLocked();
3414 }
3415 }
3416
3417 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003418 if (mCurMethodId == null) {
3419 return null;
3420 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003421 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003422 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3423 if (imi == null || imi.getSubtypeCount() == 0) {
3424 return null;
satok4e4569d2010-11-19 18:45:53 +09003425 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003426 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003427 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3428 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003429 if (subtypeId == NOT_A_SUBTYPE_ID) {
3430 // If there are no selected subtypes, the framework will try to find
3431 // the most applicable subtype from explicitly or implicitly enabled
3432 // subtypes.
3433 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003434 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003435 // If there is only one explicitly or implicitly enabled subtype,
3436 // just returns it.
3437 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3438 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3439 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003440 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003441 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003442 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003443 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003444 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003445 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3446 true);
satok4e4569d2010-11-19 18:45:53 +09003447 }
satok3ef8b292010-11-23 06:06:29 +09003448 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003449 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003450 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003451 }
3452 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003453 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003454 }
3455
satokf3db1af2010-11-23 13:34:33 +09003456 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
3457 InputMethodSubtype subtype) {
3458 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
3459 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
3460 } else {
3461 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3462 subtypes.add(subtype);
3463 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
3464 }
3465 }
3466
satok4e4569d2010-11-19 18:45:53 +09003467 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003468 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003469 @Override
satok4e4569d2010-11-19 18:45:53 +09003470 public List getShortcutInputMethodsAndSubtypes() {
3471 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09003472 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09003473 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003474 // If there are no selected shortcut subtypes, the framework will try to find
3475 // the most applicable subtype from all subtypes whose mode is
3476 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003477 Pair<InputMethodInfo, InputMethodSubtype> info =
3478 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003479 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003480 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003481 ret.add(info.first);
3482 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003483 }
satok3da92232011-01-11 22:46:30 +09003484 return ret;
satokf3db1af2010-11-23 13:34:33 +09003485 }
satokf3db1af2010-11-23 13:34:33 +09003486 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3487 ret.add(imi);
3488 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3489 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003490 }
3491 }
satokf3db1af2010-11-23 13:34:33 +09003492 return ret;
satok4e4569d2010-11-19 18:45:53 +09003493 }
3494 }
3495
satok42c5a162011-05-26 16:46:14 +09003496 @Override
satokb66d2872010-11-10 01:04:04 +09003497 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003498 // TODO: Make this work even for non-current users?
3499 if (!calledFromValidUser()) {
3500 return false;
3501 }
satokb66d2872010-11-10 01:04:04 +09003502 synchronized (mMethodMap) {
3503 if (subtype != null && mCurMethodId != null) {
3504 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003505 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003506 if (subtypeId != NOT_A_SUBTYPE_ID) {
3507 setInputMethodLocked(mCurMethodId, subtypeId);
3508 return true;
3509 }
3510 }
3511 return false;
3512 }
3513 }
3514
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003515 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003516 private static class InputMethodFileManager {
3517 private static final String SYSTEM_PATH = "system";
3518 private static final String INPUT_METHOD_PATH = "inputmethod";
3519 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3520 private static final String NODE_SUBTYPES = "subtypes";
3521 private static final String NODE_SUBTYPE = "subtype";
3522 private static final String NODE_IMI = "imi";
3523 private static final String ATTR_ID = "id";
3524 private static final String ATTR_LABEL = "label";
3525 private static final String ATTR_ICON = "icon";
3526 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3527 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3528 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3529 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3530 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3531 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003532 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
satoke7c6998e2011-06-03 17:57:59 +09003533 new HashMap<String, List<InputMethodSubtype>>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003534 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003535 if (methodMap == null) {
3536 throw new NullPointerException("methodMap is null");
3537 }
3538 mMethodMap = methodMap;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003539 final File systemDir = userId == UserHandle.USER_OWNER
3540 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3541 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003542 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3543 if (!inputMethodDir.mkdirs()) {
3544 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3545 }
3546 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3547 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3548 if (!subtypeFile.exists()) {
3549 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003550 writeAdditionalInputMethodSubtypes(
3551 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003552 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003553 readAdditionalInputMethodSubtypes(
3554 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003555 }
3556 }
3557
3558 private void deleteAllInputMethodSubtypes(String imiId) {
3559 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003560 mAdditionalSubtypesMap.remove(imiId);
3561 writeAdditionalInputMethodSubtypes(
3562 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003563 }
3564 }
3565
3566 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003567 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003568 synchronized (mMethodMap) {
3569 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3570 final int N = additionalSubtypes.length;
3571 for (int i = 0; i < N; ++i) {
3572 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003573 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003574 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003575 } else {
3576 Slog.w(TAG, "Duplicated subtype definition found: "
3577 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003578 }
3579 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003580 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3581 writeAdditionalInputMethodSubtypes(
3582 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003583 }
3584 }
3585
3586 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3587 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003588 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003589 }
3590 }
3591
3592 private static void writeAdditionalInputMethodSubtypes(
3593 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3594 HashMap<String, InputMethodInfo> methodMap) {
3595 // Safety net for the case that this function is called before methodMap is set.
3596 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3597 FileOutputStream fos = null;
3598 try {
3599 fos = subtypesFile.startWrite();
3600 final XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003601 out.setOutput(fos, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003602 out.startDocument(null, true);
3603 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3604 out.startTag(null, NODE_SUBTYPES);
3605 for (String imiId : allSubtypes.keySet()) {
3606 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3607 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3608 continue;
3609 }
3610 out.startTag(null, NODE_IMI);
3611 out.attribute(null, ATTR_ID, imiId);
3612 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3613 final int N = subtypesList.size();
3614 for (int i = 0; i < N; ++i) {
3615 final InputMethodSubtype subtype = subtypesList.get(i);
3616 out.startTag(null, NODE_SUBTYPE);
3617 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3618 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3619 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3620 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3621 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3622 out.attribute(null, ATTR_IS_AUXILIARY,
3623 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3624 out.endTag(null, NODE_SUBTYPE);
3625 }
3626 out.endTag(null, NODE_IMI);
3627 }
3628 out.endTag(null, NODE_SUBTYPES);
3629 out.endDocument();
3630 subtypesFile.finishWrite(fos);
3631 } catch (java.io.IOException e) {
3632 Slog.w(TAG, "Error writing subtypes", e);
3633 if (fos != null) {
3634 subtypesFile.failWrite(fos);
3635 }
3636 }
3637 }
3638
3639 private static void readAdditionalInputMethodSubtypes(
3640 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3641 if (allSubtypes == null || subtypesFile == null) return;
3642 allSubtypes.clear();
3643 FileInputStream fis = null;
3644 try {
3645 fis = subtypesFile.openRead();
3646 final XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003647 parser.setInput(fis, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003648 int type = parser.getEventType();
3649 // Skip parsing until START_TAG
3650 while ((type = parser.next()) != XmlPullParser.START_TAG
3651 && type != XmlPullParser.END_DOCUMENT) {}
3652 String firstNodeName = parser.getName();
3653 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3654 throw new XmlPullParserException("Xml doesn't start with subtypes");
3655 }
3656 final int depth =parser.getDepth();
3657 String currentImiId = null;
3658 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3659 while (((type = parser.next()) != XmlPullParser.END_TAG
3660 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3661 if (type != XmlPullParser.START_TAG)
3662 continue;
3663 final String nodeName = parser.getName();
3664 if (NODE_IMI.equals(nodeName)) {
3665 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3666 if (TextUtils.isEmpty(currentImiId)) {
3667 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3668 continue;
3669 }
3670 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
3671 allSubtypes.put(currentImiId, tempSubtypesArray);
3672 } else if (NODE_SUBTYPE.equals(nodeName)) {
3673 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3674 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3675 continue;
3676 }
3677 final int icon = Integer.valueOf(
3678 parser.getAttributeValue(null, ATTR_ICON));
3679 final int label = Integer.valueOf(
3680 parser.getAttributeValue(null, ATTR_LABEL));
3681 final String imeSubtypeLocale =
3682 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3683 final String imeSubtypeMode =
3684 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3685 final String imeSubtypeExtraValue =
3686 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003687 final boolean isAuxiliary = "1".equals(String.valueOf(
3688 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003689 final InputMethodSubtype subtype = new InputMethodSubtypeBuilder()
3690 .setSubtypeNameResId(label)
3691 .setSubtypeIconResId(icon)
3692 .setSubtypeLocale(imeSubtypeLocale)
3693 .setSubtypeMode(imeSubtypeMode)
3694 .setSubtypeExtraValue(imeSubtypeExtraValue)
3695 .setIsAuxiliary(isAuxiliary)
3696 .build();
satoke7c6998e2011-06-03 17:57:59 +09003697 tempSubtypesArray.add(subtype);
3698 }
3699 }
3700 } catch (XmlPullParserException e) {
3701 Slog.w(TAG, "Error reading subtypes: " + e);
3702 return;
3703 } catch (java.io.IOException e) {
3704 Slog.w(TAG, "Error reading subtypes: " + e);
3705 return;
3706 } catch (NumberFormatException e) {
3707 Slog.w(TAG, "Error reading subtypes: " + e);
3708 return;
3709 } finally {
3710 if (fis != null) {
3711 try {
3712 fis.close();
3713 } catch (java.io.IOException e1) {
3714 Slog.w(TAG, "Failed to close.");
3715 }
3716 }
3717 }
3718 }
3719 }
3720
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003721 @Override
3722 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3723 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3724 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003725
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003726 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3727 + Binder.getCallingPid()
3728 + ", uid=" + Binder.getCallingUid());
3729 return;
3730 }
3731
3732 IInputMethod method;
3733 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003734
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003735 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003736
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003737 synchronized (mMethodMap) {
3738 p.println("Current Input Method Manager state:");
3739 int N = mMethodList.size();
3740 p.println(" Input Methods:");
3741 for (int i=0; i<N; i++) {
3742 InputMethodInfo info = mMethodList.get(i);
3743 p.println(" InputMethod #" + i + ":");
3744 info.dump(p, " ");
3745 }
3746 p.println(" Clients:");
3747 for (ClientState ci : mClients.values()) {
3748 p.println(" Client " + ci + ":");
3749 p.println(" client=" + ci.client);
3750 p.println(" inputContext=" + ci.inputContext);
3751 p.println(" sessionRequested=" + ci.sessionRequested);
3752 p.println(" curSession=" + ci.curSession);
3753 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003754 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003755 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003756 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3757 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003758 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3759 + " mBoundToMethod=" + mBoundToMethod);
3760 p.println(" mCurToken=" + mCurToken);
3761 p.println(" mCurIntent=" + mCurIntent);
3762 method = mCurMethod;
3763 p.println(" mCurMethod=" + mCurMethod);
3764 p.println(" mEnabledSession=" + mEnabledSession);
3765 p.println(" mShowRequested=" + mShowRequested
3766 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3767 + " mShowForced=" + mShowForced
3768 + " mInputShown=" + mInputShown);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003769 p.println(" mCurUserActionNotificationSequenceNumber="
3770 + mCurUserActionNotificationSequenceNumber);
Dianne Hackbornd4e6d462014-05-16 16:32:37 -07003771 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mScreenOn);
Yohei Yukawa81482972015-06-04 00:58:59 -07003772 p.println(" mSettingsObserver=" + mSettingsObserver);
Yohei Yukawad7248862015-06-03 23:56:12 -07003773 p.println(" mSwitchingController:");
3774 mSwitchingController.dump(p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003775 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003776
Jeff Brownb88102f2010-09-08 11:49:43 -07003777 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003778 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003779 pw.flush();
3780 try {
3781 client.client.asBinder().dump(fd, args);
3782 } catch (RemoteException e) {
3783 p.println("Input method client dead: " + e);
3784 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003785 } else {
3786 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003787 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003788
Jeff Brownb88102f2010-09-08 11:49:43 -07003789 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003790 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003791 pw.flush();
3792 try {
3793 method.asBinder().dump(fd, args);
3794 } catch (RemoteException e) {
3795 p.println("Input method service dead: " + e);
3796 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003797 } else {
3798 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003799 }
3800 }
3801}