blob: 0a0ee3fd36dde33ffa8fe81be94fbb413a9f3c65 [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;
Adam Lesinskief2ea1f2013-12-05 16:48:06 -080035import com.android.server.statusbar.StatusBarManagerService;
satok01038492012-04-09 21:08:27 +090036import com.android.server.wm.WindowManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037
satoke7c6998e2011-06-03 17:57:59 +090038import org.xmlpull.v1.XmlPullParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import org.xmlpull.v1.XmlPullParserException;
satoke7c6998e2011-06-03 17:57:59 +090040import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041
42import android.app.ActivityManagerNative;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090043import android.app.AppGlobals;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import android.app.AlertDialog;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +090045import android.app.AppOpsManager;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090046import android.app.IUserSwitchObserver;
satokf90a33e2011-07-19 11:55:52 +090047import android.app.KeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +090048import android.app.Notification;
49import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070050import android.app.PendingIntent;
satok5b927c432012-05-01 20:09:34 +090051import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.content.ComponentName;
53import android.content.ContentResolver;
54import android.content.Context;
55import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.content.DialogInterface.OnCancelListener;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +090057import android.content.DialogInterface.OnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090059import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070061import android.content.pm.ApplicationInfo;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090062import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import android.content.pm.PackageManager;
64import android.content.pm.ResolveInfo;
65import android.content.pm.ServiceInfo;
Amith Yamasani734983f2014-03-04 16:48:05 -080066import android.content.pm.UserInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070067import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.content.res.Resources;
69import android.content.res.TypedArray;
70import android.database.ContentObserver;
Alan Viverette505e3ab2014-11-24 15:22:11 -080071import android.graphics.drawable.Drawable;
Joe Onorato857fd9b2011-01-27 15:08:35 -080072import android.inputmethodservice.InputMethodService;
Michael Wright7b5a96b2014-08-09 19:28:42 -070073import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074import android.os.Binder;
Chris Wren1ce4b6d2015-06-11 10:19:43 -040075import android.os.Bundle;
Seigo Nonakae27dc2b2015-08-14 18:21:27 -070076import android.os.Debug;
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;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700117import android.view.inputmethod.InputMethodManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900118import android.view.inputmethod.InputMethodSubtype;
Yohei Yukawa443c2ba2014-09-10 14:10:30 +0900119import android.view.inputmethod.InputMethodSubtype.InputMethodSubtypeBuilder;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900120import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +0900121import android.widget.CompoundButton;
122import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900123import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +0900124import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900125import android.widget.TextView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126
satoke7c6998e2011-06-03 17:57:59 +0900127import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900129import java.io.FileInputStream;
130import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131import java.io.IOException;
132import java.io.PrintWriter;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100133import java.nio.charset.StandardCharsets;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900135import java.util.Collections;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136import java.util.HashMap;
137import 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
Seigo Nonakad4474cb2015-05-04 16:53:24 -0700149 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 1;
150 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 2;
151 static final int MSG_SHOW_IM_CONFIG = 3;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 static final int MSG_UNBIND_INPUT = 1000;
154 static final int MSG_BIND_INPUT = 1010;
155 static final int MSG_SHOW_SOFT_INPUT = 1020;
156 static final int MSG_HIDE_SOFT_INPUT = 1030;
157 static final int MSG_ATTACH_TOKEN = 1040;
158 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 static final int MSG_START_INPUT = 2000;
161 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 static final int MSG_UNBIND_METHOD = 3000;
164 static final int MSG_BIND_METHOD = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700165 static final int MSG_SET_ACTIVE = 3020;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700166 static final int MSG_SET_INTERACTIVE = 3030;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900167 static final int MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER = 3040;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800168
satok01038492012-04-09 21:08:27 +0900169 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
170
Satoshi Kataokabcacc322013-10-21 17:57:27 -0700171 static final long TIME_TO_RECONNECT = 3 * 1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800172
satokf9f01002011-05-19 21:31:50 +0900173 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
174
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900175 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
satokb6359412011-06-28 17:47:41 +0900176 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900177
satok4e4569d2010-11-19 18:45:53 +0900178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800180 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900182 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 final IWindowManager mIWindowManager;
185 final HandlerCaller mCaller;
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700186 final boolean mHasFeature;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900187 private InputMethodFileManager mFileManager;
satok01038492012-04-09 21:08:27 +0900188 private final HardKeyboardListener mHardKeyboardListener;
189 private final WindowManagerService mWindowManagerService;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900190 private final AppOpsManager mAppOpsManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800191
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900192 final InputBindResult mNoBinding = new InputBindResult(null, null, null, -1, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 // All known input methods. mMethodMap also serves as the global
195 // lock for this class.
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700196 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<>();
197 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<>();
satokf9f01002011-05-19 21:31:50 +0900198 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700199 new LruCache<>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Satoshi Kataokad787f692013-10-26 04:44:21 +0900200 private final InputMethodSubtypeSwitchingController mSwitchingController;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800201
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700202 // Used to bring IME service up to visible adjustment while it is being shown.
203 final ServiceConnection mVisibleConnection = new ServiceConnection() {
204 @Override public void onServiceConnected(ComponentName name, IBinder service) {
205 }
206
207 @Override public void onServiceDisconnected(ComponentName name) {
208 }
209 };
210 boolean mVisibleBound = false;
211
satok7cfc0ed2011-06-20 21:29:36 +0900212 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700213 private NotificationManager mNotificationManager;
214 private KeyguardManager mKeyguardManager;
215 private StatusBarManagerService mStatusBar;
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400216 private Notification.Builder mImeSwitcherNotification;
Dianne Hackborn661cd522011-08-22 00:26:20 -0700217 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900218 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900219 private boolean mNotificationShown;
satok0a1bcf42012-05-16 19:26:31 +0900220 private final boolean mImeSelectedOnBoot;
satok7cfc0ed2011-06-20 21:29:36 +0900221
Tadashi G. Takaoka8c6d4772014-08-05 15:29:17 +0900222 static class SessionState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 final ClientState client;
224 final IInputMethod method;
Jeff Brownc28867a2013-03-26 15:42:39 -0700225
226 IInputMethodSession session;
227 InputChannel channel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 @Override
230 public String toString() {
231 return "SessionState{uid " + client.uid + " pid " + client.pid
232 + " method " + Integer.toHexString(
233 System.identityHashCode(method))
234 + " session " + Integer.toHexString(
235 System.identityHashCode(session))
Jeff Brownc28867a2013-03-26 15:42:39 -0700236 + " channel " + channel
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 + "}";
238 }
239
240 SessionState(ClientState _client, IInputMethod _method,
Jeff Brownc28867a2013-03-26 15:42:39 -0700241 IInputMethodSession _session, InputChannel _channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 client = _client;
243 method = _method;
244 session = _session;
Jeff Brownc28867a2013-03-26 15:42:39 -0700245 channel = _channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 }
247 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800248
Jeff Brownc28867a2013-03-26 15:42:39 -0700249 static final class ClientState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 final IInputMethodClient client;
251 final IInputContext inputContext;
252 final int uid;
253 final int pid;
254 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 boolean sessionRequested;
257 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 @Override
260 public String toString() {
261 return "ClientState{" + Integer.toHexString(
262 System.identityHashCode(this)) + " uid " + uid
263 + " pid " + pid + "}";
264 }
265
266 ClientState(IInputMethodClient _client, IInputContext _inputContext,
267 int _uid, int _pid) {
268 client = _client;
269 inputContext = _inputContext;
270 uid = _uid;
271 pid = _pid;
272 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
273 }
274 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800275
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700276 final HashMap<IBinder, ClientState> mClients = new HashMap<>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700279 * Set once the system is ready to run third party code.
280 */
281 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800282
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700283 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 * Id of the currently selected input method.
285 */
286 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 /**
289 * The current binding sequence number, incremented every time there is
290 * a new bind performed.
291 */
292 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800293
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 /**
295 * The client that is currently bound to an input method.
296 */
297 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700300 * The last window token that gained focus.
301 */
302 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800303
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700304 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 * The input context last provided by the current client.
306 */
307 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309 /**
310 * The attributes last provided by the current client.
311 */
312 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800313
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 /**
315 * The input method ID of the input method service that we are currently
316 * connected to or in the process of connecting to.
317 */
318 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 /**
satokab751aa2010-09-14 19:17:36 +0900321 * The current subtype of the current input method.
322 */
323 private InputMethodSubtype mCurrentSubtype;
324
satok4e4569d2010-11-19 18:45:53 +0900325 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900326 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700327 mShortcutInputMethodsAndSubtypes = new HashMap<>();
satokab751aa2010-09-14 19:17:36 +0900328
John Spurlocke0980502013-10-25 11:59:29 -0400329 // Was the keyguard locked when this client became current?
330 private boolean mCurClientInKeyguard;
331
satokab751aa2010-09-14 19:17:36 +0900332 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 * Set to true if our ServiceConnection is currently actively bound to
334 * a service (whether or not we have gotten its IBinder back yet).
335 */
336 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 /**
339 * Set if the client has asked for the input method to be shown.
340 */
341 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 /**
344 * Set if we were explicitly told to show the input method.
345 */
346 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800347
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 /**
349 * Set if we were forced to be shown.
350 */
351 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 /**
354 * Set if we last told the input method to show itself.
355 */
356 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800357
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 /**
359 * The Intent used to connect to the current input method.
360 */
361 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 /**
364 * The token we have made for the currently active input method, to
365 * identify it in the future.
366 */
367 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800368
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369 /**
370 * If non-null, this is the input method service we are currently connected
371 * to.
372 */
373 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 /**
376 * Time that we last initiated a bind to the input method, to determine
377 * if we should try to disconnect and reconnect to it.
378 */
379 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800380
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 /**
382 * Have we called mCurMethod.bindInput()?
383 */
384 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 /**
387 * Currently enabled session. Only touched by service thread, not
388 * protected by a lock.
389 */
390 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 /**
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700393 * True if the device is currently interactive with user. The value is true initially.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 */
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700395 boolean mIsInteractive = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800396
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900397 int mCurUserActionNotificationSequenceNumber = 0;
398
Joe Onorato857fd9b2011-01-27 15:08:35 -0800399 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900400
401 /**
402 * A set of status bits regarding the active IME.
403 *
404 * <p>This value is a combination of following two bits:</p>
405 * <dl>
406 * <dt>{@link InputMethodService#IME_ACTIVE}</dt>
407 * <dd>
408 * If this bit is ON, connected IME is ready to accept touch/key events.
409 * </dd>
410 * <dt>{@link InputMethodService#IME_VISIBLE}</dt>
411 * <dd>
412 * If this bit is ON, some of IME view, e.g. software input, candidate view, is visible.
413 * </dd>
414 * </dl>
415 * <em>Do not update this value outside of setImeWindowStatus.</em>
416 */
Joe Onorato857fd9b2011-01-27 15:08:35 -0800417 int mImeWindowVis;
418
Ken Wakasa05dbb652011-08-22 15:22:43 +0900419 private AlertDialog.Builder mDialogBuilder;
420 private AlertDialog mSwitchingDialog;
satok01038492012-04-09 21:08:27 +0900421 private View mSwitchingDialogTitleView;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900422 private InputMethodInfo[] mIms;
423 private int[] mSubtypeIds;
satok5b927c432012-05-01 20:09:34 +0900424 private Locale mLastSystemLocale;
Michael Wright7b5a96b2014-08-09 19:28:42 -0700425 private boolean mShowImeWithHardKeyboard;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900426 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
427 private final IPackageManager mIPackageManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 class SettingsObserver extends ContentObserver {
Yohei Yukawa81482972015-06-04 00:58:59 -0700430 int mUserId;
431 boolean mRegistered = false;
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800432 String mLastEnabled = "";
433
Yohei Yukawa81482972015-06-04 00:58:59 -0700434 /**
435 * <em>This constructor must be called within the lock.</em>
436 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800437 SettingsObserver(Handler handler) {
438 super(handler);
Yohei Yukawa81482972015-06-04 00:58:59 -0700439 }
440
441 public void registerContentObserverLocked(int userId) {
442 if (mRegistered && mUserId == userId) {
443 return;
444 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 ContentResolver resolver = mContext.getContentResolver();
Yohei Yukawa81482972015-06-04 00:58:59 -0700446 if (mRegistered) {
447 mContext.getContentResolver().unregisterContentObserver(this);
448 mRegistered = false;
449 }
450 if (mUserId != userId) {
451 mLastEnabled = "";
452 mUserId = userId;
453 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700455 Settings.Secure.DEFAULT_INPUT_METHOD), false, this, userId);
satokab751aa2010-09-14 19:17:36 +0900456 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700457 Settings.Secure.ENABLED_INPUT_METHODS), false, this, userId);
satokb6109bb2011-02-03 22:24:54 +0900458 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700459 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this, userId);
Michael Wright7b5a96b2014-08-09 19:28:42 -0700460 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700461 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD), false, this, userId);
462 mRegistered = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800464
Michael Wright7b5a96b2014-08-09 19:28:42 -0700465 @Override public void onChange(boolean selfChange, Uri uri) {
466 final Uri showImeUri =
467 Settings.Secure.getUriFor(Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 synchronized (mMethodMap) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700469 if (showImeUri.equals(uri)) {
470 updateKeyboardFromSettingsLocked();
471 } else {
472 boolean enabledChanged = false;
473 String newEnabled = mSettings.getEnabledInputMethodsStr();
474 if (!mLastEnabled.equals(newEnabled)) {
475 mLastEnabled = newEnabled;
476 enabledChanged = true;
477 }
478 updateInputMethodsFromSettingsLocked(enabledChanged);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800479 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 }
481 }
Yohei Yukawa81482972015-06-04 00:58:59 -0700482
483 @Override
484 public String toString() {
485 return "SettingsObserver{mUserId=" + mUserId + " mRegistered=" + mRegistered
486 + " mLastEnabled=" + mLastEnabled + "}";
487 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800489
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900490 class ImmsBroadcastReceiver extends android.content.BroadcastReceiver {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900491 @Override
492 public void onReceive(Context context, Intent intent) {
493 final String action = intent.getAction();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700494 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900495 hideInputMethodMenu();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700496 // No need to update mIsInteractive
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900497 return;
Amith Yamasani734983f2014-03-04 16:48:05 -0800498 } else if (Intent.ACTION_USER_ADDED.equals(action)
499 || Intent.ACTION_USER_REMOVED.equals(action)) {
Kenny Guy2a764942014-04-02 13:29:20 +0100500 updateCurrentProfileIds();
Amith Yamasani734983f2014-03-04 16:48:05 -0800501 return;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700502 } else if (Intent.ACTION_SETTING_RESTORED.equals(action)) {
503 final String name = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
504 if (Settings.Secure.ENABLED_INPUT_METHODS.equals(name)) {
505 final String prevValue = intent.getStringExtra(
506 Intent.EXTRA_SETTING_PREVIOUS_VALUE);
507 final String newValue = intent.getStringExtra(
508 Intent.EXTRA_SETTING_NEW_VALUE);
509 restoreEnabledInputMethods(mContext, prevValue, newValue);
510 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900511 } else {
512 Slog.w(TAG, "Unexpected intent " + intent);
513 }
514 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800516
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700517 // Apply the results of a restore operation to the set of enabled IMEs. Note that this
518 // does not attempt to validate on the fly with any installed device policy, so must only
519 // be run in the context of initial device setup.
520 //
521 // TODO: Move this method to InputMethodUtils with adding unit tests.
522 static void restoreEnabledInputMethods(Context context, String prevValue, String newValue) {
523 if (DEBUG_RESTORE) {
524 Slog.i(TAG, "Restoring enabled input methods:");
525 Slog.i(TAG, "prev=" + prevValue);
526 Slog.i(TAG, " new=" + newValue);
527 }
528 // 'new' is the just-restored state, 'prev' is what was in settings prior to the restore
Seigo Nonaka2028dda2015-07-06 17:41:24 +0900529 ArrayMap<String, ArraySet<String>> prevMap =
530 InputMethodUtils.parseInputMethodsAndSubtypesString(prevValue);
531 ArrayMap<String, ArraySet<String>> newMap =
532 InputMethodUtils.parseInputMethodsAndSubtypesString(newValue);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700533
534 // Merge the restored ime+subtype enabled states into the live state
535 for (ArrayMap.Entry<String, ArraySet<String>> entry : newMap.entrySet()) {
536 final String imeId = entry.getKey();
537 ArraySet<String> prevSubtypes = prevMap.get(imeId);
538 if (prevSubtypes == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700539 prevSubtypes = new ArraySet<>(2);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700540 prevMap.put(imeId, prevSubtypes);
541 }
542 prevSubtypes.addAll(entry.getValue());
543 }
544
545 final String mergedImesAndSubtypesString = buildInputMethodsAndSubtypesString(prevMap);
546 if (DEBUG_RESTORE) {
547 Slog.i(TAG, "Merged IME string:");
548 Slog.i(TAG, " " + mergedImesAndSubtypesString);
549 }
550 Settings.Secure.putString(context.getContentResolver(),
551 Settings.Secure.ENABLED_INPUT_METHODS, mergedImesAndSubtypesString);
552 }
553
554 // TODO: Move this method to InputMethodUtils with adding unit tests.
555 static String buildInputMethodsAndSubtypesString(ArrayMap<String, ArraySet<String>> map) {
556 // we want to use the canonical InputMethodSettings implementation,
557 // so we convert data structures first.
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700558 List<Pair<String, ArrayList<String>>> imeMap = new ArrayList<>(4);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700559 for (ArrayMap.Entry<String, ArraySet<String>> entry : map.entrySet()) {
560 final String imeName = entry.getKey();
561 final ArraySet<String> subtypeSet = entry.getValue();
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700562 final ArrayList<String> subtypes = new ArrayList<>(2);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700563 if (subtypeSet != null) {
564 subtypes.addAll(subtypeSet);
565 }
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700566 imeMap.add(new Pair<>(imeName, subtypes));
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700567 }
568 return InputMethodSettings.buildInputMethodsSettingString(imeMap);
569 }
570
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800571 class MyPackageMonitor extends PackageMonitor {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900572 private boolean isChangingPackagesOfCurrentUser() {
573 final int userId = getChangingUserId();
574 final boolean retval = userId == mSettings.getCurrentUserId();
575 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900576 if (!retval) {
577 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
578 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900579 }
580 return retval;
581 }
582
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800583 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800584 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900585 if (!isChangingPackagesOfCurrentUser()) {
586 return false;
587 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800588 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900589 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800590 final int N = mMethodList.size();
591 if (curInputMethodId != null) {
592 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800593 InputMethodInfo imi = mMethodList.get(i);
594 if (imi.getId().equals(curInputMethodId)) {
595 for (String pkg : packages) {
596 if (imi.getPackageName().equals(pkg)) {
597 if (!doit) {
598 return true;
599 }
satok723a27e2010-11-11 14:58:11 +0900600 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800601 chooseNewDefaultIMELocked();
602 return true;
603 }
604 }
605 }
606 }
607 }
608 }
609 return false;
610 }
611
612 @Override
613 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900614 if (!isChangingPackagesOfCurrentUser()) {
615 return;
616 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800617 synchronized (mMethodMap) {
618 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900619 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800620 final int N = mMethodList.size();
621 if (curInputMethodId != null) {
622 for (int i=0; i<N; i++) {
623 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900624 final String imiId = imi.getId();
625 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800626 curIm = imi;
627 }
satoke7c6998e2011-06-03 17:57:59 +0900628
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800629 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900630 if (isPackageModified(imi.getPackageName())) {
631 mFileManager.deleteAllInputMethodSubtypes(imiId);
632 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800633 if (change == PACKAGE_TEMPORARY_CHANGE
634 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800635 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800636 + imi.getComponent());
637 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 }
639 }
640 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800641
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900642 buildInputMethodListLocked(
643 mMethodList, mMethodMap, false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800644
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800646
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800647 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800648 int change = isPackageDisappearing(curIm.getPackageName());
649 if (change == PACKAGE_TEMPORARY_CHANGE
650 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800651 ServiceInfo si = null;
652 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900653 si = mIPackageManager.getServiceInfo(
654 curIm.getComponent(), 0, mSettings.getCurrentUserId());
655 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800656 }
657 if (si == null) {
658 // Uh oh, current input method is no longer around!
659 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800660 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900661 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800662 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800663 changed = true;
664 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800665 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900666 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800667 }
668 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800669 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800670 }
satokab751aa2010-09-14 19:17:36 +0900671
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800672 if (curIm == null) {
673 // We currently don't have a default input method... is
674 // one now available?
675 changed = chooseNewDefaultIMELocked();
Yohei Yukawa54d512c2015-05-19 22:15:02 -0700676 } else if (!changed && isPackageModified(curIm.getPackageName())) {
677 // Even if the current input method is still available, mCurrentSubtype could
678 // be obsolete when the package is modified in practice.
679 changed = true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800680 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800681
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800682 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800683 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684 }
685 }
686 }
687 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800688
Jeff Brownc28867a2013-03-26 15:42:39 -0700689 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900690 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -0700691 private final IInputMethod mMethod;
692 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800693
Jeff Brownc28867a2013-03-26 15:42:39 -0700694 MethodCallback(InputMethodManagerService imms, IInputMethod method,
695 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900696 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -0700697 mMethod = method;
698 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800699 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800700
satoke7c6998e2011-06-03 17:57:59 +0900701 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -0700702 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -0700703 long ident = Binder.clearCallingIdentity();
704 try {
705 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
706 } finally {
707 Binder.restoreCallingIdentity(ident);
708 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709 }
710 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800711
satok01038492012-04-09 21:08:27 +0900712 private class HardKeyboardListener
713 implements WindowManagerService.OnHardKeyboardStatusChangeListener {
714 @Override
Michael Wright7b5a96b2014-08-09 19:28:42 -0700715 public void onHardKeyboardStatusChange(boolean available) {
716 mHandler.sendMessage(mHandler.obtainMessage(MSG_HARD_KEYBOARD_SWITCH_CHANGED,
717 available ? 1 : 0));
satok01038492012-04-09 21:08:27 +0900718 }
719
Michael Wright7b5a96b2014-08-09 19:28:42 -0700720 public void handleHardKeyboardStatusChange(boolean available) {
satok01038492012-04-09 21:08:27 +0900721 if (DEBUG) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700722 Slog.w(TAG, "HardKeyboardStatusChanged: available=" + available);
satok01038492012-04-09 21:08:27 +0900723 }
724 synchronized(mMethodMap) {
725 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
726 && mSwitchingDialog.isShowing()) {
727 mSwitchingDialogTitleView.findViewById(
728 com.android.internal.R.id.hard_keyboard_section).setVisibility(
729 available ? View.VISIBLE : View.GONE);
730 }
731 }
732 }
733 }
734
735 public InputMethodManagerService(Context context, WindowManagerService windowManager) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900736 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800737 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800738 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800739 mHandler = new Handler(this);
Yohei Yukawa81482972015-06-04 00:58:59 -0700740 // Note: SettingsObserver doesn't register observers in its constructor.
741 mSettingsObserver = new SettingsObserver(mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800742 mIWindowManager = IWindowManager.Stub.asInterface(
743 ServiceManager.getService(Context.WINDOW_SERVICE));
Mita Yuned218c72012-12-06 17:18:25 -0800744 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900745 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746 public void executeMessage(Message msg) {
747 handleMessage(msg);
748 }
Mita Yuned218c72012-12-06 17:18:25 -0800749 }, true /*asyncHandler*/);
satok01038492012-04-09 21:08:27 +0900750 mWindowManagerService = windowManager;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900751 mAppOpsManager = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE);
satok01038492012-04-09 21:08:27 +0900752 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700753 mHasFeature = context.getPackageManager().hasSystemFeature(
754 PackageManager.FEATURE_INPUT_METHODS);
satok7cfc0ed2011-06-20 21:29:36 +0900755
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400756 Bundle extras = new Bundle();
757 extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
758 mImeSwitcherNotification = new Notification.Builder(mContext)
759 .setSmallIcon(com.android.internal.R.drawable.ic_notification_ime_default)
760 .setWhen(0)
761 .setOngoing(true)
762 .addExtras(extras)
763 .setCategory(Notification.CATEGORY_SYSTEM)
764 .setColor(com.android.internal.R.color.system_notification_accent_color);
Daniel Sandler590d5152012-06-14 16:10:13 -0400765
satok7cfc0ed2011-06-20 21:29:36 +0900766 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900767 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900768
769 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900770
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900771 final IntentFilter broadcastFilter = new IntentFilter();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900772 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Amith Yamasani734983f2014-03-04 16:48:05 -0800773 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
774 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700775 broadcastFilter.addAction(Intent.ACTION_SETTING_RESTORED);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900776 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800777
satok7cfc0ed2011-06-20 21:29:36 +0900778 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900779 int userId = 0;
780 try {
781 ActivityManagerNative.getDefault().registerUserSwitchObserver(
782 new IUserSwitchObserver.Stub() {
783 @Override
784 public void onUserSwitching(int newUserId, IRemoteCallback reply) {
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900785 synchronized(mMethodMap) {
786 switchUserLocked(newUserId);
787 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900788 if (reply != null) {
789 try {
790 reply.sendResult(null);
791 } catch (RemoteException e) {
792 }
793 }
794 }
795
796 @Override
797 public void onUserSwitchComplete(int newUserId) throws RemoteException {
798 }
Kenny Guy42979622015-04-13 18:03:05 +0000799
800 @Override
801 public void onForegroundProfileSwitch(int newProfileId) {
802 // Ignore.
803 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900804 });
805 userId = ActivityManagerNative.getDefault().getCurrentUser().id;
806 } catch (RemoteException e) {
807 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
808 }
satok81f8b7c2012-12-04 20:42:56 +0900809 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
satok913a8922010-08-26 21:53:41 +0900810
satokd87c2592010-09-29 11:52:06 +0900811 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900812 mSettings = new InputMethodSettings(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900813 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId);
Svet Ganovadc1cf42015-06-15 16:36:24 -0700814
815 // Let the package manager query which are the default imes
816 // as they get certain permissions granted by default.
817 PackageManagerInternal packageManagerInternal = LocalServices.getService(
818 PackageManagerInternal.class);
819 packageManagerInternal.setImePackagesProvider(
820 new PackageManagerInternal.PackagesProvider() {
821 @Override
822 public String[] getPackages(int userId) {
823 synchronized (mMethodMap) {
824 final int currentUserId = mSettings.getCurrentUserId();
825 // TODO: We are switching the current user id in the settings
826 // object to query it and then revert the user id. Ideally, we
827 // should call a API in settings with the user id as an argument.
828 mSettings.setCurrentUserId(userId);
829 List<InputMethodInfo> imes = mSettings
830 .getEnabledInputMethodListLocked();
831 String[] packageNames = null;
832 if (imes != null) {
833 final int imeCount = imes.size();
834 packageNames = new String[imeCount];
835 for (int i = 0; i < imeCount; i++) {
836 InputMethodInfo ime = imes.get(i);
837 packageNames[i] = ime.getPackageName();
838 }
839 }
840 mSettings.setCurrentUserId(currentUserId);
841 return packageNames;
842 }
843 }
844 });
845
Kenny Guy2a764942014-04-02 13:29:20 +0100846 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900847 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900848 synchronized (mMethodMap) {
849 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
850 mSettings, context);
851 }
satok0a1bcf42012-05-16 19:26:31 +0900852
853 // Just checking if defaultImiId is empty or not
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900854 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900855 if (DEBUG) {
856 Slog.d(TAG, "Initial default ime = " + defaultImiId);
857 }
satok0a1bcf42012-05-16 19:26:31 +0900858 mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
859
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900860 synchronized (mMethodMap) {
861 buildInputMethodListLocked(mMethodList, mMethodMap,
862 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
863 }
satokd87c2592010-09-29 11:52:06 +0900864 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800865
satok0a1bcf42012-05-16 19:26:31 +0900866 if (!mImeSelectedOnBoot) {
867 Slog.w(TAG, "No IME selected. Choose the most applicable IME.");
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900868 synchronized (mMethodMap) {
869 resetDefaultImeLocked(context);
870 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800871 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800872
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900873 synchronized (mMethodMap) {
Yohei Yukawa81482972015-06-04 00:58:59 -0700874 mSettingsObserver.registerContentObserverLocked(userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900875 updateFromSettingsLocked(true);
876 }
satok5b927c432012-05-01 20:09:34 +0900877
878 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME
879 // according to the new system locale.
880 final IntentFilter filter = new IntentFilter();
881 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
882 mContext.registerReceiver(
883 new BroadcastReceiver() {
884 @Override
885 public void onReceive(Context context, Intent intent) {
886 synchronized(mMethodMap) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900887 resetStateIfCurrentLocaleChangedLocked();
satok5b927c432012-05-01 20:09:34 +0900888 }
889 }
890 }, filter);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700891 LocalServices.addService(InputMethodManagerInternal.class, new LocalServiceImpl(mHandler));
satok5b927c432012-05-01 20:09:34 +0900892 }
893
satok5b927c432012-05-01 20:09:34 +0900894 private void resetDefaultImeLocked(Context context) {
895 // Do not reset the default (current) IME when it is a 3rd-party IME
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900896 if (mCurMethodId != null
897 && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +0900898 return;
899 }
900
901 InputMethodInfo defIm = null;
902 for (InputMethodInfo imi : mMethodList) {
Yohei Yukawa6aa03782015-02-21 03:00:22 +0900903 if (defIm == null && mSystemReady) {
904 final Locale systemLocale = context.getResources().getConfiguration().locale;
905 if (InputMethodUtils.isSystemImeThatHasSubtypeOf(imi, context,
906 true /* checkDefaultAttribute */, systemLocale, false /* checkCountry */,
907 InputMethodUtils.SUBTYPE_MODE_ANY)) {
satok5b927c432012-05-01 20:09:34 +0900908 defIm = imi;
909 Slog.i(TAG, "Selected default: " + imi.getId());
910 }
911 }
912 }
913 if (defIm == null && mMethodList.size() > 0) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900914 defIm = InputMethodUtils.getMostApplicableDefaultIME(
915 mSettings.getEnabledInputMethodListLocked());
Hyejin Kim5baaaac2014-10-27 17:17:06 +0900916 if (defIm != null) {
917 Slog.i(TAG, "Default found, using " + defIm.getId());
918 } else {
919 Slog.i(TAG, "No default found");
920 }
satok5b927c432012-05-01 20:09:34 +0900921 }
922 if (defIm != null) {
923 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
924 }
925 }
926
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900927 private void resetAllInternalStateLocked(final boolean updateOnlyWhenLocaleChanged,
928 final boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900929 if (!mSystemReady) {
930 // not system ready
931 return;
932 }
933 final Locale newLocale = mRes.getConfiguration().locale;
934 if (!updateOnlyWhenLocaleChanged
935 || (newLocale != null && !newLocale.equals(mLastSystemLocale))) {
936 if (!updateOnlyWhenLocaleChanged) {
937 hideCurrentInputLocked(0, null);
938 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -0700939 unbindCurrentMethodLocked(true, false);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900940 }
941 if (DEBUG) {
942 Slog.i(TAG, "Locale has been changed to " + newLocale);
943 }
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900944 buildInputMethodListLocked(mMethodList, mMethodMap, resetDefaultEnabledIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900945 if (!updateOnlyWhenLocaleChanged) {
946 final String selectedImiId = mSettings.getSelectedInputMethod();
947 if (TextUtils.isEmpty(selectedImiId)) {
948 // This is the first time of the user switch and
949 // set the current ime to the proper one.
950 resetDefaultImeLocked(mContext);
951 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +0900952 } else {
953 // If the locale is changed, needs to reset the default ime
954 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900955 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800956 updateFromSettingsLocked(true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900957 mLastSystemLocale = newLocale;
958 if (!updateOnlyWhenLocaleChanged) {
959 try {
960 startInputInnerLocked();
961 } catch (RuntimeException e) {
962 Slog.w(TAG, "Unexpected exception", e);
963 }
964 }
965 }
966 }
967
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900968 private void resetStateIfCurrentLocaleChangedLocked() {
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900969 resetAllInternalStateLocked(true /* updateOnlyWhenLocaleChanged */,
970 true /* resetDefaultImeLocked */);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900971 }
972
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900973 private void switchUserLocked(int newUserId) {
Yohei Yukawa90bf7082015-06-03 23:50:27 -0700974 if (DEBUG) Slog.d(TAG, "Switching user stage 1/3. newUserId=" + newUserId
975 + " currentUserId=" + mSettings.getCurrentUserId());
976
Yohei Yukawa81482972015-06-04 00:58:59 -0700977 // ContentObserver should be registered again when the user is changed
978 mSettingsObserver.registerContentObserverLocked(newUserId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900979 mSettings.setCurrentUserId(newUserId);
Kenny Guy2a764942014-04-02 13:29:20 +0100980 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900981 // InputMethodFileManager should be reset when the user is changed
982 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900983 final String defaultImiId = mSettings.getSelectedInputMethod();
Yohei Yukawa90bf7082015-06-03 23:50:27 -0700984
985 if (DEBUG) Slog.d(TAG, "Switching user stage 2/3. newUserId=" + newUserId
986 + " defaultImiId=" + defaultImiId);
987
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +0900988 // For secondary users, the list of enabled IMEs may not have been updated since the
989 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
990 // not be empty even if the IME has been uninstalled by the primary user.
991 // Even in such cases, IMMS works fine because it will find the most applicable
992 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900993 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900994 resetAllInternalStateLocked(false /* updateOnlyWhenLocaleChanged */,
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900995 initialUserSwitch /* needsToResetDefaultIme */);
996 if (initialUserSwitch) {
Yohei Yukawa094c71f2015-06-20 00:41:31 -0700997 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
998 mSettings.getEnabledInputMethodListLocked(), newUserId,
999 mContext.getBasePackageName());
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001000 }
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001001
1002 if (DEBUG) Slog.d(TAG, "Switching user stage 3/3. newUserId=" + newUserId
1003 + " selectedIme=" + mSettings.getSelectedInputMethod());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001004 }
1005
Kenny Guy2a764942014-04-02 13:29:20 +01001006 void updateCurrentProfileIds() {
1007 List<UserInfo> profiles =
1008 UserManager.get(mContext).getProfiles(mSettings.getCurrentUserId());
1009 int[] currentProfileIds = new int[profiles.size()]; // profiles will not be null
1010 for (int i = 0; i < currentProfileIds.length; i++) {
1011 currentProfileIds[i] = profiles.get(i).id;
Amith Yamasani734983f2014-03-04 16:48:05 -08001012 }
Kenny Guy2a764942014-04-02 13:29:20 +01001013 mSettings.setCurrentProfileIds(currentProfileIds);
Amith Yamasani734983f2014-03-04 16:48:05 -08001014 }
1015
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001016 @Override
1017 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
1018 throws RemoteException {
1019 try {
1020 return super.onTransact(code, data, reply, flags);
1021 } catch (RuntimeException e) {
1022 // The input method manager only throws security exceptions, so let's
1023 // log all others.
1024 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -07001025 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026 }
1027 throw e;
1028 }
1029 }
1030
Svetoslav Ganova0027152013-06-25 14:59:53 -07001031 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001032 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001033 if (DEBUG) {
1034 Slog.d(TAG, "--- systemReady");
1035 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001036 if (!mSystemReady) {
1037 mSystemReady = true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001038 mKeyguardManager =
1039 (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001040 mNotificationManager = (NotificationManager)
1041 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
1042 mStatusBar = statusBar;
1043 statusBar.setIconVisibility("ime", false);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001044 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokb858c732011-07-22 19:54:34 +09001045 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
1046 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +09001047 if (mShowOngoingImeSwitcherForPhones) {
1048 mWindowManagerService.setOnHardKeyboardStatusChangeListener(
1049 mHardKeyboardListener);
1050 }
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09001051 buildInputMethodListLocked(mMethodList, mMethodMap,
1052 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satok0a1bcf42012-05-16 19:26:31 +09001053 if (!mImeSelectedOnBoot) {
1054 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001055 resetStateIfCurrentLocaleChangedLocked();
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001056 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1057 mSettings.getEnabledInputMethodListLocked(),
1058 mSettings.getCurrentUserId(), mContext.getBasePackageName());
satok0a1bcf42012-05-16 19:26:31 +09001059 }
1060 mLastSystemLocale = mRes.getConfiguration().locale;
Dianne Hackborncc278702009-09-02 23:07:23 -07001061 try {
1062 startInputInnerLocked();
1063 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001064 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -07001065 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001066 }
1067 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001068 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001069
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001070 // ---------------------------------------------------------------------------------------
1071 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
1072 // 1) it comes from the system process
1073 // 2) the calling process' user id is identical to the current user id IMMS thinks.
1074 private boolean calledFromValidUser() {
1075 final int uid = Binder.getCallingUid();
1076 final int userId = UserHandle.getUserId(uid);
1077 if (DEBUG) {
1078 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
1079 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
1080 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +09001081 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
1082 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001083 }
Kenny Guy2a764942014-04-02 13:29:20 +01001084 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001085 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001086 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001087
1088 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
1089 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
1090 // must not manage background users' states in any functions.
1091 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
1092 // by a token.
1093 if (mContext.checkCallingOrSelfPermission(
1094 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1095 == PackageManager.PERMISSION_GRANTED) {
1096 if (DEBUG) {
1097 Slog.d(TAG, "--- Access granted because the calling process has "
1098 + "the INTERACT_ACROSS_USERS_FULL permission");
1099 }
1100 return true;
1101 }
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07001102 Slog.w(TAG, "--- IPC called from background users. Ignore. callers="
1103 + Debug.getCallers(10));
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001104 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001105 }
1106
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001107
1108 /**
1109 * Returns true iff the caller is identified to be the current input method with the token.
1110 * @param token The window token given to the input method when it was started.
1111 * @return true if and only if non-null valid token is specified.
1112 */
1113 private boolean calledWithValidToken(IBinder token) {
1114 if (token == null || mCurToken != token) {
1115 return false;
1116 }
1117 return true;
1118 }
1119
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001120 private boolean bindCurrentInputMethodService(
1121 Intent service, ServiceConnection conn, int flags) {
1122 if (service == null || conn == null) {
1123 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
1124 return false;
1125 }
Amith Yamasani27b89e62013-01-16 12:30:11 -08001126 return mContext.bindServiceAsUser(service, conn, flags,
1127 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001128 }
1129
satoke7c6998e2011-06-03 17:57:59 +09001130 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001131 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001132 // TODO: Make this work even for non-current users?
1133 if (!calledFromValidUser()) {
1134 return Collections.emptyList();
1135 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001136 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001137 return new ArrayList<>(mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001138 }
1139 }
1140
satoke7c6998e2011-06-03 17:57:59 +09001141 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001142 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001143 // TODO: Make this work even for non-current users?
1144 if (!calledFromValidUser()) {
1145 return Collections.emptyList();
1146 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001147 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001148 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001149 }
1150 }
1151
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001152 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001153 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001154 * @return enabled subtypes of the specified imi
1155 */
satoke7c6998e2011-06-03 17:57:59 +09001156 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001157 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001158 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001159 // TODO: Make this work even for non-current users?
1160 if (!calledFromValidUser()) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001161 return Collections.emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001162 }
satok67ddf9c2010-11-17 09:45:54 +09001163 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001164 final InputMethodInfo imi;
1165 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001166 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001167 } else {
1168 imi = mMethodMap.get(imiId);
1169 }
1170 if (imi == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001171 return Collections.emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001172 }
1173 return mSettings.getEnabledInputMethodSubtypeListLocked(
1174 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001175 }
1176 }
1177
satoke7c6998e2011-06-03 17:57:59 +09001178 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001179 public void addClient(IInputMethodClient client,
1180 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001181 if (!calledFromValidUser()) {
1182 return;
1183 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184 synchronized (mMethodMap) {
1185 mClients.put(client.asBinder(), new ClientState(client,
1186 inputContext, uid, pid));
1187 }
1188 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001189
satoke7c6998e2011-06-03 17:57:59 +09001190 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001192 if (!calledFromValidUser()) {
1193 return;
1194 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001195 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001196 ClientState cs = mClients.remove(client.asBinder());
1197 if (cs != null) {
1198 clearClientSessionLocked(cs);
1199 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001200 }
1201 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001202
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 void executeOrSendMessage(IInterface target, Message msg) {
1204 if (target.asBinder() instanceof Binder) {
1205 mCaller.sendMessage(msg);
1206 } else {
1207 handleMessage(msg);
1208 msg.recycle();
1209 }
1210 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001211
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001212 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001214 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 + mCurClient.client.asBinder());
1216 if (mBoundToMethod) {
1217 mBoundToMethod = false;
1218 if (mCurMethod != null) {
1219 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1220 MSG_UNBIND_INPUT, mCurMethod));
1221 }
1222 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001223
1224 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1225 MSG_SET_ACTIVE, 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1227 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1228 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001230
The Android Open Source Project10592532009-03-18 17:39:46 -07001231 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001232 }
1233 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 private int getImeShowFlags() {
1236 int flags = 0;
1237 if (mShowForced) {
1238 flags |= InputMethod.SHOW_FORCED
1239 | InputMethod.SHOW_EXPLICIT;
1240 } else if (mShowExplicitlyRequested) {
1241 flags |= InputMethod.SHOW_EXPLICIT;
1242 }
1243 return flags;
1244 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001245
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246 private int getAppShowFlags() {
1247 int flags = 0;
1248 if (mShowForced) {
1249 flags |= InputMethodManager.SHOW_FORCED;
1250 } else if (!mShowExplicitlyRequested) {
1251 flags |= InputMethodManager.SHOW_IMPLICIT;
1252 }
1253 return flags;
1254 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001255
Dianne Hackborn7663d802012-02-24 13:08:49 -08001256 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001257 if (!mBoundToMethod) {
1258 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1259 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1260 mBoundToMethod = true;
1261 }
1262 final SessionState session = mCurClient.curSession;
1263 if (initial) {
1264 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1265 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
1266 } else {
1267 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1268 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
1269 }
1270 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001271 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001272 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001274 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001275 (session.channel != null ? session.channel.dup() : null),
1276 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001279 InputBindResult startInputLocked(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001280 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001281 // If no method is currently selected, do nothing.
1282 if (mCurMethodId == null) {
1283 return mNoBinding;
1284 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001285
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001286 ClientState cs = mClients.get(client.asBinder());
1287 if (cs == null) {
1288 throw new IllegalArgumentException("unknown client "
1289 + client.asBinder());
1290 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 try {
1293 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1294 // Check with the window manager to make sure this client actually
1295 // has a window with focus. If not, reject. This is thread safe
1296 // because if the focus changes some time before or after, the
1297 // next client receiving focus that has any interest in input will
1298 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001299 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1301 return null;
1302 }
1303 } catch (RemoteException e) {
1304 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001305
Dianne Hackborn7663d802012-02-24 13:08:49 -08001306 return startInputUncheckedLocked(cs, inputContext, attribute, controlFlags);
1307 }
1308
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001309 InputBindResult startInputUncheckedLocked(@NonNull ClientState cs,
Yohei Yukawad57ba672015-06-08 16:39:46 -07001310 IInputContext inputContext, @NonNull EditorInfo attribute, int controlFlags) {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001311 // If no method is currently selected, do nothing.
1312 if (mCurMethodId == null) {
1313 return mNoBinding;
1314 }
1315
Yohei Yukawad57ba672015-06-08 16:39:46 -07001316 if (!InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.uid,
1317 attribute.packageName)) {
1318 Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
1319 + " uid=" + cs.uid + " package=" + attribute.packageName);
1320 return mNoBinding;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001321 }
1322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001323 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001324 // Was the keyguard locked when switching over to the new client?
1325 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326 // If the client is changing, we need to switch over to the new
1327 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001328 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001329 if (DEBUG) Slog.v(TAG, "switching to client: client = "
John Spurlocke0980502013-10-25 11:59:29 -04001330 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001331
1332 // If the screen is on, inform the new client it is active
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001333 if (mIsInteractive) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001334 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001335 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001336 }
1337 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001339 // Bump up the sequence for this client and attach it.
1340 mCurSeq++;
1341 if (mCurSeq <= 0) mCurSeq = 1;
1342 mCurClient = cs;
1343 mCurInputContext = inputContext;
1344 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001346 // Check if the input method is changing.
1347 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1348 if (cs.curSession != null) {
1349 // Fast case: if we are already connected to the input method,
1350 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001351 return attachNewInputLocked(
1352 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001353 }
1354 if (mHaveConnection) {
1355 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001356 // Return to client, and we will get back with it when
1357 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001358 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001359 return new InputBindResult(null, null, mCurId, mCurSeq,
1360 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001361 } else if (SystemClock.uptimeMillis()
1362 < (mLastBindTime+TIME_TO_RECONNECT)) {
1363 // In this case we have connected to the service, but
1364 // don't yet have its interface. If it hasn't been too
1365 // long since we did the connection, we'll return to
1366 // the client and wait to get the service interface so
1367 // we can report back. If it has been too long, we want
1368 // to fall through so we can try a disconnect/reconnect
1369 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001370 return new InputBindResult(null, null, mCurId, mCurSeq,
1371 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001372 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001373 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1374 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001375 }
1376 }
1377 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001378
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001379 return startInputInnerLocked();
1380 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001381
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001382 InputBindResult startInputInnerLocked() {
1383 if (mCurMethodId == null) {
1384 return mNoBinding;
1385 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001386
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001387 if (!mSystemReady) {
1388 // If the system is not yet ready, we shouldn't be running third
1389 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001390 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1391 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001392 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001394 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1395 if (info == null) {
1396 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1397 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001398
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001399 unbindCurrentMethodLocked(false, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001400
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001401 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1402 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001403 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1404 com.android.internal.R.string.input_method_binding_label);
1405 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1406 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001407 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001408 | Context.BIND_NOT_VISIBLE | Context.BIND_NOT_FOREGROUND
1409 | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001410 mLastBindTime = SystemClock.uptimeMillis();
1411 mHaveConnection = true;
1412 mCurId = info.getId();
1413 mCurToken = new Binder();
1414 try {
Craig Mautnerca0ac712013-03-14 09:43:02 -07001415 if (true || DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001416 mIWindowManager.addWindowToken(mCurToken,
1417 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1418 } catch (RemoteException e) {
1419 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001420 return new InputBindResult(null, null, mCurId, mCurSeq,
1421 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001422 } else {
1423 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001424 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001425 + mCurIntent);
1426 }
1427 return null;
1428 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001429
satoke7c6998e2011-06-03 17:57:59 +09001430 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001431 public InputBindResult startInput(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001432 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001433 if (!calledFromValidUser()) {
1434 return null;
1435 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001436 synchronized (mMethodMap) {
1437 final long ident = Binder.clearCallingIdentity();
1438 try {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001439 return startInputLocked(client, inputContext, attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001440 } finally {
1441 Binder.restoreCallingIdentity(ident);
1442 }
1443 }
1444 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001445
satoke7c6998e2011-06-03 17:57:59 +09001446 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001447 public void finishInput(IInputMethodClient client) {
1448 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001449
satoke7c6998e2011-06-03 17:57:59 +09001450 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001451 public void onServiceConnected(ComponentName name, IBinder service) {
1452 synchronized (mMethodMap) {
1453 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1454 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001455 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001456 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001457 unbindCurrentMethodLocked(false, false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001458 return;
1459 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001460 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001461 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1462 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001463 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001464 clearClientSessionLocked(mCurClient);
1465 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001466 }
1467 }
1468 }
1469 }
1470
Jeff Brownc28867a2013-03-26 15:42:39 -07001471 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1472 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001473 synchronized (mMethodMap) {
1474 if (mCurMethod != null && method != null
1475 && mCurMethod.asBinder() == method.asBinder()) {
1476 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001477 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001478 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001479 method, session, channel);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001480 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001481 if (res.method != null) {
1482 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
1483 MSG_BIND_METHOD, mCurClient.client, res));
1484 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001485 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001486 }
1487 }
1488 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001489
1490 // Session abandoned. Close its associated input channel.
1491 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001492 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001493
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001494 void unbindCurrentMethodLocked(boolean reportToClient, boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001495 if (mVisibleBound) {
1496 mContext.unbindService(mVisibleConnection);
1497 mVisibleBound = false;
1498 }
1499
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001500 if (mHaveConnection) {
1501 mContext.unbindService(this);
1502 mHaveConnection = false;
1503 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001504
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001505 if (mCurToken != null) {
1506 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001507 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001508 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001509 // The current IME is shown. Hence an IME switch (transition) is happening.
1510 mWindowManagerService.saveLastInputMethodWindowForTransition();
1511 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001512 mIWindowManager.removeWindowToken(mCurToken);
1513 } catch (RemoteException e) {
1514 }
1515 mCurToken = null;
1516 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001517
The Android Open Source Project10592532009-03-18 17:39:46 -07001518 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001519 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001520
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001521 if (reportToClient && mCurClient != null) {
1522 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1523 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1524 }
1525 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001526
1527 void requestClientSessionLocked(ClientState cs) {
1528 if (!cs.sessionRequested) {
1529 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1530 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1531 cs.sessionRequested = true;
1532 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1533 MSG_CREATE_SESSION, mCurMethod, channels[1],
1534 new MethodCallback(this, mCurMethod, channels[0])));
1535 }
1536 }
1537
1538 void clearClientSessionLocked(ClientState cs) {
1539 finishSessionLocked(cs.curSession);
1540 cs.curSession = null;
1541 cs.sessionRequested = false;
1542 }
1543
1544 private void finishSessionLocked(SessionState sessionState) {
1545 if (sessionState != null) {
1546 if (sessionState.session != null) {
1547 try {
1548 sessionState.session.finishSession();
1549 } catch (RemoteException e) {
1550 Slog.w(TAG, "Session failed to close due to remote exception", e);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001551 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Jeff Brownc28867a2013-03-26 15:42:39 -07001552 }
1553 sessionState.session = null;
1554 }
1555 if (sessionState.channel != null) {
1556 sessionState.channel.dispose();
1557 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001558 }
1559 }
1560 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001561
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001562 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001563 if (mCurMethod != null) {
1564 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001565 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001566 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001567
Jeff Brownc28867a2013-03-26 15:42:39 -07001568 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001569 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001570 mCurMethod = null;
1571 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001572 if (mStatusBar != null) {
1573 mStatusBar.setIconVisibility("ime", false);
1574 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001575 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001576
satoke7c6998e2011-06-03 17:57:59 +09001577 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001578 public void onServiceDisconnected(ComponentName name) {
1579 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001580 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001581 + " mCurIntent=" + mCurIntent);
1582 if (mCurMethod != null && mCurIntent != null
1583 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001584 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001585 // We consider this to be a new bind attempt, since the system
1586 // should now try to restart the service for us.
1587 mLastBindTime = SystemClock.uptimeMillis();
1588 mShowRequested = mInputShown;
1589 mInputShown = false;
1590 if (mCurClient != null) {
1591 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1592 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1593 }
1594 }
1595 }
1596 }
1597
satokf9f01002011-05-19 21:31:50 +09001598 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001599 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
1600 long ident = Binder.clearCallingIdentity();
1601 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001602 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09001603 if (!calledWithValidToken(token)) {
1604 final int uid = Binder.getCallingUid();
1605 Slog.e(TAG, "Ignoring updateStatusIcon due to an invalid token. uid:" + uid
1606 + " token:" + token);
1607 return;
1608 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001609 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001610 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001611 if (mStatusBar != null) {
1612 mStatusBar.setIconVisibility("ime", false);
1613 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001614 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001615 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001616 CharSequence contentDescription = null;
1617 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001618 // Use PackageManager to load label
1619 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001620 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001621 mIPackageManager.getApplicationInfo(packageName, 0,
1622 mSettings.getCurrentUserId()));
1623 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001624 /* ignore */
1625 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001626 if (mStatusBar != null) {
1627 mStatusBar.setIcon("ime", packageName, iconId, 0,
1628 contentDescription != null
1629 ? contentDescription.toString() : null);
1630 mStatusBar.setIconVisibility("ime", true);
1631 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001632 }
1633 }
1634 } finally {
1635 Binder.restoreCallingIdentity(ident);
1636 }
1637 }
1638
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001639 private boolean shouldShowImeSwitcherLocked(int visibility) {
satok7cfc0ed2011-06-20 21:29:36 +09001640 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04001641 if (mSwitchingDialog != null) return false;
satok2c93efc2012-04-02 19:33:47 +09001642 if (isScreenLocked()) return false;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001643 if ((visibility & InputMethodService.IME_ACTIVE) == 0) return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001644 if (mWindowManagerService.isHardKeyboardAvailable()) {
1645 // When physical keyboard is attached, we show the ime switcher (or notification if
1646 // NavBar is not available) because SHOW_IME_WITH_HARD_KEYBOARD settings currently
1647 // exists in the IME switcher dialog. Might be OK to remove this condition once
1648 // SHOW_IME_WITH_HARD_KEYBOARD settings finds a good place to live.
1649 return true;
1650 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001651 if ((visibility & InputMethodService.IME_VISIBLE) == 0) return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001652
1653 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1654 final int N = imis.size();
1655 if (N > 2) return true;
1656 if (N < 1) return false;
1657 int nonAuxCount = 0;
1658 int auxCount = 0;
1659 InputMethodSubtype nonAuxSubtype = null;
1660 InputMethodSubtype auxSubtype = null;
1661 for(int i = 0; i < N; ++i) {
1662 final InputMethodInfo imi = imis.get(i);
1663 final List<InputMethodSubtype> subtypes =
1664 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
1665 final int subtypeCount = subtypes.size();
1666 if (subtypeCount == 0) {
1667 ++nonAuxCount;
1668 } else {
1669 for (int j = 0; j < subtypeCount; ++j) {
1670 final InputMethodSubtype subtype = subtypes.get(j);
1671 if (!subtype.isAuxiliary()) {
1672 ++nonAuxCount;
1673 nonAuxSubtype = subtype;
1674 } else {
1675 ++auxCount;
1676 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001677 }
1678 }
satok7cfc0ed2011-06-20 21:29:36 +09001679 }
1680 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001681 if (nonAuxCount > 1 || auxCount > 1) {
1682 return true;
1683 } else if (nonAuxCount == 1 && auxCount == 1) {
1684 if (nonAuxSubtype != null && auxSubtype != null
1685 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1686 || auxSubtype.overridesImplicitlyEnabledSubtype()
1687 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
1688 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1689 return false;
1690 }
1691 return true;
1692 }
1693 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001694 }
1695
John Spurlocke0980502013-10-25 11:59:29 -04001696 private boolean isKeyguardLocked() {
1697 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1698 }
1699
satokdbf29502011-08-25 15:28:23 +09001700 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001701 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001702 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001703 if (!calledWithValidToken(token)) {
1704 final int uid = Binder.getCallingUid();
1705 Slog.e(TAG, "Ignoring setImeWindowStatus due to an invalid token. uid:" + uid
1706 + " token:" + token);
1707 return;
1708 }
1709
1710 synchronized (mMethodMap) {
1711 mImeWindowVis = vis;
1712 mBackDisposition = backDisposition;
1713 updateSystemUiLocked(token, vis, backDisposition);
1714 }
1715 }
1716
1717 private void updateSystemUi(IBinder token, int vis, int backDisposition) {
1718 synchronized (mMethodMap) {
1719 updateSystemUiLocked(token, vis, backDisposition);
1720 }
1721 }
1722
1723 // Caution! This method is called in this class. Handle multi-user carefully
1724 private void updateSystemUiLocked(IBinder token, int vis, int backDisposition) {
1725 if (!calledWithValidToken(token)) {
1726 final int uid = Binder.getCallingUid();
1727 Slog.e(TAG, "Ignoring updateSystemUiLocked due to an invalid token. uid:" + uid
1728 + " token:" + token);
1729 return;
1730 }
1731
1732 // TODO: Move this clearing calling identity block to setImeWindowStatus after making sure
1733 // all updateSystemUi happens on system previlege.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001734 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001735 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001736 // apply policy for binder calls
1737 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
1738 vis = 0;
satok06487a52010-10-29 11:37:18 +09001739 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001740 // mImeWindowVis should be updated before calling shouldShowImeSwitcherLocked().
1741 final boolean needsToShowImeSwitcher = shouldShowImeSwitcherLocked(vis);
1742 if (mStatusBar != null) {
1743 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
1744 needsToShowImeSwitcher);
1745 }
1746 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1747 if (imi != null && needsToShowImeSwitcher) {
1748 // Used to load label
1749 final CharSequence title = mRes.getText(
1750 com.android.internal.R.string.select_input_method);
1751 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
1752 mContext, imi, mCurrentSubtype);
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001753 mImeSwitcherNotification.setContentTitle(title)
1754 .setContentText(summary)
1755 .setContentIntent(mImeSwitchPendingIntent);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001756 if ((mNotificationManager != null)
1757 && !mWindowManagerService.hasNavigationBar()) {
1758 if (DEBUG) {
1759 Slog.d(TAG, "--- show notification: label = " + summary);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001760 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001761 mNotificationManager.notifyAsUser(null,
1762 com.android.internal.R.string.select_input_method,
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001763 mImeSwitcherNotification.build(), UserHandle.ALL);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001764 mNotificationShown = true;
1765 }
1766 } else {
1767 if (mNotificationShown && mNotificationManager != null) {
1768 if (DEBUG) {
1769 Slog.d(TAG, "--- hide notification");
satok7cfc0ed2011-06-20 21:29:36 +09001770 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001771 mNotificationManager.cancelAsUser(null,
1772 com.android.internal.R.string.select_input_method, UserHandle.ALL);
1773 mNotificationShown = false;
satok7cfc0ed2011-06-20 21:29:36 +09001774 }
satok06487a52010-10-29 11:37:18 +09001775 }
1776 } finally {
1777 Binder.restoreCallingIdentity(ident);
1778 }
1779 }
1780
satoke7c6998e2011-06-03 17:57:59 +09001781 @Override
satokf9f01002011-05-19 21:31:50 +09001782 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001783 if (!calledFromValidUser()) {
1784 return;
1785 }
satokf9f01002011-05-19 21:31:50 +09001786 synchronized (mMethodMap) {
1787 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1788 for (int i = 0; i < spans.length; ++i) {
1789 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001790 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001791 mSecureSuggestionSpans.put(ss, currentImi);
1792 }
1793 }
1794 }
1795 }
1796
satoke7c6998e2011-06-03 17:57:59 +09001797 @Override
satokf9f01002011-05-19 21:31:50 +09001798 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001799 if (!calledFromValidUser()) {
1800 return false;
1801 }
satokf9f01002011-05-19 21:31:50 +09001802 synchronized (mMethodMap) {
1803 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1804 // TODO: Do not send the intent if the process of the targetImi is already dead.
1805 if (targetImi != null) {
1806 final String[] suggestions = span.getSuggestions();
1807 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001808 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001809 final Intent intent = new Intent();
1810 // Ensures that only a class in the original IME package will receive the
1811 // notification.
satok42c5a162011-05-26 16:46:14 +09001812 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001813 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1814 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1815 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1816 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001817 final long ident = Binder.clearCallingIdentity();
1818 try {
1819 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1820 } finally {
1821 Binder.restoreCallingIdentity(ident);
1822 }
satokf9f01002011-05-19 21:31:50 +09001823 return true;
1824 }
1825 }
1826 return false;
1827 }
1828
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001829 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001830 updateInputMethodsFromSettingsLocked(enabledMayChange);
1831 updateKeyboardFromSettingsLocked();
1832 }
1833
1834 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001835 if (enabledMayChange) {
1836 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1837 for (int i=0; i<enabled.size(); i++) {
1838 // We allow the user to select "disabled until used" apps, so if they
1839 // are enabling one of those here we now need to make it enabled.
1840 InputMethodInfo imm = enabled.get(i);
1841 try {
1842 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1843 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1844 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09001845 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001846 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001847 if (DEBUG) {
1848 Slog.d(TAG, "Update state(" + imm.getId()
1849 + "): DISABLED_UNTIL_USED -> DEFAULT");
1850 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001851 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1852 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001853 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
1854 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001855 }
1856 } catch (RemoteException e) {
1857 }
1858 }
1859 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001860 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1861 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1862 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1863 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001864 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001865 // There is no input method selected, try to choose new applicable input method.
1866 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001867 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001868 }
1869 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001870 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001871 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001872 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001873 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001874 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001875 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001876 }
satokf3db1af2010-11-23 13:34:33 +09001877 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001878 } else {
1879 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001880 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001881 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001882 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09001883 // Here is not the perfect place to reset the switching controller. Ideally
1884 // mSwitchingController and mSettings should be able to share the same state.
1885 // TODO: Make sure that mSwitchingController and mSettings are sharing the
1886 // the same enabled IMEs list.
1887 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07001888
1889 }
1890
1891 public void updateKeyboardFromSettingsLocked() {
1892 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
1893 if (mSwitchingDialog != null
1894 && mSwitchingDialogTitleView != null
1895 && mSwitchingDialog.isShowing()) {
1896 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
1897 com.android.internal.R.id.hard_keyboard_switch);
1898 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
1899 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001900 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001901
satokab751aa2010-09-14 19:17:36 +09001902 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001903 InputMethodInfo info = mMethodMap.get(id);
1904 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001905 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001906 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001907
Yohei Yukawae63b5fa2014-09-19 13:14:55 +09001908 if (mCurClient != null && mCurAttribute != null) {
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001909 // We have already made sure that the package name belongs to the application's UID.
1910 // No further UID check is required.
1911 if (SystemConfig.getInstance().getFixedImeApps().contains(mCurAttribute.packageName)) {
1912 return;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +09001913 }
1914 }
1915
satokd81e9502012-05-21 12:58:45 +09001916 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001917 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09001918 final int subtypeCount = info.getSubtypeCount();
1919 if (subtypeCount <= 0) {
1920 return;
satokcd7cd292010-11-20 15:46:23 +09001921 }
satokd81e9502012-05-21 12:58:45 +09001922 final InputMethodSubtype oldSubtype = mCurrentSubtype;
1923 final InputMethodSubtype newSubtype;
1924 if (subtypeId >= 0 && subtypeId < subtypeCount) {
1925 newSubtype = info.getSubtypeAt(subtypeId);
1926 } else {
1927 // If subtype is null, try to find the most applicable one from
1928 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001929 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09001930 }
1931 if (newSubtype == null || oldSubtype == null) {
1932 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
1933 + ", new subtype = " + newSubtype);
1934 return;
1935 }
1936 if (newSubtype != oldSubtype) {
1937 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1938 if (mCurMethod != null) {
1939 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001940 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokd81e9502012-05-21 12:58:45 +09001941 mCurMethod.changeInputMethodSubtype(newSubtype);
1942 } catch (RemoteException e) {
1943 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
satokab751aa2010-09-14 19:17:36 +09001944 }
1945 }
1946 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001947 return;
1948 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001949
satokd81e9502012-05-21 12:58:45 +09001950 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001951 final long ident = Binder.clearCallingIdentity();
1952 try {
satokab751aa2010-09-14 19:17:36 +09001953 // Set a subtype to this input method.
1954 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001955 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1956 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1957 // because mCurMethodId is stored as a history in
1958 // setSelectedInputMethodAndSubtypeLocked().
1959 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001960
1961 if (ActivityManagerNative.isSystemReady()) {
1962 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001963 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001964 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07001965 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001966 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001967 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001968 } finally {
1969 Binder.restoreCallingIdentity(ident);
1970 }
1971 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001972
satok42c5a162011-05-26 16:46:14 +09001973 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001974 public boolean showSoftInput(IInputMethodClient client, int flags,
1975 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001976 if (!calledFromValidUser()) {
1977 return false;
1978 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001979 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001980 long ident = Binder.clearCallingIdentity();
1981 try {
1982 synchronized (mMethodMap) {
1983 if (mCurClient == null || client == null
1984 || mCurClient.client.asBinder() != client.asBinder()) {
1985 try {
1986 // We need to check if this is the current client with
1987 // focus in the window manager, to allow this call to
1988 // be made before input is started in it.
1989 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001990 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001991 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001992 }
1993 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001994 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001995 }
1996 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001997
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001998 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001999 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002000 }
2001 } finally {
2002 Binder.restoreCallingIdentity(ident);
2003 }
2004 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002005
The Android Open Source Project4df24232009-03-05 14:34:35 -08002006 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002007 mShowRequested = true;
2008 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
2009 mShowExplicitlyRequested = true;
2010 }
2011 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
2012 mShowExplicitlyRequested = true;
2013 mShowForced = true;
2014 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002015
Dianne Hackborncc278702009-09-02 23:07:23 -07002016 if (!mSystemReady) {
2017 return false;
2018 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002019
The Android Open Source Project4df24232009-03-05 14:34:35 -08002020 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002021 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002022 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002023 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
2024 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
2025 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002026 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002027 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002028 bindCurrentInputMethodService(
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07002029 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE
Dianne Hackbornd69e4c12015-04-24 09:54:54 -07002030 | Context.BIND_TREAT_LIKE_ACTIVITY
2031 | Context.BIND_FOREGROUND_SERVICE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002032 mVisibleBound = true;
2033 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002034 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002035 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09002036 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002037 // The client has asked to have the input method shown, but
2038 // we have been sitting here too long with a connection to the
2039 // service and no interface received, so let's disconnect/connect
2040 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002041 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002042 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09002043 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002044 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002045 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002046 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002047 } else {
2048 if (DEBUG) {
2049 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
2050 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
2051 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002052 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002053
The Android Open Source Project4df24232009-03-05 14:34:35 -08002054 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002055 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002056
satok42c5a162011-05-26 16:46:14 +09002057 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002058 public boolean hideSoftInput(IInputMethodClient client, int flags,
2059 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002060 if (!calledFromValidUser()) {
2061 return false;
2062 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002063 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002064 long ident = Binder.clearCallingIdentity();
2065 try {
2066 synchronized (mMethodMap) {
2067 if (mCurClient == null || client == null
2068 || mCurClient.client.asBinder() != client.asBinder()) {
2069 try {
2070 // We need to check if this is the current client with
2071 // focus in the window manager, to allow this call to
2072 // be made before input is started in it.
2073 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002074 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
2075 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002076 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002077 }
2078 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002079 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002080 }
2081 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002082
Joe Onorato8a9b2202010-02-26 18:56:32 -08002083 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002084 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002085 }
2086 } finally {
2087 Binder.restoreCallingIdentity(ident);
2088 }
2089 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002090
The Android Open Source Project4df24232009-03-05 14:34:35 -08002091 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002092 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
2093 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002094 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 -08002095 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002096 }
2097 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002098 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 -08002099 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002100 }
Seigo Nonakaec928652015-06-10 15:31:20 +09002101
2102 // There is a chance that IMM#hideSoftInput() is called in a transient state where
2103 // IMMS#InputShown is already updated to be true whereas IMMS#mImeWindowVis is still waiting
2104 // to be updated with the new value sent from IME process. Even in such a transient state
2105 // historically we have accepted an incoming call of IMM#hideSoftInput() from the
2106 // application process as a valid request, and have even promised such a behavior with CTS
2107 // since Android Eclair. That's why we need to accept IMM#hideSoftInput() even when only
2108 // IMMS#InputShown indicates that the software keyboard is shown.
2109 // TODO: Clean up, IMMS#mInputShown, IMMS#mImeWindowVis and mShowRequested.
2110 final boolean shouldHideSoftInput = (mCurMethod != null) && (mInputShown ||
2111 (mImeWindowVis & InputMethodService.IME_ACTIVE) != 0);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002112 boolean res;
Seigo Nonakaec928652015-06-10 15:31:20 +09002113 if (shouldHideSoftInput) {
2114 // The IME will report its visible state again after the following message finally
2115 // delivered to the IME process as an IPC. Hence the inconsistency between
2116 // IMMS#mInputShown and IMMS#mImeWindowVis should be resolved spontaneously in
2117 // the final state.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002118 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2119 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
2120 res = true;
2121 } else {
2122 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002123 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002124 if (mHaveConnection && mVisibleBound) {
2125 mContext.unbindService(mVisibleConnection);
2126 mVisibleBound = false;
2127 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002128 mInputShown = false;
2129 mShowRequested = false;
2130 mShowExplicitlyRequested = false;
2131 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002132 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002133 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002134
satok42c5a162011-05-26 16:46:14 +09002135 @Override
Dianne Hackborn7663d802012-02-24 13:08:49 -08002136 public InputBindResult windowGainedFocus(IInputMethodClient client, IBinder windowToken,
2137 int controlFlags, int softInputMode, int windowFlags,
2138 EditorInfo attribute, IInputContext inputContext) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002139 // Needs to check the validity before clearing calling identity
2140 final boolean calledFromValidUser = calledFromValidUser();
2141
Dianne Hackborn7663d802012-02-24 13:08:49 -08002142 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002143 long ident = Binder.clearCallingIdentity();
2144 try {
2145 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002146 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
Dianne Hackborn7663d802012-02-24 13:08:49 -08002147 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002148 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08002149 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002150
Dianne Hackborn7663d802012-02-24 13:08:49 -08002151 ClientState cs = mClients.get(client.asBinder());
2152 if (cs == null) {
2153 throw new IllegalArgumentException("unknown client "
2154 + client.asBinder());
2155 }
2156
2157 try {
2158 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2159 // Check with the window manager to make sure this client actually
2160 // has a window with focus. If not, reject. This is thread safe
2161 // because if the focus changes some time before or after, the
2162 // next client receiving focus that has any interest in input will
2163 // be calling through here after that change happens.
2164 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2165 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2166 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002167 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002168 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002169 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002170
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002171 if (!calledFromValidUser) {
2172 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2173 Slog.w(TAG, "If you want to interect with IME, you need "
2174 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2175 hideCurrentInputLocked(0, null);
2176 return null;
2177 }
2178
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002179 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07002180 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09002181 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002182 if (attribute != null) {
2183 return startInputUncheckedLocked(cs, inputContext, attribute,
2184 controlFlags);
2185 }
2186 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002187 }
2188 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002189
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002190 // Should we auto-show the IME even if the caller has not
2191 // specified what should be done with it?
2192 // We only do this automatically if the window can resize
2193 // to accommodate the IME (so what the user sees will give
2194 // them good context without input information being obscured
2195 // by the IME) or if running on a large screen where there
2196 // is more room for the target window + IME.
2197 final boolean doAutoShow =
2198 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2199 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2200 || mRes.getConfiguration().isLayoutSizeAtLeast(
2201 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002202 final boolean isTextEditor =
2203 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2204
2205 // We want to start input before showing the IME, but after closing
2206 // it. We want to do this after closing it to help the IME disappear
2207 // more quickly (not get stuck behind it initializing itself for the
2208 // new focused input, even if its window wants to hide the IME).
2209 boolean didStart = false;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002211 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2212 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002213 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002214 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2215 // There is no focus view, and this window will
2216 // be behind any soft input window, so hide the
2217 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002218 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002219 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002220 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002221 } else if (isTextEditor && doAutoShow && (softInputMode &
2222 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002223 // There is a focus view, and we are navigating forward
2224 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002225 // We only do this automatically if the window can resize
2226 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002227 // 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.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002230 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002231 if (attribute != null) {
2232 res = startInputUncheckedLocked(cs, inputContext, attribute,
2233 controlFlags);
2234 didStart = true;
2235 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002236 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002237 }
2238 break;
2239 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2240 // Do nothing.
2241 break;
2242 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2243 if ((softInputMode &
2244 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002245 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002246 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002247 }
2248 break;
2249 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002250 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002251 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002252 break;
2253 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2254 if ((softInputMode &
2255 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002256 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002257 if (attribute != null) {
2258 res = startInputUncheckedLocked(cs, inputContext, attribute,
2259 controlFlags);
2260 didStart = true;
2261 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002262 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002263 }
2264 break;
2265 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002266 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002267 if (attribute != null) {
2268 res = startInputUncheckedLocked(cs, inputContext, attribute,
2269 controlFlags);
2270 didStart = true;
2271 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002272 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002273 break;
2274 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002275
2276 if (!didStart && attribute != null) {
2277 res = startInputUncheckedLocked(cs, inputContext, attribute,
2278 controlFlags);
2279 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002280 }
2281 } finally {
2282 Binder.restoreCallingIdentity(ident);
2283 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002284
2285 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002286 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002287
satok42c5a162011-05-26 16:46:14 +09002288 @Override
Seigo Nonaka14e13912015-05-06 21:04:13 -07002289 public void showInputMethodPickerFromClient(
2290 IInputMethodClient client, int auxiliarySubtypeMode) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002291 if (!calledFromValidUser()) {
2292 return;
2293 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002294 synchronized (mMethodMap) {
2295 if (mCurClient == null || client == null
2296 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002297 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002298 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002299 }
2300
satok440aab52010-11-25 09:43:11 +09002301 // Always call subtype picker, because subtype picker is a superset of input method
2302 // picker.
Seigo Nonaka14e13912015-05-06 21:04:13 -07002303 mHandler.sendMessage(mCaller.obtainMessageI(
2304 MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode));
satokab751aa2010-09-14 19:17:36 +09002305 }
2306 }
2307
satok42c5a162011-05-26 16:46:14 +09002308 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002309 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002310 if (!calledFromValidUser()) {
2311 return;
2312 }
satok28203512010-11-24 11:06:49 +09002313 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2314 }
2315
satok42c5a162011-05-26 16:46:14 +09002316 @Override
satok28203512010-11-24 11:06:49 +09002317 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002318 if (!calledFromValidUser()) {
2319 return;
2320 }
satok28203512010-11-24 11:06:49 +09002321 synchronized (mMethodMap) {
2322 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002323 setInputMethodWithSubtypeIdLocked(token, id,
2324 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2325 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002326 } else {
2327 setInputMethod(token, id);
2328 }
2329 }
satokab751aa2010-09-14 19:17:36 +09002330 }
2331
satok42c5a162011-05-26 16:46:14 +09002332 @Override
satokb416a712010-11-25 20:42:14 +09002333 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002334 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002335 if (!calledFromValidUser()) {
2336 return;
2337 }
satokb416a712010-11-25 20:42:14 +09002338 synchronized (mMethodMap) {
2339 if (mCurClient == null || client == null
2340 || mCurClient.client.asBinder() != client.asBinder()) {
2341 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
2342 }
satok7fee71f2010-12-17 18:54:26 +09002343 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2344 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002345 }
2346 }
2347
satok4fc87d62011-05-20 16:13:43 +09002348 @Override
satok735cf382010-11-11 20:40:09 +09002349 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002350 if (!calledFromValidUser()) {
2351 return false;
2352 }
satok735cf382010-11-11 20:40:09 +09002353 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002354 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002355 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002356 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002357 lastImi = mMethodMap.get(lastIme.first);
2358 } else {
2359 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002360 }
satok4fc87d62011-05-20 16:13:43 +09002361 String targetLastImiId = null;
2362 int subtypeId = NOT_A_SUBTYPE_ID;
2363 if (lastIme != null && lastImi != null) {
2364 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
2365 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
2366 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2367 : mCurrentSubtype.hashCode();
2368 // If the last IME is the same as the current IME and the last subtype is not
2369 // defined, there is no need to switch to the last IME.
2370 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2371 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002372 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002373 }
2374 }
2375
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002376 if (TextUtils.isEmpty(targetLastImiId)
2377 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002378 // This is a safety net. If the currentSubtype can't be added to the history
2379 // and the framework couldn't find the last ime, we will make the last ime be
2380 // the most applicable enabled keyboard subtype of the system imes.
2381 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2382 if (enabled != null) {
2383 final int N = enabled.size();
2384 final String locale = mCurrentSubtype == null
2385 ? mRes.getConfiguration().locale.toString()
2386 : mCurrentSubtype.getLocale();
2387 for (int i = 0; i < N; ++i) {
2388 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002389 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002390 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002391 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2392 InputMethodUtils.getSubtypes(imi),
2393 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002394 if (keyboardSubtype != null) {
2395 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002396 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002397 imi, keyboardSubtype.hashCode());
2398 if(keyboardSubtype.getLocale().equals(locale)) {
2399 break;
2400 }
2401 }
2402 }
2403 }
2404 }
2405 }
2406
2407 if (!TextUtils.isEmpty(targetLastImiId)) {
2408 if (DEBUG) {
2409 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2410 + ", from: " + mCurMethodId + ", " + subtypeId);
2411 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002412 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002413 return true;
2414 } else {
2415 return false;
2416 }
satok735cf382010-11-11 20:40:09 +09002417 }
2418 }
2419
satoke7c6998e2011-06-03 17:57:59 +09002420 @Override
satok688bd472012-02-09 20:09:17 +09002421 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002422 if (!calledFromValidUser()) {
2423 return false;
2424 }
satok688bd472012-02-09 20:09:17 +09002425 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002426 if (!calledWithValidToken(token)) {
2427 final int uid = Binder.getCallingUid();
2428 Slog.e(TAG, "Ignoring switchToNextInputMethod due to an invalid token. uid:" + uid
2429 + " token:" + token);
2430 return false;
2431 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002432 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
satok688bd472012-02-09 20:09:17 +09002433 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2434 if (nextSubtype == null) {
2435 return false;
2436 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002437 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2438 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002439 return true;
2440 }
2441 }
2442
2443 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002444 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2445 if (!calledFromValidUser()) {
2446 return false;
2447 }
2448 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002449 if (!calledWithValidToken(token)) {
2450 final int uid = Binder.getCallingUid();
2451 Slog.e(TAG, "Ignoring shouldOfferSwitchingToNextInputMethod due to an invalid "
2452 + "token. uid:" + uid + " token:" + token);
2453 return false;
2454 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002455 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002456 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2457 if (nextSubtype == null) {
2458 return false;
2459 }
2460 return true;
2461 }
2462 }
2463
2464 @Override
satok68f1b782011-04-11 14:26:04 +09002465 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002466 if (!calledFromValidUser()) {
2467 return null;
2468 }
satok68f1b782011-04-11 14:26:04 +09002469 synchronized (mMethodMap) {
2470 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2471 // TODO: Handle the case of the last IME with no subtypes
2472 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2473 || TextUtils.isEmpty(lastIme.second)) return null;
2474 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2475 if (lastImi == null) return null;
2476 try {
2477 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002478 final int lastSubtypeId =
2479 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002480 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2481 return null;
2482 }
2483 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002484 } catch (NumberFormatException e) {
2485 return null;
2486 }
2487 }
2488 }
2489
satoke7c6998e2011-06-03 17:57:59 +09002490 @Override
satokee5e77c2011-09-02 18:50:15 +09002491 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002492 if (!calledFromValidUser()) {
2493 return;
2494 }
satok91e88122011-07-18 11:11:42 +09002495 // By this IPC call, only a process which shares the same uid with the IME can add
2496 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09002497 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09002498 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002499 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002500 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002501 final String[] packageInfos;
2502 try {
2503 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2504 } catch (RemoteException e) {
2505 Slog.e(TAG, "Failed to get package infos");
2506 return;
2507 }
satok91e88122011-07-18 11:11:42 +09002508 if (packageInfos != null) {
2509 final int packageNum = packageInfos.length;
2510 for (int i = 0; i < packageNum; ++i) {
2511 if (packageInfos[i].equals(imi.getPackageName())) {
2512 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002513 final long ident = Binder.clearCallingIdentity();
2514 try {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002515 buildInputMethodListLocked(mMethodList, mMethodMap,
2516 false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002517 } finally {
2518 Binder.restoreCallingIdentity(ident);
2519 }
satokee5e77c2011-09-02 18:50:15 +09002520 return;
satok91e88122011-07-18 11:11:42 +09002521 }
2522 }
2523 }
satoke7c6998e2011-06-03 17:57:59 +09002524 }
satokee5e77c2011-09-02 18:50:15 +09002525 return;
satoke7c6998e2011-06-03 17:57:59 +09002526 }
2527
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002528 @Override
2529 public int getInputMethodWindowVisibleHeight() {
2530 return mWindowManagerService.getInputMethodWindowVisibleHeight();
2531 }
2532
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002533 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002534 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002535 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002536 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002537 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002538 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002539 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
2540 if (DEBUG) {
2541 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
2542 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
2543 + " actual: " + sequenceNumber);
2544 }
2545 return;
2546 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002547 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2548 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09002549 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002550 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002551 }
2552 }
2553
satok28203512010-11-24 11:06:49 +09002554 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002555 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002556 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
2557 }
2558 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002559
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002560 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
2561 if (token == null) {
2562 if (mContext.checkCallingOrSelfPermission(
2563 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2564 != PackageManager.PERMISSION_GRANTED) {
2565 throw new SecurityException(
2566 "Using null token requires permission "
2567 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002568 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002569 } else if (mCurToken != token) {
2570 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2571 + " token: " + token);
2572 return;
2573 }
2574
2575 final long ident = Binder.clearCallingIdentity();
2576 try {
2577 setInputMethodLocked(id, subtypeId);
2578 } finally {
2579 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002580 }
2581 }
2582
satok42c5a162011-05-26 16:46:14 +09002583 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002584 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002585 if (!calledFromValidUser()) {
2586 return;
2587 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002588 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002589 if (!calledWithValidToken(token)) {
2590 final int uid = Binder.getCallingUid();
2591 Slog.e(TAG, "Ignoring hideInputMethod due to an invalid token. uid:"
2592 + uid + " token:" + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002593 return;
2594 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002595 long ident = Binder.clearCallingIdentity();
2596 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002597 hideCurrentInputLocked(flags, null);
2598 } finally {
2599 Binder.restoreCallingIdentity(ident);
2600 }
2601 }
2602 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002603
satok42c5a162011-05-26 16:46:14 +09002604 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002605 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002606 if (!calledFromValidUser()) {
2607 return;
2608 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002609 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002610 if (!calledWithValidToken(token)) {
2611 final int uid = Binder.getCallingUid();
2612 Slog.e(TAG, "Ignoring showMySoftInput due to an invalid token. uid:"
2613 + uid + " token:" + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002614 return;
2615 }
2616 long ident = Binder.clearCallingIdentity();
2617 try {
2618 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002619 } finally {
2620 Binder.restoreCallingIdentity(ident);
2621 }
2622 }
2623 }
2624
2625 void setEnabledSessionInMainThread(SessionState session) {
2626 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002627 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002628 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002629 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002630 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002631 } catch (RemoteException e) {
2632 }
2633 }
2634 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002635 if (mEnabledSession != null && mEnabledSession.session != null) {
2636 try {
2637 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
2638 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
2639 } catch (RemoteException e) {
2640 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002641 }
2642 }
2643 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002644
satok42c5a162011-05-26 16:46:14 +09002645 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002646 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002647 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002648 switch (msg.what) {
satokab751aa2010-09-14 19:17:36 +09002649 case MSG_SHOW_IM_SUBTYPE_PICKER:
Seigo Nonaka14e13912015-05-06 21:04:13 -07002650 final boolean showAuxSubtypes;
2651 switch (msg.arg1) {
2652 case InputMethodManager.SHOW_IM_PICKER_MODE_AUTO:
2653 // This is undocumented so far, but IMM#showInputMethodPicker() has been
2654 // implemented so that auxiliary subtypes will be excluded when the soft
2655 // keyboard is invisible.
2656 showAuxSubtypes = mInputShown;
2657 break;
2658 case InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES:
2659 showAuxSubtypes = true;
2660 break;
2661 case InputMethodManager.SHOW_IM_PICKER_MODE_EXCLUDE_AUXILIARY_SUBTYPES:
2662 showAuxSubtypes = false;
2663 break;
2664 default:
2665 Slog.e(TAG, "Unknown subtype picker mode = " + msg.arg1);
2666 return false;
2667 }
2668 showInputMethodMenu(showAuxSubtypes);
satokab751aa2010-09-14 19:17:36 +09002669 return true;
2670
satok47a44912010-10-06 16:03:58 +09002671 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002672 args = (SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09002673 showInputMethodAndSubtypeEnabler((String)args.arg1);
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002674 args.recycle();
satok217f5482010-12-15 05:19:19 +09002675 return true;
2676
2677 case MSG_SHOW_IM_CONFIG:
2678 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002679 return true;
2680
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002681 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002682
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002683 case MSG_UNBIND_INPUT:
2684 try {
2685 ((IInputMethod)msg.obj).unbindInput();
2686 } catch (RemoteException e) {
2687 // There is nothing interesting about the method dying.
2688 }
2689 return true;
2690 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002691 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002692 try {
2693 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2694 } catch (RemoteException e) {
2695 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002696 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002697 return true;
2698 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002699 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002700 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002701 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002702 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002703 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002704 } catch (RemoteException e) {
2705 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002706 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002707 return true;
2708 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002709 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002710 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002711 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002712 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002713 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002714 } catch (RemoteException e) {
2715 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002716 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002717 return true;
2718 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002719 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002720 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002721 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002722 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2723 } catch (RemoteException e) {
2724 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002725 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002726 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002727 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002728 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002729 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002730 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002731 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002732 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002733 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002734 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002735 // Dispose the channel if the input method is not local to this process
2736 // because the remote proxy will get its own copy when unparceled.
2737 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002738 channel.dispose();
2739 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002740 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002741 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002742 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002743 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002744 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002746 case MSG_START_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002747 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002748 try {
2749 SessionState session = (SessionState)args.arg1;
2750 setEnabledSessionInMainThread(session);
2751 session.method.startInput((IInputContext)args.arg2,
2752 (EditorInfo)args.arg3);
2753 } catch (RemoteException e) {
2754 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002755 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002756 return true;
2757 case MSG_RESTART_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002758 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002759 try {
2760 SessionState session = (SessionState)args.arg1;
2761 setEnabledSessionInMainThread(session);
2762 session.method.restartInput((IInputContext)args.arg2,
2763 (EditorInfo)args.arg3);
2764 } catch (RemoteException e) {
2765 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002766 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002767 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002768
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002769 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002770
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002771 case MSG_UNBIND_METHOD:
2772 try {
2773 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
2774 } catch (RemoteException e) {
2775 // There is nothing interesting about the last client dying.
2776 }
2777 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002778 case MSG_BIND_METHOD: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002779 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002780 IInputMethodClient client = (IInputMethodClient)args.arg1;
2781 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002782 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002783 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002784 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002785 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002786 } finally {
2787 // Dispose the channel if the input method is not local to this process
2788 // because the remote proxy will get its own copy when unparceled.
2789 if (res.channel != null && Binder.isProxy(client)) {
2790 res.channel.dispose();
2791 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002792 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002793 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002794 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002795 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002796 case MSG_SET_ACTIVE:
2797 try {
2798 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2799 } catch (RemoteException e) {
2800 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2801 + ((ClientState)msg.obj).pid + " uid "
2802 + ((ClientState)msg.obj).uid);
2803 }
2804 return true;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002805 case MSG_SET_INTERACTIVE:
2806 handleSetInteractive(msg.arg1 != 0);
2807 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002808 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
2809 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07002810 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002811 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07002812 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002813 } catch (RemoteException e) {
2814 Slog.w(TAG, "Got RemoteException sending "
2815 + "setUserActionNotificationSequenceNumber("
2816 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07002817 + clientState.pid + " uid "
2818 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002819 }
2820 return true;
2821 }
satok01038492012-04-09 21:08:27 +09002822
2823 // --------------------------------------------------------------
2824 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07002825 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09002826 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002827 }
2828 return false;
2829 }
2830
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002831 private void handleSetInteractive(final boolean interactive) {
2832 synchronized (mMethodMap) {
2833 mIsInteractive = interactive;
2834 updateSystemUiLocked(mCurToken, interactive ? mImeWindowVis : 0, mBackDisposition);
2835
2836 // Inform the current client of the change in active status
2837 if (mCurClient != null && mCurClient.client != null) {
2838 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
2839 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, mCurClient));
2840 }
2841 }
2842 }
2843
satokdc9ddae2011-10-06 12:22:36 +09002844 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002845 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
2846 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09002847 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002848 if (DEBUG) {
2849 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2850 }
satok723a27e2010-11-11 14:58:11 +09002851 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002852 return true;
2853 }
2854
2855 return false;
2856 }
2857
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002858 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002859 HashMap<String, InputMethodInfo> map, boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002860 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002861 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07002862 + " \n ------ caller=" + Debug.getCallers(10));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002863 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002864 list.clear();
2865 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002866
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002867 // Use for queryIntentServicesAsUser
2868 final PackageManager pm = mContext.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002869
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002870 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002871 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002872 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2873 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002874
satoke7c6998e2011-06-03 17:57:59 +09002875 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2876 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002877 for (int i = 0; i < services.size(); ++i) {
2878 ResolveInfo ri = services.get(i);
2879 ServiceInfo si = ri.serviceInfo;
2880 ComponentName compName = new ComponentName(si.packageName, si.name);
2881 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2882 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002883 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002884 + ": it does not require the permission "
2885 + android.Manifest.permission.BIND_INPUT_METHOD);
2886 continue;
2887 }
2888
Joe Onorato8a9b2202010-02-26 18:56:32 -08002889 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002890
2891 try {
satoke7c6998e2011-06-03 17:57:59 +09002892 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002893 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002894 final String id = p.getId();
2895 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002896
2897 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002898 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002899 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07002900 } catch (XmlPullParserException | IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002901 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002902 }
2903 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002904
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002905 if (resetDefaultEnabledIme) {
2906 final ArrayList<InputMethodInfo> defaultEnabledIme =
2907 InputMethodUtils.getDefaultEnabledImes(mContext, mSystemReady, list);
2908 for (int i = 0; i < defaultEnabledIme.size(); ++i) {
2909 final InputMethodInfo imi = defaultEnabledIme.get(i);
2910 if (DEBUG) {
2911 Slog.d(TAG, "--- enable ime = " + imi);
2912 }
2913 setInputMethodEnabledLocked(imi.getId(), true);
2914 }
2915 }
2916
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002917 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09002918 if (!TextUtils.isEmpty(defaultImiId)) {
2919 if (!map.containsKey(defaultImiId)) {
2920 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
2921 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07002922 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09002923 }
2924 } else {
2925 // Double check that the default IME is certainly enabled.
2926 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002927 }
2928 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09002929 // Here is not the perfect place to reset the switching controller. Ideally
2930 // mSwitchingController and mSettings should be able to share the same state.
2931 // TODO: Make sure that mSwitchingController and mSettings are sharing the
2932 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09002933 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002934 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002935
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002936 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002937
satok217f5482010-12-15 05:19:19 +09002938 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002939 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002940 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002941 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2942 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002943 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002944 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002945 }
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002946 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok217f5482010-12-15 05:19:19 +09002947 }
2948
2949 private void showConfigureInputMethods() {
2950 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2951 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2952 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2953 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002954 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09002955 }
2956
satok2c93efc2012-04-02 19:33:47 +09002957 private boolean isScreenLocked() {
2958 return mKeyguardManager != null
2959 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
2960 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002961
Seigo Nonaka14e13912015-05-06 21:04:13 -07002962 private void showInputMethodMenu(boolean showAuxSubtypes) {
2963 if (DEBUG) Slog.v(TAG, "Show switching menu. showAuxSubtypes=" + showAuxSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002964
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002965 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09002966 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002967
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002968 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002969 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002970 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002971
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002972 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002973 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09002974 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
2975 mContext);
satok7f35c8c2010-10-07 21:13:11 +09002976 if (immis == null || immis.size() == 0) {
2977 return;
2978 }
2979
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002980 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002981
satok688bd472012-02-09 20:09:17 +09002982 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002983 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
Seigo Nonaka14e13912015-05-06 21:04:13 -07002984 true /* showSubtypes */, showAuxSubtypes, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09002985
satokc3690562012-01-10 20:14:43 +09002986 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002987 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09002988 if (currentSubtype != null) {
2989 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002990 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
2991 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09002992 }
2993 }
2994
Ken Wakasa761eb372011-03-04 19:06:18 +09002995 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09002996 mIms = new InputMethodInfo[N];
2997 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002998 int checkedItem = 0;
2999 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09003000 final ImeSubtypeListItem item = imList.get(i);
3001 mIms[i] = item.mImi;
3002 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003003 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09003004 int subtypeId = mSubtypeIds[i];
3005 if ((subtypeId == NOT_A_SUBTYPE_ID)
3006 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
3007 || (subtypeId == lastInputMethodSubtypeId)) {
3008 checkedItem = i;
3009 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003010 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003011 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08003012
3013 final Context settingsContext = new ContextThemeWrapper(context,
3014 com.android.internal.R.style.Theme_DeviceDefault_Settings);
3015
3016 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003017 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
3018 @Override
3019 public void onCancel(DialogInterface dialog) {
3020 hideInputMethodMenu();
3021 }
3022 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08003023
3024 final Context dialogContext = mDialogBuilder.getContext();
3025 final TypedArray a = dialogContext.obtainStyledAttributes(null,
3026 com.android.internal.R.styleable.DialogPreference,
3027 com.android.internal.R.attr.alertDialogStyle, 0);
3028 final Drawable dialogIcon = a.getDrawable(
3029 com.android.internal.R.styleable.DialogPreference_dialogIcon);
3030 a.recycle();
3031
3032 mDialogBuilder.setIcon(dialogIcon);
3033
3034 final LayoutInflater inflater = (LayoutInflater) dialogContext.getSystemService(
3035 Context.LAYOUT_INFLATER_SERVICE);
satok01038492012-04-09 21:08:27 +09003036 final View tv = inflater.inflate(
3037 com.android.internal.R.layout.input_method_switch_dialog_title, null);
3038 mDialogBuilder.setCustomTitle(tv);
3039
3040 // Setup layout for a toggle switch of the hardware keyboard
3041 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003042 mSwitchingDialogTitleView
3043 .findViewById(com.android.internal.R.id.hard_keyboard_section)
3044 .setVisibility(mWindowManagerService.isHardKeyboardAvailable()
3045 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003046 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003047 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07003048 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003049 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
3050 @Override
3051 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003052 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003053 // Ensure that the input method dialog is dismissed when changing
3054 // the hardware keyboard state.
3055 hideInputMethodMenu();
3056 }
3057 });
3058
Alan Viverette505e3ab2014-11-24 15:22:11 -08003059 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003060 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
3061 final OnClickListener choiceListener = new OnClickListener() {
3062 @Override
3063 public void onClick(final DialogInterface dialog, final int which) {
3064 synchronized (mMethodMap) {
3065 if (mIms == null || mIms.length <= which || mSubtypeIds == null
3066 || mSubtypeIds.length <= which) {
3067 return;
satok01038492012-04-09 21:08:27 +09003068 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003069 final InputMethodInfo im = mIms[which];
3070 int subtypeId = mSubtypeIds[which];
3071 adapter.mCheckedItem = which;
3072 adapter.notifyDataSetChanged();
3073 hideInputMethodMenu();
3074 if (im != null) {
3075 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
3076 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003077 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003078 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003079 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003080 }
3081 }
3082 };
3083 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003084
Seigo Nonakad4474cb2015-05-04 16:53:24 -07003085 if (!isScreenLocked) {
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003086 final OnClickListener positiveListener = new OnClickListener() {
3087 @Override
3088 public void onClick(DialogInterface dialog, int whichButton) {
3089 showConfigureInputMethods();
3090 }
3091 };
satok82beadf2010-12-27 19:03:06 +09003092 mDialogBuilder.setPositiveButton(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003093 com.android.internal.R.string.configure_input_methods, positiveListener);
satok7f35c8c2010-10-07 21:13:11 +09003094 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003095 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003096 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003097 mSwitchingDialog.getWindow().setType(
3098 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09003099 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
3100 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003101 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003102 updateSystemUi(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003103 mSwitchingDialog.show();
3104 }
3105 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003106
Ken Wakasa05dbb652011-08-22 15:22:43 +09003107 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
3108 private final LayoutInflater mInflater;
3109 private final int mTextViewResourceId;
3110 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09003111 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09003112 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
3113 List<ImeSubtypeListItem> itemsList, int checkedItem) {
3114 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003115
Ken Wakasa05dbb652011-08-22 15:22:43 +09003116 mTextViewResourceId = textViewResourceId;
3117 mItemsList = itemsList;
3118 mCheckedItem = checkedItem;
Alan Viverette505e3ab2014-11-24 15:22:11 -08003119 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Ken Wakasa05dbb652011-08-22 15:22:43 +09003120 }
3121
3122 @Override
3123 public View getView(int position, View convertView, ViewGroup parent) {
3124 final View view = convertView != null ? convertView
3125 : mInflater.inflate(mTextViewResourceId, null);
3126 if (position < 0 || position >= mItemsList.size()) return view;
3127 final ImeSubtypeListItem item = mItemsList.get(position);
3128 final CharSequence imeName = item.mImeName;
3129 final CharSequence subtypeName = item.mSubtypeName;
3130 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
3131 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
3132 if (TextUtils.isEmpty(subtypeName)) {
3133 firstTextView.setText(imeName);
3134 secondTextView.setVisibility(View.GONE);
3135 } else {
3136 firstTextView.setText(subtypeName);
3137 secondTextView.setText(imeName);
3138 secondTextView.setVisibility(View.VISIBLE);
3139 }
3140 final RadioButton radioButton =
3141 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
3142 radioButton.setChecked(position == mCheckedItem);
3143 return view;
3144 }
3145 }
3146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003147 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003148 synchronized (mMethodMap) {
3149 hideInputMethodMenuLocked();
3150 }
3151 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003152
The Android Open Source Project10592532009-03-18 17:39:46 -07003153 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003154 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003155
The Android Open Source Project10592532009-03-18 17:39:46 -07003156 if (mSwitchingDialog != null) {
3157 mSwitchingDialog.dismiss();
3158 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003159 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003160
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003161 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project10592532009-03-18 17:39:46 -07003162 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003163 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003164 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003166 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003167
satok42c5a162011-05-26 16:46:14 +09003168 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003169 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003170 // TODO: Make this work even for non-current users?
3171 if (!calledFromValidUser()) {
3172 return false;
3173 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003174 synchronized (mMethodMap) {
3175 if (mContext.checkCallingOrSelfPermission(
3176 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3177 != PackageManager.PERMISSION_GRANTED) {
3178 throw new SecurityException(
3179 "Requires permission "
3180 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3181 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003183 long ident = Binder.clearCallingIdentity();
3184 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003185 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003186 } finally {
3187 Binder.restoreCallingIdentity(ident);
3188 }
3189 }
3190 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003191
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003192 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3193 // Make sure this is a valid input method.
3194 InputMethodInfo imm = mMethodMap.get(id);
3195 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003196 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003197 }
3198
satokd87c2592010-09-29 11:52:06 +09003199 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3200 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003201
satokd87c2592010-09-29 11:52:06 +09003202 if (enabled) {
3203 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3204 if (pair.first.equals(id)) {
3205 // We are enabling this input method, but it is already enabled.
3206 // Nothing to do. The previous state was enabled.
3207 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003208 }
3209 }
satokd87c2592010-09-29 11:52:06 +09003210 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3211 // Previous state was disabled.
3212 return false;
3213 } else {
3214 StringBuilder builder = new StringBuilder();
3215 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3216 builder, enabledInputMethodsList, id)) {
3217 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003218 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003219 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3220 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3221 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003222 }
3223 // Previous state was enabled.
3224 return true;
3225 } else {
3226 // We are disabling the input method but it is already disabled.
3227 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003228 return false;
3229 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003230 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003231 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003232
satok723a27e2010-11-11 14:58:11 +09003233 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3234 boolean setSubtypeOnly) {
3235 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003236 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003237
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003238 mCurUserActionNotificationSequenceNumber =
3239 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3240 if (DEBUG) {
3241 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3242 + mCurUserActionNotificationSequenceNumber);
3243 }
3244
3245 if (mCurClient != null && mCurClient.client != null) {
3246 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3247 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003248 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003249 }
3250
satok723a27e2010-11-11 14:58:11 +09003251 // Set Subtype here
3252 if (imi == null || subtypeId < 0) {
3253 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003254 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003255 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003256 if (subtypeId < imi.getSubtypeCount()) {
3257 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3258 mSettings.putSelectedSubtype(subtype.hashCode());
3259 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003260 } else {
3261 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003262 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003263 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003264 }
satokab751aa2010-09-14 19:17:36 +09003265 }
satok723a27e2010-11-11 14:58:11 +09003266
satok4c0e7152012-06-20 20:08:44 +09003267 // Workaround.
3268 // ASEC is not ready in the IMMS constructor. Accordingly, forward-locked
3269 // IMEs are not recognized and considered uninstalled.
3270 // Actually, we can't move everything after SystemReady because
3271 // IMMS needs to run in the encryption lock screen. So, we just skip changing
3272 // the default IME here and try cheking the default IME again in systemReady().
3273 // TODO: Do nothing before system ready and implement a separated logic for
3274 // the encryption lock screen.
3275 // TODO: ASEC should be ready before IMMS is instantiated.
3276 if (mSystemReady && !setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003277 // Set InputMethod here
3278 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3279 }
3280 }
3281
3282 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3283 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3284 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3285 // newDefaultIme is empty when there is no candidate for the selected IME.
3286 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3287 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3288 if (subtypeHashCode != null) {
3289 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003290 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok723a27e2010-11-11 14:58:11 +09003291 imi, Integer.valueOf(subtypeHashCode));
3292 } catch (NumberFormatException e) {
3293 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3294 }
3295 }
3296 }
3297 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003298 }
3299
satok4e4569d2010-11-19 18:45:53 +09003300 // If there are no selected shortcuts, tries finding the most applicable ones.
3301 private Pair<InputMethodInfo, InputMethodSubtype>
3302 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3303 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3304 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003305 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003306 boolean foundInSystemIME = false;
3307
3308 // Search applicable subtype for each InputMethodInfo
3309 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003310 final String imiId = imi.getId();
3311 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3312 continue;
3313 }
satokcd7cd292010-11-20 15:46:23 +09003314 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003315 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003316 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003317 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003318 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003319 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003320 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003321 }
satokdf31ae62011-01-15 06:19:44 +09003322 // 2. Search by the system locale from enabledSubtypes.
3323 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003324 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003325 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003326 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003327 }
satoka86f5e42011-09-02 17:12:42 +09003328 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003329 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003330 final ArrayList<InputMethodSubtype> subtypesForSearch =
3331 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003332 ? InputMethodUtils.getSubtypes(imi)
3333 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003334 // 4. Search by the current subtype's locale from all subtypes.
3335 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003336 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003337 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003338 }
3339 // 5. Search by the system locale from all subtypes.
3340 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003341 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003342 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003343 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003344 }
satokcd7cd292010-11-20 15:46:23 +09003345 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003346 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003347 // The current input method is the most applicable IME.
3348 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003349 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003350 break;
satok7599a7f2010-12-22 13:45:23 +09003351 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003352 // The system input method is 2nd applicable IME.
3353 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003354 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003355 if ((imi.getServiceInfo().applicationInfo.flags
3356 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3357 foundInSystemIME = true;
3358 }
satok4e4569d2010-11-19 18:45:53 +09003359 }
3360 }
3361 }
3362 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003363 if (mostApplicableIMI != null) {
3364 Slog.w(TAG, "Most applicable shortcut input method was:"
3365 + mostApplicableIMI.getId());
3366 if (mostApplicableSubtype != null) {
3367 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3368 + "," + mostApplicableSubtype.getMode() + ","
3369 + mostApplicableSubtype.getLocale());
3370 }
3371 }
satok4e4569d2010-11-19 18:45:53 +09003372 }
satokcd7cd292010-11-20 15:46:23 +09003373 if (mostApplicableIMI != null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003374 return new Pair<> (mostApplicableIMI, mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003375 } else {
3376 return null;
3377 }
3378 }
3379
satokab751aa2010-09-14 19:17:36 +09003380 /**
3381 * @return Return the current subtype of this input method.
3382 */
satok42c5a162011-05-26 16:46:14 +09003383 @Override
satokab751aa2010-09-14 19:17:36 +09003384 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003385 // TODO: Make this work even for non-current users?
3386 if (!calledFromValidUser()) {
3387 return null;
3388 }
3389 synchronized (mMethodMap) {
3390 return getCurrentInputMethodSubtypeLocked();
3391 }
3392 }
3393
3394 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003395 if (mCurMethodId == null) {
3396 return null;
3397 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003398 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003399 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3400 if (imi == null || imi.getSubtypeCount() == 0) {
3401 return null;
satok4e4569d2010-11-19 18:45:53 +09003402 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003403 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003404 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3405 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003406 if (subtypeId == NOT_A_SUBTYPE_ID) {
3407 // If there are no selected subtypes, the framework will try to find
3408 // the most applicable subtype from explicitly or implicitly enabled
3409 // subtypes.
3410 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003411 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003412 // If there is only one explicitly or implicitly enabled subtype,
3413 // just returns it.
3414 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3415 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3416 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003417 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003418 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003419 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003420 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003421 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003422 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3423 true);
satok4e4569d2010-11-19 18:45:53 +09003424 }
satok3ef8b292010-11-23 06:06:29 +09003425 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003426 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003427 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003428 }
3429 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003430 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003431 }
3432
satok4e4569d2010-11-19 18:45:53 +09003433 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003434 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003435 @Override
satok4e4569d2010-11-19 18:45:53 +09003436 public List getShortcutInputMethodsAndSubtypes() {
3437 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003438 ArrayList<Object> ret = new ArrayList<>();
satokf3db1af2010-11-23 13:34:33 +09003439 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003440 // If there are no selected shortcut subtypes, the framework will try to find
3441 // the most applicable subtype from all subtypes whose mode is
3442 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003443 Pair<InputMethodInfo, InputMethodSubtype> info =
3444 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003445 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003446 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003447 ret.add(info.first);
3448 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003449 }
satok3da92232011-01-11 22:46:30 +09003450 return ret;
satokf3db1af2010-11-23 13:34:33 +09003451 }
satokf3db1af2010-11-23 13:34:33 +09003452 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3453 ret.add(imi);
3454 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3455 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003456 }
3457 }
satokf3db1af2010-11-23 13:34:33 +09003458 return ret;
satok4e4569d2010-11-19 18:45:53 +09003459 }
3460 }
3461
satok42c5a162011-05-26 16:46:14 +09003462 @Override
satokb66d2872010-11-10 01:04:04 +09003463 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003464 // TODO: Make this work even for non-current users?
3465 if (!calledFromValidUser()) {
3466 return false;
3467 }
satokb66d2872010-11-10 01:04:04 +09003468 synchronized (mMethodMap) {
3469 if (subtype != null && mCurMethodId != null) {
3470 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003471 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003472 if (subtypeId != NOT_A_SUBTYPE_ID) {
3473 setInputMethodLocked(mCurMethodId, subtypeId);
3474 return true;
3475 }
3476 }
3477 return false;
3478 }
3479 }
3480
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003481 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003482 private static class InputMethodFileManager {
3483 private static final String SYSTEM_PATH = "system";
3484 private static final String INPUT_METHOD_PATH = "inputmethod";
3485 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3486 private static final String NODE_SUBTYPES = "subtypes";
3487 private static final String NODE_SUBTYPE = "subtype";
3488 private static final String NODE_IMI = "imi";
3489 private static final String ATTR_ID = "id";
3490 private static final String ATTR_LABEL = "label";
3491 private static final String ATTR_ICON = "icon";
3492 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3493 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3494 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3495 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3496 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3497 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003498 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003499 new HashMap<>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003500 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003501 if (methodMap == null) {
3502 throw new NullPointerException("methodMap is null");
3503 }
3504 mMethodMap = methodMap;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003505 final File systemDir = userId == UserHandle.USER_OWNER
3506 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3507 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003508 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
Yohei Yukawadf5af482015-08-04 22:11:11 -07003509 if (!inputMethodDir.exists() && !inputMethodDir.mkdirs()) {
satoke7c6998e2011-06-03 17:57:59 +09003510 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3511 }
3512 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3513 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3514 if (!subtypeFile.exists()) {
3515 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003516 writeAdditionalInputMethodSubtypes(
3517 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003518 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003519 readAdditionalInputMethodSubtypes(
3520 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003521 }
3522 }
3523
3524 private void deleteAllInputMethodSubtypes(String imiId) {
3525 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003526 mAdditionalSubtypesMap.remove(imiId);
3527 writeAdditionalInputMethodSubtypes(
3528 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003529 }
3530 }
3531
3532 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003533 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003534 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003535 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003536 final int N = additionalSubtypes.length;
3537 for (int i = 0; i < N; ++i) {
3538 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003539 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003540 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003541 } else {
3542 Slog.w(TAG, "Duplicated subtype definition found: "
3543 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003544 }
3545 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003546 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3547 writeAdditionalInputMethodSubtypes(
3548 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003549 }
3550 }
3551
3552 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3553 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003554 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003555 }
3556 }
3557
3558 private static void writeAdditionalInputMethodSubtypes(
3559 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3560 HashMap<String, InputMethodInfo> methodMap) {
3561 // Safety net for the case that this function is called before methodMap is set.
3562 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3563 FileOutputStream fos = null;
3564 try {
3565 fos = subtypesFile.startWrite();
3566 final XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003567 out.setOutput(fos, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003568 out.startDocument(null, true);
3569 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3570 out.startTag(null, NODE_SUBTYPES);
3571 for (String imiId : allSubtypes.keySet()) {
3572 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3573 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3574 continue;
3575 }
3576 out.startTag(null, NODE_IMI);
3577 out.attribute(null, ATTR_ID, imiId);
3578 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3579 final int N = subtypesList.size();
3580 for (int i = 0; i < N; ++i) {
3581 final InputMethodSubtype subtype = subtypesList.get(i);
3582 out.startTag(null, NODE_SUBTYPE);
3583 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3584 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3585 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3586 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3587 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3588 out.attribute(null, ATTR_IS_AUXILIARY,
3589 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3590 out.endTag(null, NODE_SUBTYPE);
3591 }
3592 out.endTag(null, NODE_IMI);
3593 }
3594 out.endTag(null, NODE_SUBTYPES);
3595 out.endDocument();
3596 subtypesFile.finishWrite(fos);
3597 } catch (java.io.IOException e) {
3598 Slog.w(TAG, "Error writing subtypes", e);
3599 if (fos != null) {
3600 subtypesFile.failWrite(fos);
3601 }
3602 }
3603 }
3604
3605 private static void readAdditionalInputMethodSubtypes(
3606 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3607 if (allSubtypes == null || subtypesFile == null) return;
3608 allSubtypes.clear();
Yohei Yukawa5894b432015-08-11 13:29:24 -07003609 try (final FileInputStream fis = subtypesFile.openRead()) {
satoke7c6998e2011-06-03 17:57:59 +09003610 final XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003611 parser.setInput(fis, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003612 int type = parser.getEventType();
3613 // Skip parsing until START_TAG
3614 while ((type = parser.next()) != XmlPullParser.START_TAG
3615 && type != XmlPullParser.END_DOCUMENT) {}
3616 String firstNodeName = parser.getName();
3617 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3618 throw new XmlPullParserException("Xml doesn't start with subtypes");
3619 }
3620 final int depth =parser.getDepth();
3621 String currentImiId = null;
3622 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3623 while (((type = parser.next()) != XmlPullParser.END_TAG
3624 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3625 if (type != XmlPullParser.START_TAG)
3626 continue;
3627 final String nodeName = parser.getName();
3628 if (NODE_IMI.equals(nodeName)) {
3629 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3630 if (TextUtils.isEmpty(currentImiId)) {
3631 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3632 continue;
3633 }
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003634 tempSubtypesArray = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003635 allSubtypes.put(currentImiId, tempSubtypesArray);
3636 } else if (NODE_SUBTYPE.equals(nodeName)) {
3637 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3638 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3639 continue;
3640 }
3641 final int icon = Integer.valueOf(
3642 parser.getAttributeValue(null, ATTR_ICON));
3643 final int label = Integer.valueOf(
3644 parser.getAttributeValue(null, ATTR_LABEL));
3645 final String imeSubtypeLocale =
3646 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3647 final String imeSubtypeMode =
3648 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3649 final String imeSubtypeExtraValue =
3650 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003651 final boolean isAuxiliary = "1".equals(String.valueOf(
3652 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003653 final InputMethodSubtype subtype = new InputMethodSubtypeBuilder()
3654 .setSubtypeNameResId(label)
3655 .setSubtypeIconResId(icon)
3656 .setSubtypeLocale(imeSubtypeLocale)
3657 .setSubtypeMode(imeSubtypeMode)
3658 .setSubtypeExtraValue(imeSubtypeExtraValue)
3659 .setIsAuxiliary(isAuxiliary)
3660 .build();
satoke7c6998e2011-06-03 17:57:59 +09003661 tempSubtypesArray.add(subtype);
3662 }
3663 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07003664 } catch (XmlPullParserException | IOException | NumberFormatException e) {
3665 Slog.w(TAG, "Error reading subtypes", e);
satoke7c6998e2011-06-03 17:57:59 +09003666 return;
satoke7c6998e2011-06-03 17:57:59 +09003667 }
3668 }
3669 }
3670
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003671 private static final class LocalServiceImpl implements InputMethodManagerInternal {
3672 @NonNull
3673 private final Handler mHandler;
3674
3675 LocalServiceImpl(@NonNull final Handler handler) {
3676 mHandler = handler;
3677 }
3678
3679 @Override
3680 public void setInteractive(boolean interactive) {
3681 // Do everything in handler so as not to block the caller.
3682 mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_INTERACTIVE,
3683 interactive ? 1 : 0, 0));
3684 }
3685 }
3686
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003687 @Override
3688 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3689 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3690 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003691
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003692 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3693 + Binder.getCallingPid()
3694 + ", uid=" + Binder.getCallingUid());
3695 return;
3696 }
3697
3698 IInputMethod method;
3699 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003700
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003701 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003702
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003703 synchronized (mMethodMap) {
3704 p.println("Current Input Method Manager state:");
3705 int N = mMethodList.size();
3706 p.println(" Input Methods:");
3707 for (int i=0; i<N; i++) {
3708 InputMethodInfo info = mMethodList.get(i);
3709 p.println(" InputMethod #" + i + ":");
3710 info.dump(p, " ");
3711 }
3712 p.println(" Clients:");
3713 for (ClientState ci : mClients.values()) {
3714 p.println(" Client " + ci + ":");
3715 p.println(" client=" + ci.client);
3716 p.println(" inputContext=" + ci.inputContext);
3717 p.println(" sessionRequested=" + ci.sessionRequested);
3718 p.println(" curSession=" + ci.curSession);
3719 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003720 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003721 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003722 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3723 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003724 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3725 + " mBoundToMethod=" + mBoundToMethod);
3726 p.println(" mCurToken=" + mCurToken);
3727 p.println(" mCurIntent=" + mCurIntent);
3728 method = mCurMethod;
3729 p.println(" mCurMethod=" + mCurMethod);
3730 p.println(" mEnabledSession=" + mEnabledSession);
3731 p.println(" mShowRequested=" + mShowRequested
3732 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3733 + " mShowForced=" + mShowForced
3734 + " mInputShown=" + mInputShown);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003735 p.println(" mCurUserActionNotificationSequenceNumber="
3736 + mCurUserActionNotificationSequenceNumber);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003737 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mIsInteractive);
Yohei Yukawa81482972015-06-04 00:58:59 -07003738 p.println(" mSettingsObserver=" + mSettingsObserver);
Yohei Yukawad7248862015-06-03 23:56:12 -07003739 p.println(" mSwitchingController:");
3740 mSwitchingController.dump(p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003741 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003742
Jeff Brownb88102f2010-09-08 11:49:43 -07003743 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003744 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003745 pw.flush();
3746 try {
3747 client.client.asBinder().dump(fd, args);
3748 } catch (RemoteException e) {
3749 p.println("Input method client dead: " + e);
3750 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003751 } else {
3752 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003753 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003754
Jeff Brownb88102f2010-09-08 11:49:43 -07003755 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003756 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003757 pw.flush();
3758 try {
3759 method.asBinder().dump(fd, args);
3760 } catch (RemoteException e) {
3761 p.println("Input method service dead: " + e);
3762 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003763 } else {
3764 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003765 }
3766 }
3767}