blob: 4b095fdffc04e1e7f391f939eac5ba2acc3c994e [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 * use this file except in compliance with the License. You may obtain a copy of
5 * the License at
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007 * http://www.apache.org/licenses/LICENSE-2.0
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 * License for the specific language governing permissions and limitations under
13 * the License.
14 */
15
16package com.android.server;
17
Yohei Yukawa0f3ad12015-04-06 16:48:24 -070018import android.annotation.NonNull;
Svet Ganovadc1cf42015-06-15 16:36:24 -070019import android.content.pm.PackageManagerInternal;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080020import com.android.internal.content.PackageMonitor;
Satoshi Kataokad7443c82013-10-15 17:45:43 +090021import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController;
Satoshi Kataokad787f692013-10-26 04:44:21 +090022import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController.ImeSubtypeListItem;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +090023import com.android.internal.inputmethod.InputMethodUtils;
24import com.android.internal.inputmethod.InputMethodUtils.InputMethodSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import com.android.internal.os.HandlerCaller;
Svetoslav Ganov758143e2012-08-06 16:40:27 -070026import com.android.internal.os.SomeArgs;
satoke7c6998e2011-06-03 17:57:59 +090027import com.android.internal.util.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import com.android.internal.view.IInputContext;
29import com.android.internal.view.IInputMethod;
Michael Wright52a53522013-03-14 10:59:38 -070030import com.android.internal.view.IInputSessionCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import com.android.internal.view.IInputMethodClient;
32import com.android.internal.view.IInputMethodManager;
33import com.android.internal.view.IInputMethodSession;
34import com.android.internal.view.InputBindResult;
Adam Lesinskief2ea1f2013-12-05 16:48:06 -080035import com.android.server.statusbar.StatusBarManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036
satoke7c6998e2011-06-03 17:57:59 +090037import org.xmlpull.v1.XmlPullParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import org.xmlpull.v1.XmlPullParserException;
satoke7c6998e2011-06-03 17:57:59 +090039import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040
41import android.app.ActivityManagerNative;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090042import android.app.AppGlobals;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.app.AlertDialog;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +090044import android.app.AppOpsManager;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090045import android.app.IUserSwitchObserver;
satokf90a33e2011-07-19 11:55:52 +090046import android.app.KeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +090047import android.app.Notification;
48import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070049import android.app.PendingIntent;
satok5b927c432012-05-01 20:09:34 +090050import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.content.ComponentName;
52import android.content.ContentResolver;
53import android.content.Context;
54import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.content.DialogInterface.OnCancelListener;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +090056import android.content.DialogInterface.OnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090058import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070060import android.content.pm.ApplicationInfo;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090061import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062import android.content.pm.PackageManager;
63import android.content.pm.ResolveInfo;
64import android.content.pm.ServiceInfo;
Amith Yamasani734983f2014-03-04 16:48:05 -080065import android.content.pm.UserInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070066import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.content.res.Resources;
68import android.content.res.TypedArray;
69import android.database.ContentObserver;
Alan Viverette505e3ab2014-11-24 15:22:11 -080070import android.graphics.drawable.Drawable;
Joe Onorato857fd9b2011-01-27 15:08:35 -080071import android.inputmethodservice.InputMethodService;
Michael Wright7b5a96b2014-08-09 19:28:42 -070072import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073import android.os.Binder;
Chris Wren1ce4b6d2015-06-11 10:19:43 -040074import android.os.Bundle;
Seigo Nonakae27dc2b2015-08-14 18:21:27 -070075import android.os.Debug;
satoke7c6998e2011-06-03 17:57:59 +090076import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import android.os.Handler;
78import android.os.IBinder;
79import android.os.IInterface;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090080import android.os.IRemoteCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081import android.os.Message;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090082import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083import android.os.Parcel;
84import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080085import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086import android.os.ServiceManager;
87import android.os.SystemClock;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090088import android.os.UserHandle;
Amith Yamasani734983f2014-03-04 16:48:05 -080089import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090import android.provider.Settings;
91import android.text.TextUtils;
Christopher Tate7b9a28c2015-03-18 13:06:16 -070092import android.text.TextUtils.SimpleStringSplitter;
satokf9f01002011-05-19 21:31:50 +090093import android.text.style.SuggestionSpan;
Christopher Tate7b9a28c2015-03-18 13:06:16 -070094import android.util.ArrayMap;
95import android.util.ArraySet;
Dianne Hackborn39606a02012-07-31 17:54:35 -070096import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +090098import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +090099import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100import android.util.PrintWriterPrinter;
101import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +0900102import android.util.Slog;
103import android.util.Xml;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +0900104import android.view.ContextThemeWrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105import android.view.IWindowManager;
Jeff Brownc28867a2013-03-26 15:42:39 -0700106import android.view.InputChannel;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900107import android.view.LayoutInflater;
108import android.view.View;
109import android.view.ViewGroup;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110import android.view.WindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700111import android.view.WindowManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900112import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113import android.view.inputmethod.InputBinding;
114import android.view.inputmethod.InputMethod;
115import android.view.inputmethod.InputMethodInfo;
116import android.view.inputmethod.InputMethodManager;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700117import android.view.inputmethod.InputMethodManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900118import android.view.inputmethod.InputMethodSubtype;
Yohei Yukawa443c2ba2014-09-10 14:10:30 +0900119import android.view.inputmethod.InputMethodSubtype.InputMethodSubtypeBuilder;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900120import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +0900121import android.widget.CompoundButton;
122import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900123import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +0900124import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900125import android.widget.TextView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126
satoke7c6998e2011-06-03 17:57:59 +0900127import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900129import java.io.FileInputStream;
130import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131import java.io.IOException;
132import java.io.PrintWriter;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100133import java.nio.charset.StandardCharsets;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900135import java.util.Collections;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136import java.util.HashMap;
137import java.util.List;
satok5b927c432012-05-01 20:09:34 +0900138import java.util.Locale;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139
140/**
141 * This class provides a system service that manages input methods.
142 */
143public class InputMethodManagerService extends IInputMethodManager.Stub
144 implements ServiceConnection, Handler.Callback {
145 static final boolean DEBUG = false;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700146 static final boolean DEBUG_RESTORE = DEBUG || false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700147 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148
Seigo Nonakad4474cb2015-05-04 16:53:24 -0700149 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 1;
150 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 2;
151 static final int MSG_SHOW_IM_CONFIG = 3;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 static final int MSG_UNBIND_INPUT = 1000;
154 static final int MSG_BIND_INPUT = 1010;
155 static final int MSG_SHOW_SOFT_INPUT = 1020;
156 static final int MSG_HIDE_SOFT_INPUT = 1030;
157 static final int MSG_ATTACH_TOKEN = 1040;
158 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 static final int MSG_START_INPUT = 2000;
161 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 static final int MSG_UNBIND_METHOD = 3000;
164 static final int MSG_BIND_METHOD = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700165 static final int MSG_SET_ACTIVE = 3020;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700166 static final int MSG_SET_INTERACTIVE = 3030;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900167 static final int MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER = 3040;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800168
satok01038492012-04-09 21:08:27 +0900169 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
170
Satoshi Kataokabcacc322013-10-21 17:57:27 -0700171 static final long TIME_TO_RECONNECT = 3 * 1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800172
satokf9f01002011-05-19 21:31:50 +0900173 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
174
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900175 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
satokb6359412011-06-28 17:47:41 +0900176 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900177
satok4e4569d2010-11-19 18:45:53 +0900178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800180 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900182 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 final IWindowManager mIWindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700185 final WindowManagerInternal mWindowManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 final HandlerCaller mCaller;
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700187 final boolean mHasFeature;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900188 private InputMethodFileManager mFileManager;
satok01038492012-04-09 21:08:27 +0900189 private final HardKeyboardListener mHardKeyboardListener;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900190 private final AppOpsManager mAppOpsManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800191
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900192 final InputBindResult mNoBinding = new InputBindResult(null, null, null, -1, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 // All known input methods. mMethodMap also serves as the global
195 // lock for this class.
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700196 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<>();
197 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<>();
satokf9f01002011-05-19 21:31:50 +0900198 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700199 new LruCache<>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Satoshi Kataokad787f692013-10-26 04:44:21 +0900200 private final InputMethodSubtypeSwitchingController mSwitchingController;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800201
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700202 // Used to bring IME service up to visible adjustment while it is being shown.
203 final ServiceConnection mVisibleConnection = new ServiceConnection() {
204 @Override public void onServiceConnected(ComponentName name, IBinder service) {
205 }
206
207 @Override public void onServiceDisconnected(ComponentName name) {
208 }
209 };
210 boolean mVisibleBound = false;
211
satok7cfc0ed2011-06-20 21:29:36 +0900212 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700213 private NotificationManager mNotificationManager;
214 private KeyguardManager mKeyguardManager;
215 private StatusBarManagerService mStatusBar;
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400216 private Notification.Builder mImeSwitcherNotification;
Dianne Hackborn661cd522011-08-22 00:26:20 -0700217 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900218 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900219 private boolean mNotificationShown;
satok0a1bcf42012-05-16 19:26:31 +0900220 private final boolean mImeSelectedOnBoot;
satok7cfc0ed2011-06-20 21:29:36 +0900221
Tadashi G. Takaoka8c6d4772014-08-05 15:29:17 +0900222 static class SessionState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 final ClientState client;
224 final IInputMethod method;
Jeff Brownc28867a2013-03-26 15:42:39 -0700225
226 IInputMethodSession session;
227 InputChannel channel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 @Override
230 public String toString() {
231 return "SessionState{uid " + client.uid + " pid " + client.pid
232 + " method " + Integer.toHexString(
233 System.identityHashCode(method))
234 + " session " + Integer.toHexString(
235 System.identityHashCode(session))
Jeff Brownc28867a2013-03-26 15:42:39 -0700236 + " channel " + channel
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 + "}";
238 }
239
240 SessionState(ClientState _client, IInputMethod _method,
Jeff Brownc28867a2013-03-26 15:42:39 -0700241 IInputMethodSession _session, InputChannel _channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 client = _client;
243 method = _method;
244 session = _session;
Jeff Brownc28867a2013-03-26 15:42:39 -0700245 channel = _channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 }
247 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800248
Jeff Brownc28867a2013-03-26 15:42:39 -0700249 static final class ClientState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 final IInputMethodClient client;
251 final IInputContext inputContext;
252 final int uid;
253 final int pid;
254 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 boolean sessionRequested;
257 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 @Override
260 public String toString() {
261 return "ClientState{" + Integer.toHexString(
262 System.identityHashCode(this)) + " uid " + uid
263 + " pid " + pid + "}";
264 }
265
266 ClientState(IInputMethodClient _client, IInputContext _inputContext,
267 int _uid, int _pid) {
268 client = _client;
269 inputContext = _inputContext;
270 uid = _uid;
271 pid = _pid;
272 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
273 }
274 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800275
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700276 final HashMap<IBinder, ClientState> mClients = new HashMap<>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700279 * Set once the system is ready to run third party code.
280 */
281 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800282
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700283 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 * Id of the currently selected input method.
285 */
286 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 /**
289 * The current binding sequence number, incremented every time there is
290 * a new bind performed.
291 */
292 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800293
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 /**
295 * The client that is currently bound to an input method.
296 */
297 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700300 * The last window token that gained focus.
301 */
302 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800303
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700304 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 * The input context last provided by the current client.
306 */
307 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309 /**
310 * The attributes last provided by the current client.
311 */
312 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800313
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 /**
315 * The input method ID of the input method service that we are currently
316 * connected to or in the process of connecting to.
317 */
318 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 /**
satokab751aa2010-09-14 19:17:36 +0900321 * The current subtype of the current input method.
322 */
323 private InputMethodSubtype mCurrentSubtype;
324
satok4e4569d2010-11-19 18:45:53 +0900325 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900326 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700327 mShortcutInputMethodsAndSubtypes = new HashMap<>();
satokab751aa2010-09-14 19:17:36 +0900328
John Spurlocke0980502013-10-25 11:59:29 -0400329 // Was the keyguard locked when this client became current?
330 private boolean mCurClientInKeyguard;
331
satokab751aa2010-09-14 19:17:36 +0900332 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 * Set to true if our ServiceConnection is currently actively bound to
334 * a service (whether or not we have gotten its IBinder back yet).
335 */
336 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 /**
339 * Set if the client has asked for the input method to be shown.
340 */
341 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 /**
344 * Set if we were explicitly told to show the input method.
345 */
346 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800347
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 /**
349 * Set if we were forced to be shown.
350 */
351 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 /**
354 * Set if we last told the input method to show itself.
355 */
356 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800357
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 /**
359 * The Intent used to connect to the current input method.
360 */
361 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 /**
364 * The token we have made for the currently active input method, to
365 * identify it in the future.
366 */
367 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800368
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369 /**
370 * If non-null, this is the input method service we are currently connected
371 * to.
372 */
373 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 /**
376 * Time that we last initiated a bind to the input method, to determine
377 * if we should try to disconnect and reconnect to it.
378 */
379 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800380
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 /**
382 * Have we called mCurMethod.bindInput()?
383 */
384 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 /**
387 * Currently enabled session. Only touched by service thread, not
388 * protected by a lock.
389 */
390 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 /**
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700393 * True if the device is currently interactive with user. The value is true initially.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 */
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700395 boolean mIsInteractive = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800396
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900397 int mCurUserActionNotificationSequenceNumber = 0;
398
Joe Onorato857fd9b2011-01-27 15:08:35 -0800399 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900400
401 /**
402 * A set of status bits regarding the active IME.
403 *
404 * <p>This value is a combination of following two bits:</p>
405 * <dl>
406 * <dt>{@link InputMethodService#IME_ACTIVE}</dt>
407 * <dd>
408 * If this bit is ON, connected IME is ready to accept touch/key events.
409 * </dd>
410 * <dt>{@link InputMethodService#IME_VISIBLE}</dt>
411 * <dd>
412 * If this bit is ON, some of IME view, e.g. software input, candidate view, is visible.
413 * </dd>
414 * </dl>
415 * <em>Do not update this value outside of setImeWindowStatus.</em>
416 */
Joe Onorato857fd9b2011-01-27 15:08:35 -0800417 int mImeWindowVis;
418
Ken Wakasa05dbb652011-08-22 15:22:43 +0900419 private AlertDialog.Builder mDialogBuilder;
420 private AlertDialog mSwitchingDialog;
satok01038492012-04-09 21:08:27 +0900421 private View mSwitchingDialogTitleView;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900422 private InputMethodInfo[] mIms;
423 private int[] mSubtypeIds;
satok5b927c432012-05-01 20:09:34 +0900424 private Locale mLastSystemLocale;
Michael Wright7b5a96b2014-08-09 19:28:42 -0700425 private boolean mShowImeWithHardKeyboard;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900426 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
427 private final IPackageManager mIPackageManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 class SettingsObserver extends ContentObserver {
Yohei Yukawa81482972015-06-04 00:58:59 -0700430 int mUserId;
431 boolean mRegistered = false;
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800432 String mLastEnabled = "";
433
Yohei Yukawa81482972015-06-04 00:58:59 -0700434 /**
435 * <em>This constructor must be called within the lock.</em>
436 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800437 SettingsObserver(Handler handler) {
438 super(handler);
Yohei Yukawa81482972015-06-04 00:58:59 -0700439 }
440
441 public void registerContentObserverLocked(int userId) {
442 if (mRegistered && mUserId == userId) {
443 return;
444 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 ContentResolver resolver = mContext.getContentResolver();
Yohei Yukawa81482972015-06-04 00:58:59 -0700446 if (mRegistered) {
447 mContext.getContentResolver().unregisterContentObserver(this);
448 mRegistered = false;
449 }
450 if (mUserId != userId) {
451 mLastEnabled = "";
452 mUserId = userId;
453 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700455 Settings.Secure.DEFAULT_INPUT_METHOD), false, this, userId);
satokab751aa2010-09-14 19:17:36 +0900456 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700457 Settings.Secure.ENABLED_INPUT_METHODS), false, this, userId);
satokb6109bb2011-02-03 22:24:54 +0900458 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700459 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this, userId);
Michael Wright7b5a96b2014-08-09 19:28:42 -0700460 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700461 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD), false, this, userId);
462 mRegistered = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800464
Michael Wright7b5a96b2014-08-09 19:28:42 -0700465 @Override public void onChange(boolean selfChange, Uri uri) {
466 final Uri showImeUri =
467 Settings.Secure.getUriFor(Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 synchronized (mMethodMap) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700469 if (showImeUri.equals(uri)) {
470 updateKeyboardFromSettingsLocked();
471 } else {
472 boolean enabledChanged = false;
473 String newEnabled = mSettings.getEnabledInputMethodsStr();
474 if (!mLastEnabled.equals(newEnabled)) {
475 mLastEnabled = newEnabled;
476 enabledChanged = true;
477 }
478 updateInputMethodsFromSettingsLocked(enabledChanged);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800479 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 }
481 }
Yohei Yukawa81482972015-06-04 00:58:59 -0700482
483 @Override
484 public String toString() {
485 return "SettingsObserver{mUserId=" + mUserId + " mRegistered=" + mRegistered
486 + " mLastEnabled=" + mLastEnabled + "}";
487 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800489
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900490 class ImmsBroadcastReceiver extends android.content.BroadcastReceiver {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900491 @Override
492 public void onReceive(Context context, Intent intent) {
493 final String action = intent.getAction();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700494 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900495 hideInputMethodMenu();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700496 // No need to update mIsInteractive
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900497 return;
Amith Yamasani734983f2014-03-04 16:48:05 -0800498 } else if (Intent.ACTION_USER_ADDED.equals(action)
499 || Intent.ACTION_USER_REMOVED.equals(action)) {
Kenny Guy2a764942014-04-02 13:29:20 +0100500 updateCurrentProfileIds();
Amith Yamasani734983f2014-03-04 16:48:05 -0800501 return;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700502 } else if (Intent.ACTION_SETTING_RESTORED.equals(action)) {
503 final String name = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
504 if (Settings.Secure.ENABLED_INPUT_METHODS.equals(name)) {
505 final String prevValue = intent.getStringExtra(
506 Intent.EXTRA_SETTING_PREVIOUS_VALUE);
507 final String newValue = intent.getStringExtra(
508 Intent.EXTRA_SETTING_NEW_VALUE);
509 restoreEnabledInputMethods(mContext, prevValue, newValue);
510 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900511 } else {
512 Slog.w(TAG, "Unexpected intent " + intent);
513 }
514 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800516
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700517 // Apply the results of a restore operation to the set of enabled IMEs. Note that this
518 // does not attempt to validate on the fly with any installed device policy, so must only
519 // be run in the context of initial device setup.
520 //
521 // TODO: Move this method to InputMethodUtils with adding unit tests.
522 static void restoreEnabledInputMethods(Context context, String prevValue, String newValue) {
523 if (DEBUG_RESTORE) {
524 Slog.i(TAG, "Restoring enabled input methods:");
525 Slog.i(TAG, "prev=" + prevValue);
526 Slog.i(TAG, " new=" + newValue);
527 }
528 // 'new' is the just-restored state, 'prev' is what was in settings prior to the restore
Seigo Nonaka2028dda2015-07-06 17:41:24 +0900529 ArrayMap<String, ArraySet<String>> prevMap =
530 InputMethodUtils.parseInputMethodsAndSubtypesString(prevValue);
531 ArrayMap<String, ArraySet<String>> newMap =
532 InputMethodUtils.parseInputMethodsAndSubtypesString(newValue);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700533
534 // Merge the restored ime+subtype enabled states into the live state
535 for (ArrayMap.Entry<String, ArraySet<String>> entry : newMap.entrySet()) {
536 final String imeId = entry.getKey();
537 ArraySet<String> prevSubtypes = prevMap.get(imeId);
538 if (prevSubtypes == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700539 prevSubtypes = new ArraySet<>(2);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700540 prevMap.put(imeId, prevSubtypes);
541 }
542 prevSubtypes.addAll(entry.getValue());
543 }
544
545 final String mergedImesAndSubtypesString = buildInputMethodsAndSubtypesString(prevMap);
546 if (DEBUG_RESTORE) {
547 Slog.i(TAG, "Merged IME string:");
548 Slog.i(TAG, " " + mergedImesAndSubtypesString);
549 }
550 Settings.Secure.putString(context.getContentResolver(),
551 Settings.Secure.ENABLED_INPUT_METHODS, mergedImesAndSubtypesString);
552 }
553
554 // TODO: Move this method to InputMethodUtils with adding unit tests.
555 static String buildInputMethodsAndSubtypesString(ArrayMap<String, ArraySet<String>> map) {
556 // we want to use the canonical InputMethodSettings implementation,
557 // so we convert data structures first.
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700558 List<Pair<String, ArrayList<String>>> imeMap = new ArrayList<>(4);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700559 for (ArrayMap.Entry<String, ArraySet<String>> entry : map.entrySet()) {
560 final String imeName = entry.getKey();
561 final ArraySet<String> subtypeSet = entry.getValue();
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700562 final ArrayList<String> subtypes = new ArrayList<>(2);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700563 if (subtypeSet != null) {
564 subtypes.addAll(subtypeSet);
565 }
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700566 imeMap.add(new Pair<>(imeName, subtypes));
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700567 }
568 return InputMethodSettings.buildInputMethodsSettingString(imeMap);
569 }
570
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800571 class MyPackageMonitor extends PackageMonitor {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900572 private boolean isChangingPackagesOfCurrentUser() {
573 final int userId = getChangingUserId();
574 final boolean retval = userId == mSettings.getCurrentUserId();
575 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900576 if (!retval) {
577 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
578 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900579 }
580 return retval;
581 }
582
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800583 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800584 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900585 if (!isChangingPackagesOfCurrentUser()) {
586 return false;
587 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800588 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900589 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800590 final int N = mMethodList.size();
591 if (curInputMethodId != null) {
592 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800593 InputMethodInfo imi = mMethodList.get(i);
594 if (imi.getId().equals(curInputMethodId)) {
595 for (String pkg : packages) {
596 if (imi.getPackageName().equals(pkg)) {
597 if (!doit) {
598 return true;
599 }
satok723a27e2010-11-11 14:58:11 +0900600 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800601 chooseNewDefaultIMELocked();
602 return true;
603 }
604 }
605 }
606 }
607 }
608 }
609 return false;
610 }
611
612 @Override
613 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900614 if (!isChangingPackagesOfCurrentUser()) {
615 return;
616 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800617 synchronized (mMethodMap) {
618 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900619 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800620 final int N = mMethodList.size();
621 if (curInputMethodId != null) {
622 for (int i=0; i<N; i++) {
623 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900624 final String imiId = imi.getId();
625 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800626 curIm = imi;
627 }
satoke7c6998e2011-06-03 17:57:59 +0900628
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800629 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900630 if (isPackageModified(imi.getPackageName())) {
631 mFileManager.deleteAllInputMethodSubtypes(imiId);
632 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800633 if (change == PACKAGE_TEMPORARY_CHANGE
634 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800635 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800636 + imi.getComponent());
637 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 }
639 }
640 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800641
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900642 buildInputMethodListLocked(
643 mMethodList, mMethodMap, false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800644
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800646
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800647 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800648 int change = isPackageDisappearing(curIm.getPackageName());
649 if (change == PACKAGE_TEMPORARY_CHANGE
650 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800651 ServiceInfo si = null;
652 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900653 si = mIPackageManager.getServiceInfo(
654 curIm.getComponent(), 0, mSettings.getCurrentUserId());
655 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800656 }
657 if (si == null) {
658 // Uh oh, current input method is no longer around!
659 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800660 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900661 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800662 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800663 changed = true;
664 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800665 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900666 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800667 }
668 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800669 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800670 }
satokab751aa2010-09-14 19:17:36 +0900671
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800672 if (curIm == null) {
673 // We currently don't have a default input method... is
674 // one now available?
675 changed = chooseNewDefaultIMELocked();
Yohei Yukawa54d512c2015-05-19 22:15:02 -0700676 } else if (!changed && isPackageModified(curIm.getPackageName())) {
677 // Even if the current input method is still available, mCurrentSubtype could
678 // be obsolete when the package is modified in practice.
679 changed = true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800680 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800681
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800682 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800683 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684 }
685 }
686 }
687 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800688
Jeff Brownc28867a2013-03-26 15:42:39 -0700689 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900690 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -0700691 private final IInputMethod mMethod;
692 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800693
Jeff Brownc28867a2013-03-26 15:42:39 -0700694 MethodCallback(InputMethodManagerService imms, IInputMethod method,
695 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900696 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -0700697 mMethod = method;
698 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800699 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800700
satoke7c6998e2011-06-03 17:57:59 +0900701 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -0700702 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -0700703 long ident = Binder.clearCallingIdentity();
704 try {
705 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
706 } finally {
707 Binder.restoreCallingIdentity(ident);
708 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709 }
710 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800711
satok01038492012-04-09 21:08:27 +0900712 private class HardKeyboardListener
Seigo Nonaka7309b122015-08-17 18:34:13 -0700713 implements WindowManagerInternal.OnHardKeyboardStatusChangeListener {
satok01038492012-04-09 21:08:27 +0900714 @Override
Michael Wright7b5a96b2014-08-09 19:28:42 -0700715 public void onHardKeyboardStatusChange(boolean available) {
716 mHandler.sendMessage(mHandler.obtainMessage(MSG_HARD_KEYBOARD_SWITCH_CHANGED,
717 available ? 1 : 0));
satok01038492012-04-09 21:08:27 +0900718 }
719
Michael Wright7b5a96b2014-08-09 19:28:42 -0700720 public void handleHardKeyboardStatusChange(boolean available) {
satok01038492012-04-09 21:08:27 +0900721 if (DEBUG) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700722 Slog.w(TAG, "HardKeyboardStatusChanged: available=" + available);
satok01038492012-04-09 21:08:27 +0900723 }
724 synchronized(mMethodMap) {
725 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
726 && mSwitchingDialog.isShowing()) {
727 mSwitchingDialogTitleView.findViewById(
728 com.android.internal.R.id.hard_keyboard_section).setVisibility(
729 available ? View.VISIBLE : View.GONE);
730 }
731 }
732 }
733 }
734
Seigo Nonaka7309b122015-08-17 18:34:13 -0700735 public InputMethodManagerService(Context context) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900736 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800737 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800738 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800739 mHandler = new Handler(this);
Yohei Yukawa81482972015-06-04 00:58:59 -0700740 // Note: SettingsObserver doesn't register observers in its constructor.
741 mSettingsObserver = new SettingsObserver(mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800742 mIWindowManager = IWindowManager.Stub.asInterface(
743 ServiceManager.getService(Context.WINDOW_SERVICE));
Seigo Nonaka7309b122015-08-17 18:34:13 -0700744 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
Mita Yuned218c72012-12-06 17:18:25 -0800745 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900746 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800747 public void executeMessage(Message msg) {
748 handleMessage(msg);
749 }
Mita Yuned218c72012-12-06 17:18:25 -0800750 }, true /*asyncHandler*/);
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900751 mAppOpsManager = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE);
satok01038492012-04-09 21:08:27 +0900752 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700753 mHasFeature = context.getPackageManager().hasSystemFeature(
754 PackageManager.FEATURE_INPUT_METHODS);
satok7cfc0ed2011-06-20 21:29:36 +0900755
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400756 Bundle extras = new Bundle();
757 extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
758 mImeSwitcherNotification = new Notification.Builder(mContext)
759 .setSmallIcon(com.android.internal.R.drawable.ic_notification_ime_default)
760 .setWhen(0)
761 .setOngoing(true)
762 .addExtras(extras)
763 .setCategory(Notification.CATEGORY_SYSTEM)
764 .setColor(com.android.internal.R.color.system_notification_accent_color);
Daniel Sandler590d5152012-06-14 16:10:13 -0400765
satok7cfc0ed2011-06-20 21:29:36 +0900766 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900767 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900768
769 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900770
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900771 final IntentFilter broadcastFilter = new IntentFilter();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900772 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Amith Yamasani734983f2014-03-04 16:48:05 -0800773 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
774 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700775 broadcastFilter.addAction(Intent.ACTION_SETTING_RESTORED);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900776 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800777
satok7cfc0ed2011-06-20 21:29:36 +0900778 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900779 int userId = 0;
780 try {
781 ActivityManagerNative.getDefault().registerUserSwitchObserver(
782 new IUserSwitchObserver.Stub() {
783 @Override
784 public void onUserSwitching(int newUserId, IRemoteCallback reply) {
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900785 synchronized(mMethodMap) {
786 switchUserLocked(newUserId);
787 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900788 if (reply != null) {
789 try {
790 reply.sendResult(null);
791 } catch (RemoteException e) {
792 }
793 }
794 }
795
796 @Override
797 public void onUserSwitchComplete(int newUserId) throws RemoteException {
798 }
Kenny Guy42979622015-04-13 18:03:05 +0000799
800 @Override
801 public void onForegroundProfileSwitch(int newProfileId) {
802 // Ignore.
803 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900804 });
805 userId = ActivityManagerNative.getDefault().getCurrentUser().id;
806 } catch (RemoteException e) {
807 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
808 }
satok81f8b7c2012-12-04 20:42:56 +0900809 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
satok913a8922010-08-26 21:53:41 +0900810
satokd87c2592010-09-29 11:52:06 +0900811 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900812 mSettings = new InputMethodSettings(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900813 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId);
Svet Ganovadc1cf42015-06-15 16:36:24 -0700814
815 // Let the package manager query which are the default imes
816 // as they get certain permissions granted by default.
817 PackageManagerInternal packageManagerInternal = LocalServices.getService(
818 PackageManagerInternal.class);
819 packageManagerInternal.setImePackagesProvider(
820 new PackageManagerInternal.PackagesProvider() {
821 @Override
822 public String[] getPackages(int userId) {
823 synchronized (mMethodMap) {
824 final int currentUserId = mSettings.getCurrentUserId();
825 // TODO: We are switching the current user id in the settings
826 // object to query it and then revert the user id. Ideally, we
827 // should call a API in settings with the user id as an argument.
828 mSettings.setCurrentUserId(userId);
829 List<InputMethodInfo> imes = mSettings
830 .getEnabledInputMethodListLocked();
831 String[] packageNames = null;
832 if (imes != null) {
833 final int imeCount = imes.size();
834 packageNames = new String[imeCount];
835 for (int i = 0; i < imeCount; i++) {
836 InputMethodInfo ime = imes.get(i);
837 packageNames[i] = ime.getPackageName();
838 }
839 }
840 mSettings.setCurrentUserId(currentUserId);
841 return packageNames;
842 }
843 }
844 });
845
Kenny Guy2a764942014-04-02 13:29:20 +0100846 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900847 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900848 synchronized (mMethodMap) {
849 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
850 mSettings, context);
851 }
satok0a1bcf42012-05-16 19:26:31 +0900852
853 // Just checking if defaultImiId is empty or not
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900854 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900855 if (DEBUG) {
856 Slog.d(TAG, "Initial default ime = " + defaultImiId);
857 }
satok0a1bcf42012-05-16 19:26:31 +0900858 mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
859
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900860 synchronized (mMethodMap) {
861 buildInputMethodListLocked(mMethodList, mMethodMap,
862 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
863 }
satokd87c2592010-09-29 11:52:06 +0900864 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800865
satok0a1bcf42012-05-16 19:26:31 +0900866 if (!mImeSelectedOnBoot) {
867 Slog.w(TAG, "No IME selected. Choose the most applicable IME.");
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900868 synchronized (mMethodMap) {
869 resetDefaultImeLocked(context);
870 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800871 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800872
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900873 synchronized (mMethodMap) {
Yohei Yukawa81482972015-06-04 00:58:59 -0700874 mSettingsObserver.registerContentObserverLocked(userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900875 updateFromSettingsLocked(true);
876 }
satok5b927c432012-05-01 20:09:34 +0900877
878 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME
879 // according to the new system locale.
880 final IntentFilter filter = new IntentFilter();
881 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
882 mContext.registerReceiver(
883 new BroadcastReceiver() {
884 @Override
885 public void onReceive(Context context, Intent intent) {
886 synchronized(mMethodMap) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900887 resetStateIfCurrentLocaleChangedLocked();
satok5b927c432012-05-01 20:09:34 +0900888 }
889 }
890 }, filter);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700891 LocalServices.addService(InputMethodManagerInternal.class, new LocalServiceImpl(mHandler));
satok5b927c432012-05-01 20:09:34 +0900892 }
893
satok5b927c432012-05-01 20:09:34 +0900894 private void resetDefaultImeLocked(Context context) {
895 // Do not reset the default (current) IME when it is a 3rd-party IME
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900896 if (mCurMethodId != null
897 && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +0900898 return;
899 }
900
901 InputMethodInfo defIm = null;
902 for (InputMethodInfo imi : mMethodList) {
Yohei Yukawa6aa03782015-02-21 03:00:22 +0900903 if (defIm == null && mSystemReady) {
904 final Locale systemLocale = context.getResources().getConfiguration().locale;
905 if (InputMethodUtils.isSystemImeThatHasSubtypeOf(imi, context,
906 true /* checkDefaultAttribute */, systemLocale, false /* checkCountry */,
907 InputMethodUtils.SUBTYPE_MODE_ANY)) {
satok5b927c432012-05-01 20:09:34 +0900908 defIm = imi;
909 Slog.i(TAG, "Selected default: " + imi.getId());
910 }
911 }
912 }
913 if (defIm == null && mMethodList.size() > 0) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900914 defIm = InputMethodUtils.getMostApplicableDefaultIME(
915 mSettings.getEnabledInputMethodListLocked());
Hyejin Kim5baaaac2014-10-27 17:17:06 +0900916 if (defIm != null) {
917 Slog.i(TAG, "Default found, using " + defIm.getId());
918 } else {
919 Slog.i(TAG, "No default found");
920 }
satok5b927c432012-05-01 20:09:34 +0900921 }
922 if (defIm != null) {
923 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
924 }
925 }
926
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900927 private void resetAllInternalStateLocked(final boolean updateOnlyWhenLocaleChanged,
928 final boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900929 if (!mSystemReady) {
930 // not system ready
931 return;
932 }
933 final Locale newLocale = mRes.getConfiguration().locale;
934 if (!updateOnlyWhenLocaleChanged
935 || (newLocale != null && !newLocale.equals(mLastSystemLocale))) {
936 if (!updateOnlyWhenLocaleChanged) {
937 hideCurrentInputLocked(0, null);
938 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -0700939 unbindCurrentMethodLocked(true, false);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900940 }
941 if (DEBUG) {
942 Slog.i(TAG, "Locale has been changed to " + newLocale);
943 }
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900944 buildInputMethodListLocked(mMethodList, mMethodMap, resetDefaultEnabledIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900945 if (!updateOnlyWhenLocaleChanged) {
946 final String selectedImiId = mSettings.getSelectedInputMethod();
947 if (TextUtils.isEmpty(selectedImiId)) {
948 // This is the first time of the user switch and
949 // set the current ime to the proper one.
950 resetDefaultImeLocked(mContext);
951 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +0900952 } else {
953 // If the locale is changed, needs to reset the default ime
954 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900955 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800956 updateFromSettingsLocked(true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900957 mLastSystemLocale = newLocale;
958 if (!updateOnlyWhenLocaleChanged) {
959 try {
960 startInputInnerLocked();
961 } catch (RuntimeException e) {
962 Slog.w(TAG, "Unexpected exception", e);
963 }
964 }
965 }
966 }
967
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900968 private void resetStateIfCurrentLocaleChangedLocked() {
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900969 resetAllInternalStateLocked(true /* updateOnlyWhenLocaleChanged */,
970 true /* resetDefaultImeLocked */);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900971 }
972
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900973 private void switchUserLocked(int newUserId) {
Yohei Yukawa90bf7082015-06-03 23:50:27 -0700974 if (DEBUG) Slog.d(TAG, "Switching user stage 1/3. newUserId=" + newUserId
975 + " currentUserId=" + mSettings.getCurrentUserId());
976
Yohei Yukawa81482972015-06-04 00:58:59 -0700977 // ContentObserver should be registered again when the user is changed
978 mSettingsObserver.registerContentObserverLocked(newUserId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900979 mSettings.setCurrentUserId(newUserId);
Kenny Guy2a764942014-04-02 13:29:20 +0100980 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900981 // InputMethodFileManager should be reset when the user is changed
982 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900983 final String defaultImiId = mSettings.getSelectedInputMethod();
Yohei Yukawa90bf7082015-06-03 23:50:27 -0700984
985 if (DEBUG) Slog.d(TAG, "Switching user stage 2/3. newUserId=" + newUserId
986 + " defaultImiId=" + defaultImiId);
987
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +0900988 // For secondary users, the list of enabled IMEs may not have been updated since the
989 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
990 // not be empty even if the IME has been uninstalled by the primary user.
991 // Even in such cases, IMMS works fine because it will find the most applicable
992 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900993 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900994 resetAllInternalStateLocked(false /* updateOnlyWhenLocaleChanged */,
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900995 initialUserSwitch /* needsToResetDefaultIme */);
996 if (initialUserSwitch) {
Yohei Yukawa094c71f2015-06-20 00:41:31 -0700997 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
998 mSettings.getEnabledInputMethodListLocked(), newUserId,
999 mContext.getBasePackageName());
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001000 }
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001001
1002 if (DEBUG) Slog.d(TAG, "Switching user stage 3/3. newUserId=" + newUserId
1003 + " selectedIme=" + mSettings.getSelectedInputMethod());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001004 }
1005
Kenny Guy2a764942014-04-02 13:29:20 +01001006 void updateCurrentProfileIds() {
1007 List<UserInfo> profiles =
1008 UserManager.get(mContext).getProfiles(mSettings.getCurrentUserId());
1009 int[] currentProfileIds = new int[profiles.size()]; // profiles will not be null
1010 for (int i = 0; i < currentProfileIds.length; i++) {
1011 currentProfileIds[i] = profiles.get(i).id;
Amith Yamasani734983f2014-03-04 16:48:05 -08001012 }
Kenny Guy2a764942014-04-02 13:29:20 +01001013 mSettings.setCurrentProfileIds(currentProfileIds);
Amith Yamasani734983f2014-03-04 16:48:05 -08001014 }
1015
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001016 @Override
1017 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
1018 throws RemoteException {
1019 try {
1020 return super.onTransact(code, data, reply, flags);
1021 } catch (RuntimeException e) {
1022 // The input method manager only throws security exceptions, so let's
1023 // log all others.
1024 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -07001025 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026 }
1027 throw e;
1028 }
1029 }
1030
Svetoslav Ganova0027152013-06-25 14:59:53 -07001031 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001032 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001033 if (DEBUG) {
1034 Slog.d(TAG, "--- systemReady");
1035 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001036 if (!mSystemReady) {
1037 mSystemReady = true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001038 mKeyguardManager =
1039 (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001040 mNotificationManager = (NotificationManager)
1041 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
1042 mStatusBar = statusBar;
1043 statusBar.setIconVisibility("ime", false);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001044 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokb858c732011-07-22 19:54:34 +09001045 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
1046 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +09001047 if (mShowOngoingImeSwitcherForPhones) {
Seigo Nonaka7309b122015-08-17 18:34:13 -07001048 mWindowManagerInternal.setOnHardKeyboardStatusChangeListener(
satok01038492012-04-09 21:08:27 +09001049 mHardKeyboardListener);
1050 }
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09001051 buildInputMethodListLocked(mMethodList, mMethodMap,
1052 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satok0a1bcf42012-05-16 19:26:31 +09001053 if (!mImeSelectedOnBoot) {
1054 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001055 resetStateIfCurrentLocaleChangedLocked();
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001056 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1057 mSettings.getEnabledInputMethodListLocked(),
1058 mSettings.getCurrentUserId(), mContext.getBasePackageName());
satok0a1bcf42012-05-16 19:26:31 +09001059 }
1060 mLastSystemLocale = mRes.getConfiguration().locale;
Dianne Hackborncc278702009-09-02 23:07:23 -07001061 try {
1062 startInputInnerLocked();
1063 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001064 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -07001065 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001066 }
1067 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001068 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001069
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001070 // ---------------------------------------------------------------------------------------
1071 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
1072 // 1) it comes from the system process
1073 // 2) the calling process' user id is identical to the current user id IMMS thinks.
1074 private boolean calledFromValidUser() {
1075 final int uid = Binder.getCallingUid();
1076 final int userId = UserHandle.getUserId(uid);
1077 if (DEBUG) {
1078 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
1079 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
1080 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +09001081 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
1082 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001083 }
Kenny Guy2a764942014-04-02 13:29:20 +01001084 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001085 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001086 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001087
1088 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
1089 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
1090 // must not manage background users' states in any functions.
1091 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
1092 // by a token.
1093 if (mContext.checkCallingOrSelfPermission(
1094 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1095 == PackageManager.PERMISSION_GRANTED) {
1096 if (DEBUG) {
1097 Slog.d(TAG, "--- Access granted because the calling process has "
1098 + "the INTERACT_ACROSS_USERS_FULL permission");
1099 }
1100 return true;
1101 }
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07001102 Slog.w(TAG, "--- IPC called from background users. Ignore. callers="
1103 + Debug.getCallers(10));
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001104 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001105 }
1106
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001107
1108 /**
1109 * Returns true iff the caller is identified to be the current input method with the token.
1110 * @param token The window token given to the input method when it was started.
1111 * @return true if and only if non-null valid token is specified.
1112 */
1113 private boolean calledWithValidToken(IBinder token) {
1114 if (token == null || mCurToken != token) {
1115 return false;
1116 }
1117 return true;
1118 }
1119
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001120 private boolean bindCurrentInputMethodService(
1121 Intent service, ServiceConnection conn, int flags) {
1122 if (service == null || conn == null) {
1123 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
1124 return false;
1125 }
Amith Yamasani27b89e62013-01-16 12:30:11 -08001126 return mContext.bindServiceAsUser(service, conn, flags,
1127 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001128 }
1129
satoke7c6998e2011-06-03 17:57:59 +09001130 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001131 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001132 // TODO: Make this work even for non-current users?
1133 if (!calledFromValidUser()) {
1134 return Collections.emptyList();
1135 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001136 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001137 return new ArrayList<>(mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001138 }
1139 }
1140
satoke7c6998e2011-06-03 17:57:59 +09001141 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001142 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001143 // TODO: Make this work even for non-current users?
1144 if (!calledFromValidUser()) {
1145 return Collections.emptyList();
1146 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001147 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001148 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001149 }
1150 }
1151
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001152 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001153 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001154 * @return enabled subtypes of the specified imi
1155 */
satoke7c6998e2011-06-03 17:57:59 +09001156 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001157 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001158 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001159 // TODO: Make this work even for non-current users?
1160 if (!calledFromValidUser()) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001161 return Collections.emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001162 }
satok67ddf9c2010-11-17 09:45:54 +09001163 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001164 final InputMethodInfo imi;
1165 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001166 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001167 } else {
1168 imi = mMethodMap.get(imiId);
1169 }
1170 if (imi == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001171 return Collections.emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001172 }
1173 return mSettings.getEnabledInputMethodSubtypeListLocked(
1174 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001175 }
1176 }
1177
satoke7c6998e2011-06-03 17:57:59 +09001178 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001179 public void addClient(IInputMethodClient client,
1180 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001181 if (!calledFromValidUser()) {
1182 return;
1183 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184 synchronized (mMethodMap) {
1185 mClients.put(client.asBinder(), new ClientState(client,
1186 inputContext, uid, pid));
1187 }
1188 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001189
satoke7c6998e2011-06-03 17:57:59 +09001190 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001192 if (!calledFromValidUser()) {
1193 return;
1194 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001195 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001196 ClientState cs = mClients.remove(client.asBinder());
1197 if (cs != null) {
1198 clearClientSessionLocked(cs);
1199 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001200 }
1201 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001202
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 void executeOrSendMessage(IInterface target, Message msg) {
1204 if (target.asBinder() instanceof Binder) {
1205 mCaller.sendMessage(msg);
1206 } else {
1207 handleMessage(msg);
1208 msg.recycle();
1209 }
1210 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001211
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001212 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001214 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 + mCurClient.client.asBinder());
1216 if (mBoundToMethod) {
1217 mBoundToMethod = false;
1218 if (mCurMethod != null) {
1219 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1220 MSG_UNBIND_INPUT, mCurMethod));
1221 }
1222 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001223
1224 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1225 MSG_SET_ACTIVE, 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1227 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1228 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001230
The Android Open Source Project10592532009-03-18 17:39:46 -07001231 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001232 }
1233 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 private int getImeShowFlags() {
1236 int flags = 0;
1237 if (mShowForced) {
1238 flags |= InputMethod.SHOW_FORCED
1239 | InputMethod.SHOW_EXPLICIT;
1240 } else if (mShowExplicitlyRequested) {
1241 flags |= InputMethod.SHOW_EXPLICIT;
1242 }
1243 return flags;
1244 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001245
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246 private int getAppShowFlags() {
1247 int flags = 0;
1248 if (mShowForced) {
1249 flags |= InputMethodManager.SHOW_FORCED;
1250 } else if (!mShowExplicitlyRequested) {
1251 flags |= InputMethodManager.SHOW_IMPLICIT;
1252 }
1253 return flags;
1254 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001255
Dianne Hackborn7663d802012-02-24 13:08:49 -08001256 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001257 if (!mBoundToMethod) {
1258 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1259 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1260 mBoundToMethod = true;
1261 }
1262 final SessionState session = mCurClient.curSession;
1263 if (initial) {
1264 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1265 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
1266 } else {
1267 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1268 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
1269 }
1270 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001271 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001272 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001274 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001275 (session.channel != null ? session.channel.dup() : null),
1276 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001279 InputBindResult startInputLocked(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001280 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001281 // If no method is currently selected, do nothing.
1282 if (mCurMethodId == null) {
1283 return mNoBinding;
1284 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001285
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001286 ClientState cs = mClients.get(client.asBinder());
1287 if (cs == null) {
1288 throw new IllegalArgumentException("unknown client "
1289 + client.asBinder());
1290 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 try {
1293 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1294 // Check with the window manager to make sure this client actually
1295 // has a window with focus. If not, reject. This is thread safe
1296 // because if the focus changes some time before or after, the
1297 // next client receiving focus that has any interest in input will
1298 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001299 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1301 return null;
1302 }
1303 } catch (RemoteException e) {
1304 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001305
Dianne Hackborn7663d802012-02-24 13:08:49 -08001306 return startInputUncheckedLocked(cs, inputContext, attribute, controlFlags);
1307 }
1308
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001309 InputBindResult startInputUncheckedLocked(@NonNull ClientState cs,
Yohei Yukawad57ba672015-06-08 16:39:46 -07001310 IInputContext inputContext, @NonNull EditorInfo attribute, int controlFlags) {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001311 // If no method is currently selected, do nothing.
1312 if (mCurMethodId == null) {
1313 return mNoBinding;
1314 }
1315
Yohei Yukawad57ba672015-06-08 16:39:46 -07001316 if (!InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.uid,
1317 attribute.packageName)) {
1318 Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
1319 + " uid=" + cs.uid + " package=" + attribute.packageName);
1320 return mNoBinding;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001321 }
1322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001323 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001324 // Was the keyguard locked when switching over to the new client?
1325 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326 // If the client is changing, we need to switch over to the new
1327 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001328 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001329 if (DEBUG) Slog.v(TAG, "switching to client: client = "
John Spurlocke0980502013-10-25 11:59:29 -04001330 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001331
1332 // If the screen is on, inform the new client it is active
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001333 if (mIsInteractive) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001334 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001335 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001336 }
1337 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001339 // Bump up the sequence for this client and attach it.
1340 mCurSeq++;
1341 if (mCurSeq <= 0) mCurSeq = 1;
1342 mCurClient = cs;
1343 mCurInputContext = inputContext;
1344 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001346 // Check if the input method is changing.
1347 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1348 if (cs.curSession != null) {
1349 // Fast case: if we are already connected to the input method,
1350 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001351 return attachNewInputLocked(
1352 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001353 }
1354 if (mHaveConnection) {
1355 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001356 // Return to client, and we will get back with it when
1357 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001358 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001359 return new InputBindResult(null, null, mCurId, mCurSeq,
1360 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001361 } else if (SystemClock.uptimeMillis()
1362 < (mLastBindTime+TIME_TO_RECONNECT)) {
1363 // In this case we have connected to the service, but
1364 // don't yet have its interface. If it hasn't been too
1365 // long since we did the connection, we'll return to
1366 // the client and wait to get the service interface so
1367 // we can report back. If it has been too long, we want
1368 // to fall through so we can try a disconnect/reconnect
1369 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001370 return new InputBindResult(null, null, mCurId, mCurSeq,
1371 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001372 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001373 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1374 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001375 }
1376 }
1377 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001378
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001379 return startInputInnerLocked();
1380 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001381
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001382 InputBindResult startInputInnerLocked() {
1383 if (mCurMethodId == null) {
1384 return mNoBinding;
1385 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001386
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001387 if (!mSystemReady) {
1388 // If the system is not yet ready, we shouldn't be running third
1389 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001390 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1391 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001392 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001394 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1395 if (info == null) {
1396 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1397 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001398
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001399 unbindCurrentMethodLocked(false, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001400
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001401 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1402 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001403 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1404 com.android.internal.R.string.input_method_binding_label);
1405 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1406 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001407 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001408 | Context.BIND_NOT_VISIBLE | Context.BIND_NOT_FOREGROUND
1409 | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001410 mLastBindTime = SystemClock.uptimeMillis();
1411 mHaveConnection = true;
1412 mCurId = info.getId();
1413 mCurToken = new Binder();
1414 try {
Craig Mautnerca0ac712013-03-14 09:43:02 -07001415 if (true || DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001416 mIWindowManager.addWindowToken(mCurToken,
1417 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1418 } catch (RemoteException e) {
1419 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001420 return new InputBindResult(null, null, mCurId, mCurSeq,
1421 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001422 } else {
1423 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001424 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001425 + mCurIntent);
1426 }
1427 return null;
1428 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001429
satoke7c6998e2011-06-03 17:57:59 +09001430 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001431 public InputBindResult startInput(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001432 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001433 if (!calledFromValidUser()) {
1434 return null;
1435 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001436 synchronized (mMethodMap) {
1437 final long ident = Binder.clearCallingIdentity();
1438 try {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001439 return startInputLocked(client, inputContext, attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001440 } finally {
1441 Binder.restoreCallingIdentity(ident);
1442 }
1443 }
1444 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001445
satoke7c6998e2011-06-03 17:57:59 +09001446 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001447 public void finishInput(IInputMethodClient client) {
1448 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001449
satoke7c6998e2011-06-03 17:57:59 +09001450 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001451 public void onServiceConnected(ComponentName name, IBinder service) {
1452 synchronized (mMethodMap) {
1453 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1454 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001455 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001456 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001457 unbindCurrentMethodLocked(false, false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001458 return;
1459 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001460 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001461 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1462 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001463 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001464 clearClientSessionLocked(mCurClient);
1465 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001466 }
1467 }
1468 }
1469 }
1470
Jeff Brownc28867a2013-03-26 15:42:39 -07001471 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1472 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001473 synchronized (mMethodMap) {
1474 if (mCurMethod != null && method != null
1475 && mCurMethod.asBinder() == method.asBinder()) {
1476 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001477 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001478 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001479 method, session, channel);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001480 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001481 if (res.method != null) {
1482 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
1483 MSG_BIND_METHOD, mCurClient.client, res));
1484 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001485 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001486 }
1487 }
1488 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001489
1490 // Session abandoned. Close its associated input channel.
1491 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001492 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001493
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001494 void unbindCurrentMethodLocked(boolean reportToClient, boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001495 if (mVisibleBound) {
1496 mContext.unbindService(mVisibleConnection);
1497 mVisibleBound = false;
1498 }
1499
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001500 if (mHaveConnection) {
1501 mContext.unbindService(this);
1502 mHaveConnection = false;
1503 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001504
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001505 if (mCurToken != null) {
1506 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001507 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001508 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001509 // The current IME is shown. Hence an IME switch (transition) is happening.
Seigo Nonaka7309b122015-08-17 18:34:13 -07001510 mWindowManagerInternal.saveLastInputMethodWindowForTransition();
satoke0a99412012-05-10 02:22:58 +09001511 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001512 mIWindowManager.removeWindowToken(mCurToken);
1513 } catch (RemoteException e) {
1514 }
1515 mCurToken = null;
1516 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001517
The Android Open Source Project10592532009-03-18 17:39:46 -07001518 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001519 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001520
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001521 if (reportToClient && mCurClient != null) {
1522 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1523 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1524 }
1525 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001526
1527 void requestClientSessionLocked(ClientState cs) {
1528 if (!cs.sessionRequested) {
1529 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1530 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1531 cs.sessionRequested = true;
1532 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1533 MSG_CREATE_SESSION, mCurMethod, channels[1],
1534 new MethodCallback(this, mCurMethod, channels[0])));
1535 }
1536 }
1537
1538 void clearClientSessionLocked(ClientState cs) {
1539 finishSessionLocked(cs.curSession);
1540 cs.curSession = null;
1541 cs.sessionRequested = false;
1542 }
1543
1544 private void finishSessionLocked(SessionState sessionState) {
1545 if (sessionState != null) {
1546 if (sessionState.session != null) {
1547 try {
1548 sessionState.session.finishSession();
1549 } catch (RemoteException e) {
1550 Slog.w(TAG, "Session failed to close due to remote exception", e);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001551 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Jeff Brownc28867a2013-03-26 15:42:39 -07001552 }
1553 sessionState.session = null;
1554 }
1555 if (sessionState.channel != null) {
1556 sessionState.channel.dispose();
1557 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001558 }
1559 }
1560 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001561
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001562 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001563 if (mCurMethod != null) {
1564 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001565 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001566 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001567
Jeff Brownc28867a2013-03-26 15:42:39 -07001568 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001569 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001570 mCurMethod = null;
1571 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001572 if (mStatusBar != null) {
1573 mStatusBar.setIconVisibility("ime", false);
1574 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001575 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001576
satoke7c6998e2011-06-03 17:57:59 +09001577 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001578 public void onServiceDisconnected(ComponentName name) {
1579 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001580 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001581 + " mCurIntent=" + mCurIntent);
1582 if (mCurMethod != null && mCurIntent != null
1583 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001584 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001585 // We consider this to be a new bind attempt, since the system
1586 // should now try to restart the service for us.
1587 mLastBindTime = SystemClock.uptimeMillis();
1588 mShowRequested = mInputShown;
1589 mInputShown = false;
1590 if (mCurClient != null) {
1591 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1592 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1593 }
1594 }
1595 }
1596 }
1597
satokf9f01002011-05-19 21:31:50 +09001598 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001599 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
1600 long ident = Binder.clearCallingIdentity();
1601 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001602 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09001603 if (!calledWithValidToken(token)) {
1604 final int uid = Binder.getCallingUid();
1605 Slog.e(TAG, "Ignoring updateStatusIcon due to an invalid token. uid:" + uid
1606 + " token:" + token);
1607 return;
1608 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001609 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001610 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001611 if (mStatusBar != null) {
1612 mStatusBar.setIconVisibility("ime", false);
1613 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001614 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001615 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001616 CharSequence contentDescription = null;
1617 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001618 // Use PackageManager to load label
1619 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001620 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001621 mIPackageManager.getApplicationInfo(packageName, 0,
1622 mSettings.getCurrentUserId()));
1623 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001624 /* ignore */
1625 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001626 if (mStatusBar != null) {
1627 mStatusBar.setIcon("ime", packageName, iconId, 0,
1628 contentDescription != null
1629 ? contentDescription.toString() : null);
1630 mStatusBar.setIconVisibility("ime", true);
1631 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001632 }
1633 }
1634 } finally {
1635 Binder.restoreCallingIdentity(ident);
1636 }
1637 }
1638
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001639 private boolean shouldShowImeSwitcherLocked(int visibility) {
satok7cfc0ed2011-06-20 21:29:36 +09001640 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04001641 if (mSwitchingDialog != null) return false;
satok2c93efc2012-04-02 19:33:47 +09001642 if (isScreenLocked()) return false;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001643 if ((visibility & InputMethodService.IME_ACTIVE) == 0) return false;
Seigo Nonaka7309b122015-08-17 18:34:13 -07001644 if (mWindowManagerInternal.isHardKeyboardAvailable()) {
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001645 // When physical keyboard is attached, we show the ime switcher (or notification if
1646 // NavBar is not available) because SHOW_IME_WITH_HARD_KEYBOARD settings currently
1647 // exists in the IME switcher dialog. Might be OK to remove this condition once
1648 // SHOW_IME_WITH_HARD_KEYBOARD settings finds a good place to live.
1649 return true;
1650 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001651 if ((visibility & InputMethodService.IME_VISIBLE) == 0) return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001652
1653 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1654 final int N = imis.size();
1655 if (N > 2) return true;
1656 if (N < 1) return false;
1657 int nonAuxCount = 0;
1658 int auxCount = 0;
1659 InputMethodSubtype nonAuxSubtype = null;
1660 InputMethodSubtype auxSubtype = null;
1661 for(int i = 0; i < N; ++i) {
1662 final InputMethodInfo imi = imis.get(i);
1663 final List<InputMethodSubtype> subtypes =
1664 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
1665 final int subtypeCount = subtypes.size();
1666 if (subtypeCount == 0) {
1667 ++nonAuxCount;
1668 } else {
1669 for (int j = 0; j < subtypeCount; ++j) {
1670 final InputMethodSubtype subtype = subtypes.get(j);
1671 if (!subtype.isAuxiliary()) {
1672 ++nonAuxCount;
1673 nonAuxSubtype = subtype;
1674 } else {
1675 ++auxCount;
1676 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001677 }
1678 }
satok7cfc0ed2011-06-20 21:29:36 +09001679 }
1680 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001681 if (nonAuxCount > 1 || auxCount > 1) {
1682 return true;
1683 } else if (nonAuxCount == 1 && auxCount == 1) {
1684 if (nonAuxSubtype != null && auxSubtype != null
1685 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1686 || auxSubtype.overridesImplicitlyEnabledSubtype()
1687 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
1688 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1689 return false;
1690 }
1691 return true;
1692 }
1693 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001694 }
1695
John Spurlocke0980502013-10-25 11:59:29 -04001696 private boolean isKeyguardLocked() {
1697 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1698 }
1699
satokdbf29502011-08-25 15:28:23 +09001700 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001701 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001702 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001703 if (!calledWithValidToken(token)) {
1704 final int uid = Binder.getCallingUid();
1705 Slog.e(TAG, "Ignoring setImeWindowStatus due to an invalid token. uid:" + uid
1706 + " token:" + token);
1707 return;
1708 }
1709
1710 synchronized (mMethodMap) {
1711 mImeWindowVis = vis;
1712 mBackDisposition = backDisposition;
1713 updateSystemUiLocked(token, vis, backDisposition);
1714 }
1715 }
1716
1717 private void updateSystemUi(IBinder token, int vis, int backDisposition) {
1718 synchronized (mMethodMap) {
1719 updateSystemUiLocked(token, vis, backDisposition);
1720 }
1721 }
1722
1723 // Caution! This method is called in this class. Handle multi-user carefully
1724 private void updateSystemUiLocked(IBinder token, int vis, int backDisposition) {
1725 if (!calledWithValidToken(token)) {
1726 final int uid = Binder.getCallingUid();
1727 Slog.e(TAG, "Ignoring updateSystemUiLocked due to an invalid token. uid:" + uid
1728 + " token:" + token);
1729 return;
1730 }
1731
1732 // TODO: Move this clearing calling identity block to setImeWindowStatus after making sure
1733 // all updateSystemUi happens on system previlege.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001734 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001735 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001736 // apply policy for binder calls
1737 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
1738 vis = 0;
satok06487a52010-10-29 11:37:18 +09001739 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001740 // mImeWindowVis should be updated before calling shouldShowImeSwitcherLocked().
1741 final boolean needsToShowImeSwitcher = shouldShowImeSwitcherLocked(vis);
1742 if (mStatusBar != null) {
1743 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
1744 needsToShowImeSwitcher);
1745 }
1746 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1747 if (imi != null && needsToShowImeSwitcher) {
1748 // Used to load label
1749 final CharSequence title = mRes.getText(
1750 com.android.internal.R.string.select_input_method);
1751 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
1752 mContext, imi, mCurrentSubtype);
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001753 mImeSwitcherNotification.setContentTitle(title)
1754 .setContentText(summary)
1755 .setContentIntent(mImeSwitchPendingIntent);
Seigo Nonaka7309b122015-08-17 18:34:13 -07001756 try {
1757 if ((mNotificationManager != null)
1758 && !mIWindowManager.hasNavigationBar()) {
1759 if (DEBUG) {
1760 Slog.d(TAG, "--- show notification: label = " + summary);
1761 }
1762 mNotificationManager.notifyAsUser(null,
1763 com.android.internal.R.string.select_input_method,
1764 mImeSwitcherNotification.build(), UserHandle.ALL);
1765 mNotificationShown = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001766 }
Seigo Nonaka7309b122015-08-17 18:34:13 -07001767 } catch (RemoteException e) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001768 }
1769 } else {
1770 if (mNotificationShown && mNotificationManager != null) {
1771 if (DEBUG) {
1772 Slog.d(TAG, "--- hide notification");
satok7cfc0ed2011-06-20 21:29:36 +09001773 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001774 mNotificationManager.cancelAsUser(null,
1775 com.android.internal.R.string.select_input_method, UserHandle.ALL);
1776 mNotificationShown = false;
satok7cfc0ed2011-06-20 21:29:36 +09001777 }
satok06487a52010-10-29 11:37:18 +09001778 }
1779 } finally {
1780 Binder.restoreCallingIdentity(ident);
1781 }
1782 }
1783
satoke7c6998e2011-06-03 17:57:59 +09001784 @Override
satokf9f01002011-05-19 21:31:50 +09001785 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001786 if (!calledFromValidUser()) {
1787 return;
1788 }
satokf9f01002011-05-19 21:31:50 +09001789 synchronized (mMethodMap) {
1790 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1791 for (int i = 0; i < spans.length; ++i) {
1792 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001793 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001794 mSecureSuggestionSpans.put(ss, currentImi);
1795 }
1796 }
1797 }
1798 }
1799
satoke7c6998e2011-06-03 17:57:59 +09001800 @Override
satokf9f01002011-05-19 21:31:50 +09001801 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001802 if (!calledFromValidUser()) {
1803 return false;
1804 }
satokf9f01002011-05-19 21:31:50 +09001805 synchronized (mMethodMap) {
1806 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1807 // TODO: Do not send the intent if the process of the targetImi is already dead.
1808 if (targetImi != null) {
1809 final String[] suggestions = span.getSuggestions();
1810 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001811 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001812 final Intent intent = new Intent();
1813 // Ensures that only a class in the original IME package will receive the
1814 // notification.
satok42c5a162011-05-26 16:46:14 +09001815 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001816 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1817 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1818 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1819 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001820 final long ident = Binder.clearCallingIdentity();
1821 try {
1822 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1823 } finally {
1824 Binder.restoreCallingIdentity(ident);
1825 }
satokf9f01002011-05-19 21:31:50 +09001826 return true;
1827 }
1828 }
1829 return false;
1830 }
1831
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001832 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001833 updateInputMethodsFromSettingsLocked(enabledMayChange);
1834 updateKeyboardFromSettingsLocked();
1835 }
1836
1837 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001838 if (enabledMayChange) {
1839 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1840 for (int i=0; i<enabled.size(); i++) {
1841 // We allow the user to select "disabled until used" apps, so if they
1842 // are enabling one of those here we now need to make it enabled.
1843 InputMethodInfo imm = enabled.get(i);
1844 try {
1845 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1846 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1847 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09001848 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001849 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001850 if (DEBUG) {
1851 Slog.d(TAG, "Update state(" + imm.getId()
1852 + "): DISABLED_UNTIL_USED -> DEFAULT");
1853 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001854 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1855 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001856 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
1857 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001858 }
1859 } catch (RemoteException e) {
1860 }
1861 }
1862 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001863 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1864 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1865 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1866 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001867 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001868 // There is no input method selected, try to choose new applicable input method.
1869 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001870 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001871 }
1872 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001873 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001874 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001875 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001876 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001877 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001878 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001879 }
satokf3db1af2010-11-23 13:34:33 +09001880 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001881 } else {
1882 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001883 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001884 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001885 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09001886 // Here is not the perfect place to reset the switching controller. Ideally
1887 // mSwitchingController and mSettings should be able to share the same state.
1888 // TODO: Make sure that mSwitchingController and mSettings are sharing the
1889 // the same enabled IMEs list.
1890 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07001891
1892 }
1893
1894 public void updateKeyboardFromSettingsLocked() {
1895 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
1896 if (mSwitchingDialog != null
1897 && mSwitchingDialogTitleView != null
1898 && mSwitchingDialog.isShowing()) {
1899 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
1900 com.android.internal.R.id.hard_keyboard_switch);
1901 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
1902 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001903 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001904
satokab751aa2010-09-14 19:17:36 +09001905 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001906 InputMethodInfo info = mMethodMap.get(id);
1907 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001908 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001909 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001910
Yohei Yukawae63b5fa2014-09-19 13:14:55 +09001911 if (mCurClient != null && mCurAttribute != null) {
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001912 // We have already made sure that the package name belongs to the application's UID.
1913 // No further UID check is required.
1914 if (SystemConfig.getInstance().getFixedImeApps().contains(mCurAttribute.packageName)) {
1915 return;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +09001916 }
1917 }
1918
satokd81e9502012-05-21 12:58:45 +09001919 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001920 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09001921 final int subtypeCount = info.getSubtypeCount();
1922 if (subtypeCount <= 0) {
1923 return;
satokcd7cd292010-11-20 15:46:23 +09001924 }
satokd81e9502012-05-21 12:58:45 +09001925 final InputMethodSubtype oldSubtype = mCurrentSubtype;
1926 final InputMethodSubtype newSubtype;
1927 if (subtypeId >= 0 && subtypeId < subtypeCount) {
1928 newSubtype = info.getSubtypeAt(subtypeId);
1929 } else {
1930 // If subtype is null, try to find the most applicable one from
1931 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001932 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09001933 }
1934 if (newSubtype == null || oldSubtype == null) {
1935 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
1936 + ", new subtype = " + newSubtype);
1937 return;
1938 }
1939 if (newSubtype != oldSubtype) {
1940 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1941 if (mCurMethod != null) {
1942 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001943 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokd81e9502012-05-21 12:58:45 +09001944 mCurMethod.changeInputMethodSubtype(newSubtype);
1945 } catch (RemoteException e) {
1946 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
satokab751aa2010-09-14 19:17:36 +09001947 }
1948 }
1949 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001950 return;
1951 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001952
satokd81e9502012-05-21 12:58:45 +09001953 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001954 final long ident = Binder.clearCallingIdentity();
1955 try {
satokab751aa2010-09-14 19:17:36 +09001956 // Set a subtype to this input method.
1957 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001958 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1959 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1960 // because mCurMethodId is stored as a history in
1961 // setSelectedInputMethodAndSubtypeLocked().
1962 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001963
1964 if (ActivityManagerNative.isSystemReady()) {
1965 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001966 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001967 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07001968 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001969 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001970 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001971 } finally {
1972 Binder.restoreCallingIdentity(ident);
1973 }
1974 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001975
satok42c5a162011-05-26 16:46:14 +09001976 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001977 public boolean showSoftInput(IInputMethodClient client, int flags,
1978 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001979 if (!calledFromValidUser()) {
1980 return false;
1981 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001982 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001983 long ident = Binder.clearCallingIdentity();
1984 try {
1985 synchronized (mMethodMap) {
1986 if (mCurClient == null || client == null
1987 || mCurClient.client.asBinder() != client.asBinder()) {
1988 try {
1989 // We need to check if this is the current client with
1990 // focus in the window manager, to allow this call to
1991 // be made before input is started in it.
1992 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001993 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001994 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001995 }
1996 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001997 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001998 }
1999 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002000
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002001 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002002 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002003 }
2004 } finally {
2005 Binder.restoreCallingIdentity(ident);
2006 }
2007 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002008
The Android Open Source Project4df24232009-03-05 14:34:35 -08002009 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002010 mShowRequested = true;
2011 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
2012 mShowExplicitlyRequested = true;
2013 }
2014 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
2015 mShowExplicitlyRequested = true;
2016 mShowForced = true;
2017 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002018
Dianne Hackborncc278702009-09-02 23:07:23 -07002019 if (!mSystemReady) {
2020 return false;
2021 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002022
The Android Open Source Project4df24232009-03-05 14:34:35 -08002023 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002024 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002025 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002026 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
2027 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
2028 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002029 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002030 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002031 bindCurrentInputMethodService(
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07002032 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE
Dianne Hackbornd69e4c12015-04-24 09:54:54 -07002033 | Context.BIND_TREAT_LIKE_ACTIVITY
2034 | Context.BIND_FOREGROUND_SERVICE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002035 mVisibleBound = true;
2036 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002037 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002038 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09002039 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002040 // The client has asked to have the input method shown, but
2041 // we have been sitting here too long with a connection to the
2042 // service and no interface received, so let's disconnect/connect
2043 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002044 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002045 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09002046 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002047 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002048 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002049 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002050 } else {
2051 if (DEBUG) {
2052 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
2053 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
2054 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002055 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002056
The Android Open Source Project4df24232009-03-05 14:34:35 -08002057 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002058 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002059
satok42c5a162011-05-26 16:46:14 +09002060 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002061 public boolean hideSoftInput(IInputMethodClient client, int flags,
2062 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002063 if (!calledFromValidUser()) {
2064 return false;
2065 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002066 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002067 long ident = Binder.clearCallingIdentity();
2068 try {
2069 synchronized (mMethodMap) {
2070 if (mCurClient == null || client == null
2071 || mCurClient.client.asBinder() != client.asBinder()) {
2072 try {
2073 // We need to check if this is the current client with
2074 // focus in the window manager, to allow this call to
2075 // be made before input is started in it.
2076 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002077 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
2078 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002079 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002080 }
2081 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002082 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002083 }
2084 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002085
Joe Onorato8a9b2202010-02-26 18:56:32 -08002086 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002087 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002088 }
2089 } finally {
2090 Binder.restoreCallingIdentity(ident);
2091 }
2092 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002093
The Android Open Source Project4df24232009-03-05 14:34:35 -08002094 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002095 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
2096 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002097 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 -08002098 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002099 }
2100 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002101 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 -08002102 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002103 }
Seigo Nonakaec928652015-06-10 15:31:20 +09002104
2105 // There is a chance that IMM#hideSoftInput() is called in a transient state where
2106 // IMMS#InputShown is already updated to be true whereas IMMS#mImeWindowVis is still waiting
2107 // to be updated with the new value sent from IME process. Even in such a transient state
2108 // historically we have accepted an incoming call of IMM#hideSoftInput() from the
2109 // application process as a valid request, and have even promised such a behavior with CTS
2110 // since Android Eclair. That's why we need to accept IMM#hideSoftInput() even when only
2111 // IMMS#InputShown indicates that the software keyboard is shown.
2112 // TODO: Clean up, IMMS#mInputShown, IMMS#mImeWindowVis and mShowRequested.
2113 final boolean shouldHideSoftInput = (mCurMethod != null) && (mInputShown ||
2114 (mImeWindowVis & InputMethodService.IME_ACTIVE) != 0);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002115 boolean res;
Seigo Nonakaec928652015-06-10 15:31:20 +09002116 if (shouldHideSoftInput) {
2117 // The IME will report its visible state again after the following message finally
2118 // delivered to the IME process as an IPC. Hence the inconsistency between
2119 // IMMS#mInputShown and IMMS#mImeWindowVis should be resolved spontaneously in
2120 // the final state.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002121 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2122 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
2123 res = true;
2124 } else {
2125 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002126 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002127 if (mHaveConnection && mVisibleBound) {
2128 mContext.unbindService(mVisibleConnection);
2129 mVisibleBound = false;
2130 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002131 mInputShown = false;
2132 mShowRequested = false;
2133 mShowExplicitlyRequested = false;
2134 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002135 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002136 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002137
satok42c5a162011-05-26 16:46:14 +09002138 @Override
Dianne Hackborn7663d802012-02-24 13:08:49 -08002139 public InputBindResult windowGainedFocus(IInputMethodClient client, IBinder windowToken,
2140 int controlFlags, int softInputMode, int windowFlags,
2141 EditorInfo attribute, IInputContext inputContext) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002142 // Needs to check the validity before clearing calling identity
2143 final boolean calledFromValidUser = calledFromValidUser();
2144
Dianne Hackborn7663d802012-02-24 13:08:49 -08002145 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002146 long ident = Binder.clearCallingIdentity();
2147 try {
2148 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002149 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
Dianne Hackborn7663d802012-02-24 13:08:49 -08002150 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002151 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08002152 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002153
Dianne Hackborn7663d802012-02-24 13:08:49 -08002154 ClientState cs = mClients.get(client.asBinder());
2155 if (cs == null) {
2156 throw new IllegalArgumentException("unknown client "
2157 + client.asBinder());
2158 }
2159
2160 try {
2161 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2162 // Check with the window manager to make sure this client actually
2163 // has a window with focus. If not, reject. This is thread safe
2164 // because if the focus changes some time before or after, the
2165 // next client receiving focus that has any interest in input will
2166 // be calling through here after that change happens.
2167 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2168 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2169 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002170 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002171 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002172 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002173
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002174 if (!calledFromValidUser) {
2175 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2176 Slog.w(TAG, "If you want to interect with IME, you need "
2177 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2178 hideCurrentInputLocked(0, null);
2179 return null;
2180 }
2181
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002182 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07002183 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09002184 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002185 if (attribute != null) {
2186 return startInputUncheckedLocked(cs, inputContext, attribute,
2187 controlFlags);
2188 }
2189 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002190 }
2191 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002192
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002193 // Should we auto-show the IME even if the caller has not
2194 // specified what should be done with it?
2195 // We only do this automatically if the window can resize
2196 // to accommodate the IME (so what the user sees will give
2197 // them good context without input information being obscured
2198 // by the IME) or if running on a large screen where there
2199 // is more room for the target window + IME.
2200 final boolean doAutoShow =
2201 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2202 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2203 || mRes.getConfiguration().isLayoutSizeAtLeast(
2204 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002205 final boolean isTextEditor =
2206 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2207
2208 // We want to start input before showing the IME, but after closing
2209 // it. We want to do this after closing it to help the IME disappear
2210 // more quickly (not get stuck behind it initializing itself for the
2211 // new focused input, even if its window wants to hide the IME).
2212 boolean didStart = false;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002214 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2215 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002216 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002217 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2218 // There is no focus view, and this window will
2219 // be behind any soft input window, so hide the
2220 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002221 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002222 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002223 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002224 } else if (isTextEditor && doAutoShow && (softInputMode &
2225 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002226 // There is a focus view, and we are navigating forward
2227 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002228 // We only do this automatically if the window can resize
2229 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002230 // them good context without input information being obscured
2231 // by the IME) or if running on a large screen where there
2232 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002233 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002234 if (attribute != null) {
2235 res = startInputUncheckedLocked(cs, inputContext, attribute,
2236 controlFlags);
2237 didStart = true;
2238 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002239 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002240 }
2241 break;
2242 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2243 // Do nothing.
2244 break;
2245 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2246 if ((softInputMode &
2247 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002248 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002249 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002250 }
2251 break;
2252 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002253 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002254 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002255 break;
2256 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2257 if ((softInputMode &
2258 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002259 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002260 if (attribute != null) {
2261 res = startInputUncheckedLocked(cs, inputContext, attribute,
2262 controlFlags);
2263 didStart = true;
2264 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002265 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002266 }
2267 break;
2268 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002269 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002270 if (attribute != null) {
2271 res = startInputUncheckedLocked(cs, inputContext, attribute,
2272 controlFlags);
2273 didStart = true;
2274 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002275 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002276 break;
2277 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002278
2279 if (!didStart && attribute != null) {
2280 res = startInputUncheckedLocked(cs, inputContext, attribute,
2281 controlFlags);
2282 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002283 }
2284 } finally {
2285 Binder.restoreCallingIdentity(ident);
2286 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002287
2288 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002289 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002290
satok42c5a162011-05-26 16:46:14 +09002291 @Override
Seigo Nonaka14e13912015-05-06 21:04:13 -07002292 public void showInputMethodPickerFromClient(
2293 IInputMethodClient client, int auxiliarySubtypeMode) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002294 if (!calledFromValidUser()) {
2295 return;
2296 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002297 synchronized (mMethodMap) {
2298 if (mCurClient == null || client == null
2299 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002300 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002301 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002302 }
2303
satok440aab52010-11-25 09:43:11 +09002304 // Always call subtype picker, because subtype picker is a superset of input method
2305 // picker.
Seigo Nonaka14e13912015-05-06 21:04:13 -07002306 mHandler.sendMessage(mCaller.obtainMessageI(
2307 MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode));
satokab751aa2010-09-14 19:17:36 +09002308 }
2309 }
2310
satok42c5a162011-05-26 16:46:14 +09002311 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002312 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002313 if (!calledFromValidUser()) {
2314 return;
2315 }
satok28203512010-11-24 11:06:49 +09002316 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2317 }
2318
satok42c5a162011-05-26 16:46:14 +09002319 @Override
satok28203512010-11-24 11:06:49 +09002320 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002321 if (!calledFromValidUser()) {
2322 return;
2323 }
satok28203512010-11-24 11:06:49 +09002324 synchronized (mMethodMap) {
2325 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002326 setInputMethodWithSubtypeIdLocked(token, id,
2327 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2328 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002329 } else {
2330 setInputMethod(token, id);
2331 }
2332 }
satokab751aa2010-09-14 19:17:36 +09002333 }
2334
satok42c5a162011-05-26 16:46:14 +09002335 @Override
satokb416a712010-11-25 20:42:14 +09002336 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002337 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002338 if (!calledFromValidUser()) {
2339 return;
2340 }
satokb416a712010-11-25 20:42:14 +09002341 synchronized (mMethodMap) {
2342 if (mCurClient == null || client == null
2343 || mCurClient.client.asBinder() != client.asBinder()) {
2344 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
2345 }
satok7fee71f2010-12-17 18:54:26 +09002346 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2347 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002348 }
2349 }
2350
satok4fc87d62011-05-20 16:13:43 +09002351 @Override
satok735cf382010-11-11 20:40:09 +09002352 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002353 if (!calledFromValidUser()) {
2354 return false;
2355 }
satok735cf382010-11-11 20:40:09 +09002356 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002357 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002358 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002359 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002360 lastImi = mMethodMap.get(lastIme.first);
2361 } else {
2362 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002363 }
satok4fc87d62011-05-20 16:13:43 +09002364 String targetLastImiId = null;
2365 int subtypeId = NOT_A_SUBTYPE_ID;
2366 if (lastIme != null && lastImi != null) {
2367 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
2368 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
2369 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2370 : mCurrentSubtype.hashCode();
2371 // If the last IME is the same as the current IME and the last subtype is not
2372 // defined, there is no need to switch to the last IME.
2373 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2374 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002375 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002376 }
2377 }
2378
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002379 if (TextUtils.isEmpty(targetLastImiId)
2380 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002381 // This is a safety net. If the currentSubtype can't be added to the history
2382 // and the framework couldn't find the last ime, we will make the last ime be
2383 // the most applicable enabled keyboard subtype of the system imes.
2384 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2385 if (enabled != null) {
2386 final int N = enabled.size();
2387 final String locale = mCurrentSubtype == null
2388 ? mRes.getConfiguration().locale.toString()
2389 : mCurrentSubtype.getLocale();
2390 for (int i = 0; i < N; ++i) {
2391 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002392 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002393 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002394 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2395 InputMethodUtils.getSubtypes(imi),
2396 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002397 if (keyboardSubtype != null) {
2398 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002399 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002400 imi, keyboardSubtype.hashCode());
2401 if(keyboardSubtype.getLocale().equals(locale)) {
2402 break;
2403 }
2404 }
2405 }
2406 }
2407 }
2408 }
2409
2410 if (!TextUtils.isEmpty(targetLastImiId)) {
2411 if (DEBUG) {
2412 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2413 + ", from: " + mCurMethodId + ", " + subtypeId);
2414 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002415 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002416 return true;
2417 } else {
2418 return false;
2419 }
satok735cf382010-11-11 20:40:09 +09002420 }
2421 }
2422
satoke7c6998e2011-06-03 17:57:59 +09002423 @Override
satok688bd472012-02-09 20:09:17 +09002424 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002425 if (!calledFromValidUser()) {
2426 return false;
2427 }
satok688bd472012-02-09 20:09:17 +09002428 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002429 if (!calledWithValidToken(token)) {
2430 final int uid = Binder.getCallingUid();
2431 Slog.e(TAG, "Ignoring switchToNextInputMethod due to an invalid token. uid:" + uid
2432 + " token:" + token);
2433 return false;
2434 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002435 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
satok688bd472012-02-09 20:09:17 +09002436 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2437 if (nextSubtype == null) {
2438 return false;
2439 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002440 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2441 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002442 return true;
2443 }
2444 }
2445
2446 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002447 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2448 if (!calledFromValidUser()) {
2449 return false;
2450 }
2451 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002452 if (!calledWithValidToken(token)) {
2453 final int uid = Binder.getCallingUid();
2454 Slog.e(TAG, "Ignoring shouldOfferSwitchingToNextInputMethod due to an invalid "
2455 + "token. uid:" + uid + " token:" + token);
2456 return false;
2457 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002458 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002459 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2460 if (nextSubtype == null) {
2461 return false;
2462 }
2463 return true;
2464 }
2465 }
2466
2467 @Override
satok68f1b782011-04-11 14:26:04 +09002468 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002469 if (!calledFromValidUser()) {
2470 return null;
2471 }
satok68f1b782011-04-11 14:26:04 +09002472 synchronized (mMethodMap) {
2473 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2474 // TODO: Handle the case of the last IME with no subtypes
2475 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2476 || TextUtils.isEmpty(lastIme.second)) return null;
2477 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2478 if (lastImi == null) return null;
2479 try {
2480 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002481 final int lastSubtypeId =
2482 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002483 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2484 return null;
2485 }
2486 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002487 } catch (NumberFormatException e) {
2488 return null;
2489 }
2490 }
2491 }
2492
satoke7c6998e2011-06-03 17:57:59 +09002493 @Override
satokee5e77c2011-09-02 18:50:15 +09002494 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002495 if (!calledFromValidUser()) {
2496 return;
2497 }
satok91e88122011-07-18 11:11:42 +09002498 // By this IPC call, only a process which shares the same uid with the IME can add
2499 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09002500 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09002501 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002502 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002503 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002504 final String[] packageInfos;
2505 try {
2506 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2507 } catch (RemoteException e) {
2508 Slog.e(TAG, "Failed to get package infos");
2509 return;
2510 }
satok91e88122011-07-18 11:11:42 +09002511 if (packageInfos != null) {
2512 final int packageNum = packageInfos.length;
2513 for (int i = 0; i < packageNum; ++i) {
2514 if (packageInfos[i].equals(imi.getPackageName())) {
2515 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002516 final long ident = Binder.clearCallingIdentity();
2517 try {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002518 buildInputMethodListLocked(mMethodList, mMethodMap,
2519 false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002520 } finally {
2521 Binder.restoreCallingIdentity(ident);
2522 }
satokee5e77c2011-09-02 18:50:15 +09002523 return;
satok91e88122011-07-18 11:11:42 +09002524 }
2525 }
2526 }
satoke7c6998e2011-06-03 17:57:59 +09002527 }
satokee5e77c2011-09-02 18:50:15 +09002528 return;
satoke7c6998e2011-06-03 17:57:59 +09002529 }
2530
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002531 @Override
2532 public int getInputMethodWindowVisibleHeight() {
Seigo Nonaka7309b122015-08-17 18:34:13 -07002533 return mWindowManagerInternal.getInputMethodWindowVisibleHeight();
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002534 }
2535
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002536 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002537 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002538 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002539 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002540 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002541 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002542 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
2543 if (DEBUG) {
2544 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
2545 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
2546 + " actual: " + sequenceNumber);
2547 }
2548 return;
2549 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002550 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2551 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09002552 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002553 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002554 }
2555 }
2556
satok28203512010-11-24 11:06:49 +09002557 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002558 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002559 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
2560 }
2561 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002562
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002563 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
2564 if (token == null) {
2565 if (mContext.checkCallingOrSelfPermission(
2566 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2567 != PackageManager.PERMISSION_GRANTED) {
2568 throw new SecurityException(
2569 "Using null token requires permission "
2570 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002571 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002572 } else if (mCurToken != token) {
2573 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2574 + " token: " + token);
2575 return;
2576 }
2577
2578 final long ident = Binder.clearCallingIdentity();
2579 try {
2580 setInputMethodLocked(id, subtypeId);
2581 } finally {
2582 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002583 }
2584 }
2585
satok42c5a162011-05-26 16:46:14 +09002586 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002587 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002588 if (!calledFromValidUser()) {
2589 return;
2590 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002591 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002592 if (!calledWithValidToken(token)) {
2593 final int uid = Binder.getCallingUid();
2594 Slog.e(TAG, "Ignoring hideInputMethod due to an invalid token. uid:"
2595 + uid + " token:" + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002596 return;
2597 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002598 long ident = Binder.clearCallingIdentity();
2599 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002600 hideCurrentInputLocked(flags, null);
2601 } finally {
2602 Binder.restoreCallingIdentity(ident);
2603 }
2604 }
2605 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002606
satok42c5a162011-05-26 16:46:14 +09002607 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002608 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002609 if (!calledFromValidUser()) {
2610 return;
2611 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002612 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002613 if (!calledWithValidToken(token)) {
2614 final int uid = Binder.getCallingUid();
2615 Slog.e(TAG, "Ignoring showMySoftInput due to an invalid token. uid:"
2616 + uid + " token:" + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002617 return;
2618 }
2619 long ident = Binder.clearCallingIdentity();
2620 try {
2621 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002622 } finally {
2623 Binder.restoreCallingIdentity(ident);
2624 }
2625 }
2626 }
2627
2628 void setEnabledSessionInMainThread(SessionState session) {
2629 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002630 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002631 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002632 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002633 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002634 } catch (RemoteException e) {
2635 }
2636 }
2637 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002638 if (mEnabledSession != null && mEnabledSession.session != null) {
2639 try {
2640 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
2641 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
2642 } catch (RemoteException e) {
2643 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002644 }
2645 }
2646 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002647
satok42c5a162011-05-26 16:46:14 +09002648 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002649 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002650 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002651 switch (msg.what) {
satokab751aa2010-09-14 19:17:36 +09002652 case MSG_SHOW_IM_SUBTYPE_PICKER:
Seigo Nonaka14e13912015-05-06 21:04:13 -07002653 final boolean showAuxSubtypes;
2654 switch (msg.arg1) {
2655 case InputMethodManager.SHOW_IM_PICKER_MODE_AUTO:
2656 // This is undocumented so far, but IMM#showInputMethodPicker() has been
2657 // implemented so that auxiliary subtypes will be excluded when the soft
2658 // keyboard is invisible.
2659 showAuxSubtypes = mInputShown;
2660 break;
2661 case InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES:
2662 showAuxSubtypes = true;
2663 break;
2664 case InputMethodManager.SHOW_IM_PICKER_MODE_EXCLUDE_AUXILIARY_SUBTYPES:
2665 showAuxSubtypes = false;
2666 break;
2667 default:
2668 Slog.e(TAG, "Unknown subtype picker mode = " + msg.arg1);
2669 return false;
2670 }
2671 showInputMethodMenu(showAuxSubtypes);
satokab751aa2010-09-14 19:17:36 +09002672 return true;
2673
satok47a44912010-10-06 16:03:58 +09002674 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002675 args = (SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09002676 showInputMethodAndSubtypeEnabler((String)args.arg1);
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002677 args.recycle();
satok217f5482010-12-15 05:19:19 +09002678 return true;
2679
2680 case MSG_SHOW_IM_CONFIG:
2681 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002682 return true;
2683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002684 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002685
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002686 case MSG_UNBIND_INPUT:
2687 try {
2688 ((IInputMethod)msg.obj).unbindInput();
2689 } catch (RemoteException e) {
2690 // There is nothing interesting about the method dying.
2691 }
2692 return true;
2693 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002694 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002695 try {
2696 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2697 } catch (RemoteException e) {
2698 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002699 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002700 return true;
2701 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002702 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002703 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002704 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002705 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002706 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002707 } catch (RemoteException e) {
2708 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002709 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002710 return true;
2711 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002712 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002713 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002714 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002715 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002716 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002717 } catch (RemoteException e) {
2718 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002719 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002720 return true;
2721 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002722 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002723 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002724 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002725 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2726 } catch (RemoteException e) {
2727 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002728 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002729 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002730 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002731 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002732 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002733 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002734 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002735 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002736 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002737 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002738 // Dispose the channel if the input method is not local to this process
2739 // because the remote proxy will get its own copy when unparceled.
2740 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002741 channel.dispose();
2742 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002743 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002744 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002745 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002746 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002747 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002748
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002749 case MSG_START_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002750 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002751 try {
2752 SessionState session = (SessionState)args.arg1;
2753 setEnabledSessionInMainThread(session);
2754 session.method.startInput((IInputContext)args.arg2,
2755 (EditorInfo)args.arg3);
2756 } catch (RemoteException e) {
2757 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002758 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002759 return true;
2760 case MSG_RESTART_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002761 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002762 try {
2763 SessionState session = (SessionState)args.arg1;
2764 setEnabledSessionInMainThread(session);
2765 session.method.restartInput((IInputContext)args.arg2,
2766 (EditorInfo)args.arg3);
2767 } catch (RemoteException e) {
2768 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002769 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002770 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002771
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002772 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002773
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002774 case MSG_UNBIND_METHOD:
2775 try {
2776 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
2777 } catch (RemoteException e) {
2778 // There is nothing interesting about the last client dying.
2779 }
2780 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002781 case MSG_BIND_METHOD: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002782 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002783 IInputMethodClient client = (IInputMethodClient)args.arg1;
2784 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002785 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002786 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002787 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002788 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002789 } finally {
2790 // Dispose the channel if the input method is not local to this process
2791 // because the remote proxy will get its own copy when unparceled.
2792 if (res.channel != null && Binder.isProxy(client)) {
2793 res.channel.dispose();
2794 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002795 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002796 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002797 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002798 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002799 case MSG_SET_ACTIVE:
2800 try {
2801 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2802 } catch (RemoteException e) {
2803 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2804 + ((ClientState)msg.obj).pid + " uid "
2805 + ((ClientState)msg.obj).uid);
2806 }
2807 return true;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002808 case MSG_SET_INTERACTIVE:
2809 handleSetInteractive(msg.arg1 != 0);
2810 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002811 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
2812 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07002813 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002814 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07002815 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002816 } catch (RemoteException e) {
2817 Slog.w(TAG, "Got RemoteException sending "
2818 + "setUserActionNotificationSequenceNumber("
2819 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07002820 + clientState.pid + " uid "
2821 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002822 }
2823 return true;
2824 }
satok01038492012-04-09 21:08:27 +09002825
2826 // --------------------------------------------------------------
2827 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07002828 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09002829 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002830 }
2831 return false;
2832 }
2833
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002834 private void handleSetInteractive(final boolean interactive) {
2835 synchronized (mMethodMap) {
2836 mIsInteractive = interactive;
2837 updateSystemUiLocked(mCurToken, interactive ? mImeWindowVis : 0, mBackDisposition);
2838
2839 // Inform the current client of the change in active status
2840 if (mCurClient != null && mCurClient.client != null) {
2841 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
2842 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, mCurClient));
2843 }
2844 }
2845 }
2846
satokdc9ddae2011-10-06 12:22:36 +09002847 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002848 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
2849 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09002850 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002851 if (DEBUG) {
2852 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2853 }
satok723a27e2010-11-11 14:58:11 +09002854 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002855 return true;
2856 }
2857
2858 return false;
2859 }
2860
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002861 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002862 HashMap<String, InputMethodInfo> map, boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002863 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002864 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07002865 + " \n ------ caller=" + Debug.getCallers(10));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002866 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002867 list.clear();
2868 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002869
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002870 // Use for queryIntentServicesAsUser
2871 final PackageManager pm = mContext.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002872
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002873 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002874 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002875 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2876 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002877
satoke7c6998e2011-06-03 17:57:59 +09002878 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2879 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002880 for (int i = 0; i < services.size(); ++i) {
2881 ResolveInfo ri = services.get(i);
2882 ServiceInfo si = ri.serviceInfo;
2883 ComponentName compName = new ComponentName(si.packageName, si.name);
2884 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2885 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002886 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002887 + ": it does not require the permission "
2888 + android.Manifest.permission.BIND_INPUT_METHOD);
2889 continue;
2890 }
2891
Joe Onorato8a9b2202010-02-26 18:56:32 -08002892 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002893
2894 try {
satoke7c6998e2011-06-03 17:57:59 +09002895 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002896 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002897 final String id = p.getId();
2898 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002899
2900 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002901 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002902 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07002903 } catch (XmlPullParserException | IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002904 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002905 }
2906 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002907
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002908 if (resetDefaultEnabledIme) {
2909 final ArrayList<InputMethodInfo> defaultEnabledIme =
2910 InputMethodUtils.getDefaultEnabledImes(mContext, mSystemReady, list);
2911 for (int i = 0; i < defaultEnabledIme.size(); ++i) {
2912 final InputMethodInfo imi = defaultEnabledIme.get(i);
2913 if (DEBUG) {
2914 Slog.d(TAG, "--- enable ime = " + imi);
2915 }
2916 setInputMethodEnabledLocked(imi.getId(), true);
2917 }
2918 }
2919
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002920 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09002921 if (!TextUtils.isEmpty(defaultImiId)) {
2922 if (!map.containsKey(defaultImiId)) {
2923 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
2924 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07002925 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09002926 }
2927 } else {
2928 // Double check that the default IME is certainly enabled.
2929 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002930 }
2931 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09002932 // Here is not the perfect place to reset the switching controller. Ideally
2933 // mSwitchingController and mSettings should be able to share the same state.
2934 // TODO: Make sure that mSwitchingController and mSettings are sharing the
2935 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09002936 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002937 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002938
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002939 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002940
satok217f5482010-12-15 05:19:19 +09002941 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002942 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002943 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002944 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2945 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002946 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002947 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002948 }
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002949 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok217f5482010-12-15 05:19:19 +09002950 }
2951
2952 private void showConfigureInputMethods() {
2953 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2954 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2955 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2956 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002957 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09002958 }
2959
satok2c93efc2012-04-02 19:33:47 +09002960 private boolean isScreenLocked() {
2961 return mKeyguardManager != null
2962 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
2963 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002964
Seigo Nonaka14e13912015-05-06 21:04:13 -07002965 private void showInputMethodMenu(boolean showAuxSubtypes) {
2966 if (DEBUG) Slog.v(TAG, "Show switching menu. showAuxSubtypes=" + showAuxSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002967
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002968 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09002969 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002970
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002971 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002972 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002973 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002974
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002975 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002976 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09002977 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
2978 mContext);
satok7f35c8c2010-10-07 21:13:11 +09002979 if (immis == null || immis.size() == 0) {
2980 return;
2981 }
2982
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002983 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002984
satok688bd472012-02-09 20:09:17 +09002985 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002986 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
Seigo Nonaka14e13912015-05-06 21:04:13 -07002987 true /* showSubtypes */, showAuxSubtypes, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09002988
satokc3690562012-01-10 20:14:43 +09002989 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002990 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09002991 if (currentSubtype != null) {
2992 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002993 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
2994 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09002995 }
2996 }
2997
Ken Wakasa761eb372011-03-04 19:06:18 +09002998 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09002999 mIms = new InputMethodInfo[N];
3000 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003001 int checkedItem = 0;
3002 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09003003 final ImeSubtypeListItem item = imList.get(i);
3004 mIms[i] = item.mImi;
3005 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003006 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09003007 int subtypeId = mSubtypeIds[i];
3008 if ((subtypeId == NOT_A_SUBTYPE_ID)
3009 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
3010 || (subtypeId == lastInputMethodSubtypeId)) {
3011 checkedItem = i;
3012 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003013 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003014 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08003015
3016 final Context settingsContext = new ContextThemeWrapper(context,
3017 com.android.internal.R.style.Theme_DeviceDefault_Settings);
3018
3019 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003020 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
3021 @Override
3022 public void onCancel(DialogInterface dialog) {
3023 hideInputMethodMenu();
3024 }
3025 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08003026
3027 final Context dialogContext = mDialogBuilder.getContext();
3028 final TypedArray a = dialogContext.obtainStyledAttributes(null,
3029 com.android.internal.R.styleable.DialogPreference,
3030 com.android.internal.R.attr.alertDialogStyle, 0);
3031 final Drawable dialogIcon = a.getDrawable(
3032 com.android.internal.R.styleable.DialogPreference_dialogIcon);
3033 a.recycle();
3034
3035 mDialogBuilder.setIcon(dialogIcon);
3036
3037 final LayoutInflater inflater = (LayoutInflater) dialogContext.getSystemService(
3038 Context.LAYOUT_INFLATER_SERVICE);
satok01038492012-04-09 21:08:27 +09003039 final View tv = inflater.inflate(
3040 com.android.internal.R.layout.input_method_switch_dialog_title, null);
3041 mDialogBuilder.setCustomTitle(tv);
3042
3043 // Setup layout for a toggle switch of the hardware keyboard
3044 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003045 mSwitchingDialogTitleView
3046 .findViewById(com.android.internal.R.id.hard_keyboard_section)
Seigo Nonaka7309b122015-08-17 18:34:13 -07003047 .setVisibility(mWindowManagerInternal.isHardKeyboardAvailable()
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003048 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003049 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003050 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07003051 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003052 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
3053 @Override
3054 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003055 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003056 // Ensure that the input method dialog is dismissed when changing
3057 // the hardware keyboard state.
3058 hideInputMethodMenu();
3059 }
3060 });
3061
Alan Viverette505e3ab2014-11-24 15:22:11 -08003062 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003063 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
3064 final OnClickListener choiceListener = new OnClickListener() {
3065 @Override
3066 public void onClick(final DialogInterface dialog, final int which) {
3067 synchronized (mMethodMap) {
3068 if (mIms == null || mIms.length <= which || mSubtypeIds == null
3069 || mSubtypeIds.length <= which) {
3070 return;
satok01038492012-04-09 21:08:27 +09003071 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003072 final InputMethodInfo im = mIms[which];
3073 int subtypeId = mSubtypeIds[which];
3074 adapter.mCheckedItem = which;
3075 adapter.notifyDataSetChanged();
3076 hideInputMethodMenu();
3077 if (im != null) {
3078 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
3079 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003080 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003081 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003082 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003083 }
3084 }
3085 };
3086 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003087
Seigo Nonakad4474cb2015-05-04 16:53:24 -07003088 if (!isScreenLocked) {
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003089 final OnClickListener positiveListener = new OnClickListener() {
3090 @Override
3091 public void onClick(DialogInterface dialog, int whichButton) {
3092 showConfigureInputMethods();
3093 }
3094 };
satok82beadf2010-12-27 19:03:06 +09003095 mDialogBuilder.setPositiveButton(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003096 com.android.internal.R.string.configure_input_methods, positiveListener);
satok7f35c8c2010-10-07 21:13:11 +09003097 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003098 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003099 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003100 mSwitchingDialog.getWindow().setType(
3101 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09003102 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
3103 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003104 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003105 updateSystemUi(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003106 mSwitchingDialog.show();
3107 }
3108 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003109
Ken Wakasa05dbb652011-08-22 15:22:43 +09003110 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
3111 private final LayoutInflater mInflater;
3112 private final int mTextViewResourceId;
3113 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09003114 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09003115 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
3116 List<ImeSubtypeListItem> itemsList, int checkedItem) {
3117 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003118
Ken Wakasa05dbb652011-08-22 15:22:43 +09003119 mTextViewResourceId = textViewResourceId;
3120 mItemsList = itemsList;
3121 mCheckedItem = checkedItem;
Alan Viverette505e3ab2014-11-24 15:22:11 -08003122 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Ken Wakasa05dbb652011-08-22 15:22:43 +09003123 }
3124
3125 @Override
3126 public View getView(int position, View convertView, ViewGroup parent) {
3127 final View view = convertView != null ? convertView
3128 : mInflater.inflate(mTextViewResourceId, null);
3129 if (position < 0 || position >= mItemsList.size()) return view;
3130 final ImeSubtypeListItem item = mItemsList.get(position);
3131 final CharSequence imeName = item.mImeName;
3132 final CharSequence subtypeName = item.mSubtypeName;
3133 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
3134 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
3135 if (TextUtils.isEmpty(subtypeName)) {
3136 firstTextView.setText(imeName);
3137 secondTextView.setVisibility(View.GONE);
3138 } else {
3139 firstTextView.setText(subtypeName);
3140 secondTextView.setText(imeName);
3141 secondTextView.setVisibility(View.VISIBLE);
3142 }
3143 final RadioButton radioButton =
3144 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
3145 radioButton.setChecked(position == mCheckedItem);
3146 return view;
3147 }
3148 }
3149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003150 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003151 synchronized (mMethodMap) {
3152 hideInputMethodMenuLocked();
3153 }
3154 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003155
The Android Open Source Project10592532009-03-18 17:39:46 -07003156 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003157 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003158
The Android Open Source Project10592532009-03-18 17:39:46 -07003159 if (mSwitchingDialog != null) {
3160 mSwitchingDialog.dismiss();
3161 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003162 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003163
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003164 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project10592532009-03-18 17:39:46 -07003165 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003166 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003167 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003169 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003170
satok42c5a162011-05-26 16:46:14 +09003171 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003172 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003173 // TODO: Make this work even for non-current users?
3174 if (!calledFromValidUser()) {
3175 return false;
3176 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003177 synchronized (mMethodMap) {
3178 if (mContext.checkCallingOrSelfPermission(
3179 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3180 != PackageManager.PERMISSION_GRANTED) {
3181 throw new SecurityException(
3182 "Requires permission "
3183 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3184 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003186 long ident = Binder.clearCallingIdentity();
3187 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003188 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003189 } finally {
3190 Binder.restoreCallingIdentity(ident);
3191 }
3192 }
3193 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003194
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003195 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3196 // Make sure this is a valid input method.
3197 InputMethodInfo imm = mMethodMap.get(id);
3198 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003199 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003200 }
3201
satokd87c2592010-09-29 11:52:06 +09003202 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3203 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003204
satokd87c2592010-09-29 11:52:06 +09003205 if (enabled) {
3206 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3207 if (pair.first.equals(id)) {
3208 // We are enabling this input method, but it is already enabled.
3209 // Nothing to do. The previous state was enabled.
3210 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003211 }
3212 }
satokd87c2592010-09-29 11:52:06 +09003213 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3214 // Previous state was disabled.
3215 return false;
3216 } else {
3217 StringBuilder builder = new StringBuilder();
3218 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3219 builder, enabledInputMethodsList, id)) {
3220 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003221 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003222 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3223 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3224 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003225 }
3226 // Previous state was enabled.
3227 return true;
3228 } else {
3229 // We are disabling the input method but it is already disabled.
3230 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003231 return false;
3232 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003233 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003234 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003235
satok723a27e2010-11-11 14:58:11 +09003236 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3237 boolean setSubtypeOnly) {
3238 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003239 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003240
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003241 mCurUserActionNotificationSequenceNumber =
3242 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3243 if (DEBUG) {
3244 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3245 + mCurUserActionNotificationSequenceNumber);
3246 }
3247
3248 if (mCurClient != null && mCurClient.client != null) {
3249 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3250 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003251 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003252 }
3253
satok723a27e2010-11-11 14:58:11 +09003254 // Set Subtype here
3255 if (imi == null || subtypeId < 0) {
3256 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003257 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003258 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003259 if (subtypeId < imi.getSubtypeCount()) {
3260 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3261 mSettings.putSelectedSubtype(subtype.hashCode());
3262 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003263 } else {
3264 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003265 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003266 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003267 }
satokab751aa2010-09-14 19:17:36 +09003268 }
satok723a27e2010-11-11 14:58:11 +09003269
satok4c0e7152012-06-20 20:08:44 +09003270 // Workaround.
3271 // ASEC is not ready in the IMMS constructor. Accordingly, forward-locked
3272 // IMEs are not recognized and considered uninstalled.
3273 // Actually, we can't move everything after SystemReady because
3274 // IMMS needs to run in the encryption lock screen. So, we just skip changing
3275 // the default IME here and try cheking the default IME again in systemReady().
3276 // TODO: Do nothing before system ready and implement a separated logic for
3277 // the encryption lock screen.
3278 // TODO: ASEC should be ready before IMMS is instantiated.
3279 if (mSystemReady && !setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003280 // Set InputMethod here
3281 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3282 }
3283 }
3284
3285 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3286 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3287 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3288 // newDefaultIme is empty when there is no candidate for the selected IME.
3289 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3290 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3291 if (subtypeHashCode != null) {
3292 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003293 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok723a27e2010-11-11 14:58:11 +09003294 imi, Integer.valueOf(subtypeHashCode));
3295 } catch (NumberFormatException e) {
3296 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3297 }
3298 }
3299 }
3300 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003301 }
3302
satok4e4569d2010-11-19 18:45:53 +09003303 // If there are no selected shortcuts, tries finding the most applicable ones.
3304 private Pair<InputMethodInfo, InputMethodSubtype>
3305 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3306 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3307 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003308 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003309 boolean foundInSystemIME = false;
3310
3311 // Search applicable subtype for each InputMethodInfo
3312 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003313 final String imiId = imi.getId();
3314 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3315 continue;
3316 }
satokcd7cd292010-11-20 15:46:23 +09003317 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003318 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003319 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003320 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003321 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003322 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003323 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003324 }
satokdf31ae62011-01-15 06:19:44 +09003325 // 2. Search by the system locale from enabledSubtypes.
3326 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003327 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003328 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003329 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003330 }
satoka86f5e42011-09-02 17:12:42 +09003331 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003332 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003333 final ArrayList<InputMethodSubtype> subtypesForSearch =
3334 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003335 ? InputMethodUtils.getSubtypes(imi)
3336 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003337 // 4. Search by the current subtype's locale from all subtypes.
3338 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003339 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003340 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003341 }
3342 // 5. Search by the system locale from all subtypes.
3343 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003344 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003345 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003346 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003347 }
satokcd7cd292010-11-20 15:46:23 +09003348 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003349 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003350 // The current input method is the most applicable IME.
3351 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003352 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003353 break;
satok7599a7f2010-12-22 13:45:23 +09003354 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003355 // The system input method is 2nd applicable IME.
3356 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003357 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003358 if ((imi.getServiceInfo().applicationInfo.flags
3359 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3360 foundInSystemIME = true;
3361 }
satok4e4569d2010-11-19 18:45:53 +09003362 }
3363 }
3364 }
3365 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003366 if (mostApplicableIMI != null) {
3367 Slog.w(TAG, "Most applicable shortcut input method was:"
3368 + mostApplicableIMI.getId());
3369 if (mostApplicableSubtype != null) {
3370 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3371 + "," + mostApplicableSubtype.getMode() + ","
3372 + mostApplicableSubtype.getLocale());
3373 }
3374 }
satok4e4569d2010-11-19 18:45:53 +09003375 }
satokcd7cd292010-11-20 15:46:23 +09003376 if (mostApplicableIMI != null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003377 return new Pair<> (mostApplicableIMI, mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003378 } else {
3379 return null;
3380 }
3381 }
3382
satokab751aa2010-09-14 19:17:36 +09003383 /**
3384 * @return Return the current subtype of this input method.
3385 */
satok42c5a162011-05-26 16:46:14 +09003386 @Override
satokab751aa2010-09-14 19:17:36 +09003387 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003388 // TODO: Make this work even for non-current users?
3389 if (!calledFromValidUser()) {
3390 return null;
3391 }
3392 synchronized (mMethodMap) {
3393 return getCurrentInputMethodSubtypeLocked();
3394 }
3395 }
3396
3397 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003398 if (mCurMethodId == null) {
3399 return null;
3400 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003401 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003402 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3403 if (imi == null || imi.getSubtypeCount() == 0) {
3404 return null;
satok4e4569d2010-11-19 18:45:53 +09003405 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003406 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003407 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3408 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003409 if (subtypeId == NOT_A_SUBTYPE_ID) {
3410 // If there are no selected subtypes, the framework will try to find
3411 // the most applicable subtype from explicitly or implicitly enabled
3412 // subtypes.
3413 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003414 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003415 // If there is only one explicitly or implicitly enabled subtype,
3416 // just returns it.
3417 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3418 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3419 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003420 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003421 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003422 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003423 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003424 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003425 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3426 true);
satok4e4569d2010-11-19 18:45:53 +09003427 }
satok3ef8b292010-11-23 06:06:29 +09003428 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003429 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003430 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003431 }
3432 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003433 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003434 }
3435
satok4e4569d2010-11-19 18:45:53 +09003436 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003437 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003438 @Override
satok4e4569d2010-11-19 18:45:53 +09003439 public List getShortcutInputMethodsAndSubtypes() {
3440 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003441 ArrayList<Object> ret = new ArrayList<>();
satokf3db1af2010-11-23 13:34:33 +09003442 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003443 // If there are no selected shortcut subtypes, the framework will try to find
3444 // the most applicable subtype from all subtypes whose mode is
3445 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003446 Pair<InputMethodInfo, InputMethodSubtype> info =
3447 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003448 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003449 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003450 ret.add(info.first);
3451 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003452 }
satok3da92232011-01-11 22:46:30 +09003453 return ret;
satokf3db1af2010-11-23 13:34:33 +09003454 }
satokf3db1af2010-11-23 13:34:33 +09003455 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3456 ret.add(imi);
3457 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3458 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003459 }
3460 }
satokf3db1af2010-11-23 13:34:33 +09003461 return ret;
satok4e4569d2010-11-19 18:45:53 +09003462 }
3463 }
3464
satok42c5a162011-05-26 16:46:14 +09003465 @Override
satokb66d2872010-11-10 01:04:04 +09003466 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003467 // TODO: Make this work even for non-current users?
3468 if (!calledFromValidUser()) {
3469 return false;
3470 }
satokb66d2872010-11-10 01:04:04 +09003471 synchronized (mMethodMap) {
3472 if (subtype != null && mCurMethodId != null) {
3473 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003474 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003475 if (subtypeId != NOT_A_SUBTYPE_ID) {
3476 setInputMethodLocked(mCurMethodId, subtypeId);
3477 return true;
3478 }
3479 }
3480 return false;
3481 }
3482 }
3483
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003484 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003485 private static class InputMethodFileManager {
3486 private static final String SYSTEM_PATH = "system";
3487 private static final String INPUT_METHOD_PATH = "inputmethod";
3488 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3489 private static final String NODE_SUBTYPES = "subtypes";
3490 private static final String NODE_SUBTYPE = "subtype";
3491 private static final String NODE_IMI = "imi";
3492 private static final String ATTR_ID = "id";
3493 private static final String ATTR_LABEL = "label";
3494 private static final String ATTR_ICON = "icon";
3495 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3496 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3497 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3498 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3499 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3500 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003501 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003502 new HashMap<>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003503 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003504 if (methodMap == null) {
3505 throw new NullPointerException("methodMap is null");
3506 }
3507 mMethodMap = methodMap;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003508 final File systemDir = userId == UserHandle.USER_OWNER
3509 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3510 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003511 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
Yohei Yukawadf5af482015-08-04 22:11:11 -07003512 if (!inputMethodDir.exists() && !inputMethodDir.mkdirs()) {
satoke7c6998e2011-06-03 17:57:59 +09003513 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3514 }
3515 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3516 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3517 if (!subtypeFile.exists()) {
3518 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003519 writeAdditionalInputMethodSubtypes(
3520 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003521 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003522 readAdditionalInputMethodSubtypes(
3523 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003524 }
3525 }
3526
3527 private void deleteAllInputMethodSubtypes(String imiId) {
3528 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003529 mAdditionalSubtypesMap.remove(imiId);
3530 writeAdditionalInputMethodSubtypes(
3531 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003532 }
3533 }
3534
3535 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003536 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003537 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003538 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003539 final int N = additionalSubtypes.length;
3540 for (int i = 0; i < N; ++i) {
3541 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003542 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003543 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003544 } else {
3545 Slog.w(TAG, "Duplicated subtype definition found: "
3546 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003547 }
3548 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003549 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3550 writeAdditionalInputMethodSubtypes(
3551 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003552 }
3553 }
3554
3555 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3556 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003557 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003558 }
3559 }
3560
3561 private static void writeAdditionalInputMethodSubtypes(
3562 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3563 HashMap<String, InputMethodInfo> methodMap) {
3564 // Safety net for the case that this function is called before methodMap is set.
3565 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3566 FileOutputStream fos = null;
3567 try {
3568 fos = subtypesFile.startWrite();
3569 final XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003570 out.setOutput(fos, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003571 out.startDocument(null, true);
3572 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3573 out.startTag(null, NODE_SUBTYPES);
3574 for (String imiId : allSubtypes.keySet()) {
3575 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3576 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3577 continue;
3578 }
3579 out.startTag(null, NODE_IMI);
3580 out.attribute(null, ATTR_ID, imiId);
3581 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3582 final int N = subtypesList.size();
3583 for (int i = 0; i < N; ++i) {
3584 final InputMethodSubtype subtype = subtypesList.get(i);
3585 out.startTag(null, NODE_SUBTYPE);
3586 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3587 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3588 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3589 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3590 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3591 out.attribute(null, ATTR_IS_AUXILIARY,
3592 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3593 out.endTag(null, NODE_SUBTYPE);
3594 }
3595 out.endTag(null, NODE_IMI);
3596 }
3597 out.endTag(null, NODE_SUBTYPES);
3598 out.endDocument();
3599 subtypesFile.finishWrite(fos);
3600 } catch (java.io.IOException e) {
3601 Slog.w(TAG, "Error writing subtypes", e);
3602 if (fos != null) {
3603 subtypesFile.failWrite(fos);
3604 }
3605 }
3606 }
3607
3608 private static void readAdditionalInputMethodSubtypes(
3609 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3610 if (allSubtypes == null || subtypesFile == null) return;
3611 allSubtypes.clear();
Yohei Yukawa5894b432015-08-11 13:29:24 -07003612 try (final FileInputStream fis = subtypesFile.openRead()) {
satoke7c6998e2011-06-03 17:57:59 +09003613 final XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003614 parser.setInput(fis, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003615 int type = parser.getEventType();
3616 // Skip parsing until START_TAG
3617 while ((type = parser.next()) != XmlPullParser.START_TAG
3618 && type != XmlPullParser.END_DOCUMENT) {}
3619 String firstNodeName = parser.getName();
3620 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3621 throw new XmlPullParserException("Xml doesn't start with subtypes");
3622 }
3623 final int depth =parser.getDepth();
3624 String currentImiId = null;
3625 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3626 while (((type = parser.next()) != XmlPullParser.END_TAG
3627 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3628 if (type != XmlPullParser.START_TAG)
3629 continue;
3630 final String nodeName = parser.getName();
3631 if (NODE_IMI.equals(nodeName)) {
3632 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3633 if (TextUtils.isEmpty(currentImiId)) {
3634 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3635 continue;
3636 }
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003637 tempSubtypesArray = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003638 allSubtypes.put(currentImiId, tempSubtypesArray);
3639 } else if (NODE_SUBTYPE.equals(nodeName)) {
3640 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3641 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3642 continue;
3643 }
3644 final int icon = Integer.valueOf(
3645 parser.getAttributeValue(null, ATTR_ICON));
3646 final int label = Integer.valueOf(
3647 parser.getAttributeValue(null, ATTR_LABEL));
3648 final String imeSubtypeLocale =
3649 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3650 final String imeSubtypeMode =
3651 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3652 final String imeSubtypeExtraValue =
3653 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003654 final boolean isAuxiliary = "1".equals(String.valueOf(
3655 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003656 final InputMethodSubtype subtype = new InputMethodSubtypeBuilder()
3657 .setSubtypeNameResId(label)
3658 .setSubtypeIconResId(icon)
3659 .setSubtypeLocale(imeSubtypeLocale)
3660 .setSubtypeMode(imeSubtypeMode)
3661 .setSubtypeExtraValue(imeSubtypeExtraValue)
3662 .setIsAuxiliary(isAuxiliary)
3663 .build();
satoke7c6998e2011-06-03 17:57:59 +09003664 tempSubtypesArray.add(subtype);
3665 }
3666 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07003667 } catch (XmlPullParserException | IOException | NumberFormatException e) {
3668 Slog.w(TAG, "Error reading subtypes", e);
satoke7c6998e2011-06-03 17:57:59 +09003669 return;
satoke7c6998e2011-06-03 17:57:59 +09003670 }
3671 }
3672 }
3673
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003674 private static final class LocalServiceImpl implements InputMethodManagerInternal {
3675 @NonNull
3676 private final Handler mHandler;
3677
3678 LocalServiceImpl(@NonNull final Handler handler) {
3679 mHandler = handler;
3680 }
3681
3682 @Override
3683 public void setInteractive(boolean interactive) {
3684 // Do everything in handler so as not to block the caller.
3685 mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_INTERACTIVE,
3686 interactive ? 1 : 0, 0));
3687 }
3688 }
3689
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003690 @Override
3691 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3692 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3693 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003694
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003695 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3696 + Binder.getCallingPid()
3697 + ", uid=" + Binder.getCallingUid());
3698 return;
3699 }
3700
3701 IInputMethod method;
3702 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003703
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003704 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003706 synchronized (mMethodMap) {
3707 p.println("Current Input Method Manager state:");
3708 int N = mMethodList.size();
3709 p.println(" Input Methods:");
3710 for (int i=0; i<N; i++) {
3711 InputMethodInfo info = mMethodList.get(i);
3712 p.println(" InputMethod #" + i + ":");
3713 info.dump(p, " ");
3714 }
3715 p.println(" Clients:");
3716 for (ClientState ci : mClients.values()) {
3717 p.println(" Client " + ci + ":");
3718 p.println(" client=" + ci.client);
3719 p.println(" inputContext=" + ci.inputContext);
3720 p.println(" sessionRequested=" + ci.sessionRequested);
3721 p.println(" curSession=" + ci.curSession);
3722 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003723 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003724 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003725 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3726 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003727 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3728 + " mBoundToMethod=" + mBoundToMethod);
3729 p.println(" mCurToken=" + mCurToken);
3730 p.println(" mCurIntent=" + mCurIntent);
3731 method = mCurMethod;
3732 p.println(" mCurMethod=" + mCurMethod);
3733 p.println(" mEnabledSession=" + mEnabledSession);
3734 p.println(" mShowRequested=" + mShowRequested
3735 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3736 + " mShowForced=" + mShowForced
3737 + " mInputShown=" + mInputShown);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003738 p.println(" mCurUserActionNotificationSequenceNumber="
3739 + mCurUserActionNotificationSequenceNumber);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003740 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mIsInteractive);
Yohei Yukawa81482972015-06-04 00:58:59 -07003741 p.println(" mSettingsObserver=" + mSettingsObserver);
Yohei Yukawad7248862015-06-03 23:56:12 -07003742 p.println(" mSwitchingController:");
3743 mSwitchingController.dump(p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003744 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003745
Jeff Brownb88102f2010-09-08 11:49:43 -07003746 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003747 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003748 pw.flush();
3749 try {
3750 client.client.asBinder().dump(fd, args);
3751 } catch (RemoteException e) {
3752 p.println("Input method client dead: " + e);
3753 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003754 } else {
3755 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003756 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003757
Jeff Brownb88102f2010-09-08 11:49:43 -07003758 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003759 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003760 pw.flush();
3761 try {
3762 method.asBinder().dump(fd, args);
3763 } catch (RemoteException e) {
3764 p.println("Input method service dead: " + e);
3765 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003766 } else {
3767 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003768 }
3769 }
3770}