blob: 6e871a8e919c0590b95ade0c4e7cc1ccad18d304 [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
Wale Ogunwaleac2561e2016-11-01 15:43:46 -070018import static android.view.Display.DEFAULT_DISPLAY;
19import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -070020import static java.lang.annotation.RetentionPolicy.SOURCE;
21
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080022import com.android.internal.content.PackageMonitor;
Yohei Yukawa25e08132016-06-22 16:31:41 -070023import com.android.internal.inputmethod.IInputContentUriToken;
Satoshi Kataokad7443c82013-10-15 17:45:43 +090024import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController;
Satoshi Kataokad787f692013-10-26 04:44:21 +090025import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController.ImeSubtypeListItem;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +090026import com.android.internal.inputmethod.InputMethodUtils;
27import com.android.internal.inputmethod.InputMethodUtils.InputMethodSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import com.android.internal.os.HandlerCaller;
Svetoslav Ganov758143e2012-08-06 16:40:27 -070029import com.android.internal.os.SomeArgs;
satoke7c6998e2011-06-03 17:57:59 +090030import com.android.internal.util.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import com.android.internal.view.IInputContext;
32import com.android.internal.view.IInputMethod;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import com.android.internal.view.IInputMethodClient;
34import com.android.internal.view.IInputMethodManager;
35import com.android.internal.view.IInputMethodSession;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070036import com.android.internal.view.IInputSessionCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import com.android.internal.view.InputBindResult;
Yohei Yukawa33e81792015-11-17 21:14:42 -080038import com.android.internal.view.InputMethodClient;
Adam Lesinskief2ea1f2013-12-05 16:48:06 -080039import com.android.server.statusbar.StatusBarManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040
satoke7c6998e2011-06-03 17:57:59 +090041import org.xmlpull.v1.XmlPullParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import org.xmlpull.v1.XmlPullParserException;
satoke7c6998e2011-06-03 17:57:59 +090043import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044
Yohei Yukawafa0e47e2016-04-05 09:55:56 -070045import android.annotation.IntDef;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070046import android.annotation.NonNull;
Yohei Yukawae13a20fa2015-09-30 19:11:32 -070047import android.annotation.Nullable;
Yohei Yukawa7b18aec2016-03-07 13:04:32 -080048import android.annotation.UserIdInt;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.app.ActivityManagerNative;
50import android.app.AlertDialog;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070051import android.app.AppGlobals;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +090052import android.app.AppOpsManager;
satokf90a33e2011-07-19 11:55:52 +090053import android.app.KeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +090054import android.app.Notification;
55import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070056import android.app.PendingIntent;
satok5b927c432012-05-01 20:09:34 +090057import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058import android.content.ComponentName;
59import android.content.ContentResolver;
60import android.content.Context;
61import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062import android.content.DialogInterface.OnCancelListener;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +090063import android.content.DialogInterface.OnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090065import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070067import android.content.pm.ApplicationInfo;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090068import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.content.pm.PackageManager;
70import android.content.pm.ResolveInfo;
71import android.content.pm.ServiceInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070072import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073import android.content.res.Resources;
74import android.content.res.TypedArray;
75import android.database.ContentObserver;
Alan Viverette505e3ab2014-11-24 15:22:11 -080076import android.graphics.drawable.Drawable;
Yohei Yukawab097b822015-12-01 10:43:08 -080077import android.hardware.input.InputManagerInternal;
Joe Onorato857fd9b2011-01-27 15:08:35 -080078import android.inputmethodservice.InputMethodService;
Michael Wright7b5a96b2014-08-09 19:28:42 -070079import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080import android.os.Binder;
Chris Wren1ce4b6d2015-06-11 10:19:43 -040081import android.os.Bundle;
Seigo Nonakae27dc2b2015-08-14 18:21:27 -070082import android.os.Debug;
satoke7c6998e2011-06-03 17:57:59 +090083import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084import android.os.Handler;
85import android.os.IBinder;
86import android.os.IInterface;
87import android.os.Message;
Yohei Yukawa23cbe852016-05-17 16:42:58 -070088import android.os.LocaleList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089import android.os.Parcel;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070090import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080092import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093import android.os.ServiceManager;
94import android.os.SystemClock;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090095import android.os.UserHandle;
Amith Yamasani734983f2014-03-04 16:48:05 -080096import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097import android.provider.Settings;
98import android.text.TextUtils;
satokf9f01002011-05-19 21:31:50 +090099import android.text.style.SuggestionSpan;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700100import android.util.ArrayMap;
101import android.util.ArraySet;
Dianne Hackborn39606a02012-07-31 17:54:35 -0700102import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +0900104import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +0900105import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106import android.util.PrintWriterPrinter;
107import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +0900108import android.util.Slog;
109import android.util.Xml;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +0900110import android.view.ContextThemeWrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111import android.view.IWindowManager;
Jeff Brownc28867a2013-03-26 15:42:39 -0700112import android.view.InputChannel;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900113import android.view.LayoutInflater;
114import android.view.View;
115import android.view.ViewGroup;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116import android.view.WindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700117import android.view.WindowManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900118import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119import android.view.inputmethod.InputBinding;
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700120import android.view.inputmethod.InputConnectionInspector;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121import android.view.inputmethod.InputMethod;
122import android.view.inputmethod.InputMethodInfo;
123import android.view.inputmethod.InputMethodManager;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700124import android.view.inputmethod.InputMethodManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900125import android.view.inputmethod.InputMethodSubtype;
Yohei Yukawa443c2ba2014-09-10 14:10:30 +0900126import android.view.inputmethod.InputMethodSubtype.InputMethodSubtypeBuilder;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900127import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +0900128import android.widget.CompoundButton;
129import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900130import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +0900131import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900132import android.widget.TextView;
Yohei Yukawaebda7d72016-04-02 17:39:23 -0700133import android.widget.Toast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134
satoke7c6998e2011-06-03 17:57:59 +0900135import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900137import java.io.FileInputStream;
138import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139import java.io.IOException;
140import java.io.PrintWriter;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700141import java.lang.annotation.Retention;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100142import java.nio.charset.StandardCharsets;
Yohei Yukawa25e08132016-06-22 16:31:41 -0700143import java.security.InvalidParameterException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900145import java.util.Collections;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146import java.util.HashMap;
147import java.util.List;
148
149/**
150 * This class provides a system service that manages input methods.
151 */
152public class InputMethodManagerService extends IInputMethodManager.Stub
153 implements ServiceConnection, Handler.Callback {
154 static final boolean DEBUG = false;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700155 static final boolean DEBUG_RESTORE = DEBUG || false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700156 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157
Seigo Nonakad4474cb2015-05-04 16:53:24 -0700158 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 1;
159 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 2;
160 static final int MSG_SHOW_IM_CONFIG = 3;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 static final int MSG_UNBIND_INPUT = 1000;
163 static final int MSG_BIND_INPUT = 1010;
164 static final int MSG_SHOW_SOFT_INPUT = 1020;
165 static final int MSG_HIDE_SOFT_INPUT = 1030;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -0700166 static final int MSG_HIDE_CURRENT_INPUT_METHOD = 1035;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 static final int MSG_ATTACH_TOKEN = 1040;
168 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 static final int MSG_START_INPUT = 2000;
171 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800172
Yohei Yukawa33e81792015-11-17 21:14:42 -0800173 static final int MSG_UNBIND_CLIENT = 3000;
174 static final int MSG_BIND_CLIENT = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700175 static final int MSG_SET_ACTIVE = 3020;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700176 static final int MSG_SET_INTERACTIVE = 3030;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900177 static final int MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER = 3040;
Yohei Yukawaae61f712015-12-09 13:00:10 -0800178 static final int MSG_SWITCH_IME = 3050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800179
satok01038492012-04-09 21:08:27 +0900180 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
181
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700182 static final int MSG_SYSTEM_UNLOCK_USER = 5000;
183
Satoshi Kataokabcacc322013-10-21 17:57:27 -0700184 static final long TIME_TO_RECONNECT = 3 * 1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800185
satokf9f01002011-05-19 21:31:50 +0900186 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
187
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900188 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
satokb6359412011-06-28 17:47:41 +0900189 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900190
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700191 @Retention(SOURCE)
192 @IntDef({HardKeyboardBehavior.WIRELESS_AFFORDANCE, HardKeyboardBehavior.WIRED_AFFORDANCE})
193 private @interface HardKeyboardBehavior {
194 int WIRELESS_AFFORDANCE = 0;
195 int WIRED_AFFORDANCE = 1;
196 }
satok4e4569d2010-11-19 18:45:53 +0900197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800199 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900201 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 final IWindowManager mIWindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700204 final WindowManagerInternal mWindowManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 final HandlerCaller mCaller;
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700206 final boolean mHasFeature;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900207 private InputMethodFileManager mFileManager;
satok01038492012-04-09 21:08:27 +0900208 private final HardKeyboardListener mHardKeyboardListener;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900209 private final AppOpsManager mAppOpsManager;
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800210 private final UserManager mUserManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800211
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900212 final InputBindResult mNoBinding = new InputBindResult(null, null, null, -1, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 // All known input methods. mMethodMap also serves as the global
215 // lock for this class.
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700216 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<>();
217 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<>();
satokf9f01002011-05-19 21:31:50 +0900218 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700219 new LruCache<>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Satoshi Kataokad787f692013-10-26 04:44:21 +0900220 private final InputMethodSubtypeSwitchingController mSwitchingController;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800221
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700222 // Used to bring IME service up to visible adjustment while it is being shown.
223 final ServiceConnection mVisibleConnection = new ServiceConnection() {
224 @Override public void onServiceConnected(ComponentName name, IBinder service) {
225 }
226
227 @Override public void onServiceDisconnected(ComponentName name) {
228 }
229 };
230 boolean mVisibleBound = false;
231
satok7cfc0ed2011-06-20 21:29:36 +0900232 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700233 private NotificationManager mNotificationManager;
234 private KeyguardManager mKeyguardManager;
Griff Hazen6090c262016-03-25 08:11:24 -0700235 private @Nullable StatusBarManagerService mStatusBar;
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400236 private Notification.Builder mImeSwitcherNotification;
Dianne Hackborn661cd522011-08-22 00:26:20 -0700237 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900238 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900239 private boolean mNotificationShown;
satok0a1bcf42012-05-16 19:26:31 +0900240 private final boolean mImeSelectedOnBoot;
satok7cfc0ed2011-06-20 21:29:36 +0900241
Tadashi G. Takaoka8c6d4772014-08-05 15:29:17 +0900242 static class SessionState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 final ClientState client;
244 final IInputMethod method;
Jeff Brownc28867a2013-03-26 15:42:39 -0700245
246 IInputMethodSession session;
247 InputChannel channel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800248
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 @Override
250 public String toString() {
251 return "SessionState{uid " + client.uid + " pid " + client.pid
252 + " method " + Integer.toHexString(
253 System.identityHashCode(method))
254 + " session " + Integer.toHexString(
255 System.identityHashCode(session))
Jeff Brownc28867a2013-03-26 15:42:39 -0700256 + " channel " + channel
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 + "}";
258 }
259
260 SessionState(ClientState _client, IInputMethod _method,
Jeff Brownc28867a2013-03-26 15:42:39 -0700261 IInputMethodSession _session, InputChannel _channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 client = _client;
263 method = _method;
264 session = _session;
Jeff Brownc28867a2013-03-26 15:42:39 -0700265 channel = _channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 }
267 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800268
Jeff Brownc28867a2013-03-26 15:42:39 -0700269 static final class ClientState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 final IInputMethodClient client;
271 final IInputContext inputContext;
272 final int uid;
273 final int pid;
274 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800275
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 boolean sessionRequested;
277 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 @Override
280 public String toString() {
281 return "ClientState{" + Integer.toHexString(
282 System.identityHashCode(this)) + " uid " + uid
283 + " pid " + pid + "}";
284 }
285
286 ClientState(IInputMethodClient _client, IInputContext _inputContext,
287 int _uid, int _pid) {
288 client = _client;
289 inputContext = _inputContext;
290 uid = _uid;
291 pid = _pid;
292 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
293 }
294 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800295
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700296 final HashMap<IBinder, ClientState> mClients = new HashMap<>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800297
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700299 * Set once the system is ready to run third party code.
300 */
301 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800302
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700303 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700304 * Id obtained with {@link InputMethodInfo#getId()} for the currently selected input method.
305 * method. This is to be synchronized with the secure settings keyed with
306 * {@link Settings.Secure#DEFAULT_INPUT_METHOD}.
307 *
308 * <p>This can be transiently {@code null} when the system is re-initializing input method
309 * settings, e.g., the system locale is just changed.</p>
310 *
311 * <p>Note that {@link #mCurId} is used to track which IME is being connected to
312 * {@link InputMethodManagerService}.</p>
313 *
314 * @see #mCurId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700316 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 /**
320 * The current binding sequence number, incremented every time there is
321 * a new bind performed.
322 */
323 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800324
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 /**
326 * The client that is currently bound to an input method.
327 */
328 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800329
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800331 * The last window token that we confirmed to be focused. This is always updated upon reports
332 * from the input method client. If the window state is already changed before the report is
333 * handled, this field just keeps the last value.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700334 */
335 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800336
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700337 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800338 * The client by which {@link #mCurFocusedWindow} was reported. Used only for debugging.
339 */
340 ClientState mCurFocusedWindowClient;
341
342 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 * The input context last provided by the current client.
344 */
345 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 /**
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700348 * The missing method flags for the input context last provided by the current client.
349 *
350 * @see android.view.inputmethod.InputConnectionInspector.MissingMethodFlags
351 */
352 int mCurInputContextMissingMethods;
353
354 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 * The attributes last provided by the current client.
356 */
357 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700360 * Id obtained with {@link InputMethodInfo#getId()} for the input method that we are currently
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 * connected to or in the process of connecting to.
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700362 *
363 * <p>This can be {@code null} when no input method is connected.</p>
364 *
365 * @see #mCurMethodId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700367 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 /**
satokab751aa2010-09-14 19:17:36 +0900371 * The current subtype of the current input method.
372 */
373 private InputMethodSubtype mCurrentSubtype;
374
satok4e4569d2010-11-19 18:45:53 +0900375 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900376 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700377 mShortcutInputMethodsAndSubtypes = new HashMap<>();
satokab751aa2010-09-14 19:17:36 +0900378
John Spurlocke0980502013-10-25 11:59:29 -0400379 // Was the keyguard locked when this client became current?
380 private boolean mCurClientInKeyguard;
381
satokab751aa2010-09-14 19:17:36 +0900382 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383 * Set to true if our ServiceConnection is currently actively bound to
384 * a service (whether or not we have gotten its IBinder back yet).
385 */
386 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 /**
389 * Set if the client has asked for the input method to be shown.
390 */
391 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800392
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 /**
394 * Set if we were explicitly told to show the input method.
395 */
396 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398 /**
399 * Set if we were forced to be shown.
400 */
401 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800402
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 /**
404 * Set if we last told the input method to show itself.
405 */
406 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 /**
409 * The Intent used to connect to the current input method.
410 */
411 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 /**
414 * The token we have made for the currently active input method, to
415 * identify it in the future.
416 */
417 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800418
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 /**
420 * If non-null, this is the input method service we are currently connected
421 * to.
422 */
423 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 /**
426 * Time that we last initiated a bind to the input method, to determine
427 * if we should try to disconnect and reconnect to it.
428 */
429 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 /**
432 * Have we called mCurMethod.bindInput()?
433 */
434 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800435
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 /**
437 * Currently enabled session. Only touched by service thread, not
438 * protected by a lock.
439 */
440 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 /**
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700443 * True if the device is currently interactive with user. The value is true initially.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 */
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700445 boolean mIsInteractive = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800446
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900447 int mCurUserActionNotificationSequenceNumber = 0;
448
Joe Onorato857fd9b2011-01-27 15:08:35 -0800449 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900450
451 /**
452 * A set of status bits regarding the active IME.
453 *
454 * <p>This value is a combination of following two bits:</p>
455 * <dl>
456 * <dt>{@link InputMethodService#IME_ACTIVE}</dt>
457 * <dd>
458 * If this bit is ON, connected IME is ready to accept touch/key events.
459 * </dd>
460 * <dt>{@link InputMethodService#IME_VISIBLE}</dt>
461 * <dd>
462 * If this bit is ON, some of IME view, e.g. software input, candidate view, is visible.
463 * </dd>
464 * </dl>
465 * <em>Do not update this value outside of setImeWindowStatus.</em>
466 */
Joe Onorato857fd9b2011-01-27 15:08:35 -0800467 int mImeWindowVis;
468
Ken Wakasa05dbb652011-08-22 15:22:43 +0900469 private AlertDialog.Builder mDialogBuilder;
470 private AlertDialog mSwitchingDialog;
satok01038492012-04-09 21:08:27 +0900471 private View mSwitchingDialogTitleView;
Yohei Yukawaebda7d72016-04-02 17:39:23 -0700472 private Toast mSubtypeSwitchedByShortCutToast;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900473 private InputMethodInfo[] mIms;
474 private int[] mSubtypeIds;
Yohei Yukawae985c242016-02-24 18:27:04 -0800475 private LocaleList mLastSystemLocales;
Michael Wright7b5a96b2014-08-09 19:28:42 -0700476 private boolean mShowImeWithHardKeyboard;
Anna Galusza9b278112016-01-04 11:37:37 -0800477 private boolean mAccessibilityRequestingNoSoftKeyboard;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900478 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
479 private final IPackageManager mIPackageManager;
Jason Monk3e189872016-01-12 09:10:34 -0500480 private final String mSlotIme;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700481 @HardKeyboardBehavior
482 private final int mHardKeyboardBehavior;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800483
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800484 class SettingsObserver extends ContentObserver {
Yohei Yukawa81482972015-06-04 00:58:59 -0700485 int mUserId;
486 boolean mRegistered = false;
Yohei Yukawa7b574cb2016-03-16 17:22:22 -0700487 @NonNull
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800488 String mLastEnabled = "";
489
Yohei Yukawa81482972015-06-04 00:58:59 -0700490 /**
491 * <em>This constructor must be called within the lock.</em>
492 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 SettingsObserver(Handler handler) {
494 super(handler);
Yohei Yukawa81482972015-06-04 00:58:59 -0700495 }
496
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800497 public void registerContentObserverLocked(@UserIdInt int userId) {
Yohei Yukawa81482972015-06-04 00:58:59 -0700498 if (mRegistered && mUserId == userId) {
499 return;
500 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501 ContentResolver resolver = mContext.getContentResolver();
Yohei Yukawa81482972015-06-04 00:58:59 -0700502 if (mRegistered) {
503 mContext.getContentResolver().unregisterContentObserver(this);
504 mRegistered = false;
505 }
506 if (mUserId != userId) {
507 mLastEnabled = "";
508 mUserId = userId;
509 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700511 Settings.Secure.DEFAULT_INPUT_METHOD), false, this, userId);
satokab751aa2010-09-14 19:17:36 +0900512 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700513 Settings.Secure.ENABLED_INPUT_METHODS), false, this, userId);
satokb6109bb2011-02-03 22:24:54 +0900514 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700515 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this, userId);
Michael Wright7b5a96b2014-08-09 19:28:42 -0700516 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700517 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD), false, this, userId);
Anna Galusza9b278112016-01-04 11:37:37 -0800518 resolver.registerContentObserver(Settings.Secure.getUriFor(
519 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE), false, this, userId);
Yohei Yukawa81482972015-06-04 00:58:59 -0700520 mRegistered = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800522
Michael Wright7b5a96b2014-08-09 19:28:42 -0700523 @Override public void onChange(boolean selfChange, Uri uri) {
Anna Galusza9b278112016-01-04 11:37:37 -0800524 final Uri showImeUri = Settings.Secure.getUriFor(
525 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
526 final Uri accessibilityRequestingNoImeUri = Settings.Secure.getUriFor(
527 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528 synchronized (mMethodMap) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700529 if (showImeUri.equals(uri)) {
530 updateKeyboardFromSettingsLocked();
Anna Galusza9b278112016-01-04 11:37:37 -0800531 } else if (accessibilityRequestingNoImeUri.equals(uri)) {
532 mAccessibilityRequestingNoSoftKeyboard = Settings.Secure.getIntForUser(
533 mContext.getContentResolver(),
534 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE,
535 0, mUserId) == 1;
536 if (mAccessibilityRequestingNoSoftKeyboard) {
537 final boolean showRequested = mShowRequested;
538 hideCurrentInputLocked(0, null);
539 mShowRequested = showRequested;
540 } else if (mShowRequested) {
541 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
542 }
Michael Wright7b5a96b2014-08-09 19:28:42 -0700543 } else {
544 boolean enabledChanged = false;
545 String newEnabled = mSettings.getEnabledInputMethodsStr();
546 if (!mLastEnabled.equals(newEnabled)) {
547 mLastEnabled = newEnabled;
548 enabledChanged = true;
549 }
550 updateInputMethodsFromSettingsLocked(enabledChanged);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800551 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 }
553 }
Yohei Yukawa81482972015-06-04 00:58:59 -0700554
555 @Override
556 public String toString() {
557 return "SettingsObserver{mUserId=" + mUserId + " mRegistered=" + mRegistered
558 + " mLastEnabled=" + mLastEnabled + "}";
559 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800561
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900562 class ImmsBroadcastReceiver extends android.content.BroadcastReceiver {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900563 @Override
564 public void onReceive(Context context, Intent intent) {
565 final String action = intent.getAction();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700566 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900567 hideInputMethodMenu();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700568 // No need to update mIsInteractive
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900569 return;
Amith Yamasani734983f2014-03-04 16:48:05 -0800570 } else if (Intent.ACTION_USER_ADDED.equals(action)
571 || Intent.ACTION_USER_REMOVED.equals(action)) {
Kenny Guy2a764942014-04-02 13:29:20 +0100572 updateCurrentProfileIds();
Amith Yamasani734983f2014-03-04 16:48:05 -0800573 return;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700574 } else if (Intent.ACTION_SETTING_RESTORED.equals(action)) {
575 final String name = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
576 if (Settings.Secure.ENABLED_INPUT_METHODS.equals(name)) {
577 final String prevValue = intent.getStringExtra(
578 Intent.EXTRA_SETTING_PREVIOUS_VALUE);
579 final String newValue = intent.getStringExtra(
580 Intent.EXTRA_SETTING_NEW_VALUE);
581 restoreEnabledInputMethods(mContext, prevValue, newValue);
582 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900583 } else {
584 Slog.w(TAG, "Unexpected intent " + intent);
585 }
586 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800588
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700589 // Apply the results of a restore operation to the set of enabled IMEs. Note that this
590 // does not attempt to validate on the fly with any installed device policy, so must only
591 // be run in the context of initial device setup.
592 //
593 // TODO: Move this method to InputMethodUtils with adding unit tests.
594 static void restoreEnabledInputMethods(Context context, String prevValue, String newValue) {
595 if (DEBUG_RESTORE) {
596 Slog.i(TAG, "Restoring enabled input methods:");
597 Slog.i(TAG, "prev=" + prevValue);
598 Slog.i(TAG, " new=" + newValue);
599 }
600 // 'new' is the just-restored state, 'prev' is what was in settings prior to the restore
Seigo Nonaka2028dda2015-07-06 17:41:24 +0900601 ArrayMap<String, ArraySet<String>> prevMap =
602 InputMethodUtils.parseInputMethodsAndSubtypesString(prevValue);
603 ArrayMap<String, ArraySet<String>> newMap =
604 InputMethodUtils.parseInputMethodsAndSubtypesString(newValue);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700605
606 // Merge the restored ime+subtype enabled states into the live state
607 for (ArrayMap.Entry<String, ArraySet<String>> entry : newMap.entrySet()) {
608 final String imeId = entry.getKey();
609 ArraySet<String> prevSubtypes = prevMap.get(imeId);
610 if (prevSubtypes == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700611 prevSubtypes = new ArraySet<>(2);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700612 prevMap.put(imeId, prevSubtypes);
613 }
614 prevSubtypes.addAll(entry.getValue());
615 }
616
Seigo Nonaka2a099bc2015-08-14 19:29:45 -0700617 final String mergedImesAndSubtypesString =
618 InputMethodUtils.buildInputMethodsAndSubtypesString(prevMap);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700619 if (DEBUG_RESTORE) {
620 Slog.i(TAG, "Merged IME string:");
621 Slog.i(TAG, " " + mergedImesAndSubtypesString);
622 }
623 Settings.Secure.putString(context.getContentResolver(),
624 Settings.Secure.ENABLED_INPUT_METHODS, mergedImesAndSubtypesString);
625 }
626
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800627 class MyPackageMonitor extends PackageMonitor {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900628 private boolean isChangingPackagesOfCurrentUser() {
629 final int userId = getChangingUserId();
630 final boolean retval = userId == mSettings.getCurrentUserId();
631 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900632 if (!retval) {
633 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
634 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900635 }
636 return retval;
637 }
638
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800640 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900641 if (!isChangingPackagesOfCurrentUser()) {
642 return false;
643 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900645 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 final int N = mMethodList.size();
647 if (curInputMethodId != null) {
648 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800649 InputMethodInfo imi = mMethodList.get(i);
650 if (imi.getId().equals(curInputMethodId)) {
651 for (String pkg : packages) {
652 if (imi.getPackageName().equals(pkg)) {
653 if (!doit) {
654 return true;
655 }
satok723a27e2010-11-11 14:58:11 +0900656 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800657 chooseNewDefaultIMELocked();
658 return true;
659 }
660 }
661 }
662 }
663 }
664 }
665 return false;
666 }
667
668 @Override
669 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900670 if (!isChangingPackagesOfCurrentUser()) {
671 return;
672 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800673 synchronized (mMethodMap) {
674 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900675 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800676 final int N = mMethodList.size();
677 if (curInputMethodId != null) {
678 for (int i=0; i<N; i++) {
679 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900680 final String imiId = imi.getId();
681 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800682 curIm = imi;
683 }
satoke7c6998e2011-06-03 17:57:59 +0900684
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800685 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900686 if (isPackageModified(imi.getPackageName())) {
687 mFileManager.deleteAllInputMethodSubtypes(imiId);
688 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800689 if (change == PACKAGE_TEMPORARY_CHANGE
690 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800691 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800692 + imi.getComponent());
693 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800694 }
695 }
696 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800697
Yohei Yukawa94e33302016-02-12 19:37:03 -0800698 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800699
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800700 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800701
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800702 if (curIm != null) {
Anna Galusza9b278112016-01-04 11:37:37 -0800703 int change = isPackageDisappearing(curIm.getPackageName());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800704 if (change == PACKAGE_TEMPORARY_CHANGE
705 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800706 ServiceInfo si = null;
707 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900708 si = mIPackageManager.getServiceInfo(
709 curIm.getComponent(), 0, mSettings.getCurrentUserId());
710 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800711 }
712 if (si == null) {
713 // Uh oh, current input method is no longer around!
714 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800715 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900716 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800717 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800718 changed = true;
719 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800720 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900721 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800722 }
723 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800724 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800725 }
satokab751aa2010-09-14 19:17:36 +0900726
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800727 if (curIm == null) {
728 // We currently don't have a default input method... is
729 // one now available?
730 changed = chooseNewDefaultIMELocked();
Yohei Yukawa54d512c2015-05-19 22:15:02 -0700731 } else if (!changed && isPackageModified(curIm.getPackageName())) {
732 // Even if the current input method is still available, mCurrentSubtype could
733 // be obsolete when the package is modified in practice.
734 changed = true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800735 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800736
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800737 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800738 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800739 }
740 }
741 }
742 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800743
Jeff Brownc28867a2013-03-26 15:42:39 -0700744 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900745 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -0700746 private final IInputMethod mMethod;
747 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800748
Jeff Brownc28867a2013-03-26 15:42:39 -0700749 MethodCallback(InputMethodManagerService imms, IInputMethod method,
750 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900751 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -0700752 mMethod = method;
753 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800754 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800755
satoke7c6998e2011-06-03 17:57:59 +0900756 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -0700757 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -0700758 long ident = Binder.clearCallingIdentity();
759 try {
760 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
761 } finally {
762 Binder.restoreCallingIdentity(ident);
763 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800764 }
765 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800766
satok01038492012-04-09 21:08:27 +0900767 private class HardKeyboardListener
Seigo Nonaka7309b122015-08-17 18:34:13 -0700768 implements WindowManagerInternal.OnHardKeyboardStatusChangeListener {
satok01038492012-04-09 21:08:27 +0900769 @Override
Michael Wright7b5a96b2014-08-09 19:28:42 -0700770 public void onHardKeyboardStatusChange(boolean available) {
771 mHandler.sendMessage(mHandler.obtainMessage(MSG_HARD_KEYBOARD_SWITCH_CHANGED,
772 available ? 1 : 0));
satok01038492012-04-09 21:08:27 +0900773 }
774
Michael Wright7b5a96b2014-08-09 19:28:42 -0700775 public void handleHardKeyboardStatusChange(boolean available) {
satok01038492012-04-09 21:08:27 +0900776 if (DEBUG) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700777 Slog.w(TAG, "HardKeyboardStatusChanged: available=" + available);
satok01038492012-04-09 21:08:27 +0900778 }
779 synchronized(mMethodMap) {
780 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
781 && mSwitchingDialog.isShowing()) {
782 mSwitchingDialogTitleView.findViewById(
783 com.android.internal.R.id.hard_keyboard_section).setVisibility(
784 available ? View.VISIBLE : View.GONE);
785 }
786 }
787 }
788 }
789
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800790 public static final class Lifecycle extends SystemService {
791 private InputMethodManagerService mService;
792
793 public Lifecycle(Context context) {
794 super(context);
795 mService = new InputMethodManagerService(context);
796 }
797
798 @Override
799 public void onStart() {
800 LocalServices.addService(InputMethodManagerInternal.class,
801 new LocalServiceImpl(mService.mHandler));
802 publishBinderService(Context.INPUT_METHOD_SERVICE, mService);
803 }
804
805 @Override
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800806 public void onSwitchUser(@UserIdInt int userHandle) {
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700807 // Called on ActivityManager thread.
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800808 // TODO: Dispatch this to a worker thread as needed.
809 mService.onSwitchUser(userHandle);
810 }
811
812 @Override
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800813 public void onBootPhase(int phase) {
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700814 // Called on ActivityManager thread.
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800815 // TODO: Dispatch this to a worker thread as needed.
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800816 if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) {
817 StatusBarManagerService statusBarService = (StatusBarManagerService) ServiceManager
818 .getService(Context.STATUS_BAR_SERVICE);
819 mService.systemRunning(statusBarService);
820 }
821 }
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800822
823 @Override
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700824 public void onUnlockUser(final @UserIdInt int userHandle) {
825 // Called on ActivityManager thread.
826 mService.mHandler.sendMessage(mService.mHandler.obtainMessage(MSG_SYSTEM_UNLOCK_USER,
Fyodor Kupolov0f57cce2016-09-09 10:36:30 -0700827 userHandle /* arg1 */, 0 /* arg2 */));
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800828 }
829 }
830
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800831 void onUnlockUser(@UserIdInt int userId) {
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800832 synchronized(mMethodMap) {
833 final int currentUserId = mSettings.getCurrentUserId();
834 if (DEBUG) {
835 Slog.d(TAG, "onUnlockUser: userId=" + userId + " curUserId=" + currentUserId);
836 }
837 if (userId != currentUserId) {
838 return;
839 }
840 mSettings.switchCurrentUser(currentUserId, !mSystemReady);
841 // We need to rebuild IMEs.
842 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
843 updateInputMethodsFromSettingsLocked(true /* enabledChanged */);
844 }
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800845 }
846
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800847 void onSwitchUser(@UserIdInt int userId) {
848 synchronized (mMethodMap) {
849 switchUserLocked(userId);
850 }
851 }
852
Seigo Nonaka7309b122015-08-17 18:34:13 -0700853 public InputMethodManagerService(Context context) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900854 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800855 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800856 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800857 mHandler = new Handler(this);
Yohei Yukawa81482972015-06-04 00:58:59 -0700858 // Note: SettingsObserver doesn't register observers in its constructor.
859 mSettingsObserver = new SettingsObserver(mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800860 mIWindowManager = IWindowManager.Stub.asInterface(
861 ServiceManager.getService(Context.WINDOW_SERVICE));
Seigo Nonaka7309b122015-08-17 18:34:13 -0700862 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
Mita Yuned218c72012-12-06 17:18:25 -0800863 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900864 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800865 public void executeMessage(Message msg) {
866 handleMessage(msg);
867 }
Mita Yuned218c72012-12-06 17:18:25 -0800868 }, true /*asyncHandler*/);
Yohei Yukawad34e1482016-02-11 08:03:52 -0800869 mAppOpsManager = mContext.getSystemService(AppOpsManager.class);
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800870 mUserManager = mContext.getSystemService(UserManager.class);
satok01038492012-04-09 21:08:27 +0900871 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700872 mHasFeature = context.getPackageManager().hasSystemFeature(
873 PackageManager.FEATURE_INPUT_METHODS);
Jason Monk3e189872016-01-12 09:10:34 -0500874 mSlotIme = mContext.getString(com.android.internal.R.string.status_bar_ime);
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700875 mHardKeyboardBehavior = mContext.getResources().getInteger(
876 com.android.internal.R.integer.config_externalHardKeyboardBehavior);
satok7cfc0ed2011-06-20 21:29:36 +0900877
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400878 Bundle extras = new Bundle();
879 extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
880 mImeSwitcherNotification = new Notification.Builder(mContext)
881 .setSmallIcon(com.android.internal.R.drawable.ic_notification_ime_default)
882 .setWhen(0)
883 .setOngoing(true)
884 .addExtras(extras)
885 .setCategory(Notification.CATEGORY_SYSTEM)
886 .setColor(com.android.internal.R.color.system_notification_accent_color);
Daniel Sandler590d5152012-06-14 16:10:13 -0400887
satok7cfc0ed2011-06-20 21:29:36 +0900888 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900889 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900890
891 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900892
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900893 final IntentFilter broadcastFilter = new IntentFilter();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900894 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Amith Yamasani734983f2014-03-04 16:48:05 -0800895 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
896 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700897 broadcastFilter.addAction(Intent.ACTION_SETTING_RESTORED);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900898 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800899
satok7cfc0ed2011-06-20 21:29:36 +0900900 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900901 int userId = 0;
902 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900903 userId = ActivityManagerNative.getDefault().getCurrentUser().id;
904 } catch (RemoteException e) {
905 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
906 }
satok81f8b7c2012-12-04 20:42:56 +0900907 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
satok913a8922010-08-26 21:53:41 +0900908
satokd87c2592010-09-29 11:52:06 +0900909 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900910 mSettings = new InputMethodSettings(
Yohei Yukawa68645a62016-02-17 07:54:20 -0800911 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId, !mSystemReady);
Svet Ganovadc1cf42015-06-15 16:36:24 -0700912
Kenny Guy2a764942014-04-02 13:29:20 +0100913 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900914 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900915 synchronized (mMethodMap) {
916 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
917 mSettings, context);
918 }
satok0a1bcf42012-05-16 19:26:31 +0900919
920 // Just checking if defaultImiId is empty or not
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900921 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900922 if (DEBUG) {
923 Slog.d(TAG, "Initial default ime = " + defaultImiId);
924 }
satok0a1bcf42012-05-16 19:26:31 +0900925 mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
926
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900927 synchronized (mMethodMap) {
Yohei Yukawa94e33302016-02-12 19:37:03 -0800928 buildInputMethodListLocked(!mImeSelectedOnBoot /* resetDefaultEnabledIme */);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900929 }
satokd87c2592010-09-29 11:52:06 +0900930 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800931
satok0a1bcf42012-05-16 19:26:31 +0900932 if (!mImeSelectedOnBoot) {
933 Slog.w(TAG, "No IME selected. Choose the most applicable IME.");
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900934 synchronized (mMethodMap) {
935 resetDefaultImeLocked(context);
936 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800937 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800938
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900939 synchronized (mMethodMap) {
Yohei Yukawa81482972015-06-04 00:58:59 -0700940 mSettingsObserver.registerContentObserverLocked(userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900941 updateFromSettingsLocked(true);
942 }
satok5b927c432012-05-01 20:09:34 +0900943
944 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME
945 // according to the new system locale.
946 final IntentFilter filter = new IntentFilter();
947 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
948 mContext.registerReceiver(
949 new BroadcastReceiver() {
950 @Override
951 public void onReceive(Context context, Intent intent) {
952 synchronized(mMethodMap) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900953 resetStateIfCurrentLocaleChangedLocked();
satok5b927c432012-05-01 20:09:34 +0900954 }
955 }
956 }, filter);
957 }
958
satok5b927c432012-05-01 20:09:34 +0900959 private void resetDefaultImeLocked(Context context) {
960 // Do not reset the default (current) IME when it is a 3rd-party IME
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800961 if (mCurMethodId != null && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +0900962 return;
963 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800964 final List<InputMethodInfo> suitableImes = InputMethodUtils.getDefaultEnabledImes(
965 context, mSystemReady, mSettings.getEnabledInputMethodListLocked());
966 if (suitableImes.isEmpty()) {
967 Slog.i(TAG, "No default found");
968 return;
satok5b927c432012-05-01 20:09:34 +0900969 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800970 final InputMethodInfo defIm = suitableImes.get(0);
971 Slog.i(TAG, "Default found, using " + defIm.getId());
972 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
satok5b927c432012-05-01 20:09:34 +0900973 }
974
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900975 private void resetAllInternalStateLocked(final boolean updateOnlyWhenLocaleChanged,
976 final boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900977 if (!mSystemReady) {
978 // not system ready
979 return;
980 }
Yohei Yukawae985c242016-02-24 18:27:04 -0800981 final LocaleList newLocales = mRes.getConfiguration().getLocales();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900982 if (!updateOnlyWhenLocaleChanged
Yohei Yukawae985c242016-02-24 18:27:04 -0800983 || (newLocales != null && !newLocales.equals(mLastSystemLocales))) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900984 if (!updateOnlyWhenLocaleChanged) {
985 hideCurrentInputLocked(0, null);
Yohei Yukawa33e81792015-11-17 21:14:42 -0800986 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_RESET_IME);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900987 }
988 if (DEBUG) {
Yohei Yukawae985c242016-02-24 18:27:04 -0800989 Slog.i(TAG, "LocaleList has been changed to " + newLocales);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900990 }
Yohei Yukawa94e33302016-02-12 19:37:03 -0800991 buildInputMethodListLocked(resetDefaultEnabledIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900992 if (!updateOnlyWhenLocaleChanged) {
993 final String selectedImiId = mSettings.getSelectedInputMethod();
994 if (TextUtils.isEmpty(selectedImiId)) {
995 // This is the first time of the user switch and
996 // set the current ime to the proper one.
997 resetDefaultImeLocked(mContext);
998 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +0900999 } else {
1000 // If the locale is changed, needs to reset the default ime
1001 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001002 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001003 updateFromSettingsLocked(true);
Yohei Yukawae985c242016-02-24 18:27:04 -08001004 mLastSystemLocales = newLocales;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001005 if (!updateOnlyWhenLocaleChanged) {
1006 try {
1007 startInputInnerLocked();
1008 } catch (RuntimeException e) {
1009 Slog.w(TAG, "Unexpected exception", e);
1010 }
1011 }
1012 }
1013 }
1014
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001015 private void resetStateIfCurrentLocaleChangedLocked() {
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001016 resetAllInternalStateLocked(true /* updateOnlyWhenLocaleChanged */,
1017 true /* resetDefaultImeLocked */);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001018 }
1019
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001020 private void switchUserLocked(int newUserId) {
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001021 if (DEBUG) Slog.d(TAG, "Switching user stage 1/3. newUserId=" + newUserId
1022 + " currentUserId=" + mSettings.getCurrentUserId());
1023
Yohei Yukawa81482972015-06-04 00:58:59 -07001024 // ContentObserver should be registered again when the user is changed
1025 mSettingsObserver.registerContentObserverLocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001026
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001027 // If the system is not ready or the device is not yed unlocked by the user, then we use
1028 // copy-on-write settings.
1029 final boolean useCopyOnWriteSettings =
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001030 !mSystemReady || !mUserManager.isUserUnlockingOrUnlocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001031 mSettings.switchCurrentUser(newUserId, useCopyOnWriteSettings);
Kenny Guy2a764942014-04-02 13:29:20 +01001032 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001033 // InputMethodFileManager should be reset when the user is changed
1034 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001035 final String defaultImiId = mSettings.getSelectedInputMethod();
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001036
1037 if (DEBUG) Slog.d(TAG, "Switching user stage 2/3. newUserId=" + newUserId
1038 + " defaultImiId=" + defaultImiId);
1039
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +09001040 // For secondary users, the list of enabled IMEs may not have been updated since the
1041 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
1042 // not be empty even if the IME has been uninstalled by the primary user.
1043 // Even in such cases, IMMS works fine because it will find the most applicable
1044 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001045 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001046 resetAllInternalStateLocked(false /* updateOnlyWhenLocaleChanged */,
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001047 initialUserSwitch /* needsToResetDefaultIme */);
1048 if (initialUserSwitch) {
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001049 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1050 mSettings.getEnabledInputMethodListLocked(), newUserId,
1051 mContext.getBasePackageName());
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001052 }
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001053
1054 if (DEBUG) Slog.d(TAG, "Switching user stage 3/3. newUserId=" + newUserId
1055 + " selectedIme=" + mSettings.getSelectedInputMethod());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001056 }
1057
Kenny Guy2a764942014-04-02 13:29:20 +01001058 void updateCurrentProfileIds() {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07001059 mSettings.setCurrentProfileIds(
1060 mUserManager.getProfileIdsWithDisabled(mSettings.getCurrentUserId()));
Amith Yamasani734983f2014-03-04 16:48:05 -08001061 }
1062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001063 @Override
1064 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
1065 throws RemoteException {
1066 try {
1067 return super.onTransact(code, data, reply, flags);
1068 } catch (RuntimeException e) {
1069 // The input method manager only throws security exceptions, so let's
1070 // log all others.
1071 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -07001072 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073 }
1074 throw e;
1075 }
1076 }
1077
Svetoslav Ganova0027152013-06-25 14:59:53 -07001078 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001079 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001080 if (DEBUG) {
1081 Slog.d(TAG, "--- systemReady");
1082 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001083 if (!mSystemReady) {
1084 mSystemReady = true;
Yohei Yukawa68645a62016-02-17 07:54:20 -08001085 final int currentUserId = mSettings.getCurrentUserId();
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001086 mSettings.switchCurrentUser(currentUserId,
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001087 !mUserManager.isUserUnlockingOrUnlocked(currentUserId));
Yohei Yukawad34e1482016-02-11 08:03:52 -08001088 mKeyguardManager = mContext.getSystemService(KeyguardManager.class);
1089 mNotificationManager = mContext.getSystemService(NotificationManager.class);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001090 mStatusBar = statusBar;
Griff Hazen6090c262016-03-25 08:11:24 -07001091 if (mStatusBar != null) {
1092 mStatusBar.setIconVisibility(mSlotIme, false);
1093 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001094 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokb858c732011-07-22 19:54:34 +09001095 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
1096 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +09001097 if (mShowOngoingImeSwitcherForPhones) {
Seigo Nonaka7309b122015-08-17 18:34:13 -07001098 mWindowManagerInternal.setOnHardKeyboardStatusChangeListener(
satok01038492012-04-09 21:08:27 +09001099 mHardKeyboardListener);
1100 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08001101 buildInputMethodListLocked(!mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satok0a1bcf42012-05-16 19:26:31 +09001102 if (!mImeSelectedOnBoot) {
1103 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001104 resetStateIfCurrentLocaleChangedLocked();
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001105 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1106 mSettings.getEnabledInputMethodListLocked(),
1107 mSettings.getCurrentUserId(), mContext.getBasePackageName());
satok0a1bcf42012-05-16 19:26:31 +09001108 }
Yohei Yukawae985c242016-02-24 18:27:04 -08001109 mLastSystemLocales = mRes.getConfiguration().getLocales();
Dianne Hackborncc278702009-09-02 23:07:23 -07001110 try {
1111 startInputInnerLocked();
1112 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001113 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -07001114 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001115 }
1116 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001117 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001118
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001119 // ---------------------------------------------------------------------------------------
1120 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
1121 // 1) it comes from the system process
1122 // 2) the calling process' user id is identical to the current user id IMMS thinks.
1123 private boolean calledFromValidUser() {
1124 final int uid = Binder.getCallingUid();
1125 final int userId = UserHandle.getUserId(uid);
1126 if (DEBUG) {
1127 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
1128 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
1129 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +09001130 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
1131 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001132 }
Kenny Guy2a764942014-04-02 13:29:20 +01001133 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001134 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001135 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001136
1137 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
1138 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
1139 // must not manage background users' states in any functions.
1140 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
1141 // by a token.
1142 if (mContext.checkCallingOrSelfPermission(
1143 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1144 == PackageManager.PERMISSION_GRANTED) {
1145 if (DEBUG) {
1146 Slog.d(TAG, "--- Access granted because the calling process has "
1147 + "the INTERACT_ACROSS_USERS_FULL permission");
1148 }
1149 return true;
1150 }
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07001151 Slog.w(TAG, "--- IPC called from background users. Ignore. callers="
1152 + Debug.getCallers(10));
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001153 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001154 }
1155
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001156
1157 /**
1158 * Returns true iff the caller is identified to be the current input method with the token.
1159 * @param token The window token given to the input method when it was started.
1160 * @return true if and only if non-null valid token is specified.
1161 */
1162 private boolean calledWithValidToken(IBinder token) {
1163 if (token == null || mCurToken != token) {
1164 return false;
1165 }
1166 return true;
1167 }
1168
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001169 private boolean bindCurrentInputMethodService(
1170 Intent service, ServiceConnection conn, int flags) {
1171 if (service == null || conn == null) {
1172 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
1173 return false;
1174 }
Amith Yamasani27b89e62013-01-16 12:30:11 -08001175 return mContext.bindServiceAsUser(service, conn, flags,
1176 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001177 }
1178
satoke7c6998e2011-06-03 17:57:59 +09001179 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001181 // TODO: Make this work even for non-current users?
1182 if (!calledFromValidUser()) {
1183 return Collections.emptyList();
1184 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001186 return new ArrayList<>(mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001187 }
1188 }
1189
satoke7c6998e2011-06-03 17:57:59 +09001190 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001192 // TODO: Make this work even for non-current users?
1193 if (!calledFromValidUser()) {
1194 return Collections.emptyList();
1195 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001196 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001197 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 }
1199 }
1200
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001201 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001202 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001203 * @return enabled subtypes of the specified imi
1204 */
satoke7c6998e2011-06-03 17:57:59 +09001205 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001206 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001207 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001208 // TODO: Make this work even for non-current users?
1209 if (!calledFromValidUser()) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001210 return Collections.emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001211 }
satok67ddf9c2010-11-17 09:45:54 +09001212 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001213 final InputMethodInfo imi;
1214 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001215 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001216 } else {
1217 imi = mMethodMap.get(imiId);
1218 }
1219 if (imi == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001220 return Collections.emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001221 }
1222 return mSettings.getEnabledInputMethodSubtypeListLocked(
1223 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001224 }
1225 }
1226
satoke7c6998e2011-06-03 17:57:59 +09001227 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 public void addClient(IInputMethodClient client,
1229 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001230 if (!calledFromValidUser()) {
1231 return;
1232 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001233 synchronized (mMethodMap) {
1234 mClients.put(client.asBinder(), new ClientState(client,
1235 inputContext, uid, pid));
1236 }
1237 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001238
satoke7c6998e2011-06-03 17:57:59 +09001239 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001240 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001241 if (!calledFromValidUser()) {
1242 return;
1243 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001244 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001245 ClientState cs = mClients.remove(client.asBinder());
1246 if (cs != null) {
1247 clearClientSessionLocked(cs);
Yohei Yukawa072b1b52015-11-18 15:54:34 -08001248 if (mCurClient == cs) {
1249 mCurClient = null;
1250 }
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08001251 if (mCurFocusedWindowClient == cs) {
1252 mCurFocusedWindowClient = null;
1253 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001254 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255 }
1256 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001258 void executeOrSendMessage(IInterface target, Message msg) {
1259 if (target.asBinder() instanceof Binder) {
1260 mCaller.sendMessage(msg);
1261 } else {
1262 handleMessage(msg);
1263 msg.recycle();
1264 }
1265 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001266
Yohei Yukawa33e81792015-11-17 21:14:42 -08001267 void unbindCurrentClientLocked(
1268 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001269 if (mCurClient != null) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001270 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001271 + mCurClient.client.asBinder());
1272 if (mBoundToMethod) {
1273 mBoundToMethod = false;
1274 if (mCurMethod != null) {
1275 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1276 MSG_UNBIND_INPUT, mCurMethod));
1277 }
1278 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001279
1280 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1281 MSG_SET_ACTIVE, 0, mCurClient));
Yohei Yukawa33e81792015-11-17 21:14:42 -08001282 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1283 MSG_UNBIND_CLIENT, mCurSeq, unbindClientReason, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001285 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001286
The Android Open Source Project10592532009-03-18 17:39:46 -07001287 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288 }
1289 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001291 private int getImeShowFlags() {
1292 int flags = 0;
1293 if (mShowForced) {
1294 flags |= InputMethod.SHOW_FORCED
1295 | InputMethod.SHOW_EXPLICIT;
1296 } else if (mShowExplicitlyRequested) {
1297 flags |= InputMethod.SHOW_EXPLICIT;
1298 }
1299 return flags;
1300 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001302 private int getAppShowFlags() {
1303 int flags = 0;
1304 if (mShowForced) {
1305 flags |= InputMethodManager.SHOW_FORCED;
1306 } else if (!mShowExplicitlyRequested) {
1307 flags |= InputMethodManager.SHOW_IMPLICIT;
1308 }
1309 return flags;
1310 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001311
Dianne Hackborn7663d802012-02-24 13:08:49 -08001312 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001313 if (!mBoundToMethod) {
1314 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1315 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1316 mBoundToMethod = true;
1317 }
1318 final SessionState session = mCurClient.curSession;
1319 if (initial) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001320 executeOrSendMessage(session.method, mCaller.obtainMessageIOOO(
1321 MSG_START_INPUT, mCurInputContextMissingMethods, session, mCurInputContext,
1322 mCurAttribute));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001323 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001324 executeOrSendMessage(session.method, mCaller.obtainMessageIOOO(
1325 MSG_RESTART_INPUT, mCurInputContextMissingMethods, session, mCurInputContext,
1326 mCurAttribute));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001327 }
1328 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001329 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001330 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001331 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001332 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001333 (session.channel != null ? session.channel.dup() : null),
1334 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001335 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001336
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001337 InputBindResult startInputLocked(
1338 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001339 IInputMethodClient client, IInputContext inputContext,
1340 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001341 @Nullable EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001342 // If no method is currently selected, do nothing.
1343 if (mCurMethodId == null) {
1344 return mNoBinding;
1345 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001347 ClientState cs = mClients.get(client.asBinder());
1348 if (cs == null) {
1349 throw new IllegalArgumentException("unknown client "
1350 + client.asBinder());
1351 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001352
Yohei Yukawa74750f22016-03-22 12:54:22 -07001353 if (attribute == null) {
1354 Slog.w(TAG, "Ignoring startInput with null EditorInfo."
1355 + " uid=" + cs.uid + " pid=" + cs.pid);
1356 return null;
1357 }
1358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001359 try {
1360 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1361 // Check with the window manager to make sure this client actually
1362 // has a window with focus. If not, reject. This is thread safe
1363 // because if the focus changes some time before or after, the
1364 // next client receiving focus that has any interest in input will
1365 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001366 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001367 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1368 return null;
1369 }
1370 } catch (RemoteException e) {
1371 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001372
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001373 return startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
1374 controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001375 }
1376
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001377 InputBindResult startInputUncheckedLocked(@NonNull ClientState cs, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001378 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001379 @NonNull EditorInfo attribute, int controlFlags) {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001380 // If no method is currently selected, do nothing.
1381 if (mCurMethodId == null) {
1382 return mNoBinding;
1383 }
1384
Yohei Yukawad57ba672015-06-08 16:39:46 -07001385 if (!InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.uid,
1386 attribute.packageName)) {
1387 Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
1388 + " uid=" + cs.uid + " package=" + attribute.packageName);
1389 return mNoBinding;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001390 }
1391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001392 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001393 // Was the keyguard locked when switching over to the new client?
1394 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001395 // If the client is changing, we need to switch over to the new
1396 // one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001397 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_CLIENT);
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001398 if (DEBUG) Slog.v(TAG, "switching to client: client="
John Spurlocke0980502013-10-25 11:59:29 -04001399 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001400
1401 // If the screen is on, inform the new client it is active
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001402 if (mIsInteractive) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001403 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001404 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001405 }
1406 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001408 // Bump up the sequence for this client and attach it.
1409 mCurSeq++;
1410 if (mCurSeq <= 0) mCurSeq = 1;
1411 mCurClient = cs;
1412 mCurInputContext = inputContext;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001413 mCurInputContextMissingMethods = missingMethods;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001414 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001415
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001416 // Check if the input method is changing.
1417 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1418 if (cs.curSession != null) {
1419 // Fast case: if we are already connected to the input method,
1420 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001421 return attachNewInputLocked(
1422 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001423 }
1424 if (mHaveConnection) {
1425 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001426 // Return to client, and we will get back with it when
1427 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001428 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001429 return new InputBindResult(null, null, mCurId, mCurSeq,
1430 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001431 } else if (SystemClock.uptimeMillis()
1432 < (mLastBindTime+TIME_TO_RECONNECT)) {
1433 // In this case we have connected to the service, but
1434 // don't yet have its interface. If it hasn't been too
1435 // long since we did the connection, we'll return to
1436 // the client and wait to get the service interface so
1437 // we can report back. If it has been too long, we want
1438 // to fall through so we can try a disconnect/reconnect
1439 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001440 return new InputBindResult(null, null, mCurId, mCurSeq,
1441 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001442 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001443 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1444 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001445 }
1446 }
1447 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001448
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001449 return startInputInnerLocked();
1450 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001451
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001452 InputBindResult startInputInnerLocked() {
1453 if (mCurMethodId == null) {
1454 return mNoBinding;
1455 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001456
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001457 if (!mSystemReady) {
1458 // If the system is not yet ready, we shouldn't be running third
1459 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001460 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1461 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001462 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001463
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001464 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1465 if (info == null) {
1466 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1467 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001468
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001469 unbindCurrentMethodLocked(true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001470
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001471 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1472 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001473 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1474 com.android.internal.R.string.input_method_binding_label);
1475 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1476 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001477 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001478 | Context.BIND_NOT_VISIBLE | Context.BIND_NOT_FOREGROUND
1479 | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001480 mLastBindTime = SystemClock.uptimeMillis();
1481 mHaveConnection = true;
1482 mCurId = info.getId();
1483 mCurToken = new Binder();
1484 try {
Craig Mautnerca0ac712013-03-14 09:43:02 -07001485 if (true || DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001486 mIWindowManager.addWindowToken(mCurToken, TYPE_INPUT_METHOD, DEFAULT_DISPLAY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001487 } catch (RemoteException e) {
1488 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001489 return new InputBindResult(null, null, mCurId, mCurSeq,
1490 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001491 } else {
1492 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001493 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001494 + mCurIntent);
1495 }
1496 return null;
1497 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001498
Yohei Yukawa05c25f82016-02-22 12:41:17 -08001499 private InputBindResult startInput(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001500 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001501 IInputMethodClient client, IInputContext inputContext,
1502 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001503 @Nullable EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001504 if (!calledFromValidUser()) {
1505 return null;
1506 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001507 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001508 if (DEBUG) {
1509 Slog.v(TAG, "startInput: reason="
1510 + InputMethodClient.getStartInputReason(startInputReason)
1511 + " client = " + client.asBinder()
1512 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001513 + " missingMethods="
1514 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001515 + " attribute=" + attribute
1516 + " controlFlags=#" + Integer.toHexString(controlFlags));
1517 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001518 final long ident = Binder.clearCallingIdentity();
1519 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001520 return startInputLocked(startInputReason, client, inputContext, missingMethods,
1521 attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001522 } finally {
1523 Binder.restoreCallingIdentity(ident);
1524 }
1525 }
1526 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001527
satoke7c6998e2011-06-03 17:57:59 +09001528 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001529 public void finishInput(IInputMethodClient client) {
1530 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001531
satoke7c6998e2011-06-03 17:57:59 +09001532 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001533 public void onServiceConnected(ComponentName name, IBinder service) {
1534 synchronized (mMethodMap) {
1535 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1536 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001537 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001538 Slog.w(TAG, "Service connected without a token!");
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001539 unbindCurrentMethodLocked(false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001540 return;
1541 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001542 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001543 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1544 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001545 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001546 clearClientSessionLocked(mCurClient);
1547 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001548 }
1549 }
1550 }
1551 }
1552
Jeff Brownc28867a2013-03-26 15:42:39 -07001553 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1554 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001555 synchronized (mMethodMap) {
1556 if (mCurMethod != null && method != null
1557 && mCurMethod.asBinder() == method.asBinder()) {
1558 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001559 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001560 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001561 method, session, channel);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001562 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001563 if (res.method != null) {
1564 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
Yohei Yukawa33e81792015-11-17 21:14:42 -08001565 MSG_BIND_CLIENT, mCurClient.client, res));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001566 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001567 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001568 }
1569 }
1570 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001571
1572 // Session abandoned. Close its associated input channel.
1573 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001574 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001575
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001576 void unbindCurrentMethodLocked(boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001577 if (mVisibleBound) {
1578 mContext.unbindService(mVisibleConnection);
1579 mVisibleBound = false;
1580 }
1581
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001582 if (mHaveConnection) {
1583 mContext.unbindService(this);
1584 mHaveConnection = false;
1585 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001586
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001587 if (mCurToken != null) {
1588 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001589 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001590 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001591 // The current IME is shown. Hence an IME switch (transition) is happening.
Seigo Nonaka7309b122015-08-17 18:34:13 -07001592 mWindowManagerInternal.saveLastInputMethodWindowForTransition();
satoke0a99412012-05-10 02:22:58 +09001593 }
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001594 mIWindowManager.removeWindowToken(mCurToken, DEFAULT_DISPLAY);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001595 } catch (RemoteException e) {
1596 }
1597 mCurToken = null;
1598 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001599
The Android Open Source Project10592532009-03-18 17:39:46 -07001600 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001601 clearCurMethodLocked();
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001602 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001603
Yohei Yukawa33e81792015-11-17 21:14:42 -08001604 void resetCurrentMethodAndClient(
1605 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001606 mCurMethodId = null;
1607 unbindCurrentMethodLocked(false);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001608 unbindCurrentClientLocked(unbindClientReason);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001609 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001610
1611 void requestClientSessionLocked(ClientState cs) {
1612 if (!cs.sessionRequested) {
1613 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1614 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1615 cs.sessionRequested = true;
1616 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1617 MSG_CREATE_SESSION, mCurMethod, channels[1],
1618 new MethodCallback(this, mCurMethod, channels[0])));
1619 }
1620 }
1621
1622 void clearClientSessionLocked(ClientState cs) {
1623 finishSessionLocked(cs.curSession);
1624 cs.curSession = null;
1625 cs.sessionRequested = false;
1626 }
1627
1628 private void finishSessionLocked(SessionState sessionState) {
1629 if (sessionState != null) {
1630 if (sessionState.session != null) {
1631 try {
1632 sessionState.session.finishSession();
1633 } catch (RemoteException e) {
1634 Slog.w(TAG, "Session failed to close due to remote exception", e);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001635 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Jeff Brownc28867a2013-03-26 15:42:39 -07001636 }
1637 sessionState.session = null;
1638 }
1639 if (sessionState.channel != null) {
1640 sessionState.channel.dispose();
1641 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001642 }
1643 }
1644 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001645
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001646 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001647 if (mCurMethod != null) {
1648 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001649 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001650 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001651
Jeff Brownc28867a2013-03-26 15:42:39 -07001652 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001653 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001654 mCurMethod = null;
1655 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001656 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001657 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001658 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001659 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001660
satoke7c6998e2011-06-03 17:57:59 +09001661 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001662 public void onServiceDisconnected(ComponentName name) {
1663 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001664 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001665 + " mCurIntent=" + mCurIntent);
1666 if (mCurMethod != null && mCurIntent != null
1667 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001668 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001669 // We consider this to be a new bind attempt, since the system
1670 // should now try to restart the service for us.
1671 mLastBindTime = SystemClock.uptimeMillis();
1672 mShowRequested = mInputShown;
1673 mInputShown = false;
1674 if (mCurClient != null) {
Yohei Yukawa33e81792015-11-17 21:14:42 -08001675 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1676 MSG_UNBIND_CLIENT, InputMethodClient.UNBIND_REASON_DISCONNECT_IME,
1677 mCurSeq, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001678 }
1679 }
1680 }
1681 }
1682
satokf9f01002011-05-19 21:31:50 +09001683 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001684 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
1685 long ident = Binder.clearCallingIdentity();
1686 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001687 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09001688 if (!calledWithValidToken(token)) {
1689 final int uid = Binder.getCallingUid();
1690 Slog.e(TAG, "Ignoring updateStatusIcon due to an invalid token. uid:" + uid
1691 + " token:" + token);
1692 return;
1693 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001694 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001695 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001696 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001697 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001698 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001699 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001700 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001701 CharSequence contentDescription = null;
1702 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001703 // Use PackageManager to load label
1704 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001705 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001706 mIPackageManager.getApplicationInfo(packageName, 0,
1707 mSettings.getCurrentUserId()));
1708 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001709 /* ignore */
1710 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001711 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001712 mStatusBar.setIcon(mSlotIme, packageName, iconId, 0,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001713 contentDescription != null
1714 ? contentDescription.toString() : null);
Jason Monk3e189872016-01-12 09:10:34 -05001715 mStatusBar.setIconVisibility(mSlotIme, true);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001716 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001717 }
1718 }
1719 } finally {
1720 Binder.restoreCallingIdentity(ident);
1721 }
1722 }
1723
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001724 private boolean shouldShowImeSwitcherLocked(int visibility) {
satok7cfc0ed2011-06-20 21:29:36 +09001725 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04001726 if (mSwitchingDialog != null) return false;
satok2c93efc2012-04-02 19:33:47 +09001727 if (isScreenLocked()) return false;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001728 if ((visibility & InputMethodService.IME_ACTIVE) == 0) return false;
Seigo Nonaka7309b122015-08-17 18:34:13 -07001729 if (mWindowManagerInternal.isHardKeyboardAvailable()) {
Yohei Yukawafa0e47e2016-04-05 09:55:56 -07001730 if (mHardKeyboardBehavior == HardKeyboardBehavior.WIRELESS_AFFORDANCE) {
1731 // When physical keyboard is attached, we show the ime switcher (or notification if
1732 // NavBar is not available) because SHOW_IME_WITH_HARD_KEYBOARD settings currently
1733 // exists in the IME switcher dialog. Might be OK to remove this condition once
1734 // SHOW_IME_WITH_HARD_KEYBOARD settings finds a good place to live.
1735 return true;
1736 }
Yohei Yukawa89398382016-03-29 11:37:04 -07001737 } else if ((visibility & InputMethodService.IME_VISIBLE) == 0) {
1738 return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001739 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001740
1741 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1742 final int N = imis.size();
1743 if (N > 2) return true;
1744 if (N < 1) return false;
1745 int nonAuxCount = 0;
1746 int auxCount = 0;
1747 InputMethodSubtype nonAuxSubtype = null;
1748 InputMethodSubtype auxSubtype = null;
1749 for(int i = 0; i < N; ++i) {
1750 final InputMethodInfo imi = imis.get(i);
1751 final List<InputMethodSubtype> subtypes =
1752 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
1753 final int subtypeCount = subtypes.size();
1754 if (subtypeCount == 0) {
1755 ++nonAuxCount;
1756 } else {
1757 for (int j = 0; j < subtypeCount; ++j) {
1758 final InputMethodSubtype subtype = subtypes.get(j);
1759 if (!subtype.isAuxiliary()) {
1760 ++nonAuxCount;
1761 nonAuxSubtype = subtype;
1762 } else {
1763 ++auxCount;
1764 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001765 }
1766 }
satok7cfc0ed2011-06-20 21:29:36 +09001767 }
1768 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001769 if (nonAuxCount > 1 || auxCount > 1) {
1770 return true;
1771 } else if (nonAuxCount == 1 && auxCount == 1) {
1772 if (nonAuxSubtype != null && auxSubtype != null
1773 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1774 || auxSubtype.overridesImplicitlyEnabledSubtype()
1775 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
1776 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1777 return false;
1778 }
1779 return true;
1780 }
1781 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001782 }
1783
John Spurlocke0980502013-10-25 11:59:29 -04001784 private boolean isKeyguardLocked() {
1785 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1786 }
1787
satokdbf29502011-08-25 15:28:23 +09001788 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001789 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001790 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001791 if (!calledWithValidToken(token)) {
1792 final int uid = Binder.getCallingUid();
1793 Slog.e(TAG, "Ignoring setImeWindowStatus due to an invalid token. uid:" + uid
1794 + " token:" + token);
1795 return;
1796 }
1797
1798 synchronized (mMethodMap) {
1799 mImeWindowVis = vis;
1800 mBackDisposition = backDisposition;
1801 updateSystemUiLocked(token, vis, backDisposition);
1802 }
1803 }
1804
1805 private void updateSystemUi(IBinder token, int vis, int backDisposition) {
1806 synchronized (mMethodMap) {
1807 updateSystemUiLocked(token, vis, backDisposition);
1808 }
1809 }
1810
1811 // Caution! This method is called in this class. Handle multi-user carefully
1812 private void updateSystemUiLocked(IBinder token, int vis, int backDisposition) {
1813 if (!calledWithValidToken(token)) {
1814 final int uid = Binder.getCallingUid();
1815 Slog.e(TAG, "Ignoring updateSystemUiLocked due to an invalid token. uid:" + uid
1816 + " token:" + token);
1817 return;
1818 }
1819
1820 // TODO: Move this clearing calling identity block to setImeWindowStatus after making sure
1821 // all updateSystemUi happens on system previlege.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001822 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001823 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001824 // apply policy for binder calls
1825 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
1826 vis = 0;
satok06487a52010-10-29 11:37:18 +09001827 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001828 // mImeWindowVis should be updated before calling shouldShowImeSwitcherLocked().
1829 final boolean needsToShowImeSwitcher = shouldShowImeSwitcherLocked(vis);
1830 if (mStatusBar != null) {
1831 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
1832 needsToShowImeSwitcher);
1833 }
1834 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1835 if (imi != null && needsToShowImeSwitcher) {
1836 // Used to load label
1837 final CharSequence title = mRes.getText(
1838 com.android.internal.R.string.select_input_method);
1839 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
1840 mContext, imi, mCurrentSubtype);
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001841 mImeSwitcherNotification.setContentTitle(title)
1842 .setContentText(summary)
1843 .setContentIntent(mImeSwitchPendingIntent);
Seigo Nonaka7309b122015-08-17 18:34:13 -07001844 try {
1845 if ((mNotificationManager != null)
1846 && !mIWindowManager.hasNavigationBar()) {
1847 if (DEBUG) {
1848 Slog.d(TAG, "--- show notification: label = " + summary);
1849 }
1850 mNotificationManager.notifyAsUser(null,
1851 com.android.internal.R.string.select_input_method,
1852 mImeSwitcherNotification.build(), UserHandle.ALL);
1853 mNotificationShown = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001854 }
Seigo Nonaka7309b122015-08-17 18:34:13 -07001855 } catch (RemoteException e) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001856 }
1857 } else {
1858 if (mNotificationShown && mNotificationManager != null) {
1859 if (DEBUG) {
1860 Slog.d(TAG, "--- hide notification");
satok7cfc0ed2011-06-20 21:29:36 +09001861 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001862 mNotificationManager.cancelAsUser(null,
1863 com.android.internal.R.string.select_input_method, UserHandle.ALL);
1864 mNotificationShown = false;
satok7cfc0ed2011-06-20 21:29:36 +09001865 }
satok06487a52010-10-29 11:37:18 +09001866 }
1867 } finally {
1868 Binder.restoreCallingIdentity(ident);
1869 }
1870 }
1871
satoke7c6998e2011-06-03 17:57:59 +09001872 @Override
satokf9f01002011-05-19 21:31:50 +09001873 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001874 if (!calledFromValidUser()) {
1875 return;
1876 }
satokf9f01002011-05-19 21:31:50 +09001877 synchronized (mMethodMap) {
1878 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1879 for (int i = 0; i < spans.length; ++i) {
1880 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001881 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001882 mSecureSuggestionSpans.put(ss, currentImi);
1883 }
1884 }
1885 }
1886 }
1887
satoke7c6998e2011-06-03 17:57:59 +09001888 @Override
satokf9f01002011-05-19 21:31:50 +09001889 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001890 if (!calledFromValidUser()) {
1891 return false;
1892 }
satokf9f01002011-05-19 21:31:50 +09001893 synchronized (mMethodMap) {
1894 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1895 // TODO: Do not send the intent if the process of the targetImi is already dead.
1896 if (targetImi != null) {
1897 final String[] suggestions = span.getSuggestions();
1898 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001899 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001900 final Intent intent = new Intent();
1901 // Ensures that only a class in the original IME package will receive the
1902 // notification.
satok42c5a162011-05-26 16:46:14 +09001903 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001904 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1905 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1906 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1907 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001908 final long ident = Binder.clearCallingIdentity();
1909 try {
1910 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1911 } finally {
1912 Binder.restoreCallingIdentity(ident);
1913 }
satokf9f01002011-05-19 21:31:50 +09001914 return true;
1915 }
1916 }
1917 return false;
1918 }
1919
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001920 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001921 updateInputMethodsFromSettingsLocked(enabledMayChange);
1922 updateKeyboardFromSettingsLocked();
1923 }
1924
1925 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001926 if (enabledMayChange) {
1927 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1928 for (int i=0; i<enabled.size(); i++) {
1929 // We allow the user to select "disabled until used" apps, so if they
1930 // are enabling one of those here we now need to make it enabled.
1931 InputMethodInfo imm = enabled.get(i);
1932 try {
1933 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1934 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1935 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09001936 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001937 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001938 if (DEBUG) {
1939 Slog.d(TAG, "Update state(" + imm.getId()
1940 + "): DISABLED_UNTIL_USED -> DEFAULT");
1941 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001942 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1943 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001944 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
1945 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001946 }
1947 } catch (RemoteException e) {
1948 }
1949 }
1950 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001951 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1952 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1953 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1954 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001955 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001956 // There is no input method selected, try to choose new applicable input method.
1957 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001958 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001959 }
1960 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001961 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001962 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001963 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001964 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001965 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_IME_FAILED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001966 }
satokf3db1af2010-11-23 13:34:33 +09001967 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001968 } else {
1969 // There is no longer an input method set, so stop any current one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001970 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_NO_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001971 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09001972 // Here is not the perfect place to reset the switching controller. Ideally
1973 // mSwitchingController and mSettings should be able to share the same state.
1974 // TODO: Make sure that mSwitchingController and mSettings are sharing the
1975 // the same enabled IMEs list.
1976 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07001977
1978 }
1979
1980 public void updateKeyboardFromSettingsLocked() {
1981 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
1982 if (mSwitchingDialog != null
1983 && mSwitchingDialogTitleView != null
1984 && mSwitchingDialog.isShowing()) {
1985 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
1986 com.android.internal.R.id.hard_keyboard_switch);
1987 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
1988 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001989 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001990
Yohei Yukawab097b822015-12-01 10:43:08 -08001991 private void notifyInputMethodSubtypeChanged(final int userId,
1992 @Nullable final InputMethodInfo inputMethodInfo,
1993 @Nullable final InputMethodSubtype subtype) {
1994 final InputManagerInternal inputManagerInternal =
1995 LocalServices.getService(InputManagerInternal.class);
1996 if (inputManagerInternal != null) {
1997 inputManagerInternal.onInputMethodSubtypeChanged(userId, inputMethodInfo, subtype);
1998 }
1999 }
2000
satokab751aa2010-09-14 19:17:36 +09002001 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002002 InputMethodInfo info = mMethodMap.get(id);
2003 if (info == null) {
satok913a8922010-08-26 21:53:41 +09002004 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002005 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002006
satokd81e9502012-05-21 12:58:45 +09002007 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002008 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09002009 final int subtypeCount = info.getSubtypeCount();
2010 if (subtypeCount <= 0) {
2011 return;
satokcd7cd292010-11-20 15:46:23 +09002012 }
satokd81e9502012-05-21 12:58:45 +09002013 final InputMethodSubtype oldSubtype = mCurrentSubtype;
2014 final InputMethodSubtype newSubtype;
2015 if (subtypeId >= 0 && subtypeId < subtypeCount) {
2016 newSubtype = info.getSubtypeAt(subtypeId);
2017 } else {
2018 // If subtype is null, try to find the most applicable one from
2019 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002020 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09002021 }
2022 if (newSubtype == null || oldSubtype == null) {
2023 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
2024 + ", new subtype = " + newSubtype);
2025 return;
2026 }
2027 if (newSubtype != oldSubtype) {
2028 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
2029 if (mCurMethod != null) {
2030 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002031 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokd81e9502012-05-21 12:58:45 +09002032 mCurMethod.changeInputMethodSubtype(newSubtype);
2033 } catch (RemoteException e) {
2034 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
Yohei Yukawab097b822015-12-01 10:43:08 -08002035 return;
satokab751aa2010-09-14 19:17:36 +09002036 }
2037 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002038 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info, newSubtype);
satokab751aa2010-09-14 19:17:36 +09002039 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002040 return;
2041 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002042
satokd81e9502012-05-21 12:58:45 +09002043 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002044 final long ident = Binder.clearCallingIdentity();
2045 try {
satokab751aa2010-09-14 19:17:36 +09002046 // Set a subtype to this input method.
2047 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09002048 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
2049 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
2050 // because mCurMethodId is stored as a history in
2051 // setSelectedInputMethodAndSubtypeLocked().
2052 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002053
2054 if (ActivityManagerNative.isSystemReady()) {
2055 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08002056 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002057 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07002058 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002059 }
Yohei Yukawa33e81792015-11-17 21:14:42 -08002060 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002061 } finally {
2062 Binder.restoreCallingIdentity(ident);
2063 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002064
2065 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info,
2066 getCurrentInputMethodSubtypeLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002067 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002068
satok42c5a162011-05-26 16:46:14 +09002069 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002070 public boolean showSoftInput(IInputMethodClient client, int flags,
2071 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002072 if (!calledFromValidUser()) {
2073 return false;
2074 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002075 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002076 long ident = Binder.clearCallingIdentity();
2077 try {
2078 synchronized (mMethodMap) {
2079 if (mCurClient == null || client == null
2080 || mCurClient.client.asBinder() != client.asBinder()) {
2081 try {
2082 // We need to check if this is the current client with
2083 // focus in the window manager, to allow this call to
2084 // be made before input is started in it.
2085 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002086 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002087 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002088 }
2089 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002090 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002091 }
2092 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002093
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002094 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002095 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002096 }
2097 } finally {
2098 Binder.restoreCallingIdentity(ident);
2099 }
2100 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002101
The Android Open Source Project4df24232009-03-05 14:34:35 -08002102 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002103 mShowRequested = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002104 if (mAccessibilityRequestingNoSoftKeyboard) {
2105 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002106 }
Anna Galusza9b278112016-01-04 11:37:37 -08002107
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002108 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
2109 mShowExplicitlyRequested = true;
2110 mShowForced = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002111 } else if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
2112 mShowExplicitlyRequested = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002113 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002114
Dianne Hackborncc278702009-09-02 23:07:23 -07002115 if (!mSystemReady) {
2116 return false;
2117 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002118
The Android Open Source Project4df24232009-03-05 14:34:35 -08002119 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002120 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002121 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002122 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
2123 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
2124 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002125 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002126 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002127 bindCurrentInputMethodService(
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07002128 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE
Dianne Hackbornd69e4c12015-04-24 09:54:54 -07002129 | Context.BIND_TREAT_LIKE_ACTIVITY
2130 | Context.BIND_FOREGROUND_SERVICE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002131 mVisibleBound = true;
2132 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002133 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002134 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09002135 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002136 // The client has asked to have the input method shown, but
2137 // we have been sitting here too long with a connection to the
2138 // service and no interface received, so let's disconnect/connect
2139 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002140 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002141 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09002142 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002143 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002144 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002145 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002146 } else {
2147 if (DEBUG) {
2148 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
2149 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
2150 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002151 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002152
The Android Open Source Project4df24232009-03-05 14:34:35 -08002153 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002154 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002155
satok42c5a162011-05-26 16:46:14 +09002156 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002157 public boolean hideSoftInput(IInputMethodClient client, int flags,
2158 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002159 if (!calledFromValidUser()) {
2160 return false;
2161 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002162 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002163 long ident = Binder.clearCallingIdentity();
2164 try {
2165 synchronized (mMethodMap) {
2166 if (mCurClient == null || client == null
2167 || mCurClient.client.asBinder() != client.asBinder()) {
2168 try {
2169 // We need to check if this is the current client with
2170 // focus in the window manager, to allow this call to
2171 // be made before input is started in it.
2172 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002173 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
2174 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002175 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002176 }
2177 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002178 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002179 }
2180 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002181
Joe Onorato8a9b2202010-02-26 18:56:32 -08002182 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002183 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002184 }
2185 } finally {
2186 Binder.restoreCallingIdentity(ident);
2187 }
2188 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002189
The Android Open Source Project4df24232009-03-05 14:34:35 -08002190 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002191 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
2192 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002193 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 -08002194 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002195 }
2196 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002197 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 -08002198 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002199 }
Seigo Nonakaec928652015-06-10 15:31:20 +09002200
2201 // There is a chance that IMM#hideSoftInput() is called in a transient state where
2202 // IMMS#InputShown is already updated to be true whereas IMMS#mImeWindowVis is still waiting
2203 // to be updated with the new value sent from IME process. Even in such a transient state
2204 // historically we have accepted an incoming call of IMM#hideSoftInput() from the
2205 // application process as a valid request, and have even promised such a behavior with CTS
2206 // since Android Eclair. That's why we need to accept IMM#hideSoftInput() even when only
2207 // IMMS#InputShown indicates that the software keyboard is shown.
2208 // TODO: Clean up, IMMS#mInputShown, IMMS#mImeWindowVis and mShowRequested.
2209 final boolean shouldHideSoftInput = (mCurMethod != null) && (mInputShown ||
2210 (mImeWindowVis & InputMethodService.IME_ACTIVE) != 0);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002211 boolean res;
Seigo Nonakaec928652015-06-10 15:31:20 +09002212 if (shouldHideSoftInput) {
2213 // The IME will report its visible state again after the following message finally
2214 // delivered to the IME process as an IPC. Hence the inconsistency between
2215 // IMMS#mInputShown and IMMS#mImeWindowVis should be resolved spontaneously in
2216 // the final state.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002217 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2218 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
2219 res = true;
2220 } else {
2221 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002222 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002223 if (mHaveConnection && mVisibleBound) {
2224 mContext.unbindService(mVisibleConnection);
2225 mVisibleBound = false;
2226 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002227 mInputShown = false;
2228 mShowRequested = false;
2229 mShowExplicitlyRequested = false;
2230 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002231 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002232 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002233
satok42c5a162011-05-26 16:46:14 +09002234 @Override
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002235 public InputBindResult startInputOrWindowGainedFocus(
2236 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2237 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa74750f22016-03-22 12:54:22 -07002238 int windowFlags, @Nullable EditorInfo attribute, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002239 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002240 if (windowToken != null) {
2241 return windowGainedFocus(startInputReason, client, windowToken, controlFlags,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002242 softInputMode, windowFlags, attribute, inputContext, missingMethods);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002243 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002244 return startInput(startInputReason, client, inputContext, missingMethods, attribute,
2245 controlFlags);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002246 }
2247 }
2248
2249 private InputBindResult windowGainedFocus(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002250 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2251 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002252 int windowFlags, EditorInfo attribute, IInputContext inputContext,
2253 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002254 // Needs to check the validity before clearing calling identity
2255 final boolean calledFromValidUser = calledFromValidUser();
Dianne Hackborn7663d802012-02-24 13:08:49 -08002256 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002257 long ident = Binder.clearCallingIdentity();
2258 try {
2259 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002260 if (DEBUG) Slog.v(TAG, "windowGainedFocus: reason="
2261 + InputMethodClient.getStartInputReason(startInputReason)
2262 + " client=" + client.asBinder()
2263 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002264 + " missingMethods="
2265 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002266 + " attribute=" + attribute
Dianne Hackborn7663d802012-02-24 13:08:49 -08002267 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002268 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08002269 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002270
Dianne Hackborn7663d802012-02-24 13:08:49 -08002271 ClientState cs = mClients.get(client.asBinder());
2272 if (cs == null) {
2273 throw new IllegalArgumentException("unknown client "
2274 + client.asBinder());
2275 }
2276
2277 try {
2278 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2279 // Check with the window manager to make sure this client actually
2280 // has a window with focus. If not, reject. This is thread safe
2281 // because if the focus changes some time before or after, the
2282 // next client receiving focus that has any interest in input will
2283 // be calling through here after that change happens.
2284 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2285 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2286 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002287 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002288 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002289 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002290
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002291 if (!calledFromValidUser) {
2292 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2293 Slog.w(TAG, "If you want to interect with IME, you need "
2294 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2295 hideCurrentInputLocked(0, null);
2296 return null;
2297 }
2298
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002299 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07002300 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09002301 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002302 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002303 return startInputUncheckedLocked(cs, inputContext, missingMethods,
2304 attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002305 }
2306 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002307 }
2308 mCurFocusedWindow = windowToken;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08002309 mCurFocusedWindowClient = cs;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002310
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002311 // Should we auto-show the IME even if the caller has not
2312 // specified what should be done with it?
2313 // We only do this automatically if the window can resize
2314 // to accommodate the IME (so what the user sees will give
2315 // them good context without input information being obscured
2316 // by the IME) or if running on a large screen where there
2317 // is more room for the target window + IME.
2318 final boolean doAutoShow =
2319 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2320 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2321 || mRes.getConfiguration().isLayoutSizeAtLeast(
2322 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002323 final boolean isTextEditor =
2324 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2325
2326 // We want to start input before showing the IME, but after closing
2327 // it. We want to do this after closing it to help the IME disappear
2328 // more quickly (not get stuck behind it initializing itself for the
2329 // new focused input, even if its window wants to hide the IME).
2330 boolean didStart = false;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002332 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2333 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002334 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002335 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2336 // There is no focus view, and this window will
2337 // be behind any soft input window, so hide the
2338 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002339 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002340 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002341 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002342 } else if (isTextEditor && doAutoShow && (softInputMode &
2343 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002344 // There is a focus view, and we are navigating forward
2345 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002346 // We only do this automatically if the window can resize
2347 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002348 // them good context without input information being obscured
2349 // by the IME) or if running on a large screen where there
2350 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002351 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002352 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002353 res = startInputUncheckedLocked(cs, inputContext,
2354 missingMethods, attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002355 didStart = true;
2356 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002357 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002358 }
2359 break;
2360 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2361 // Do nothing.
2362 break;
2363 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2364 if ((softInputMode &
2365 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002366 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002367 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002368 }
2369 break;
2370 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002371 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002372 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002373 break;
2374 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2375 if ((softInputMode &
2376 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002377 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002378 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002379 res = startInputUncheckedLocked(cs, inputContext,
2380 missingMethods, attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002381 didStart = true;
2382 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002383 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002384 }
2385 break;
2386 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002387 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002388 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002389 res = startInputUncheckedLocked(cs, inputContext, missingMethods,
2390 attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002391 didStart = true;
2392 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002393 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002394 break;
2395 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002396
2397 if (!didStart && attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002398 res = startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Dianne Hackborn7663d802012-02-24 13:08:49 -08002399 controlFlags);
2400 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002401 }
2402 } finally {
2403 Binder.restoreCallingIdentity(ident);
2404 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002405
2406 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002407 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002408
satok42c5a162011-05-26 16:46:14 +09002409 @Override
Seigo Nonaka14e13912015-05-06 21:04:13 -07002410 public void showInputMethodPickerFromClient(
2411 IInputMethodClient client, int auxiliarySubtypeMode) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002412 if (!calledFromValidUser()) {
2413 return;
2414 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002415 synchronized (mMethodMap) {
2416 if (mCurClient == null || client == null
2417 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002418 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002419 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002420 }
2421
satok440aab52010-11-25 09:43:11 +09002422 // Always call subtype picker, because subtype picker is a superset of input method
2423 // picker.
Seigo Nonaka14e13912015-05-06 21:04:13 -07002424 mHandler.sendMessage(mCaller.obtainMessageI(
2425 MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode));
satokab751aa2010-09-14 19:17:36 +09002426 }
2427 }
2428
satok42c5a162011-05-26 16:46:14 +09002429 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002430 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002431 if (!calledFromValidUser()) {
2432 return;
2433 }
satok28203512010-11-24 11:06:49 +09002434 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2435 }
2436
satok42c5a162011-05-26 16:46:14 +09002437 @Override
satok28203512010-11-24 11:06:49 +09002438 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002439 if (!calledFromValidUser()) {
2440 return;
2441 }
satok28203512010-11-24 11:06:49 +09002442 synchronized (mMethodMap) {
2443 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002444 setInputMethodWithSubtypeIdLocked(token, id,
2445 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2446 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002447 } else {
2448 setInputMethod(token, id);
2449 }
2450 }
satokab751aa2010-09-14 19:17:36 +09002451 }
2452
satok42c5a162011-05-26 16:46:14 +09002453 @Override
satokb416a712010-11-25 20:42:14 +09002454 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002455 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002456 if (!calledFromValidUser()) {
2457 return;
2458 }
satokb416a712010-11-25 20:42:14 +09002459 synchronized (mMethodMap) {
satok7fee71f2010-12-17 18:54:26 +09002460 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2461 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002462 }
2463 }
2464
satok4fc87d62011-05-20 16:13:43 +09002465 @Override
satok735cf382010-11-11 20:40:09 +09002466 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002467 if (!calledFromValidUser()) {
2468 return false;
2469 }
satok735cf382010-11-11 20:40:09 +09002470 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002471 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002472 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002473 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002474 lastImi = mMethodMap.get(lastIme.first);
2475 } else {
2476 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002477 }
satok4fc87d62011-05-20 16:13:43 +09002478 String targetLastImiId = null;
2479 int subtypeId = NOT_A_SUBTYPE_ID;
2480 if (lastIme != null && lastImi != null) {
2481 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002482 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
satok4fc87d62011-05-20 16:13:43 +09002483 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2484 : mCurrentSubtype.hashCode();
2485 // If the last IME is the same as the current IME and the last subtype is not
2486 // defined, there is no need to switch to the last IME.
2487 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2488 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002489 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002490 }
2491 }
2492
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002493 if (TextUtils.isEmpty(targetLastImiId)
2494 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002495 // This is a safety net. If the currentSubtype can't be added to the history
2496 // and the framework couldn't find the last ime, we will make the last ime be
2497 // the most applicable enabled keyboard subtype of the system imes.
2498 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2499 if (enabled != null) {
2500 final int N = enabled.size();
2501 final String locale = mCurrentSubtype == null
2502 ? mRes.getConfiguration().locale.toString()
2503 : mCurrentSubtype.getLocale();
2504 for (int i = 0; i < N; ++i) {
2505 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002506 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002507 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002508 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2509 InputMethodUtils.getSubtypes(imi),
2510 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002511 if (keyboardSubtype != null) {
2512 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002513 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002514 imi, keyboardSubtype.hashCode());
2515 if(keyboardSubtype.getLocale().equals(locale)) {
2516 break;
2517 }
2518 }
2519 }
2520 }
2521 }
2522 }
2523
2524 if (!TextUtils.isEmpty(targetLastImiId)) {
2525 if (DEBUG) {
2526 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2527 + ", from: " + mCurMethodId + ", " + subtypeId);
2528 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002529 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002530 return true;
2531 } else {
2532 return false;
2533 }
satok735cf382010-11-11 20:40:09 +09002534 }
2535 }
2536
satoke7c6998e2011-06-03 17:57:59 +09002537 @Override
satok688bd472012-02-09 20:09:17 +09002538 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002539 if (!calledFromValidUser()) {
2540 return false;
2541 }
satok688bd472012-02-09 20:09:17 +09002542 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002543 if (!calledWithValidToken(token)) {
2544 final int uid = Binder.getCallingUid();
2545 Slog.e(TAG, "Ignoring switchToNextInputMethod due to an invalid token. uid:" + uid
2546 + " token:" + token);
2547 return false;
2548 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002549 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002550 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2551 true /* forward */);
satok688bd472012-02-09 20:09:17 +09002552 if (nextSubtype == null) {
2553 return false;
2554 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002555 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2556 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002557 return true;
2558 }
2559 }
2560
2561 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002562 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2563 if (!calledFromValidUser()) {
2564 return false;
2565 }
2566 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002567 if (!calledWithValidToken(token)) {
2568 final int uid = Binder.getCallingUid();
2569 Slog.e(TAG, "Ignoring shouldOfferSwitchingToNextInputMethod due to an invalid "
2570 + "token. uid:" + uid + " token:" + token);
2571 return false;
2572 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002573 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002574 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2575 true /* forward */);
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002576 if (nextSubtype == null) {
2577 return false;
2578 }
2579 return true;
2580 }
2581 }
2582
2583 @Override
satok68f1b782011-04-11 14:26:04 +09002584 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002585 if (!calledFromValidUser()) {
2586 return null;
2587 }
satok68f1b782011-04-11 14:26:04 +09002588 synchronized (mMethodMap) {
2589 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2590 // TODO: Handle the case of the last IME with no subtypes
2591 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2592 || TextUtils.isEmpty(lastIme.second)) return null;
2593 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2594 if (lastImi == null) return null;
2595 try {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002596 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002597 final int lastSubtypeId =
2598 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002599 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2600 return null;
2601 }
2602 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002603 } catch (NumberFormatException e) {
2604 return null;
2605 }
2606 }
2607 }
2608
satoke7c6998e2011-06-03 17:57:59 +09002609 @Override
satokee5e77c2011-09-02 18:50:15 +09002610 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002611 if (!calledFromValidUser()) {
2612 return;
2613 }
satok91e88122011-07-18 11:11:42 +09002614 // By this IPC call, only a process which shares the same uid with the IME can add
2615 // additional input method subtypes to the IME.
Yohei Yukawa70f5c482016-01-04 19:42:36 -08002616 if (TextUtils.isEmpty(imiId) || subtypes == null) return;
satoke7c6998e2011-06-03 17:57:59 +09002617 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002618 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002619 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002620 final String[] packageInfos;
2621 try {
2622 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2623 } catch (RemoteException e) {
2624 Slog.e(TAG, "Failed to get package infos");
2625 return;
2626 }
satok91e88122011-07-18 11:11:42 +09002627 if (packageInfos != null) {
2628 final int packageNum = packageInfos.length;
2629 for (int i = 0; i < packageNum; ++i) {
2630 if (packageInfos[i].equals(imi.getPackageName())) {
2631 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002632 final long ident = Binder.clearCallingIdentity();
2633 try {
Yohei Yukawa94e33302016-02-12 19:37:03 -08002634 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002635 } finally {
2636 Binder.restoreCallingIdentity(ident);
2637 }
satokee5e77c2011-09-02 18:50:15 +09002638 return;
satok91e88122011-07-18 11:11:42 +09002639 }
2640 }
2641 }
satoke7c6998e2011-06-03 17:57:59 +09002642 }
satokee5e77c2011-09-02 18:50:15 +09002643 return;
satoke7c6998e2011-06-03 17:57:59 +09002644 }
2645
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002646 @Override
2647 public int getInputMethodWindowVisibleHeight() {
Seigo Nonaka7309b122015-08-17 18:34:13 -07002648 return mWindowManagerInternal.getInputMethodWindowVisibleHeight();
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002649 }
2650
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002651 @Override
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002652 public void clearLastInputMethodWindowForTransition(IBinder token) {
2653 if (!calledFromValidUser()) {
2654 return;
2655 }
2656 final long ident = Binder.clearCallingIdentity();
2657 try {
2658 synchronized (mMethodMap) {
2659 if (!calledWithValidToken(token)) {
2660 final int uid = Binder.getCallingUid();
2661 Slog.e(TAG, "Ignoring clearLastInputMethodWindowForTransition due to an "
2662 + "invalid token. uid:" + uid + " token:" + token);
2663 return;
2664 }
2665 }
2666 mWindowManagerInternal.clearLastInputMethodWindowForTransition();
2667 } finally {
2668 Binder.restoreCallingIdentity(ident);
2669 }
2670 }
2671
2672 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002673 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002674 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002675 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002676 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002677 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002678 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
2679 if (DEBUG) {
2680 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
2681 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
2682 + " actual: " + sequenceNumber);
2683 }
2684 return;
2685 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002686 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2687 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09002688 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002689 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002690 }
2691 }
2692
satok28203512010-11-24 11:06:49 +09002693 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002694 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002695 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
2696 }
2697 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002698
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002699 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
2700 if (token == null) {
2701 if (mContext.checkCallingOrSelfPermission(
2702 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2703 != PackageManager.PERMISSION_GRANTED) {
2704 throw new SecurityException(
2705 "Using null token requires permission "
2706 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002707 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002708 } else if (mCurToken != token) {
2709 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2710 + " token: " + token);
2711 return;
2712 }
2713
2714 final long ident = Binder.clearCallingIdentity();
2715 try {
2716 setInputMethodLocked(id, subtypeId);
2717 } finally {
2718 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002719 }
2720 }
2721
satok42c5a162011-05-26 16:46:14 +09002722 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002723 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002724 if (!calledFromValidUser()) {
2725 return;
2726 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002727 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002728 if (!calledWithValidToken(token)) {
2729 final int uid = Binder.getCallingUid();
2730 Slog.e(TAG, "Ignoring hideInputMethod due to an invalid token. uid:"
2731 + uid + " token:" + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002732 return;
2733 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002734 long ident = Binder.clearCallingIdentity();
2735 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002736 hideCurrentInputLocked(flags, null);
2737 } finally {
2738 Binder.restoreCallingIdentity(ident);
2739 }
2740 }
2741 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002742
satok42c5a162011-05-26 16:46:14 +09002743 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002744 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002745 if (!calledFromValidUser()) {
2746 return;
2747 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002748 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002749 if (!calledWithValidToken(token)) {
2750 final int uid = Binder.getCallingUid();
2751 Slog.e(TAG, "Ignoring showMySoftInput due to an invalid token. uid:"
2752 + uid + " token:" + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002753 return;
2754 }
2755 long ident = Binder.clearCallingIdentity();
2756 try {
2757 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002758 } finally {
2759 Binder.restoreCallingIdentity(ident);
2760 }
2761 }
2762 }
2763
2764 void setEnabledSessionInMainThread(SessionState session) {
2765 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002766 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002767 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002768 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002769 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002770 } catch (RemoteException e) {
2771 }
2772 }
2773 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002774 if (mEnabledSession != null && mEnabledSession.session != null) {
2775 try {
2776 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
2777 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
2778 } catch (RemoteException e) {
2779 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002780 }
2781 }
2782 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002783
satok42c5a162011-05-26 16:46:14 +09002784 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002785 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002786 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002787 switch (msg.what) {
satokab751aa2010-09-14 19:17:36 +09002788 case MSG_SHOW_IM_SUBTYPE_PICKER:
Seigo Nonaka14e13912015-05-06 21:04:13 -07002789 final boolean showAuxSubtypes;
2790 switch (msg.arg1) {
2791 case InputMethodManager.SHOW_IM_PICKER_MODE_AUTO:
2792 // This is undocumented so far, but IMM#showInputMethodPicker() has been
2793 // implemented so that auxiliary subtypes will be excluded when the soft
2794 // keyboard is invisible.
2795 showAuxSubtypes = mInputShown;
2796 break;
2797 case InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES:
2798 showAuxSubtypes = true;
2799 break;
2800 case InputMethodManager.SHOW_IM_PICKER_MODE_EXCLUDE_AUXILIARY_SUBTYPES:
2801 showAuxSubtypes = false;
2802 break;
2803 default:
2804 Slog.e(TAG, "Unknown subtype picker mode = " + msg.arg1);
2805 return false;
2806 }
2807 showInputMethodMenu(showAuxSubtypes);
satokab751aa2010-09-14 19:17:36 +09002808 return true;
2809
satok47a44912010-10-06 16:03:58 +09002810 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Yohei Yukawa41f34272015-12-14 15:41:52 -08002811 showInputMethodAndSubtypeEnabler((String)msg.obj);
satok217f5482010-12-15 05:19:19 +09002812 return true;
2813
2814 case MSG_SHOW_IM_CONFIG:
2815 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002816 return true;
2817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002818 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002819
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002820 case MSG_UNBIND_INPUT:
2821 try {
2822 ((IInputMethod)msg.obj).unbindInput();
2823 } catch (RemoteException e) {
2824 // There is nothing interesting about the method dying.
2825 }
2826 return true;
2827 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002828 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002829 try {
2830 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2831 } catch (RemoteException e) {
2832 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002833 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002834 return true;
2835 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002836 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002837 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002838 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002839 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002840 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002841 } catch (RemoteException e) {
2842 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002843 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002844 return true;
2845 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002846 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002847 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002848 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002849 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002850 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002851 } catch (RemoteException e) {
2852 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002853 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002854 return true;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07002855 case MSG_HIDE_CURRENT_INPUT_METHOD:
2856 synchronized (mMethodMap) {
2857 hideCurrentInputLocked(0, null);
2858 }
2859 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002860 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002861 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002862 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002863 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002864 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2865 } catch (RemoteException e) {
2866 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002867 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002868 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002869 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002870 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002871 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002872 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002873 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002874 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002875 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002876 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002877 // Dispose the channel if the input method is not local to this process
2878 // because the remote proxy will get its own copy when unparceled.
2879 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002880 channel.dispose();
2881 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002882 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002883 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002884 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002885 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002886 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002887
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002888 case MSG_START_INPUT: {
2889 int missingMethods = msg.arg1;
2890 args = (SomeArgs) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002891 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002892 SessionState session = (SessionState) args.arg1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002893 setEnabledSessionInMainThread(session);
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002894 session.method.startInput((IInputContext) args.arg2, missingMethods,
2895 (EditorInfo) args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002896 } catch (RemoteException e) {
2897 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002898 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002899 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002900 }
2901 case MSG_RESTART_INPUT: {
2902 int missingMethods = msg.arg1;
2903 args = (SomeArgs) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002904 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002905 SessionState session = (SessionState) args.arg1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002906 setEnabledSessionInMainThread(session);
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002907 session.method.restartInput((IInputContext) args.arg2, missingMethods,
2908 (EditorInfo) args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002909 } catch (RemoteException e) {
2910 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002911 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002912 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002913 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002914
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002915 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002916
Yohei Yukawa33e81792015-11-17 21:14:42 -08002917 case MSG_UNBIND_CLIENT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002918 try {
Yohei Yukawa33e81792015-11-17 21:14:42 -08002919 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1, msg.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002920 } catch (RemoteException e) {
2921 // There is nothing interesting about the last client dying.
2922 }
2923 return true;
Yohei Yukawa33e81792015-11-17 21:14:42 -08002924 case MSG_BIND_CLIENT: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002925 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002926 IInputMethodClient client = (IInputMethodClient)args.arg1;
2927 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002928 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002929 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002930 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002931 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002932 } finally {
2933 // Dispose the channel if the input method is not local to this process
2934 // because the remote proxy will get its own copy when unparceled.
2935 if (res.channel != null && Binder.isProxy(client)) {
2936 res.channel.dispose();
2937 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002938 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002939 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002940 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002941 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002942 case MSG_SET_ACTIVE:
2943 try {
2944 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2945 } catch (RemoteException e) {
2946 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2947 + ((ClientState)msg.obj).pid + " uid "
2948 + ((ClientState)msg.obj).uid);
2949 }
2950 return true;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002951 case MSG_SET_INTERACTIVE:
2952 handleSetInteractive(msg.arg1 != 0);
2953 return true;
Yohei Yukawaae61f712015-12-09 13:00:10 -08002954 case MSG_SWITCH_IME:
2955 handleSwitchInputMethod(msg.arg1 != 0);
2956 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002957 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
2958 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07002959 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002960 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07002961 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002962 } catch (RemoteException e) {
2963 Slog.w(TAG, "Got RemoteException sending "
2964 + "setUserActionNotificationSequenceNumber("
2965 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07002966 + clientState.pid + " uid "
2967 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002968 }
2969 return true;
2970 }
satok01038492012-04-09 21:08:27 +09002971
2972 // --------------------------------------------------------------
2973 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07002974 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09002975 return true;
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07002976 case MSG_SYSTEM_UNLOCK_USER:
2977 final int userId = msg.arg1;
2978 onUnlockUser(userId);
2979 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002980 }
2981 return false;
2982 }
2983
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002984 private void handleSetInteractive(final boolean interactive) {
2985 synchronized (mMethodMap) {
2986 mIsInteractive = interactive;
2987 updateSystemUiLocked(mCurToken, interactive ? mImeWindowVis : 0, mBackDisposition);
2988
2989 // Inform the current client of the change in active status
2990 if (mCurClient != null && mCurClient.client != null) {
2991 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
2992 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, mCurClient));
2993 }
2994 }
2995 }
2996
Yohei Yukawaae61f712015-12-09 13:00:10 -08002997 private void handleSwitchInputMethod(final boolean forwardDirection) {
2998 synchronized (mMethodMap) {
Yohei Yukawaae61f712015-12-09 13:00:10 -08002999 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07003000 false, mMethodMap.get(mCurMethodId), mCurrentSubtype, forwardDirection);
Yohei Yukawaae61f712015-12-09 13:00:10 -08003001 if (nextSubtype == null) {
3002 return;
3003 }
3004 setInputMethodLocked(nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003005 final InputMethodInfo newInputMethodInfo = mMethodMap.get(mCurMethodId);
3006 if (newInputMethodInfo == null) {
3007 return;
3008 }
3009 final CharSequence toastText = InputMethodUtils.getImeAndSubtypeDisplayName(mContext,
3010 newInputMethodInfo, mCurrentSubtype);
3011 if (!TextUtils.isEmpty(toastText)) {
Yohei Yukawab2f901a2016-04-12 01:19:31 -07003012 if (mSubtypeSwitchedByShortCutToast == null) {
3013 mSubtypeSwitchedByShortCutToast = Toast.makeText(mContext, toastText,
3014 Toast.LENGTH_SHORT);
3015 } else {
3016 mSubtypeSwitchedByShortCutToast.setText(toastText);
3017 }
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003018 mSubtypeSwitchedByShortCutToast.show();
3019 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003020 }
3021 }
3022
satokdc9ddae2011-10-06 12:22:36 +09003023 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003024 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
3025 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09003026 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09003027 if (DEBUG) {
3028 Slog.d(TAG, "New default IME was selected: " + imi.getId());
3029 }
satok723a27e2010-11-11 14:58:11 +09003030 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003031 return true;
3032 }
3033
3034 return false;
3035 }
3036
Yohei Yukawa94e33302016-02-12 19:37:03 -08003037 void buildInputMethodListLocked(boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003038 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003039 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07003040 + " \n ------ caller=" + Debug.getCallers(10));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003041 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08003042 mMethodList.clear();
3043 mMethodMap.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003044
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003045 // Use for queryIntentServicesAsUser
3046 final PackageManager pm = mContext.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003047
Yohei Yukawaed4952a2016-02-17 07:57:25 -08003048 // Note: We do not specify PackageManager.MATCH_ENCRYPTION_* flags here because the default
3049 // behavior of PackageManager is exactly what we want. It by default picks up appropriate
3050 // services depending on the unlock state for the specified user.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003051 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003052 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08003053 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
3054 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003055
satoke7c6998e2011-06-03 17:57:59 +09003056 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
3057 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003058 for (int i = 0; i < services.size(); ++i) {
3059 ResolveInfo ri = services.get(i);
3060 ServiceInfo si = ri.serviceInfo;
3061 ComponentName compName = new ComponentName(si.packageName, si.name);
3062 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
3063 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003064 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003065 + ": it does not require the permission "
3066 + android.Manifest.permission.BIND_INPUT_METHOD);
3067 continue;
3068 }
3069
Joe Onorato8a9b2202010-02-26 18:56:32 -08003070 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003071
3072 try {
satoke7c6998e2011-06-03 17:57:59 +09003073 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
Yohei Yukawa94e33302016-02-12 19:37:03 -08003074 mMethodList.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07003075 final String id = p.getId();
Yohei Yukawa94e33302016-02-12 19:37:03 -08003076 mMethodMap.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003077
3078 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003079 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003080 }
Tadashi G. Takaoka3c23d5b2016-09-16 11:41:07 +09003081 } catch (Exception e) {
3082 Slog.wtf(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003083 }
3084 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003085
Yohei Yukawa859df052016-02-17 07:56:46 -08003086 // TODO: The following code should find better place to live.
3087 if (!resetDefaultEnabledIme) {
3088 boolean enabledImeFound = false;
3089 final List<InputMethodInfo> enabledImes = mSettings.getEnabledInputMethodListLocked();
3090 final int N = enabledImes.size();
3091 for (int i = 0; i < N; ++i) {
3092 final InputMethodInfo imi = enabledImes.get(i);
3093 if (mMethodList.contains(imi)) {
3094 enabledImeFound = true;
3095 break;
3096 }
3097 }
3098 if (!enabledImeFound) {
3099 Slog.i(TAG, "All the enabled IMEs are gone. Reset default enabled IMEs.");
3100 resetDefaultEnabledIme = true;
3101 resetSelectedInputMethodAndSubtypeLocked("");
3102 }
3103 }
3104
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003105 if (resetDefaultEnabledIme) {
3106 final ArrayList<InputMethodInfo> defaultEnabledIme =
Yohei Yukawa94e33302016-02-12 19:37:03 -08003107 InputMethodUtils.getDefaultEnabledImes(mContext, mSystemReady, mMethodList);
Yohei Yukawa68645a62016-02-17 07:54:20 -08003108 final int N = defaultEnabledIme.size();
3109 for (int i = 0; i < N; ++i) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003110 final InputMethodInfo imi = defaultEnabledIme.get(i);
3111 if (DEBUG) {
3112 Slog.d(TAG, "--- enable ime = " + imi);
3113 }
3114 setInputMethodEnabledLocked(imi.getId(), true);
3115 }
3116 }
3117
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003118 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09003119 if (!TextUtils.isEmpty(defaultImiId)) {
Yohei Yukawa94e33302016-02-12 19:37:03 -08003120 if (!mMethodMap.containsKey(defaultImiId)) {
satok0a1bcf42012-05-16 19:26:31 +09003121 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
3122 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003123 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09003124 }
3125 } else {
3126 // Double check that the default IME is certainly enabled.
3127 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003128 }
3129 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09003130 // Here is not the perfect place to reset the switching controller. Ideally
3131 // mSwitchingController and mSettings should be able to share the same state.
3132 // TODO: Make sure that mSwitchingController and mSettings are sharing the
3133 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09003134 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003135 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003136
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003137 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003138
satok217f5482010-12-15 05:19:19 +09003139 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09003140 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09003141 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09003142 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3143 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09003144 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09003145 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09003146 }
Yohei Yukawa41f34272015-12-14 15:41:52 -08003147 final int userId;
3148 synchronized (mMethodMap) {
3149 userId = mSettings.getCurrentUserId();
3150 }
3151 mContext.startActivityAsUser(intent, null, UserHandle.of(userId));
satok217f5482010-12-15 05:19:19 +09003152 }
3153
3154 private void showConfigureInputMethods() {
3155 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
3156 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
3157 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3158 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09003159 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09003160 }
3161
satok2c93efc2012-04-02 19:33:47 +09003162 private boolean isScreenLocked() {
3163 return mKeyguardManager != null
3164 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
3165 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003166
Seigo Nonaka14e13912015-05-06 21:04:13 -07003167 private void showInputMethodMenu(boolean showAuxSubtypes) {
3168 if (DEBUG) Slog.v(TAG, "Show switching menu. showAuxSubtypes=" + showAuxSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003170 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09003171 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003172
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003173 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003174 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003175 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003176
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003177 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09003178 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09003179 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
3180 mContext);
satok7f35c8c2010-10-07 21:13:11 +09003181 if (immis == null || immis.size() == 0) {
3182 return;
3183 }
3184
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003185 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003186
satok688bd472012-02-09 20:09:17 +09003187 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003188 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
Yohei Yukawa5f8e7312015-12-10 00:58:55 -08003189 showAuxSubtypes, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09003190
satokc3690562012-01-10 20:14:43 +09003191 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003192 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09003193 if (currentSubtype != null) {
3194 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003195 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
3196 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09003197 }
3198 }
3199
Ken Wakasa761eb372011-03-04 19:06:18 +09003200 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09003201 mIms = new InputMethodInfo[N];
3202 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003203 int checkedItem = 0;
3204 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09003205 final ImeSubtypeListItem item = imList.get(i);
3206 mIms[i] = item.mImi;
3207 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003208 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09003209 int subtypeId = mSubtypeIds[i];
3210 if ((subtypeId == NOT_A_SUBTYPE_ID)
3211 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
3212 || (subtypeId == lastInputMethodSubtypeId)) {
3213 checkedItem = i;
3214 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003215 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003216 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08003217
3218 final Context settingsContext = new ContextThemeWrapper(context,
3219 com.android.internal.R.style.Theme_DeviceDefault_Settings);
3220
3221 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003222 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
3223 @Override
3224 public void onCancel(DialogInterface dialog) {
3225 hideInputMethodMenu();
3226 }
3227 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08003228
3229 final Context dialogContext = mDialogBuilder.getContext();
3230 final TypedArray a = dialogContext.obtainStyledAttributes(null,
3231 com.android.internal.R.styleable.DialogPreference,
3232 com.android.internal.R.attr.alertDialogStyle, 0);
3233 final Drawable dialogIcon = a.getDrawable(
3234 com.android.internal.R.styleable.DialogPreference_dialogIcon);
3235 a.recycle();
3236
3237 mDialogBuilder.setIcon(dialogIcon);
3238
Yohei Yukawad34e1482016-02-11 08:03:52 -08003239 final LayoutInflater inflater = dialogContext.getSystemService(LayoutInflater.class);
satok01038492012-04-09 21:08:27 +09003240 final View tv = inflater.inflate(
3241 com.android.internal.R.layout.input_method_switch_dialog_title, null);
3242 mDialogBuilder.setCustomTitle(tv);
3243
3244 // Setup layout for a toggle switch of the hardware keyboard
3245 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003246 mSwitchingDialogTitleView
3247 .findViewById(com.android.internal.R.id.hard_keyboard_section)
Seigo Nonaka7309b122015-08-17 18:34:13 -07003248 .setVisibility(mWindowManagerInternal.isHardKeyboardAvailable()
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003249 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003250 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003251 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07003252 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003253 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
3254 @Override
3255 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003256 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003257 // Ensure that the input method dialog is dismissed when changing
3258 // the hardware keyboard state.
3259 hideInputMethodMenu();
3260 }
3261 });
3262
Alan Viverette505e3ab2014-11-24 15:22:11 -08003263 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003264 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
3265 final OnClickListener choiceListener = new OnClickListener() {
3266 @Override
3267 public void onClick(final DialogInterface dialog, final int which) {
3268 synchronized (mMethodMap) {
3269 if (mIms == null || mIms.length <= which || mSubtypeIds == null
3270 || mSubtypeIds.length <= which) {
3271 return;
satok01038492012-04-09 21:08:27 +09003272 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003273 final InputMethodInfo im = mIms[which];
3274 int subtypeId = mSubtypeIds[which];
3275 adapter.mCheckedItem = which;
3276 adapter.notifyDataSetChanged();
3277 hideInputMethodMenu();
3278 if (im != null) {
3279 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
3280 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003281 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003282 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003283 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003284 }
3285 }
3286 };
3287 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003289 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003290 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003291 mSwitchingDialog.getWindow().setType(
3292 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09003293 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
3294 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003295 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003296 updateSystemUi(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003297 mSwitchingDialog.show();
3298 }
3299 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003300
Ken Wakasa05dbb652011-08-22 15:22:43 +09003301 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
3302 private final LayoutInflater mInflater;
3303 private final int mTextViewResourceId;
3304 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09003305 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09003306 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
3307 List<ImeSubtypeListItem> itemsList, int checkedItem) {
3308 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003309
Ken Wakasa05dbb652011-08-22 15:22:43 +09003310 mTextViewResourceId = textViewResourceId;
3311 mItemsList = itemsList;
3312 mCheckedItem = checkedItem;
Yohei Yukawad34e1482016-02-11 08:03:52 -08003313 mInflater = context.getSystemService(LayoutInflater.class);
Ken Wakasa05dbb652011-08-22 15:22:43 +09003314 }
3315
3316 @Override
3317 public View getView(int position, View convertView, ViewGroup parent) {
3318 final View view = convertView != null ? convertView
3319 : mInflater.inflate(mTextViewResourceId, null);
3320 if (position < 0 || position >= mItemsList.size()) return view;
3321 final ImeSubtypeListItem item = mItemsList.get(position);
3322 final CharSequence imeName = item.mImeName;
3323 final CharSequence subtypeName = item.mSubtypeName;
3324 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
3325 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
3326 if (TextUtils.isEmpty(subtypeName)) {
3327 firstTextView.setText(imeName);
3328 secondTextView.setVisibility(View.GONE);
3329 } else {
3330 firstTextView.setText(subtypeName);
3331 secondTextView.setText(imeName);
3332 secondTextView.setVisibility(View.VISIBLE);
3333 }
3334 final RadioButton radioButton =
3335 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
3336 radioButton.setChecked(position == mCheckedItem);
3337 return view;
3338 }
3339 }
3340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003341 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003342 synchronized (mMethodMap) {
3343 hideInputMethodMenuLocked();
3344 }
3345 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003346
The Android Open Source Project10592532009-03-18 17:39:46 -07003347 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003348 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003349
The Android Open Source Project10592532009-03-18 17:39:46 -07003350 if (mSwitchingDialog != null) {
3351 mSwitchingDialog.dismiss();
3352 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003353 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003354
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003355 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project10592532009-03-18 17:39:46 -07003356 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003357 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003358 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003360 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003361
satok42c5a162011-05-26 16:46:14 +09003362 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003363 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003364 // TODO: Make this work even for non-current users?
3365 if (!calledFromValidUser()) {
3366 return false;
3367 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003368 synchronized (mMethodMap) {
3369 if (mContext.checkCallingOrSelfPermission(
3370 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3371 != PackageManager.PERMISSION_GRANTED) {
3372 throw new SecurityException(
3373 "Requires permission "
3374 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3375 }
Anna Galusza9b278112016-01-04 11:37:37 -08003376
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003377 long ident = Binder.clearCallingIdentity();
3378 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003379 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003380 } finally {
3381 Binder.restoreCallingIdentity(ident);
3382 }
3383 }
3384 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003385
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003386 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3387 // Make sure this is a valid input method.
3388 InputMethodInfo imm = mMethodMap.get(id);
3389 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003390 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003391 }
3392
satokd87c2592010-09-29 11:52:06 +09003393 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3394 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003395
satokd87c2592010-09-29 11:52:06 +09003396 if (enabled) {
3397 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3398 if (pair.first.equals(id)) {
3399 // We are enabling this input method, but it is already enabled.
3400 // Nothing to do. The previous state was enabled.
3401 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003402 }
3403 }
satokd87c2592010-09-29 11:52:06 +09003404 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3405 // Previous state was disabled.
3406 return false;
3407 } else {
3408 StringBuilder builder = new StringBuilder();
3409 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3410 builder, enabledInputMethodsList, id)) {
3411 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003412 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003413 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3414 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3415 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003416 }
3417 // Previous state was enabled.
3418 return true;
3419 } else {
3420 // We are disabling the input method but it is already disabled.
3421 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003422 return false;
3423 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003424 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003425 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003426
satok723a27e2010-11-11 14:58:11 +09003427 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3428 boolean setSubtypeOnly) {
3429 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003430 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003431
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003432 mCurUserActionNotificationSequenceNumber =
3433 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3434 if (DEBUG) {
3435 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3436 + mCurUserActionNotificationSequenceNumber);
3437 }
3438
3439 if (mCurClient != null && mCurClient.client != null) {
3440 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3441 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003442 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003443 }
3444
satok723a27e2010-11-11 14:58:11 +09003445 // Set Subtype here
3446 if (imi == null || subtypeId < 0) {
3447 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003448 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003449 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003450 if (subtypeId < imi.getSubtypeCount()) {
3451 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3452 mSettings.putSelectedSubtype(subtype.hashCode());
3453 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003454 } else {
3455 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003456 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003457 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003458 }
satokab751aa2010-09-14 19:17:36 +09003459 }
satok723a27e2010-11-11 14:58:11 +09003460
Yohei Yukawa68645a62016-02-17 07:54:20 -08003461 if (!setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003462 // Set InputMethod here
3463 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3464 }
3465 }
3466
3467 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3468 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3469 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3470 // newDefaultIme is empty when there is no candidate for the selected IME.
3471 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3472 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3473 if (subtypeHashCode != null) {
3474 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003475 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003476 imi, Integer.parseInt(subtypeHashCode));
satok723a27e2010-11-11 14:58:11 +09003477 } catch (NumberFormatException e) {
3478 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3479 }
3480 }
3481 }
3482 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003483 }
3484
satok4e4569d2010-11-19 18:45:53 +09003485 // If there are no selected shortcuts, tries finding the most applicable ones.
3486 private Pair<InputMethodInfo, InputMethodSubtype>
3487 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3488 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3489 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003490 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003491 boolean foundInSystemIME = false;
3492
3493 // Search applicable subtype for each InputMethodInfo
3494 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003495 final String imiId = imi.getId();
3496 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3497 continue;
3498 }
satokcd7cd292010-11-20 15:46:23 +09003499 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003500 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003501 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003502 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003503 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003504 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003505 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003506 }
satokdf31ae62011-01-15 06:19:44 +09003507 // 2. Search by the system locale from enabledSubtypes.
3508 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003509 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003510 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003511 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003512 }
satoka86f5e42011-09-02 17:12:42 +09003513 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003514 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003515 final ArrayList<InputMethodSubtype> subtypesForSearch =
3516 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003517 ? InputMethodUtils.getSubtypes(imi)
3518 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003519 // 4. Search by the current subtype's locale from all subtypes.
3520 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003521 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003522 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003523 }
3524 // 5. Search by the system locale from all subtypes.
3525 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003526 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003527 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003528 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003529 }
satokcd7cd292010-11-20 15:46:23 +09003530 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003531 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003532 // The current input method is the most applicable IME.
3533 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003534 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003535 break;
satok7599a7f2010-12-22 13:45:23 +09003536 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003537 // The system input method is 2nd applicable IME.
3538 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003539 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003540 if ((imi.getServiceInfo().applicationInfo.flags
3541 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3542 foundInSystemIME = true;
3543 }
satok4e4569d2010-11-19 18:45:53 +09003544 }
3545 }
3546 }
3547 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003548 if (mostApplicableIMI != null) {
3549 Slog.w(TAG, "Most applicable shortcut input method was:"
3550 + mostApplicableIMI.getId());
3551 if (mostApplicableSubtype != null) {
3552 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3553 + "," + mostApplicableSubtype.getMode() + ","
3554 + mostApplicableSubtype.getLocale());
3555 }
3556 }
satok4e4569d2010-11-19 18:45:53 +09003557 }
satokcd7cd292010-11-20 15:46:23 +09003558 if (mostApplicableIMI != null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003559 return new Pair<> (mostApplicableIMI, mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003560 } else {
3561 return null;
3562 }
3563 }
3564
satokab751aa2010-09-14 19:17:36 +09003565 /**
3566 * @return Return the current subtype of this input method.
3567 */
satok42c5a162011-05-26 16:46:14 +09003568 @Override
satokab751aa2010-09-14 19:17:36 +09003569 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003570 // TODO: Make this work even for non-current users?
3571 if (!calledFromValidUser()) {
3572 return null;
3573 }
3574 synchronized (mMethodMap) {
3575 return getCurrentInputMethodSubtypeLocked();
3576 }
3577 }
3578
3579 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003580 if (mCurMethodId == null) {
3581 return null;
3582 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003583 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003584 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3585 if (imi == null || imi.getSubtypeCount() == 0) {
3586 return null;
satok4e4569d2010-11-19 18:45:53 +09003587 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003588 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003589 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3590 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003591 if (subtypeId == NOT_A_SUBTYPE_ID) {
3592 // If there are no selected subtypes, the framework will try to find
3593 // the most applicable subtype from explicitly or implicitly enabled
3594 // subtypes.
3595 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003596 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003597 // If there is only one explicitly or implicitly enabled subtype,
3598 // just returns it.
3599 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3600 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3601 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003602 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003603 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003604 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003605 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003606 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003607 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3608 true);
satok4e4569d2010-11-19 18:45:53 +09003609 }
satok3ef8b292010-11-23 06:06:29 +09003610 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003611 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003612 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003613 }
3614 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003615 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003616 }
3617
satok4e4569d2010-11-19 18:45:53 +09003618 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003619 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003620 @Override
satok4e4569d2010-11-19 18:45:53 +09003621 public List getShortcutInputMethodsAndSubtypes() {
3622 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003623 ArrayList<Object> ret = new ArrayList<>();
satokf3db1af2010-11-23 13:34:33 +09003624 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003625 // If there are no selected shortcut subtypes, the framework will try to find
3626 // the most applicable subtype from all subtypes whose mode is
3627 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003628 Pair<InputMethodInfo, InputMethodSubtype> info =
3629 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003630 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003631 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003632 ret.add(info.first);
3633 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003634 }
satok3da92232011-01-11 22:46:30 +09003635 return ret;
satokf3db1af2010-11-23 13:34:33 +09003636 }
satokf3db1af2010-11-23 13:34:33 +09003637 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3638 ret.add(imi);
3639 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3640 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003641 }
3642 }
satokf3db1af2010-11-23 13:34:33 +09003643 return ret;
satok4e4569d2010-11-19 18:45:53 +09003644 }
3645 }
3646
satok42c5a162011-05-26 16:46:14 +09003647 @Override
satokb66d2872010-11-10 01:04:04 +09003648 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003649 // TODO: Make this work even for non-current users?
3650 if (!calledFromValidUser()) {
3651 return false;
3652 }
satokb66d2872010-11-10 01:04:04 +09003653 synchronized (mMethodMap) {
3654 if (subtype != null && mCurMethodId != null) {
3655 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003656 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003657 if (subtypeId != NOT_A_SUBTYPE_ID) {
3658 setInputMethodLocked(mCurMethodId, subtypeId);
3659 return true;
3660 }
3661 }
3662 return false;
3663 }
3664 }
3665
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003666 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003667 private static class InputMethodFileManager {
3668 private static final String SYSTEM_PATH = "system";
3669 private static final String INPUT_METHOD_PATH = "inputmethod";
3670 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3671 private static final String NODE_SUBTYPES = "subtypes";
3672 private static final String NODE_SUBTYPE = "subtype";
3673 private static final String NODE_IMI = "imi";
3674 private static final String ATTR_ID = "id";
3675 private static final String ATTR_LABEL = "label";
3676 private static final String ATTR_ICON = "icon";
Yohei Yukawa66baf692016-04-11 02:29:35 -07003677 private static final String ATTR_IME_SUBTYPE_ID = "subtypeId";
satoke7c6998e2011-06-03 17:57:59 +09003678 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003679 private static final String ATTR_IME_SUBTYPE_LANGUAGE_TAG = "languageTag";
satoke7c6998e2011-06-03 17:57:59 +09003680 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3681 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3682 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003683 private static final String ATTR_IS_ASCII_CAPABLE = "isAsciiCapable";
satoke7c6998e2011-06-03 17:57:59 +09003684 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3685 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003686 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003687 new HashMap<>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003688 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003689 if (methodMap == null) {
3690 throw new NullPointerException("methodMap is null");
3691 }
3692 mMethodMap = methodMap;
Xiaohui Chen7c696362015-09-16 09:56:14 -07003693 final File systemDir = userId == UserHandle.USER_SYSTEM
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003694 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3695 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003696 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
Yohei Yukawadf5af482015-08-04 22:11:11 -07003697 if (!inputMethodDir.exists() && !inputMethodDir.mkdirs()) {
satoke7c6998e2011-06-03 17:57:59 +09003698 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3699 }
3700 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3701 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3702 if (!subtypeFile.exists()) {
3703 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003704 writeAdditionalInputMethodSubtypes(
3705 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003706 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003707 readAdditionalInputMethodSubtypes(
3708 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003709 }
3710 }
3711
3712 private void deleteAllInputMethodSubtypes(String imiId) {
3713 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003714 mAdditionalSubtypesMap.remove(imiId);
3715 writeAdditionalInputMethodSubtypes(
3716 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003717 }
3718 }
3719
3720 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003721 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003722 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003723 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003724 final int N = additionalSubtypes.length;
3725 for (int i = 0; i < N; ++i) {
3726 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003727 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003728 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003729 } else {
3730 Slog.w(TAG, "Duplicated subtype definition found: "
3731 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003732 }
3733 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003734 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3735 writeAdditionalInputMethodSubtypes(
3736 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003737 }
3738 }
3739
3740 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3741 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003742 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003743 }
3744 }
3745
3746 private static void writeAdditionalInputMethodSubtypes(
3747 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3748 HashMap<String, InputMethodInfo> methodMap) {
3749 // Safety net for the case that this function is called before methodMap is set.
3750 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3751 FileOutputStream fos = null;
3752 try {
3753 fos = subtypesFile.startWrite();
3754 final XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003755 out.setOutput(fos, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003756 out.startDocument(null, true);
3757 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3758 out.startTag(null, NODE_SUBTYPES);
3759 for (String imiId : allSubtypes.keySet()) {
3760 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3761 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3762 continue;
3763 }
3764 out.startTag(null, NODE_IMI);
3765 out.attribute(null, ATTR_ID, imiId);
3766 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3767 final int N = subtypesList.size();
3768 for (int i = 0; i < N; ++i) {
3769 final InputMethodSubtype subtype = subtypesList.get(i);
3770 out.startTag(null, NODE_SUBTYPE);
Yohei Yukawa66baf692016-04-11 02:29:35 -07003771 if (subtype.hasSubtypeId()) {
3772 out.attribute(null, ATTR_IME_SUBTYPE_ID,
3773 String.valueOf(subtype.getSubtypeId()));
3774 }
satoke7c6998e2011-06-03 17:57:59 +09003775 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3776 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3777 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003778 out.attribute(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG,
3779 subtype.getLanguageTag());
satoke7c6998e2011-06-03 17:57:59 +09003780 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3781 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3782 out.attribute(null, ATTR_IS_AUXILIARY,
3783 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003784 out.attribute(null, ATTR_IS_ASCII_CAPABLE,
3785 String.valueOf(subtype.isAsciiCapable() ? 1 : 0));
satoke7c6998e2011-06-03 17:57:59 +09003786 out.endTag(null, NODE_SUBTYPE);
3787 }
3788 out.endTag(null, NODE_IMI);
3789 }
3790 out.endTag(null, NODE_SUBTYPES);
3791 out.endDocument();
3792 subtypesFile.finishWrite(fos);
3793 } catch (java.io.IOException e) {
3794 Slog.w(TAG, "Error writing subtypes", e);
3795 if (fos != null) {
3796 subtypesFile.failWrite(fos);
3797 }
3798 }
3799 }
3800
3801 private static void readAdditionalInputMethodSubtypes(
3802 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3803 if (allSubtypes == null || subtypesFile == null) return;
3804 allSubtypes.clear();
Yohei Yukawa5894b432015-08-11 13:29:24 -07003805 try (final FileInputStream fis = subtypesFile.openRead()) {
satoke7c6998e2011-06-03 17:57:59 +09003806 final XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003807 parser.setInput(fis, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003808 int type = parser.getEventType();
3809 // Skip parsing until START_TAG
3810 while ((type = parser.next()) != XmlPullParser.START_TAG
3811 && type != XmlPullParser.END_DOCUMENT) {}
3812 String firstNodeName = parser.getName();
3813 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3814 throw new XmlPullParserException("Xml doesn't start with subtypes");
3815 }
3816 final int depth =parser.getDepth();
3817 String currentImiId = null;
3818 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3819 while (((type = parser.next()) != XmlPullParser.END_TAG
3820 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3821 if (type != XmlPullParser.START_TAG)
3822 continue;
3823 final String nodeName = parser.getName();
3824 if (NODE_IMI.equals(nodeName)) {
3825 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3826 if (TextUtils.isEmpty(currentImiId)) {
3827 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3828 continue;
3829 }
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003830 tempSubtypesArray = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003831 allSubtypes.put(currentImiId, tempSubtypesArray);
3832 } else if (NODE_SUBTYPE.equals(nodeName)) {
3833 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3834 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3835 continue;
3836 }
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003837 final int icon = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09003838 parser.getAttributeValue(null, ATTR_ICON));
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003839 final int label = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09003840 parser.getAttributeValue(null, ATTR_LABEL));
3841 final String imeSubtypeLocale =
3842 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003843 final String languageTag =
3844 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG);
satoke7c6998e2011-06-03 17:57:59 +09003845 final String imeSubtypeMode =
3846 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3847 final String imeSubtypeExtraValue =
3848 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003849 final boolean isAuxiliary = "1".equals(String.valueOf(
3850 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003851 final boolean isAsciiCapable = "1".equals(String.valueOf(
3852 parser.getAttributeValue(null, ATTR_IS_ASCII_CAPABLE)));
Yohei Yukawa66baf692016-04-11 02:29:35 -07003853 final InputMethodSubtypeBuilder builder = new InputMethodSubtypeBuilder()
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003854 .setSubtypeNameResId(label)
3855 .setSubtypeIconResId(icon)
3856 .setSubtypeLocale(imeSubtypeLocale)
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003857 .setLanguageTag(languageTag)
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003858 .setSubtypeMode(imeSubtypeMode)
3859 .setSubtypeExtraValue(imeSubtypeExtraValue)
3860 .setIsAuxiliary(isAuxiliary)
Yohei Yukawa66baf692016-04-11 02:29:35 -07003861 .setIsAsciiCapable(isAsciiCapable);
3862 final String subtypeIdString =
3863 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_ID);
3864 if (subtypeIdString != null) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003865 builder.setSubtypeId(Integer.parseInt(subtypeIdString));
Yohei Yukawa66baf692016-04-11 02:29:35 -07003866 }
3867 tempSubtypesArray.add(builder.build());
satoke7c6998e2011-06-03 17:57:59 +09003868 }
3869 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07003870 } catch (XmlPullParserException | IOException | NumberFormatException e) {
3871 Slog.w(TAG, "Error reading subtypes", e);
satoke7c6998e2011-06-03 17:57:59 +09003872 return;
satoke7c6998e2011-06-03 17:57:59 +09003873 }
3874 }
3875 }
3876
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003877 private static final class LocalServiceImpl implements InputMethodManagerInternal {
3878 @NonNull
3879 private final Handler mHandler;
3880
3881 LocalServiceImpl(@NonNull final Handler handler) {
3882 mHandler = handler;
3883 }
3884
3885 @Override
3886 public void setInteractive(boolean interactive) {
3887 // Do everything in handler so as not to block the caller.
3888 mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_INTERACTIVE,
3889 interactive ? 1 : 0, 0));
3890 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003891
3892 @Override
3893 public void switchInputMethod(boolean forwardDirection) {
3894 // Do everything in handler so as not to block the caller.
3895 mHandler.sendMessage(mHandler.obtainMessage(MSG_SWITCH_IME,
3896 forwardDirection ? 1 : 0, 0));
3897 }
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07003898
3899 @Override
3900 public void hideCurrentInputMethod() {
3901 mHandler.removeMessages(MSG_HIDE_CURRENT_INPUT_METHOD);
3902 mHandler.sendEmptyMessage(MSG_HIDE_CURRENT_INPUT_METHOD);
3903 }
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003904 }
3905
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003906 private static String imeWindowStatusToString(final int imeWindowVis) {
3907 final StringBuilder sb = new StringBuilder();
3908 boolean first = true;
3909 if ((imeWindowVis & InputMethodService.IME_ACTIVE) != 0) {
3910 sb.append("Active");
3911 first = false;
3912 }
3913 if ((imeWindowVis & InputMethodService.IME_VISIBLE) != 0) {
3914 if (!first) {
3915 sb.append("|");
3916 }
3917 sb.append("Visible");
3918 }
3919 return sb.toString();
3920 }
3921
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003922 @Override
Yohei Yukawa25e08132016-06-22 16:31:41 -07003923 public IInputContentUriToken createInputContentUriToken(@Nullable IBinder token,
3924 @Nullable Uri contentUri, @Nullable String packageName) {
3925 if (!calledFromValidUser()) {
3926 return null;
3927 }
3928
3929 if (token == null) {
3930 throw new NullPointerException("token");
3931 }
3932 if (packageName == null) {
3933 throw new NullPointerException("packageName");
3934 }
3935 if (contentUri == null) {
3936 throw new NullPointerException("contentUri");
3937 }
3938 final String contentUriScheme = contentUri.getScheme();
3939 if (!"content".equals(contentUriScheme)) {
3940 throw new InvalidParameterException("contentUri must have content scheme");
3941 }
3942
3943 synchronized (mMethodMap) {
3944 final int uid = Binder.getCallingUid();
3945 if (mCurMethodId == null) {
3946 return null;
3947 }
3948 if (mCurToken != token) {
3949 Slog.e(TAG, "Ignoring createInputContentUriToken mCurToken=" + mCurToken
3950 + " token=" + token);
3951 return null;
3952 }
3953 // We cannot simply distinguish a bad IME that reports an arbitrary package name from
3954 // an unfortunate IME whose internal state is already obsolete due to the asynchronous
3955 // nature of our system. Let's compare it with our internal record.
3956 if (!TextUtils.equals(mCurAttribute.packageName, packageName)) {
3957 Slog.e(TAG, "Ignoring createInputContentUriToken mCurAttribute.packageName="
3958 + mCurAttribute.packageName + " packageName=" + packageName);
3959 return null;
3960 }
3961 final int imeUserId = UserHandle.getUserId(uid);
3962 final int appUserId = UserHandle.getUserId(mCurClient.uid);
3963 return new InputContentUriTokenHandler(contentUri, uid, packageName, imeUserId,
3964 appUserId);
3965 }
3966 }
3967
3968 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003969 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3970 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3971 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003972
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003973 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3974 + Binder.getCallingPid()
3975 + ", uid=" + Binder.getCallingUid());
3976 return;
3977 }
3978
3979 IInputMethod method;
3980 ClientState client;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08003981 ClientState focusedWindowClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003982
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003983 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003984
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003985 synchronized (mMethodMap) {
3986 p.println("Current Input Method Manager state:");
3987 int N = mMethodList.size();
3988 p.println(" Input Methods:");
3989 for (int i=0; i<N; i++) {
3990 InputMethodInfo info = mMethodList.get(i);
3991 p.println(" InputMethod #" + i + ":");
3992 info.dump(p, " ");
3993 }
3994 p.println(" Clients:");
3995 for (ClientState ci : mClients.values()) {
3996 p.println(" Client " + ci + ":");
3997 p.println(" client=" + ci.client);
3998 p.println(" inputContext=" + ci.inputContext);
3999 p.println(" sessionRequested=" + ci.sessionRequested);
4000 p.println(" curSession=" + ci.curSession);
4001 }
The Android Open Source Project10592532009-03-18 17:39:46 -07004002 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004003 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07004004 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
4005 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004006 focusedWindowClient = mCurFocusedWindowClient;
4007 p.println(" mCurFocusedWindowClient=" + focusedWindowClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004008 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
4009 + " mBoundToMethod=" + mBoundToMethod);
4010 p.println(" mCurToken=" + mCurToken);
4011 p.println(" mCurIntent=" + mCurIntent);
4012 method = mCurMethod;
4013 p.println(" mCurMethod=" + mCurMethod);
4014 p.println(" mEnabledSession=" + mEnabledSession);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07004015 p.println(" mImeWindowVis=" + imeWindowStatusToString(mImeWindowVis));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004016 p.println(" mShowRequested=" + mShowRequested
4017 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
4018 + " mShowForced=" + mShowForced
4019 + " mInputShown=" + mInputShown);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09004020 p.println(" mCurUserActionNotificationSequenceNumber="
4021 + mCurUserActionNotificationSequenceNumber);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004022 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mIsInteractive);
Yohei Yukawa81482972015-06-04 00:58:59 -07004023 p.println(" mSettingsObserver=" + mSettingsObserver);
Yohei Yukawad7248862015-06-03 23:56:12 -07004024 p.println(" mSwitchingController:");
4025 mSwitchingController.dump(p);
Yohei Yukawa68645a62016-02-17 07:54:20 -08004026 p.println(" mSettings:");
4027 mSettings.dumpLocked(p, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004028 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004029
Jeff Brownb88102f2010-09-08 11:49:43 -07004030 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004031 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004032 pw.flush();
4033 try {
4034 client.client.asBinder().dump(fd, args);
4035 } catch (RemoteException e) {
4036 p.println("Input method client dead: " + e);
4037 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004038 } else {
4039 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004040 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004041
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004042 if (focusedWindowClient != null && client != focusedWindowClient) {
4043 p.println(" ");
4044 p.println("Warning: Current input method client doesn't match the last focused. "
4045 + "window.");
4046 p.println("Dumping input method client in the last focused window just in case.");
4047 p.println(" ");
4048 pw.flush();
4049 try {
4050 focusedWindowClient.client.asBinder().dump(fd, args);
4051 } catch (RemoteException e) {
4052 p.println("Input method client in focused window dead: " + e);
4053 }
4054 }
4055
Jeff Brownb88102f2010-09-08 11:49:43 -07004056 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004057 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004058 pw.flush();
4059 try {
4060 method.asBinder().dump(fd, args);
4061 } catch (RemoteException e) {
4062 p.println("Input method service dead: " + e);
4063 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004064 } else {
4065 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004066 }
4067 }
4068}