blob: 64ee5f1a1037c4ad13ca9978955c61716100e04a [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 * use this file except in compliance with the License. You may obtain a copy of
5 * the License at
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007 * http://www.apache.org/licenses/LICENSE-2.0
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 * License for the specific language governing permissions and limitations under
13 * the License.
14 */
15
16package com.android.server;
17
Yohei Yukawa0f3ad12015-04-06 16:48:24 -070018import android.annotation.NonNull;
Svet Ganovadc1cf42015-06-15 16:36:24 -070019import android.content.pm.PackageManagerInternal;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080020import com.android.internal.content.PackageMonitor;
Satoshi Kataokad7443c82013-10-15 17:45:43 +090021import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController;
Satoshi Kataokad787f692013-10-26 04:44:21 +090022import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController.ImeSubtypeListItem;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +090023import com.android.internal.inputmethod.InputMethodUtils;
24import com.android.internal.inputmethod.InputMethodUtils.InputMethodSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import com.android.internal.os.HandlerCaller;
Svetoslav Ganov758143e2012-08-06 16:40:27 -070026import com.android.internal.os.SomeArgs;
satoke7c6998e2011-06-03 17:57:59 +090027import com.android.internal.util.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import com.android.internal.view.IInputContext;
29import com.android.internal.view.IInputMethod;
Michael Wright52a53522013-03-14 10:59:38 -070030import com.android.internal.view.IInputSessionCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import com.android.internal.view.IInputMethodClient;
32import com.android.internal.view.IInputMethodManager;
33import com.android.internal.view.IInputMethodSession;
34import com.android.internal.view.InputBindResult;
Svet Ganovadc1cf42015-06-15 16:36:24 -070035import com.android.server.pm.UserManagerService;
Adam Lesinskief2ea1f2013-12-05 16:48:06 -080036import com.android.server.statusbar.StatusBarManagerService;
satok01038492012-04-09 21:08:27 +090037import com.android.server.wm.WindowManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038
satoke7c6998e2011-06-03 17:57:59 +090039import org.xmlpull.v1.XmlPullParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import org.xmlpull.v1.XmlPullParserException;
satoke7c6998e2011-06-03 17:57:59 +090041import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042
43import android.app.ActivityManagerNative;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090044import android.app.AppGlobals;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.app.AlertDialog;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +090046import android.app.AppOpsManager;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090047import android.app.IUserSwitchObserver;
satokf90a33e2011-07-19 11:55:52 +090048import android.app.KeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +090049import android.app.Notification;
50import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070051import android.app.PendingIntent;
satok5b927c432012-05-01 20:09:34 +090052import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.content.ComponentName;
54import android.content.ContentResolver;
55import android.content.Context;
56import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057import android.content.DialogInterface.OnCancelListener;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +090058import android.content.DialogInterface.OnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090060import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070062import android.content.pm.ApplicationInfo;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090063import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.content.pm.PackageManager;
65import android.content.pm.ResolveInfo;
66import android.content.pm.ServiceInfo;
Amith Yamasani734983f2014-03-04 16:48:05 -080067import android.content.pm.UserInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070068import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.content.res.Resources;
70import android.content.res.TypedArray;
71import android.database.ContentObserver;
Alan Viverette505e3ab2014-11-24 15:22:11 -080072import android.graphics.drawable.Drawable;
Joe Onorato857fd9b2011-01-27 15:08:35 -080073import android.inputmethodservice.InputMethodService;
Michael Wright7b5a96b2014-08-09 19:28:42 -070074import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.os.Binder;
Chris Wren1ce4b6d2015-06-11 10:19:43 -040076import android.os.Bundle;
satoke7c6998e2011-06-03 17:57:59 +090077import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078import android.os.Handler;
79import android.os.IBinder;
80import android.os.IInterface;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090081import android.os.IRemoteCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082import android.os.Message;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090083import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084import android.os.Parcel;
85import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080086import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087import android.os.ServiceManager;
88import android.os.SystemClock;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090089import android.os.UserHandle;
Amith Yamasani734983f2014-03-04 16:48:05 -080090import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091import android.provider.Settings;
92import android.text.TextUtils;
Christopher Tate7b9a28c2015-03-18 13:06:16 -070093import android.text.TextUtils.SimpleStringSplitter;
satokf9f01002011-05-19 21:31:50 +090094import android.text.style.SuggestionSpan;
Christopher Tate7b9a28c2015-03-18 13:06:16 -070095import android.util.ArrayMap;
96import android.util.ArraySet;
Dianne Hackborn39606a02012-07-31 17:54:35 -070097import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +090099import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +0900100import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101import android.util.PrintWriterPrinter;
102import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +0900103import android.util.Slog;
104import android.util.Xml;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +0900105import android.view.ContextThemeWrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106import android.view.IWindowManager;
Jeff Brownc28867a2013-03-26 15:42:39 -0700107import android.view.InputChannel;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900108import android.view.LayoutInflater;
109import android.view.View;
110import android.view.ViewGroup;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111import android.view.WindowManager;
satokab751aa2010-09-14 19:17:36 +0900112import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113import android.view.inputmethod.InputBinding;
114import android.view.inputmethod.InputMethod;
115import android.view.inputmethod.InputMethodInfo;
116import android.view.inputmethod.InputMethodManager;
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;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700137import java.util.HashSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138import java.util.List;
satok5b927c432012-05-01 20:09:34 +0900139import java.util.Locale;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140
141/**
142 * This class provides a system service that manages input methods.
143 */
144public class InputMethodManagerService extends IInputMethodManager.Stub
145 implements ServiceConnection, Handler.Callback {
146 static final boolean DEBUG = false;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700147 static final boolean DEBUG_RESTORE = DEBUG || false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700148 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700150 private static final char INPUT_METHOD_SEPARATOR = ':';
151 private static final char INPUT_METHOD_SUBTYPE_SEPARATOR = ';';
152
Seigo Nonakad4474cb2015-05-04 16:53:24 -0700153 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 1;
154 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 2;
155 static final int MSG_SHOW_IM_CONFIG = 3;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 static final int MSG_UNBIND_INPUT = 1000;
158 static final int MSG_BIND_INPUT = 1010;
159 static final int MSG_SHOW_SOFT_INPUT = 1020;
160 static final int MSG_HIDE_SOFT_INPUT = 1030;
161 static final int MSG_ATTACH_TOKEN = 1040;
162 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 static final int MSG_START_INPUT = 2000;
165 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 static final int MSG_UNBIND_METHOD = 3000;
168 static final int MSG_BIND_METHOD = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700169 static final int MSG_SET_ACTIVE = 3020;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700170 static final int MSG_SET_INTERACTIVE = 3030;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900171 static final int MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER = 3040;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800172
satok01038492012-04-09 21:08:27 +0900173 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
174
Satoshi Kataokabcacc322013-10-21 17:57:27 -0700175 static final long TIME_TO_RECONNECT = 3 * 1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800176
satokf9f01002011-05-19 21:31:50 +0900177 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
178
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900179 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
satokb6359412011-06-28 17:47:41 +0900180 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900181
satok4e4569d2010-11-19 18:45:53 +0900182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800184 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900186 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 final IWindowManager mIWindowManager;
189 final HandlerCaller mCaller;
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700190 final boolean mHasFeature;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900191 private InputMethodFileManager mFileManager;
satok01038492012-04-09 21:08:27 +0900192 private final HardKeyboardListener mHardKeyboardListener;
193 private final WindowManagerService mWindowManagerService;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900194 private final AppOpsManager mAppOpsManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800195
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900196 final InputBindResult mNoBinding = new InputBindResult(null, null, null, -1, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 // All known input methods. mMethodMap also serves as the global
199 // lock for this class.
satokd87c2592010-09-29 11:52:06 +0900200 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<InputMethodInfo>();
201 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<String, InputMethodInfo>();
satokf9f01002011-05-19 21:31:50 +0900202 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
203 new LruCache<SuggestionSpan, InputMethodInfo>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Satoshi Kataokad787f692013-10-26 04:44:21 +0900204 private final InputMethodSubtypeSwitchingController mSwitchingController;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800205
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700206 // Used to bring IME service up to visible adjustment while it is being shown.
207 final ServiceConnection mVisibleConnection = new ServiceConnection() {
208 @Override public void onServiceConnected(ComponentName name, IBinder service) {
209 }
210
211 @Override public void onServiceDisconnected(ComponentName name) {
212 }
213 };
214 boolean mVisibleBound = false;
215
satok7cfc0ed2011-06-20 21:29:36 +0900216 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700217 private NotificationManager mNotificationManager;
218 private KeyguardManager mKeyguardManager;
219 private StatusBarManagerService mStatusBar;
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400220 private Notification.Builder mImeSwitcherNotification;
Dianne Hackborn661cd522011-08-22 00:26:20 -0700221 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900222 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900223 private boolean mNotificationShown;
satok0a1bcf42012-05-16 19:26:31 +0900224 private final boolean mImeSelectedOnBoot;
satok7cfc0ed2011-06-20 21:29:36 +0900225
Tadashi G. Takaoka8c6d4772014-08-05 15:29:17 +0900226 static class SessionState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 final ClientState client;
228 final IInputMethod method;
Jeff Brownc28867a2013-03-26 15:42:39 -0700229
230 IInputMethodSession session;
231 InputChannel channel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 @Override
234 public String toString() {
235 return "SessionState{uid " + client.uid + " pid " + client.pid
236 + " method " + Integer.toHexString(
237 System.identityHashCode(method))
238 + " session " + Integer.toHexString(
239 System.identityHashCode(session))
Jeff Brownc28867a2013-03-26 15:42:39 -0700240 + " channel " + channel
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 + "}";
242 }
243
244 SessionState(ClientState _client, IInputMethod _method,
Jeff Brownc28867a2013-03-26 15:42:39 -0700245 IInputMethodSession _session, InputChannel _channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 client = _client;
247 method = _method;
248 session = _session;
Jeff Brownc28867a2013-03-26 15:42:39 -0700249 channel = _channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 }
251 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800252
Jeff Brownc28867a2013-03-26 15:42:39 -0700253 static final class ClientState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 final IInputMethodClient client;
255 final IInputContext inputContext;
256 final int uid;
257 final int pid;
258 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 boolean sessionRequested;
261 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 @Override
264 public String toString() {
265 return "ClientState{" + Integer.toHexString(
266 System.identityHashCode(this)) + " uid " + uid
267 + " pid " + pid + "}";
268 }
269
270 ClientState(IInputMethodClient _client, IInputContext _inputContext,
271 int _uid, int _pid) {
272 client = _client;
273 inputContext = _inputContext;
274 uid = _uid;
275 pid = _pid;
276 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
277 }
278 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 final HashMap<IBinder, ClientState> mClients
281 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700284 * Set once the system is ready to run third party code.
285 */
286 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800287
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700288 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 * Id of the currently selected input method.
290 */
291 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 /**
294 * The current binding sequence number, incremented every time there is
295 * a new bind performed.
296 */
297 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 /**
300 * The client that is currently bound to an input method.
301 */
302 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800303
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700305 * The last window token that gained focus.
306 */
307 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800308
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700309 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310 * The input context last provided by the current client.
311 */
312 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800313
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 /**
315 * The attributes last provided by the current client.
316 */
317 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 /**
320 * The input method ID of the input method service that we are currently
321 * connected to or in the process of connecting to.
322 */
323 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800324
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 /**
satokab751aa2010-09-14 19:17:36 +0900326 * The current subtype of the current input method.
327 */
328 private InputMethodSubtype mCurrentSubtype;
329
satok4e4569d2010-11-19 18:45:53 +0900330 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900331 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
332 mShortcutInputMethodsAndSubtypes =
333 new HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>();
satokab751aa2010-09-14 19:17:36 +0900334
John Spurlocke0980502013-10-25 11:59:29 -0400335 // Was the keyguard locked when this client became current?
336 private boolean mCurClientInKeyguard;
337
satokab751aa2010-09-14 19:17:36 +0900338 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 * Set to true if our ServiceConnection is currently actively bound to
340 * a service (whether or not we have gotten its IBinder back yet).
341 */
342 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 /**
345 * Set if the client has asked for the input method to be shown.
346 */
347 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 /**
350 * Set if we were explicitly told to show the input method.
351 */
352 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 /**
355 * Set if we were forced to be shown.
356 */
357 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 /**
360 * Set if we last told the input method to show itself.
361 */
362 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 /**
365 * The Intent used to connect to the current input method.
366 */
367 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800368
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369 /**
370 * The token we have made for the currently active input method, to
371 * identify it in the future.
372 */
373 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 /**
376 * If non-null, this is the input method service we are currently connected
377 * to.
378 */
379 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800380
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 /**
382 * Time that we last initiated a bind to the input method, to determine
383 * if we should try to disconnect and reconnect to it.
384 */
385 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800386
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 /**
388 * Have we called mCurMethod.bindInput()?
389 */
390 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 /**
393 * Currently enabled session. Only touched by service thread, not
394 * protected by a lock.
395 */
396 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398 /**
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700399 * True if the device is currently interactive with user. The value is true initially.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 */
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700401 boolean mIsInteractive = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800402
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900403 int mCurUserActionNotificationSequenceNumber = 0;
404
Joe Onorato857fd9b2011-01-27 15:08:35 -0800405 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900406
407 /**
408 * A set of status bits regarding the active IME.
409 *
410 * <p>This value is a combination of following two bits:</p>
411 * <dl>
412 * <dt>{@link InputMethodService#IME_ACTIVE}</dt>
413 * <dd>
414 * If this bit is ON, connected IME is ready to accept touch/key events.
415 * </dd>
416 * <dt>{@link InputMethodService#IME_VISIBLE}</dt>
417 * <dd>
418 * If this bit is ON, some of IME view, e.g. software input, candidate view, is visible.
419 * </dd>
420 * </dl>
421 * <em>Do not update this value outside of setImeWindowStatus.</em>
422 */
Joe Onorato857fd9b2011-01-27 15:08:35 -0800423 int mImeWindowVis;
424
Ken Wakasa05dbb652011-08-22 15:22:43 +0900425 private AlertDialog.Builder mDialogBuilder;
426 private AlertDialog mSwitchingDialog;
satok01038492012-04-09 21:08:27 +0900427 private View mSwitchingDialogTitleView;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900428 private InputMethodInfo[] mIms;
429 private int[] mSubtypeIds;
satok5b927c432012-05-01 20:09:34 +0900430 private Locale mLastSystemLocale;
Michael Wright7b5a96b2014-08-09 19:28:42 -0700431 private boolean mShowImeWithHardKeyboard;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900432 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
433 private final IPackageManager mIPackageManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800434
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 class SettingsObserver extends ContentObserver {
Yohei Yukawa81482972015-06-04 00:58:59 -0700436 int mUserId;
437 boolean mRegistered = false;
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800438 String mLastEnabled = "";
439
Yohei Yukawa81482972015-06-04 00:58:59 -0700440 /**
441 * <em>This constructor must be called within the lock.</em>
442 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 SettingsObserver(Handler handler) {
444 super(handler);
Yohei Yukawa81482972015-06-04 00:58:59 -0700445 }
446
447 public void registerContentObserverLocked(int userId) {
448 if (mRegistered && mUserId == userId) {
449 return;
450 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451 ContentResolver resolver = mContext.getContentResolver();
Yohei Yukawa81482972015-06-04 00:58:59 -0700452 if (mRegistered) {
453 mContext.getContentResolver().unregisterContentObserver(this);
454 mRegistered = false;
455 }
456 if (mUserId != userId) {
457 mLastEnabled = "";
458 mUserId = userId;
459 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700461 Settings.Secure.DEFAULT_INPUT_METHOD), false, this, userId);
satokab751aa2010-09-14 19:17:36 +0900462 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700463 Settings.Secure.ENABLED_INPUT_METHODS), false, this, userId);
satokb6109bb2011-02-03 22:24:54 +0900464 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700465 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this, userId);
Michael Wright7b5a96b2014-08-09 19:28:42 -0700466 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700467 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD), false, this, userId);
468 mRegistered = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800470
Michael Wright7b5a96b2014-08-09 19:28:42 -0700471 @Override public void onChange(boolean selfChange, Uri uri) {
472 final Uri showImeUri =
473 Settings.Secure.getUriFor(Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 synchronized (mMethodMap) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700475 if (showImeUri.equals(uri)) {
476 updateKeyboardFromSettingsLocked();
477 } else {
478 boolean enabledChanged = false;
479 String newEnabled = mSettings.getEnabledInputMethodsStr();
480 if (!mLastEnabled.equals(newEnabled)) {
481 mLastEnabled = newEnabled;
482 enabledChanged = true;
483 }
484 updateInputMethodsFromSettingsLocked(enabledChanged);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800485 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486 }
487 }
Yohei Yukawa81482972015-06-04 00:58:59 -0700488
489 @Override
490 public String toString() {
491 return "SettingsObserver{mUserId=" + mUserId + " mRegistered=" + mRegistered
492 + " mLastEnabled=" + mLastEnabled + "}";
493 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800495
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900496 class ImmsBroadcastReceiver extends android.content.BroadcastReceiver {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900497 @Override
498 public void onReceive(Context context, Intent intent) {
499 final String action = intent.getAction();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700500 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900501 hideInputMethodMenu();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700502 // No need to update mIsInteractive
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900503 return;
Amith Yamasani734983f2014-03-04 16:48:05 -0800504 } else if (Intent.ACTION_USER_ADDED.equals(action)
505 || Intent.ACTION_USER_REMOVED.equals(action)) {
Kenny Guy2a764942014-04-02 13:29:20 +0100506 updateCurrentProfileIds();
Amith Yamasani734983f2014-03-04 16:48:05 -0800507 return;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700508 } else if (Intent.ACTION_SETTING_RESTORED.equals(action)) {
509 final String name = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
510 if (Settings.Secure.ENABLED_INPUT_METHODS.equals(name)) {
511 final String prevValue = intent.getStringExtra(
512 Intent.EXTRA_SETTING_PREVIOUS_VALUE);
513 final String newValue = intent.getStringExtra(
514 Intent.EXTRA_SETTING_NEW_VALUE);
515 restoreEnabledInputMethods(mContext, prevValue, newValue);
516 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900517 } else {
518 Slog.w(TAG, "Unexpected intent " + intent);
519 }
520 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800522
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700523 // Apply the results of a restore operation to the set of enabled IMEs. Note that this
524 // does not attempt to validate on the fly with any installed device policy, so must only
525 // be run in the context of initial device setup.
526 //
527 // TODO: Move this method to InputMethodUtils with adding unit tests.
528 static void restoreEnabledInputMethods(Context context, String prevValue, String newValue) {
529 if (DEBUG_RESTORE) {
530 Slog.i(TAG, "Restoring enabled input methods:");
531 Slog.i(TAG, "prev=" + prevValue);
532 Slog.i(TAG, " new=" + newValue);
533 }
534 // 'new' is the just-restored state, 'prev' is what was in settings prior to the restore
535 ArrayMap<String, ArraySet<String>> prevMap = parseInputMethodsAndSubtypesString(prevValue);
536 ArrayMap<String, ArraySet<String>> newMap = parseInputMethodsAndSubtypesString(newValue);
537
538 // Merge the restored ime+subtype enabled states into the live state
539 for (ArrayMap.Entry<String, ArraySet<String>> entry : newMap.entrySet()) {
540 final String imeId = entry.getKey();
541 ArraySet<String> prevSubtypes = prevMap.get(imeId);
542 if (prevSubtypes == null) {
543 prevSubtypes = new ArraySet<String>(2);
544 prevMap.put(imeId, prevSubtypes);
545 }
546 prevSubtypes.addAll(entry.getValue());
547 }
548
549 final String mergedImesAndSubtypesString = buildInputMethodsAndSubtypesString(prevMap);
550 if (DEBUG_RESTORE) {
551 Slog.i(TAG, "Merged IME string:");
552 Slog.i(TAG, " " + mergedImesAndSubtypesString);
553 }
554 Settings.Secure.putString(context.getContentResolver(),
555 Settings.Secure.ENABLED_INPUT_METHODS, mergedImesAndSubtypesString);
556 }
557
558 // TODO: Move this method to InputMethodUtils with adding unit tests.
559 static String buildInputMethodsAndSubtypesString(ArrayMap<String, ArraySet<String>> map) {
560 // we want to use the canonical InputMethodSettings implementation,
561 // so we convert data structures first.
562 List<Pair<String, ArrayList<String>>> imeMap =
563 new ArrayList<Pair<String, ArrayList<String>>>(4);
564 for (ArrayMap.Entry<String, ArraySet<String>> entry : map.entrySet()) {
565 final String imeName = entry.getKey();
566 final ArraySet<String> subtypeSet = entry.getValue();
567 final ArrayList<String> subtypes = new ArrayList<String>(2);
568 if (subtypeSet != null) {
569 subtypes.addAll(subtypeSet);
570 }
571 imeMap.add(new Pair<String, ArrayList<String>>(imeName, subtypes));
572 }
573 return InputMethodSettings.buildInputMethodsSettingString(imeMap);
574 }
575
576 // TODO: Move this method to InputMethodUtils with adding unit tests.
577 static ArrayMap<String, ArraySet<String>> parseInputMethodsAndSubtypesString(
578 final String inputMethodsAndSubtypesString) {
579 final ArrayMap<String, ArraySet<String>> imeMap =
580 new ArrayMap<String, ArraySet<String>>();
581 if (TextUtils.isEmpty(inputMethodsAndSubtypesString)) {
582 return imeMap;
583 }
584
585 final SimpleStringSplitter typeSplitter =
586 new SimpleStringSplitter(INPUT_METHOD_SEPARATOR);
587 final SimpleStringSplitter subtypeSplitter =
588 new SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATOR);
589 List<Pair<String, ArrayList<String>>> allImeSettings =
590 InputMethodSettings.buildInputMethodsAndSubtypeList(inputMethodsAndSubtypesString,
591 typeSplitter,
592 subtypeSplitter);
593 for (Pair<String, ArrayList<String>> ime : allImeSettings) {
594 ArraySet<String> subtypes = new ArraySet<String>();
595 if (ime.second != null) {
596 subtypes.addAll(ime.second);
597 }
598 imeMap.put(ime.first, subtypes);
599 }
600 return imeMap;
601 }
602
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800603 class MyPackageMonitor extends PackageMonitor {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900604 private boolean isChangingPackagesOfCurrentUser() {
605 final int userId = getChangingUserId();
606 final boolean retval = userId == mSettings.getCurrentUserId();
607 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900608 if (!retval) {
609 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
610 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900611 }
612 return retval;
613 }
614
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800616 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900617 if (!isChangingPackagesOfCurrentUser()) {
618 return false;
619 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900621 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 final int N = mMethodList.size();
623 if (curInputMethodId != null) {
624 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800625 InputMethodInfo imi = mMethodList.get(i);
626 if (imi.getId().equals(curInputMethodId)) {
627 for (String pkg : packages) {
628 if (imi.getPackageName().equals(pkg)) {
629 if (!doit) {
630 return true;
631 }
satok723a27e2010-11-11 14:58:11 +0900632 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800633 chooseNewDefaultIMELocked();
634 return true;
635 }
636 }
637 }
638 }
639 }
640 }
641 return false;
642 }
643
644 @Override
645 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900646 if (!isChangingPackagesOfCurrentUser()) {
647 return;
648 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800649 synchronized (mMethodMap) {
650 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900651 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800652 final int N = mMethodList.size();
653 if (curInputMethodId != null) {
654 for (int i=0; i<N; i++) {
655 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900656 final String imiId = imi.getId();
657 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800658 curIm = imi;
659 }
satoke7c6998e2011-06-03 17:57:59 +0900660
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800661 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900662 if (isPackageModified(imi.getPackageName())) {
663 mFileManager.deleteAllInputMethodSubtypes(imiId);
664 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800665 if (change == PACKAGE_TEMPORARY_CHANGE
666 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800667 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800668 + imi.getComponent());
669 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670 }
671 }
672 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800673
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900674 buildInputMethodListLocked(
675 mMethodList, mMethodMap, false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800676
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800678
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800679 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800680 int change = isPackageDisappearing(curIm.getPackageName());
681 if (change == PACKAGE_TEMPORARY_CHANGE
682 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800683 ServiceInfo si = null;
684 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900685 si = mIPackageManager.getServiceInfo(
686 curIm.getComponent(), 0, mSettings.getCurrentUserId());
687 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800688 }
689 if (si == null) {
690 // Uh oh, current input method is no longer around!
691 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800692 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900693 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800694 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800695 changed = true;
696 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800697 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900698 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800699 }
700 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800701 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800702 }
satokab751aa2010-09-14 19:17:36 +0900703
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800704 if (curIm == null) {
705 // We currently don't have a default input method... is
706 // one now available?
707 changed = chooseNewDefaultIMELocked();
Yohei Yukawa54d512c2015-05-19 22:15:02 -0700708 } else if (!changed && isPackageModified(curIm.getPackageName())) {
709 // Even if the current input method is still available, mCurrentSubtype could
710 // be obsolete when the package is modified in practice.
711 changed = true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800712 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800713
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800714 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800715 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800716 }
717 }
718 }
719 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800720
Jeff Brownc28867a2013-03-26 15:42:39 -0700721 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900722 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -0700723 private final IInputMethod mMethod;
724 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800725
Jeff Brownc28867a2013-03-26 15:42:39 -0700726 MethodCallback(InputMethodManagerService imms, IInputMethod method,
727 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900728 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -0700729 mMethod = method;
730 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800731 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800732
satoke7c6998e2011-06-03 17:57:59 +0900733 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -0700734 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -0700735 long ident = Binder.clearCallingIdentity();
736 try {
737 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
738 } finally {
739 Binder.restoreCallingIdentity(ident);
740 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800741 }
742 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800743
satok01038492012-04-09 21:08:27 +0900744 private class HardKeyboardListener
745 implements WindowManagerService.OnHardKeyboardStatusChangeListener {
746 @Override
Michael Wright7b5a96b2014-08-09 19:28:42 -0700747 public void onHardKeyboardStatusChange(boolean available) {
748 mHandler.sendMessage(mHandler.obtainMessage(MSG_HARD_KEYBOARD_SWITCH_CHANGED,
749 available ? 1 : 0));
satok01038492012-04-09 21:08:27 +0900750 }
751
Michael Wright7b5a96b2014-08-09 19:28:42 -0700752 public void handleHardKeyboardStatusChange(boolean available) {
satok01038492012-04-09 21:08:27 +0900753 if (DEBUG) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700754 Slog.w(TAG, "HardKeyboardStatusChanged: available=" + available);
satok01038492012-04-09 21:08:27 +0900755 }
756 synchronized(mMethodMap) {
757 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
758 && mSwitchingDialog.isShowing()) {
759 mSwitchingDialogTitleView.findViewById(
760 com.android.internal.R.id.hard_keyboard_section).setVisibility(
761 available ? View.VISIBLE : View.GONE);
762 }
763 }
764 }
765 }
766
767 public InputMethodManagerService(Context context, WindowManagerService windowManager) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900768 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800769 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800770 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800771 mHandler = new Handler(this);
Yohei Yukawa81482972015-06-04 00:58:59 -0700772 // Note: SettingsObserver doesn't register observers in its constructor.
773 mSettingsObserver = new SettingsObserver(mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800774 mIWindowManager = IWindowManager.Stub.asInterface(
775 ServiceManager.getService(Context.WINDOW_SERVICE));
Mita Yuned218c72012-12-06 17:18:25 -0800776 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900777 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800778 public void executeMessage(Message msg) {
779 handleMessage(msg);
780 }
Mita Yuned218c72012-12-06 17:18:25 -0800781 }, true /*asyncHandler*/);
satok01038492012-04-09 21:08:27 +0900782 mWindowManagerService = windowManager;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900783 mAppOpsManager = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE);
satok01038492012-04-09 21:08:27 +0900784 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700785 mHasFeature = context.getPackageManager().hasSystemFeature(
786 PackageManager.FEATURE_INPUT_METHODS);
satok7cfc0ed2011-06-20 21:29:36 +0900787
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400788 Bundle extras = new Bundle();
789 extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
790 mImeSwitcherNotification = new Notification.Builder(mContext)
791 .setSmallIcon(com.android.internal.R.drawable.ic_notification_ime_default)
792 .setWhen(0)
793 .setOngoing(true)
794 .addExtras(extras)
795 .setCategory(Notification.CATEGORY_SYSTEM)
796 .setColor(com.android.internal.R.color.system_notification_accent_color);
Daniel Sandler590d5152012-06-14 16:10:13 -0400797
satok7cfc0ed2011-06-20 21:29:36 +0900798 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900799 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900800
801 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900802
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900803 final IntentFilter broadcastFilter = new IntentFilter();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900804 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Amith Yamasani734983f2014-03-04 16:48:05 -0800805 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
806 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700807 broadcastFilter.addAction(Intent.ACTION_SETTING_RESTORED);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900808 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800809
satok7cfc0ed2011-06-20 21:29:36 +0900810 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900811 int userId = 0;
812 try {
813 ActivityManagerNative.getDefault().registerUserSwitchObserver(
814 new IUserSwitchObserver.Stub() {
815 @Override
816 public void onUserSwitching(int newUserId, IRemoteCallback reply) {
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900817 synchronized(mMethodMap) {
818 switchUserLocked(newUserId);
819 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900820 if (reply != null) {
821 try {
822 reply.sendResult(null);
823 } catch (RemoteException e) {
824 }
825 }
826 }
827
828 @Override
829 public void onUserSwitchComplete(int newUserId) throws RemoteException {
830 }
Kenny Guy42979622015-04-13 18:03:05 +0000831
832 @Override
833 public void onForegroundProfileSwitch(int newProfileId) {
834 // Ignore.
835 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900836 });
837 userId = ActivityManagerNative.getDefault().getCurrentUser().id;
838 } catch (RemoteException e) {
839 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
840 }
satok81f8b7c2012-12-04 20:42:56 +0900841 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
satok913a8922010-08-26 21:53:41 +0900842
satokd87c2592010-09-29 11:52:06 +0900843 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900844 mSettings = new InputMethodSettings(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900845 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId);
Svet Ganovadc1cf42015-06-15 16:36:24 -0700846
847 // Let the package manager query which are the default imes
848 // as they get certain permissions granted by default.
849 PackageManagerInternal packageManagerInternal = LocalServices.getService(
850 PackageManagerInternal.class);
851 packageManagerInternal.setImePackagesProvider(
852 new PackageManagerInternal.PackagesProvider() {
853 @Override
854 public String[] getPackages(int userId) {
855 synchronized (mMethodMap) {
856 final int currentUserId = mSettings.getCurrentUserId();
857 // TODO: We are switching the current user id in the settings
858 // object to query it and then revert the user id. Ideally, we
859 // should call a API in settings with the user id as an argument.
860 mSettings.setCurrentUserId(userId);
861 List<InputMethodInfo> imes = mSettings
862 .getEnabledInputMethodListLocked();
863 String[] packageNames = null;
864 if (imes != null) {
865 final int imeCount = imes.size();
866 packageNames = new String[imeCount];
867 for (int i = 0; i < imeCount; i++) {
868 InputMethodInfo ime = imes.get(i);
869 packageNames[i] = ime.getPackageName();
870 }
871 }
872 mSettings.setCurrentUserId(currentUserId);
873 return packageNames;
874 }
875 }
876 });
877
Kenny Guy2a764942014-04-02 13:29:20 +0100878 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900879 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900880 synchronized (mMethodMap) {
881 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
882 mSettings, context);
883 }
satok0a1bcf42012-05-16 19:26:31 +0900884
885 // Just checking if defaultImiId is empty or not
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900886 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900887 if (DEBUG) {
888 Slog.d(TAG, "Initial default ime = " + defaultImiId);
889 }
satok0a1bcf42012-05-16 19:26:31 +0900890 mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
891
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900892 synchronized (mMethodMap) {
893 buildInputMethodListLocked(mMethodList, mMethodMap,
894 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
895 }
satokd87c2592010-09-29 11:52:06 +0900896 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800897
satok0a1bcf42012-05-16 19:26:31 +0900898 if (!mImeSelectedOnBoot) {
899 Slog.w(TAG, "No IME selected. Choose the most applicable IME.");
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900900 synchronized (mMethodMap) {
901 resetDefaultImeLocked(context);
902 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800903 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800904
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900905 synchronized (mMethodMap) {
Yohei Yukawa81482972015-06-04 00:58:59 -0700906 mSettingsObserver.registerContentObserverLocked(userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900907 updateFromSettingsLocked(true);
908 }
satok5b927c432012-05-01 20:09:34 +0900909
910 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME
911 // according to the new system locale.
912 final IntentFilter filter = new IntentFilter();
913 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
914 mContext.registerReceiver(
915 new BroadcastReceiver() {
916 @Override
917 public void onReceive(Context context, Intent intent) {
918 synchronized(mMethodMap) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900919 resetStateIfCurrentLocaleChangedLocked();
satok5b927c432012-05-01 20:09:34 +0900920 }
921 }
922 }, filter);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700923 LocalServices.addService(InputMethodManagerInternal.class, new LocalServiceImpl(mHandler));
satok5b927c432012-05-01 20:09:34 +0900924 }
925
satok5b927c432012-05-01 20:09:34 +0900926 private void resetDefaultImeLocked(Context context) {
927 // Do not reset the default (current) IME when it is a 3rd-party IME
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900928 if (mCurMethodId != null
929 && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +0900930 return;
931 }
932
933 InputMethodInfo defIm = null;
934 for (InputMethodInfo imi : mMethodList) {
Yohei Yukawa6aa03782015-02-21 03:00:22 +0900935 if (defIm == null && mSystemReady) {
936 final Locale systemLocale = context.getResources().getConfiguration().locale;
937 if (InputMethodUtils.isSystemImeThatHasSubtypeOf(imi, context,
938 true /* checkDefaultAttribute */, systemLocale, false /* checkCountry */,
939 InputMethodUtils.SUBTYPE_MODE_ANY)) {
satok5b927c432012-05-01 20:09:34 +0900940 defIm = imi;
941 Slog.i(TAG, "Selected default: " + imi.getId());
942 }
943 }
944 }
945 if (defIm == null && mMethodList.size() > 0) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900946 defIm = InputMethodUtils.getMostApplicableDefaultIME(
947 mSettings.getEnabledInputMethodListLocked());
Hyejin Kim5baaaac2014-10-27 17:17:06 +0900948 if (defIm != null) {
949 Slog.i(TAG, "Default found, using " + defIm.getId());
950 } else {
951 Slog.i(TAG, "No default found");
952 }
satok5b927c432012-05-01 20:09:34 +0900953 }
954 if (defIm != null) {
955 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
956 }
957 }
958
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900959 private void resetAllInternalStateLocked(final boolean updateOnlyWhenLocaleChanged,
960 final boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900961 if (!mSystemReady) {
962 // not system ready
963 return;
964 }
965 final Locale newLocale = mRes.getConfiguration().locale;
966 if (!updateOnlyWhenLocaleChanged
967 || (newLocale != null && !newLocale.equals(mLastSystemLocale))) {
968 if (!updateOnlyWhenLocaleChanged) {
969 hideCurrentInputLocked(0, null);
970 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -0700971 unbindCurrentMethodLocked(true, false);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900972 }
973 if (DEBUG) {
974 Slog.i(TAG, "Locale has been changed to " + newLocale);
975 }
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900976 buildInputMethodListLocked(mMethodList, mMethodMap, resetDefaultEnabledIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900977 if (!updateOnlyWhenLocaleChanged) {
978 final String selectedImiId = mSettings.getSelectedInputMethod();
979 if (TextUtils.isEmpty(selectedImiId)) {
980 // This is the first time of the user switch and
981 // set the current ime to the proper one.
982 resetDefaultImeLocked(mContext);
983 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +0900984 } else {
985 // If the locale is changed, needs to reset the default ime
986 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900987 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800988 updateFromSettingsLocked(true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900989 mLastSystemLocale = newLocale;
990 if (!updateOnlyWhenLocaleChanged) {
991 try {
992 startInputInnerLocked();
993 } catch (RuntimeException e) {
994 Slog.w(TAG, "Unexpected exception", e);
995 }
996 }
997 }
998 }
999
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001000 private void resetStateIfCurrentLocaleChangedLocked() {
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001001 resetAllInternalStateLocked(true /* updateOnlyWhenLocaleChanged */,
1002 true /* resetDefaultImeLocked */);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001003 }
1004
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001005 private void switchUserLocked(int newUserId) {
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001006 if (DEBUG) Slog.d(TAG, "Switching user stage 1/3. newUserId=" + newUserId
1007 + " currentUserId=" + mSettings.getCurrentUserId());
1008
Yohei Yukawa81482972015-06-04 00:58:59 -07001009 // ContentObserver should be registered again when the user is changed
1010 mSettingsObserver.registerContentObserverLocked(newUserId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001011 mSettings.setCurrentUserId(newUserId);
Kenny Guy2a764942014-04-02 13:29:20 +01001012 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001013 // InputMethodFileManager should be reset when the user is changed
1014 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001015 final String defaultImiId = mSettings.getSelectedInputMethod();
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001016
1017 if (DEBUG) Slog.d(TAG, "Switching user stage 2/3. newUserId=" + newUserId
1018 + " defaultImiId=" + defaultImiId);
1019
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +09001020 // For secondary users, the list of enabled IMEs may not have been updated since the
1021 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
1022 // not be empty even if the IME has been uninstalled by the primary user.
1023 // Even in such cases, IMMS works fine because it will find the most applicable
1024 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001025 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001026 resetAllInternalStateLocked(false /* updateOnlyWhenLocaleChanged */,
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001027 initialUserSwitch /* needsToResetDefaultIme */);
1028 if (initialUserSwitch) {
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001029 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1030 mSettings.getEnabledInputMethodListLocked(), newUserId,
1031 mContext.getBasePackageName());
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001032 }
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001033
1034 if (DEBUG) Slog.d(TAG, "Switching user stage 3/3. newUserId=" + newUserId
1035 + " selectedIme=" + mSettings.getSelectedInputMethod());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001036 }
1037
Kenny Guy2a764942014-04-02 13:29:20 +01001038 void updateCurrentProfileIds() {
1039 List<UserInfo> profiles =
1040 UserManager.get(mContext).getProfiles(mSettings.getCurrentUserId());
1041 int[] currentProfileIds = new int[profiles.size()]; // profiles will not be null
1042 for (int i = 0; i < currentProfileIds.length; i++) {
1043 currentProfileIds[i] = profiles.get(i).id;
Amith Yamasani734983f2014-03-04 16:48:05 -08001044 }
Kenny Guy2a764942014-04-02 13:29:20 +01001045 mSettings.setCurrentProfileIds(currentProfileIds);
Amith Yamasani734983f2014-03-04 16:48:05 -08001046 }
1047
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001048 @Override
1049 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
1050 throws RemoteException {
1051 try {
1052 return super.onTransact(code, data, reply, flags);
1053 } catch (RuntimeException e) {
1054 // The input method manager only throws security exceptions, so let's
1055 // log all others.
1056 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -07001057 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001058 }
1059 throw e;
1060 }
1061 }
1062
Svetoslav Ganova0027152013-06-25 14:59:53 -07001063 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001064 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001065 if (DEBUG) {
1066 Slog.d(TAG, "--- systemReady");
1067 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001068 if (!mSystemReady) {
1069 mSystemReady = true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001070 mKeyguardManager =
1071 (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001072 mNotificationManager = (NotificationManager)
1073 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
1074 mStatusBar = statusBar;
1075 statusBar.setIconVisibility("ime", false);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001076 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokb858c732011-07-22 19:54:34 +09001077 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
1078 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +09001079 if (mShowOngoingImeSwitcherForPhones) {
1080 mWindowManagerService.setOnHardKeyboardStatusChangeListener(
1081 mHardKeyboardListener);
1082 }
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09001083 buildInputMethodListLocked(mMethodList, mMethodMap,
1084 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satok0a1bcf42012-05-16 19:26:31 +09001085 if (!mImeSelectedOnBoot) {
1086 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001087 resetStateIfCurrentLocaleChangedLocked();
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001088 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1089 mSettings.getEnabledInputMethodListLocked(),
1090 mSettings.getCurrentUserId(), mContext.getBasePackageName());
satok0a1bcf42012-05-16 19:26:31 +09001091 }
1092 mLastSystemLocale = mRes.getConfiguration().locale;
Dianne Hackborncc278702009-09-02 23:07:23 -07001093 try {
1094 startInputInnerLocked();
1095 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001096 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -07001097 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001098 }
1099 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001100 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001101
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001102 // ---------------------------------------------------------------------------------------
1103 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
1104 // 1) it comes from the system process
1105 // 2) the calling process' user id is identical to the current user id IMMS thinks.
1106 private boolean calledFromValidUser() {
1107 final int uid = Binder.getCallingUid();
1108 final int userId = UserHandle.getUserId(uid);
1109 if (DEBUG) {
1110 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
1111 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
1112 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +09001113 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
1114 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001115 }
Kenny Guy2a764942014-04-02 13:29:20 +01001116 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001117 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001118 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001119
1120 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
1121 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
1122 // must not manage background users' states in any functions.
1123 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
1124 // by a token.
1125 if (mContext.checkCallingOrSelfPermission(
1126 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1127 == PackageManager.PERMISSION_GRANTED) {
1128 if (DEBUG) {
1129 Slog.d(TAG, "--- Access granted because the calling process has "
1130 + "the INTERACT_ACROSS_USERS_FULL permission");
1131 }
1132 return true;
1133 }
Satoshi Kataoka0766eb02013-07-31 18:30:13 +09001134 Slog.w(TAG, "--- IPC called from background users. Ignore. \n"
1135 + InputMethodUtils.getStackTrace());
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001136 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001137 }
1138
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001139
1140 /**
1141 * Returns true iff the caller is identified to be the current input method with the token.
1142 * @param token The window token given to the input method when it was started.
1143 * @return true if and only if non-null valid token is specified.
1144 */
1145 private boolean calledWithValidToken(IBinder token) {
1146 if (token == null || mCurToken != token) {
1147 return false;
1148 }
1149 return true;
1150 }
1151
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001152 private boolean bindCurrentInputMethodService(
1153 Intent service, ServiceConnection conn, int flags) {
1154 if (service == null || conn == null) {
1155 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
1156 return false;
1157 }
Amith Yamasani27b89e62013-01-16 12:30:11 -08001158 return mContext.bindServiceAsUser(service, conn, flags,
1159 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001160 }
1161
satoke7c6998e2011-06-03 17:57:59 +09001162 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001164 // TODO: Make this work even for non-current users?
1165 if (!calledFromValidUser()) {
1166 return Collections.emptyList();
1167 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001168 synchronized (mMethodMap) {
1169 return new ArrayList<InputMethodInfo>(mMethodList);
1170 }
1171 }
1172
satoke7c6998e2011-06-03 17:57:59 +09001173 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001174 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001175 // TODO: Make this work even for non-current users?
1176 if (!calledFromValidUser()) {
1177 return Collections.emptyList();
1178 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001179 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001180 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 }
1182 }
1183
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001184 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001185 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001186 * @return enabled subtypes of the specified imi
1187 */
satoke7c6998e2011-06-03 17:57:59 +09001188 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001189 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001190 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001191 // TODO: Make this work even for non-current users?
1192 if (!calledFromValidUser()) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001193 return Collections.<InputMethodSubtype>emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001194 }
satok67ddf9c2010-11-17 09:45:54 +09001195 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001196 final InputMethodInfo imi;
1197 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001198 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001199 } else {
1200 imi = mMethodMap.get(imiId);
1201 }
1202 if (imi == null) {
1203 return Collections.<InputMethodSubtype>emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001204 }
1205 return mSettings.getEnabledInputMethodSubtypeListLocked(
1206 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001207 }
1208 }
1209
satoke7c6998e2011-06-03 17:57:59 +09001210 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 public void addClient(IInputMethodClient client,
1212 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001213 if (!calledFromValidUser()) {
1214 return;
1215 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001216 synchronized (mMethodMap) {
1217 mClients.put(client.asBinder(), new ClientState(client,
1218 inputContext, uid, pid));
1219 }
1220 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001221
satoke7c6998e2011-06-03 17:57:59 +09001222 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001224 if (!calledFromValidUser()) {
1225 return;
1226 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001228 ClientState cs = mClients.remove(client.asBinder());
1229 if (cs != null) {
1230 clearClientSessionLocked(cs);
1231 }
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 void executeOrSendMessage(IInterface target, Message msg) {
1236 if (target.asBinder() instanceof Binder) {
1237 mCaller.sendMessage(msg);
1238 } else {
1239 handleMessage(msg);
1240 msg.recycle();
1241 }
1242 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001243
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001244 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001246 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001247 + mCurClient.client.asBinder());
1248 if (mBoundToMethod) {
1249 mBoundToMethod = false;
1250 if (mCurMethod != null) {
1251 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1252 MSG_UNBIND_INPUT, mCurMethod));
1253 }
1254 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001255
1256 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1257 MSG_SET_ACTIVE, 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001258 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1259 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1260 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001261 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001262
The Android Open Source Project10592532009-03-18 17:39:46 -07001263 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 }
1265 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001266
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001267 private int getImeShowFlags() {
1268 int flags = 0;
1269 if (mShowForced) {
1270 flags |= InputMethod.SHOW_FORCED
1271 | InputMethod.SHOW_EXPLICIT;
1272 } else if (mShowExplicitlyRequested) {
1273 flags |= InputMethod.SHOW_EXPLICIT;
1274 }
1275 return flags;
1276 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001278 private int getAppShowFlags() {
1279 int flags = 0;
1280 if (mShowForced) {
1281 flags |= InputMethodManager.SHOW_FORCED;
1282 } else if (!mShowExplicitlyRequested) {
1283 flags |= InputMethodManager.SHOW_IMPLICIT;
1284 }
1285 return flags;
1286 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001287
Dianne Hackborn7663d802012-02-24 13:08:49 -08001288 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001289 if (!mBoundToMethod) {
1290 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1291 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1292 mBoundToMethod = true;
1293 }
1294 final SessionState session = mCurClient.curSession;
1295 if (initial) {
1296 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1297 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
1298 } else {
1299 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1300 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
1301 }
1302 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001303 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001304 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001305 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001306 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001307 (session.channel != null ? session.channel.dup() : null),
1308 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001311 InputBindResult startInputLocked(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001312 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001313 // If no method is currently selected, do nothing.
1314 if (mCurMethodId == null) {
1315 return mNoBinding;
1316 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001317
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001318 ClientState cs = mClients.get(client.asBinder());
1319 if (cs == null) {
1320 throw new IllegalArgumentException("unknown client "
1321 + client.asBinder());
1322 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001324 try {
1325 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1326 // Check with the window manager to make sure this client actually
1327 // has a window with focus. If not, reject. This is thread safe
1328 // because if the focus changes some time before or after, the
1329 // next client receiving focus that has any interest in input will
1330 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001331 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001332 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1333 return null;
1334 }
1335 } catch (RemoteException e) {
1336 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001337
Dianne Hackborn7663d802012-02-24 13:08:49 -08001338 return startInputUncheckedLocked(cs, inputContext, attribute, controlFlags);
1339 }
1340
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001341 InputBindResult startInputUncheckedLocked(@NonNull ClientState cs,
Yohei Yukawad57ba672015-06-08 16:39:46 -07001342 IInputContext inputContext, @NonNull EditorInfo attribute, int controlFlags) {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001343 // If no method is currently selected, do nothing.
1344 if (mCurMethodId == null) {
1345 return mNoBinding;
1346 }
1347
Yohei Yukawad57ba672015-06-08 16:39:46 -07001348 if (!InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.uid,
1349 attribute.packageName)) {
1350 Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
1351 + " uid=" + cs.uid + " package=" + attribute.packageName);
1352 return mNoBinding;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001353 }
1354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001355 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001356 // Was the keyguard locked when switching over to the new client?
1357 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001358 // If the client is changing, we need to switch over to the new
1359 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001360 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001361 if (DEBUG) Slog.v(TAG, "switching to client: client = "
John Spurlocke0980502013-10-25 11:59:29 -04001362 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001363
1364 // If the screen is on, inform the new client it is active
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001365 if (mIsInteractive) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001366 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001367 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001368 }
1369 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001371 // Bump up the sequence for this client and attach it.
1372 mCurSeq++;
1373 if (mCurSeq <= 0) mCurSeq = 1;
1374 mCurClient = cs;
1375 mCurInputContext = inputContext;
1376 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001377
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001378 // Check if the input method is changing.
1379 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1380 if (cs.curSession != null) {
1381 // Fast case: if we are already connected to the input method,
1382 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001383 return attachNewInputLocked(
1384 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001385 }
1386 if (mHaveConnection) {
1387 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001388 // Return to client, and we will get back with it when
1389 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001390 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001391 return new InputBindResult(null, null, mCurId, mCurSeq,
1392 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001393 } else if (SystemClock.uptimeMillis()
1394 < (mLastBindTime+TIME_TO_RECONNECT)) {
1395 // In this case we have connected to the service, but
1396 // don't yet have its interface. If it hasn't been too
1397 // long since we did the connection, we'll return to
1398 // the client and wait to get the service interface so
1399 // we can report back. If it has been too long, we want
1400 // to fall through so we can try a disconnect/reconnect
1401 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001402 return new InputBindResult(null, null, mCurId, mCurSeq,
1403 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001404 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001405 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1406 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001407 }
1408 }
1409 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001410
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001411 return startInputInnerLocked();
1412 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001413
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001414 InputBindResult startInputInnerLocked() {
1415 if (mCurMethodId == null) {
1416 return mNoBinding;
1417 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001418
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001419 if (!mSystemReady) {
1420 // If the system is not yet ready, we shouldn't be running third
1421 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001422 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1423 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001424 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001425
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001426 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1427 if (info == null) {
1428 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1429 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001430
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001431 unbindCurrentMethodLocked(false, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001433 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1434 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001435 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1436 com.android.internal.R.string.input_method_binding_label);
1437 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1438 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001439 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001440 | Context.BIND_NOT_VISIBLE | Context.BIND_NOT_FOREGROUND
1441 | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001442 mLastBindTime = SystemClock.uptimeMillis();
1443 mHaveConnection = true;
1444 mCurId = info.getId();
1445 mCurToken = new Binder();
1446 try {
Craig Mautnerca0ac712013-03-14 09:43:02 -07001447 if (true || DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001448 mIWindowManager.addWindowToken(mCurToken,
1449 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1450 } catch (RemoteException e) {
1451 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001452 return new InputBindResult(null, null, mCurId, mCurSeq,
1453 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001454 } else {
1455 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001456 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001457 + mCurIntent);
1458 }
1459 return null;
1460 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001461
satoke7c6998e2011-06-03 17:57:59 +09001462 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001463 public InputBindResult startInput(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001464 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001465 if (!calledFromValidUser()) {
1466 return null;
1467 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001468 synchronized (mMethodMap) {
1469 final long ident = Binder.clearCallingIdentity();
1470 try {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001471 return startInputLocked(client, inputContext, attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001472 } finally {
1473 Binder.restoreCallingIdentity(ident);
1474 }
1475 }
1476 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001477
satoke7c6998e2011-06-03 17:57:59 +09001478 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001479 public void finishInput(IInputMethodClient client) {
1480 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001481
satoke7c6998e2011-06-03 17:57:59 +09001482 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001483 public void onServiceConnected(ComponentName name, IBinder service) {
1484 synchronized (mMethodMap) {
1485 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1486 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001487 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001488 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001489 unbindCurrentMethodLocked(false, false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001490 return;
1491 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001492 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001493 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1494 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001495 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001496 clearClientSessionLocked(mCurClient);
1497 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001498 }
1499 }
1500 }
1501 }
1502
Jeff Brownc28867a2013-03-26 15:42:39 -07001503 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1504 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001505 synchronized (mMethodMap) {
1506 if (mCurMethod != null && method != null
1507 && mCurMethod.asBinder() == method.asBinder()) {
1508 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001509 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001510 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001511 method, session, channel);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001512 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001513 if (res.method != null) {
1514 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
1515 MSG_BIND_METHOD, mCurClient.client, res));
1516 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001517 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001518 }
1519 }
1520 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001521
1522 // Session abandoned. Close its associated input channel.
1523 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001524 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001525
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001526 void unbindCurrentMethodLocked(boolean reportToClient, boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001527 if (mVisibleBound) {
1528 mContext.unbindService(mVisibleConnection);
1529 mVisibleBound = false;
1530 }
1531
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001532 if (mHaveConnection) {
1533 mContext.unbindService(this);
1534 mHaveConnection = false;
1535 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001536
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001537 if (mCurToken != null) {
1538 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001539 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001540 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001541 // The current IME is shown. Hence an IME switch (transition) is happening.
1542 mWindowManagerService.saveLastInputMethodWindowForTransition();
1543 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001544 mIWindowManager.removeWindowToken(mCurToken);
1545 } catch (RemoteException e) {
1546 }
1547 mCurToken = null;
1548 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001549
The Android Open Source Project10592532009-03-18 17:39:46 -07001550 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001551 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001552
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001553 if (reportToClient && mCurClient != null) {
1554 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1555 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1556 }
1557 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001558
1559 void requestClientSessionLocked(ClientState cs) {
1560 if (!cs.sessionRequested) {
1561 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1562 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1563 cs.sessionRequested = true;
1564 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1565 MSG_CREATE_SESSION, mCurMethod, channels[1],
1566 new MethodCallback(this, mCurMethod, channels[0])));
1567 }
1568 }
1569
1570 void clearClientSessionLocked(ClientState cs) {
1571 finishSessionLocked(cs.curSession);
1572 cs.curSession = null;
1573 cs.sessionRequested = false;
1574 }
1575
1576 private void finishSessionLocked(SessionState sessionState) {
1577 if (sessionState != null) {
1578 if (sessionState.session != null) {
1579 try {
1580 sessionState.session.finishSession();
1581 } catch (RemoteException e) {
1582 Slog.w(TAG, "Session failed to close due to remote exception", e);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001583 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Jeff Brownc28867a2013-03-26 15:42:39 -07001584 }
1585 sessionState.session = null;
1586 }
1587 if (sessionState.channel != null) {
1588 sessionState.channel.dispose();
1589 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001590 }
1591 }
1592 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001593
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001594 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001595 if (mCurMethod != null) {
1596 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001597 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001598 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001599
Jeff Brownc28867a2013-03-26 15:42:39 -07001600 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001601 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001602 mCurMethod = null;
1603 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001604 if (mStatusBar != null) {
1605 mStatusBar.setIconVisibility("ime", false);
1606 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001607 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001608
satoke7c6998e2011-06-03 17:57:59 +09001609 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001610 public void onServiceDisconnected(ComponentName name) {
1611 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001612 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001613 + " mCurIntent=" + mCurIntent);
1614 if (mCurMethod != null && mCurIntent != null
1615 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001616 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001617 // We consider this to be a new bind attempt, since the system
1618 // should now try to restart the service for us.
1619 mLastBindTime = SystemClock.uptimeMillis();
1620 mShowRequested = mInputShown;
1621 mInputShown = false;
1622 if (mCurClient != null) {
1623 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1624 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1625 }
1626 }
1627 }
1628 }
1629
satokf9f01002011-05-19 21:31:50 +09001630 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001631 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
1632 long ident = Binder.clearCallingIdentity();
1633 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001634 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09001635 if (!calledWithValidToken(token)) {
1636 final int uid = Binder.getCallingUid();
1637 Slog.e(TAG, "Ignoring updateStatusIcon due to an invalid token. uid:" + uid
1638 + " token:" + token);
1639 return;
1640 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001641 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001642 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001643 if (mStatusBar != null) {
1644 mStatusBar.setIconVisibility("ime", false);
1645 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001646 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001647 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001648 CharSequence contentDescription = null;
1649 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001650 // Use PackageManager to load label
1651 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001652 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001653 mIPackageManager.getApplicationInfo(packageName, 0,
1654 mSettings.getCurrentUserId()));
1655 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001656 /* ignore */
1657 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001658 if (mStatusBar != null) {
1659 mStatusBar.setIcon("ime", packageName, iconId, 0,
1660 contentDescription != null
1661 ? contentDescription.toString() : null);
1662 mStatusBar.setIconVisibility("ime", true);
1663 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001664 }
1665 }
1666 } finally {
1667 Binder.restoreCallingIdentity(ident);
1668 }
1669 }
1670
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001671 private boolean shouldShowImeSwitcherLocked(int visibility) {
satok7cfc0ed2011-06-20 21:29:36 +09001672 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04001673 if (mSwitchingDialog != null) return false;
satok2c93efc2012-04-02 19:33:47 +09001674 if (isScreenLocked()) return false;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001675 if ((visibility & InputMethodService.IME_ACTIVE) == 0) return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001676 if (mWindowManagerService.isHardKeyboardAvailable()) {
1677 // When physical keyboard is attached, we show the ime switcher (or notification if
1678 // NavBar is not available) because SHOW_IME_WITH_HARD_KEYBOARD settings currently
1679 // exists in the IME switcher dialog. Might be OK to remove this condition once
1680 // SHOW_IME_WITH_HARD_KEYBOARD settings finds a good place to live.
1681 return true;
1682 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001683 if ((visibility & InputMethodService.IME_VISIBLE) == 0) return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001684
1685 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1686 final int N = imis.size();
1687 if (N > 2) return true;
1688 if (N < 1) return false;
1689 int nonAuxCount = 0;
1690 int auxCount = 0;
1691 InputMethodSubtype nonAuxSubtype = null;
1692 InputMethodSubtype auxSubtype = null;
1693 for(int i = 0; i < N; ++i) {
1694 final InputMethodInfo imi = imis.get(i);
1695 final List<InputMethodSubtype> subtypes =
1696 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
1697 final int subtypeCount = subtypes.size();
1698 if (subtypeCount == 0) {
1699 ++nonAuxCount;
1700 } else {
1701 for (int j = 0; j < subtypeCount; ++j) {
1702 final InputMethodSubtype subtype = subtypes.get(j);
1703 if (!subtype.isAuxiliary()) {
1704 ++nonAuxCount;
1705 nonAuxSubtype = subtype;
1706 } else {
1707 ++auxCount;
1708 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001709 }
1710 }
satok7cfc0ed2011-06-20 21:29:36 +09001711 }
1712 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001713 if (nonAuxCount > 1 || auxCount > 1) {
1714 return true;
1715 } else if (nonAuxCount == 1 && auxCount == 1) {
1716 if (nonAuxSubtype != null && auxSubtype != null
1717 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1718 || auxSubtype.overridesImplicitlyEnabledSubtype()
1719 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
1720 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1721 return false;
1722 }
1723 return true;
1724 }
1725 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001726 }
1727
John Spurlocke0980502013-10-25 11:59:29 -04001728 private boolean isKeyguardLocked() {
1729 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1730 }
1731
satokdbf29502011-08-25 15:28:23 +09001732 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001733 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001734 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001735 if (!calledWithValidToken(token)) {
1736 final int uid = Binder.getCallingUid();
1737 Slog.e(TAG, "Ignoring setImeWindowStatus due to an invalid token. uid:" + uid
1738 + " token:" + token);
1739 return;
1740 }
1741
1742 synchronized (mMethodMap) {
1743 mImeWindowVis = vis;
1744 mBackDisposition = backDisposition;
1745 updateSystemUiLocked(token, vis, backDisposition);
1746 }
1747 }
1748
1749 private void updateSystemUi(IBinder token, int vis, int backDisposition) {
1750 synchronized (mMethodMap) {
1751 updateSystemUiLocked(token, vis, backDisposition);
1752 }
1753 }
1754
1755 // Caution! This method is called in this class. Handle multi-user carefully
1756 private void updateSystemUiLocked(IBinder token, int vis, int backDisposition) {
1757 if (!calledWithValidToken(token)) {
1758 final int uid = Binder.getCallingUid();
1759 Slog.e(TAG, "Ignoring updateSystemUiLocked due to an invalid token. uid:" + uid
1760 + " token:" + token);
1761 return;
1762 }
1763
1764 // TODO: Move this clearing calling identity block to setImeWindowStatus after making sure
1765 // all updateSystemUi happens on system previlege.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001766 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001767 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001768 // apply policy for binder calls
1769 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
1770 vis = 0;
satok06487a52010-10-29 11:37:18 +09001771 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001772 // mImeWindowVis should be updated before calling shouldShowImeSwitcherLocked().
1773 final boolean needsToShowImeSwitcher = shouldShowImeSwitcherLocked(vis);
1774 if (mStatusBar != null) {
1775 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
1776 needsToShowImeSwitcher);
1777 }
1778 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1779 if (imi != null && needsToShowImeSwitcher) {
1780 // Used to load label
1781 final CharSequence title = mRes.getText(
1782 com.android.internal.R.string.select_input_method);
1783 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
1784 mContext, imi, mCurrentSubtype);
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001785 mImeSwitcherNotification.setContentTitle(title)
1786 .setContentText(summary)
1787 .setContentIntent(mImeSwitchPendingIntent);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001788 if ((mNotificationManager != null)
1789 && !mWindowManagerService.hasNavigationBar()) {
1790 if (DEBUG) {
1791 Slog.d(TAG, "--- show notification: label = " + summary);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001792 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001793 mNotificationManager.notifyAsUser(null,
1794 com.android.internal.R.string.select_input_method,
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001795 mImeSwitcherNotification.build(), UserHandle.ALL);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001796 mNotificationShown = true;
1797 }
1798 } else {
1799 if (mNotificationShown && mNotificationManager != null) {
1800 if (DEBUG) {
1801 Slog.d(TAG, "--- hide notification");
satok7cfc0ed2011-06-20 21:29:36 +09001802 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001803 mNotificationManager.cancelAsUser(null,
1804 com.android.internal.R.string.select_input_method, UserHandle.ALL);
1805 mNotificationShown = false;
satok7cfc0ed2011-06-20 21:29:36 +09001806 }
satok06487a52010-10-29 11:37:18 +09001807 }
1808 } finally {
1809 Binder.restoreCallingIdentity(ident);
1810 }
1811 }
1812
satoke7c6998e2011-06-03 17:57:59 +09001813 @Override
satokf9f01002011-05-19 21:31:50 +09001814 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001815 if (!calledFromValidUser()) {
1816 return;
1817 }
satokf9f01002011-05-19 21:31:50 +09001818 synchronized (mMethodMap) {
1819 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1820 for (int i = 0; i < spans.length; ++i) {
1821 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001822 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001823 mSecureSuggestionSpans.put(ss, currentImi);
1824 }
1825 }
1826 }
1827 }
1828
satoke7c6998e2011-06-03 17:57:59 +09001829 @Override
satokf9f01002011-05-19 21:31:50 +09001830 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001831 if (!calledFromValidUser()) {
1832 return false;
1833 }
satokf9f01002011-05-19 21:31:50 +09001834 synchronized (mMethodMap) {
1835 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1836 // TODO: Do not send the intent if the process of the targetImi is already dead.
1837 if (targetImi != null) {
1838 final String[] suggestions = span.getSuggestions();
1839 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001840 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001841 final Intent intent = new Intent();
1842 // Ensures that only a class in the original IME package will receive the
1843 // notification.
satok42c5a162011-05-26 16:46:14 +09001844 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001845 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1846 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1847 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1848 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001849 final long ident = Binder.clearCallingIdentity();
1850 try {
1851 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1852 } finally {
1853 Binder.restoreCallingIdentity(ident);
1854 }
satokf9f01002011-05-19 21:31:50 +09001855 return true;
1856 }
1857 }
1858 return false;
1859 }
1860
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001861 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001862 updateInputMethodsFromSettingsLocked(enabledMayChange);
1863 updateKeyboardFromSettingsLocked();
1864 }
1865
1866 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001867 if (enabledMayChange) {
1868 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1869 for (int i=0; i<enabled.size(); i++) {
1870 // We allow the user to select "disabled until used" apps, so if they
1871 // are enabling one of those here we now need to make it enabled.
1872 InputMethodInfo imm = enabled.get(i);
1873 try {
1874 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1875 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1876 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09001877 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001878 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001879 if (DEBUG) {
1880 Slog.d(TAG, "Update state(" + imm.getId()
1881 + "): DISABLED_UNTIL_USED -> DEFAULT");
1882 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001883 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1884 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001885 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
1886 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001887 }
1888 } catch (RemoteException e) {
1889 }
1890 }
1891 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001892 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1893 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1894 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1895 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001896 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001897 // There is no input method selected, try to choose new applicable input method.
1898 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001899 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001900 }
1901 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001902 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001903 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001904 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001905 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001906 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001907 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001908 }
satokf3db1af2010-11-23 13:34:33 +09001909 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001910 } else {
1911 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001912 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001913 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001914 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09001915 // Here is not the perfect place to reset the switching controller. Ideally
1916 // mSwitchingController and mSettings should be able to share the same state.
1917 // TODO: Make sure that mSwitchingController and mSettings are sharing the
1918 // the same enabled IMEs list.
1919 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07001920
1921 }
1922
1923 public void updateKeyboardFromSettingsLocked() {
1924 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
1925 if (mSwitchingDialog != null
1926 && mSwitchingDialogTitleView != null
1927 && mSwitchingDialog.isShowing()) {
1928 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
1929 com.android.internal.R.id.hard_keyboard_switch);
1930 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
1931 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001932 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001933
satokab751aa2010-09-14 19:17:36 +09001934 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001935 InputMethodInfo info = mMethodMap.get(id);
1936 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001937 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001938 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001939
Yohei Yukawae63b5fa2014-09-19 13:14:55 +09001940 if (mCurClient != null && mCurAttribute != null) {
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001941 // We have already made sure that the package name belongs to the application's UID.
1942 // No further UID check is required.
1943 if (SystemConfig.getInstance().getFixedImeApps().contains(mCurAttribute.packageName)) {
1944 return;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +09001945 }
1946 }
1947
satokd81e9502012-05-21 12:58:45 +09001948 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001949 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09001950 final int subtypeCount = info.getSubtypeCount();
1951 if (subtypeCount <= 0) {
1952 return;
satokcd7cd292010-11-20 15:46:23 +09001953 }
satokd81e9502012-05-21 12:58:45 +09001954 final InputMethodSubtype oldSubtype = mCurrentSubtype;
1955 final InputMethodSubtype newSubtype;
1956 if (subtypeId >= 0 && subtypeId < subtypeCount) {
1957 newSubtype = info.getSubtypeAt(subtypeId);
1958 } else {
1959 // If subtype is null, try to find the most applicable one from
1960 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001961 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09001962 }
1963 if (newSubtype == null || oldSubtype == null) {
1964 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
1965 + ", new subtype = " + newSubtype);
1966 return;
1967 }
1968 if (newSubtype != oldSubtype) {
1969 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1970 if (mCurMethod != null) {
1971 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001972 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokd81e9502012-05-21 12:58:45 +09001973 mCurMethod.changeInputMethodSubtype(newSubtype);
1974 } catch (RemoteException e) {
1975 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
satokab751aa2010-09-14 19:17:36 +09001976 }
1977 }
1978 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001979 return;
1980 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001981
satokd81e9502012-05-21 12:58:45 +09001982 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001983 final long ident = Binder.clearCallingIdentity();
1984 try {
satokab751aa2010-09-14 19:17:36 +09001985 // Set a subtype to this input method.
1986 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001987 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1988 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1989 // because mCurMethodId is stored as a history in
1990 // setSelectedInputMethodAndSubtypeLocked().
1991 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001992
1993 if (ActivityManagerNative.isSystemReady()) {
1994 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001995 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001996 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07001997 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001998 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001999 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002000 } finally {
2001 Binder.restoreCallingIdentity(ident);
2002 }
2003 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002004
satok42c5a162011-05-26 16:46:14 +09002005 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002006 public boolean showSoftInput(IInputMethodClient client, int flags,
2007 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002008 if (!calledFromValidUser()) {
2009 return false;
2010 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002011 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002012 long ident = Binder.clearCallingIdentity();
2013 try {
2014 synchronized (mMethodMap) {
2015 if (mCurClient == null || client == null
2016 || mCurClient.client.asBinder() != client.asBinder()) {
2017 try {
2018 // We need to check if this is the current client with
2019 // focus in the window manager, to allow this call to
2020 // be made before input is started in it.
2021 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002022 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002023 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002024 }
2025 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002026 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002027 }
2028 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002029
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002030 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002031 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002032 }
2033 } finally {
2034 Binder.restoreCallingIdentity(ident);
2035 }
2036 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002037
The Android Open Source Project4df24232009-03-05 14:34:35 -08002038 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002039 mShowRequested = true;
2040 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
2041 mShowExplicitlyRequested = true;
2042 }
2043 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
2044 mShowExplicitlyRequested = true;
2045 mShowForced = true;
2046 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002047
Dianne Hackborncc278702009-09-02 23:07:23 -07002048 if (!mSystemReady) {
2049 return false;
2050 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002051
The Android Open Source Project4df24232009-03-05 14:34:35 -08002052 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002053 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002054 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002055 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
2056 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
2057 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002058 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002059 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002060 bindCurrentInputMethodService(
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07002061 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE
Dianne Hackbornd69e4c12015-04-24 09:54:54 -07002062 | Context.BIND_TREAT_LIKE_ACTIVITY
2063 | Context.BIND_FOREGROUND_SERVICE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002064 mVisibleBound = true;
2065 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002066 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002067 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09002068 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002069 // The client has asked to have the input method shown, but
2070 // we have been sitting here too long with a connection to the
2071 // service and no interface received, so let's disconnect/connect
2072 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002073 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002074 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09002075 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002076 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002077 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002078 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002079 } else {
2080 if (DEBUG) {
2081 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
2082 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
2083 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002084 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002085
The Android Open Source Project4df24232009-03-05 14:34:35 -08002086 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002087 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002088
satok42c5a162011-05-26 16:46:14 +09002089 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002090 public boolean hideSoftInput(IInputMethodClient client, int flags,
2091 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002092 if (!calledFromValidUser()) {
2093 return false;
2094 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002095 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002096 long ident = Binder.clearCallingIdentity();
2097 try {
2098 synchronized (mMethodMap) {
2099 if (mCurClient == null || client == null
2100 || mCurClient.client.asBinder() != client.asBinder()) {
2101 try {
2102 // We need to check if this is the current client with
2103 // focus in the window manager, to allow this call to
2104 // be made before input is started in it.
2105 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002106 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
2107 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002108 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002109 }
2110 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002111 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002112 }
2113 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002114
Joe Onorato8a9b2202010-02-26 18:56:32 -08002115 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002116 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002117 }
2118 } finally {
2119 Binder.restoreCallingIdentity(ident);
2120 }
2121 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002122
The Android Open Source Project4df24232009-03-05 14:34:35 -08002123 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002124 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
2125 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002126 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 -08002127 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002128 }
2129 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002130 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 -08002131 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002132 }
Seigo Nonakaec928652015-06-10 15:31:20 +09002133
2134 // There is a chance that IMM#hideSoftInput() is called in a transient state where
2135 // IMMS#InputShown is already updated to be true whereas IMMS#mImeWindowVis is still waiting
2136 // to be updated with the new value sent from IME process. Even in such a transient state
2137 // historically we have accepted an incoming call of IMM#hideSoftInput() from the
2138 // application process as a valid request, and have even promised such a behavior with CTS
2139 // since Android Eclair. That's why we need to accept IMM#hideSoftInput() even when only
2140 // IMMS#InputShown indicates that the software keyboard is shown.
2141 // TODO: Clean up, IMMS#mInputShown, IMMS#mImeWindowVis and mShowRequested.
2142 final boolean shouldHideSoftInput = (mCurMethod != null) && (mInputShown ||
2143 (mImeWindowVis & InputMethodService.IME_ACTIVE) != 0);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002144 boolean res;
Seigo Nonakaec928652015-06-10 15:31:20 +09002145 if (shouldHideSoftInput) {
2146 // The IME will report its visible state again after the following message finally
2147 // delivered to the IME process as an IPC. Hence the inconsistency between
2148 // IMMS#mInputShown and IMMS#mImeWindowVis should be resolved spontaneously in
2149 // the final state.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002150 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2151 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
2152 res = true;
2153 } else {
2154 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002155 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002156 if (mHaveConnection && mVisibleBound) {
2157 mContext.unbindService(mVisibleConnection);
2158 mVisibleBound = false;
2159 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002160 mInputShown = false;
2161 mShowRequested = false;
2162 mShowExplicitlyRequested = false;
2163 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002164 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002165 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002166
satok42c5a162011-05-26 16:46:14 +09002167 @Override
Dianne Hackborn7663d802012-02-24 13:08:49 -08002168 public InputBindResult windowGainedFocus(IInputMethodClient client, IBinder windowToken,
2169 int controlFlags, int softInputMode, int windowFlags,
2170 EditorInfo attribute, IInputContext inputContext) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002171 // Needs to check the validity before clearing calling identity
2172 final boolean calledFromValidUser = calledFromValidUser();
2173
Dianne Hackborn7663d802012-02-24 13:08:49 -08002174 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002175 long ident = Binder.clearCallingIdentity();
2176 try {
2177 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002178 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
Dianne Hackborn7663d802012-02-24 13:08:49 -08002179 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002180 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08002181 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002182
Dianne Hackborn7663d802012-02-24 13:08:49 -08002183 ClientState cs = mClients.get(client.asBinder());
2184 if (cs == null) {
2185 throw new IllegalArgumentException("unknown client "
2186 + client.asBinder());
2187 }
2188
2189 try {
2190 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2191 // Check with the window manager to make sure this client actually
2192 // has a window with focus. If not, reject. This is thread safe
2193 // because if the focus changes some time before or after, the
2194 // next client receiving focus that has any interest in input will
2195 // be calling through here after that change happens.
2196 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2197 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2198 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002199 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002200 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002201 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002202
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002203 if (!calledFromValidUser) {
2204 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2205 Slog.w(TAG, "If you want to interect with IME, you need "
2206 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2207 hideCurrentInputLocked(0, null);
2208 return null;
2209 }
2210
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002211 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07002212 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09002213 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002214 if (attribute != null) {
2215 return startInputUncheckedLocked(cs, inputContext, attribute,
2216 controlFlags);
2217 }
2218 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002219 }
2220 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002221
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002222 // Should we auto-show the IME even if the caller has not
2223 // specified what should be done with it?
2224 // We only do this automatically if the window can resize
2225 // to accommodate the IME (so what the user sees will give
2226 // them good context without input information being obscured
2227 // by the IME) or if running on a large screen where there
2228 // is more room for the target window + IME.
2229 final boolean doAutoShow =
2230 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2231 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2232 || mRes.getConfiguration().isLayoutSizeAtLeast(
2233 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002234 final boolean isTextEditor =
2235 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2236
2237 // We want to start input before showing the IME, but after closing
2238 // it. We want to do this after closing it to help the IME disappear
2239 // more quickly (not get stuck behind it initializing itself for the
2240 // new focused input, even if its window wants to hide the IME).
2241 boolean didStart = false;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002242
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002243 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2244 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002245 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002246 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2247 // There is no focus view, and this window will
2248 // be behind any soft input window, so hide the
2249 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002250 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002251 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002252 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002253 } else if (isTextEditor && doAutoShow && (softInputMode &
2254 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002255 // There is a focus view, and we are navigating forward
2256 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002257 // We only do this automatically if the window can resize
2258 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002259 // them good context without input information being obscured
2260 // by the IME) or if running on a large screen where there
2261 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002262 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002263 if (attribute != null) {
2264 res = startInputUncheckedLocked(cs, inputContext, attribute,
2265 controlFlags);
2266 didStart = true;
2267 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002268 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002269 }
2270 break;
2271 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2272 // Do nothing.
2273 break;
2274 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2275 if ((softInputMode &
2276 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002277 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002278 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002279 }
2280 break;
2281 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002282 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002283 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002284 break;
2285 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2286 if ((softInputMode &
2287 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002288 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002289 if (attribute != null) {
2290 res = startInputUncheckedLocked(cs, inputContext, attribute,
2291 controlFlags);
2292 didStart = true;
2293 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002294 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002295 }
2296 break;
2297 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002298 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002299 if (attribute != null) {
2300 res = startInputUncheckedLocked(cs, inputContext, attribute,
2301 controlFlags);
2302 didStart = true;
2303 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002304 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002305 break;
2306 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002307
2308 if (!didStart && attribute != null) {
2309 res = startInputUncheckedLocked(cs, inputContext, attribute,
2310 controlFlags);
2311 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002312 }
2313 } finally {
2314 Binder.restoreCallingIdentity(ident);
2315 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002316
2317 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002318 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002319
satok42c5a162011-05-26 16:46:14 +09002320 @Override
Seigo Nonaka14e13912015-05-06 21:04:13 -07002321 public void showInputMethodPickerFromClient(
2322 IInputMethodClient client, int auxiliarySubtypeMode) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002323 if (!calledFromValidUser()) {
2324 return;
2325 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002326 synchronized (mMethodMap) {
2327 if (mCurClient == null || client == null
2328 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002329 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002330 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002331 }
2332
satok440aab52010-11-25 09:43:11 +09002333 // Always call subtype picker, because subtype picker is a superset of input method
2334 // picker.
Seigo Nonaka14e13912015-05-06 21:04:13 -07002335 mHandler.sendMessage(mCaller.obtainMessageI(
2336 MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode));
satokab751aa2010-09-14 19:17:36 +09002337 }
2338 }
2339
satok42c5a162011-05-26 16:46:14 +09002340 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002341 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002342 if (!calledFromValidUser()) {
2343 return;
2344 }
satok28203512010-11-24 11:06:49 +09002345 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2346 }
2347
satok42c5a162011-05-26 16:46:14 +09002348 @Override
satok28203512010-11-24 11:06:49 +09002349 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002350 if (!calledFromValidUser()) {
2351 return;
2352 }
satok28203512010-11-24 11:06:49 +09002353 synchronized (mMethodMap) {
2354 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002355 setInputMethodWithSubtypeIdLocked(token, id,
2356 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2357 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002358 } else {
2359 setInputMethod(token, id);
2360 }
2361 }
satokab751aa2010-09-14 19:17:36 +09002362 }
2363
satok42c5a162011-05-26 16:46:14 +09002364 @Override
satokb416a712010-11-25 20:42:14 +09002365 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002366 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002367 if (!calledFromValidUser()) {
2368 return;
2369 }
satokb416a712010-11-25 20:42:14 +09002370 synchronized (mMethodMap) {
2371 if (mCurClient == null || client == null
2372 || mCurClient.client.asBinder() != client.asBinder()) {
2373 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
2374 }
satok7fee71f2010-12-17 18:54:26 +09002375 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2376 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002377 }
2378 }
2379
satok4fc87d62011-05-20 16:13:43 +09002380 @Override
satok735cf382010-11-11 20:40:09 +09002381 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002382 if (!calledFromValidUser()) {
2383 return false;
2384 }
satok735cf382010-11-11 20:40:09 +09002385 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002386 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002387 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002388 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002389 lastImi = mMethodMap.get(lastIme.first);
2390 } else {
2391 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002392 }
satok4fc87d62011-05-20 16:13:43 +09002393 String targetLastImiId = null;
2394 int subtypeId = NOT_A_SUBTYPE_ID;
2395 if (lastIme != null && lastImi != null) {
2396 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
2397 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
2398 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2399 : mCurrentSubtype.hashCode();
2400 // If the last IME is the same as the current IME and the last subtype is not
2401 // defined, there is no need to switch to the last IME.
2402 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2403 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002404 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002405 }
2406 }
2407
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002408 if (TextUtils.isEmpty(targetLastImiId)
2409 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002410 // This is a safety net. If the currentSubtype can't be added to the history
2411 // and the framework couldn't find the last ime, we will make the last ime be
2412 // the most applicable enabled keyboard subtype of the system imes.
2413 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2414 if (enabled != null) {
2415 final int N = enabled.size();
2416 final String locale = mCurrentSubtype == null
2417 ? mRes.getConfiguration().locale.toString()
2418 : mCurrentSubtype.getLocale();
2419 for (int i = 0; i < N; ++i) {
2420 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002421 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002422 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002423 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2424 InputMethodUtils.getSubtypes(imi),
2425 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002426 if (keyboardSubtype != null) {
2427 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002428 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002429 imi, keyboardSubtype.hashCode());
2430 if(keyboardSubtype.getLocale().equals(locale)) {
2431 break;
2432 }
2433 }
2434 }
2435 }
2436 }
2437 }
2438
2439 if (!TextUtils.isEmpty(targetLastImiId)) {
2440 if (DEBUG) {
2441 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2442 + ", from: " + mCurMethodId + ", " + subtypeId);
2443 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002444 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002445 return true;
2446 } else {
2447 return false;
2448 }
satok735cf382010-11-11 20:40:09 +09002449 }
2450 }
2451
satoke7c6998e2011-06-03 17:57:59 +09002452 @Override
satok688bd472012-02-09 20:09:17 +09002453 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002454 if (!calledFromValidUser()) {
2455 return false;
2456 }
satok688bd472012-02-09 20:09:17 +09002457 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002458 if (!calledWithValidToken(token)) {
2459 final int uid = Binder.getCallingUid();
2460 Slog.e(TAG, "Ignoring switchToNextInputMethod due to an invalid token. uid:" + uid
2461 + " token:" + token);
2462 return false;
2463 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002464 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
satok688bd472012-02-09 20:09:17 +09002465 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2466 if (nextSubtype == null) {
2467 return false;
2468 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002469 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2470 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002471 return true;
2472 }
2473 }
2474
2475 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002476 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2477 if (!calledFromValidUser()) {
2478 return false;
2479 }
2480 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002481 if (!calledWithValidToken(token)) {
2482 final int uid = Binder.getCallingUid();
2483 Slog.e(TAG, "Ignoring shouldOfferSwitchingToNextInputMethod due to an invalid "
2484 + "token. uid:" + uid + " token:" + token);
2485 return false;
2486 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002487 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002488 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2489 if (nextSubtype == null) {
2490 return false;
2491 }
2492 return true;
2493 }
2494 }
2495
2496 @Override
satok68f1b782011-04-11 14:26:04 +09002497 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002498 if (!calledFromValidUser()) {
2499 return null;
2500 }
satok68f1b782011-04-11 14:26:04 +09002501 synchronized (mMethodMap) {
2502 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2503 // TODO: Handle the case of the last IME with no subtypes
2504 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2505 || TextUtils.isEmpty(lastIme.second)) return null;
2506 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2507 if (lastImi == null) return null;
2508 try {
2509 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002510 final int lastSubtypeId =
2511 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002512 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2513 return null;
2514 }
2515 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002516 } catch (NumberFormatException e) {
2517 return null;
2518 }
2519 }
2520 }
2521
satoke7c6998e2011-06-03 17:57:59 +09002522 @Override
satokee5e77c2011-09-02 18:50:15 +09002523 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002524 if (!calledFromValidUser()) {
2525 return;
2526 }
satok91e88122011-07-18 11:11:42 +09002527 // By this IPC call, only a process which shares the same uid with the IME can add
2528 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09002529 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09002530 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002531 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002532 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002533 final String[] packageInfos;
2534 try {
2535 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2536 } catch (RemoteException e) {
2537 Slog.e(TAG, "Failed to get package infos");
2538 return;
2539 }
satok91e88122011-07-18 11:11:42 +09002540 if (packageInfos != null) {
2541 final int packageNum = packageInfos.length;
2542 for (int i = 0; i < packageNum; ++i) {
2543 if (packageInfos[i].equals(imi.getPackageName())) {
2544 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002545 final long ident = Binder.clearCallingIdentity();
2546 try {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002547 buildInputMethodListLocked(mMethodList, mMethodMap,
2548 false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002549 } finally {
2550 Binder.restoreCallingIdentity(ident);
2551 }
satokee5e77c2011-09-02 18:50:15 +09002552 return;
satok91e88122011-07-18 11:11:42 +09002553 }
2554 }
2555 }
satoke7c6998e2011-06-03 17:57:59 +09002556 }
satokee5e77c2011-09-02 18:50:15 +09002557 return;
satoke7c6998e2011-06-03 17:57:59 +09002558 }
2559
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002560 @Override
2561 public int getInputMethodWindowVisibleHeight() {
2562 return mWindowManagerService.getInputMethodWindowVisibleHeight();
2563 }
2564
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002565 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002566 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002567 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002568 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002569 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002570 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002571 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
2572 if (DEBUG) {
2573 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
2574 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
2575 + " actual: " + sequenceNumber);
2576 }
2577 return;
2578 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002579 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2580 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09002581 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002582 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002583 }
2584 }
2585
satok28203512010-11-24 11:06:49 +09002586 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002587 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002588 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
2589 }
2590 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002591
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002592 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
2593 if (token == null) {
2594 if (mContext.checkCallingOrSelfPermission(
2595 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2596 != PackageManager.PERMISSION_GRANTED) {
2597 throw new SecurityException(
2598 "Using null token requires permission "
2599 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002600 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002601 } else if (mCurToken != token) {
2602 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2603 + " token: " + token);
2604 return;
2605 }
2606
2607 final long ident = Binder.clearCallingIdentity();
2608 try {
2609 setInputMethodLocked(id, subtypeId);
2610 } finally {
2611 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002612 }
2613 }
2614
satok42c5a162011-05-26 16:46:14 +09002615 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002616 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002617 if (!calledFromValidUser()) {
2618 return;
2619 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002620 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002621 if (!calledWithValidToken(token)) {
2622 final int uid = Binder.getCallingUid();
2623 Slog.e(TAG, "Ignoring hideInputMethod due to an invalid token. uid:"
2624 + uid + " token:" + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002625 return;
2626 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002627 long ident = Binder.clearCallingIdentity();
2628 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002629 hideCurrentInputLocked(flags, null);
2630 } finally {
2631 Binder.restoreCallingIdentity(ident);
2632 }
2633 }
2634 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002635
satok42c5a162011-05-26 16:46:14 +09002636 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002637 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002638 if (!calledFromValidUser()) {
2639 return;
2640 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002641 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002642 if (!calledWithValidToken(token)) {
2643 final int uid = Binder.getCallingUid();
2644 Slog.e(TAG, "Ignoring showMySoftInput due to an invalid token. uid:"
2645 + uid + " token:" + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002646 return;
2647 }
2648 long ident = Binder.clearCallingIdentity();
2649 try {
2650 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002651 } finally {
2652 Binder.restoreCallingIdentity(ident);
2653 }
2654 }
2655 }
2656
2657 void setEnabledSessionInMainThread(SessionState session) {
2658 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002659 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002660 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002661 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002662 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002663 } catch (RemoteException e) {
2664 }
2665 }
2666 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002667 if (mEnabledSession != null && mEnabledSession.session != null) {
2668 try {
2669 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
2670 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
2671 } catch (RemoteException e) {
2672 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002673 }
2674 }
2675 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002676
satok42c5a162011-05-26 16:46:14 +09002677 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002678 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002679 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002680 switch (msg.what) {
satokab751aa2010-09-14 19:17:36 +09002681 case MSG_SHOW_IM_SUBTYPE_PICKER:
Seigo Nonaka14e13912015-05-06 21:04:13 -07002682 final boolean showAuxSubtypes;
2683 switch (msg.arg1) {
2684 case InputMethodManager.SHOW_IM_PICKER_MODE_AUTO:
2685 // This is undocumented so far, but IMM#showInputMethodPicker() has been
2686 // implemented so that auxiliary subtypes will be excluded when the soft
2687 // keyboard is invisible.
2688 showAuxSubtypes = mInputShown;
2689 break;
2690 case InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES:
2691 showAuxSubtypes = true;
2692 break;
2693 case InputMethodManager.SHOW_IM_PICKER_MODE_EXCLUDE_AUXILIARY_SUBTYPES:
2694 showAuxSubtypes = false;
2695 break;
2696 default:
2697 Slog.e(TAG, "Unknown subtype picker mode = " + msg.arg1);
2698 return false;
2699 }
2700 showInputMethodMenu(showAuxSubtypes);
satokab751aa2010-09-14 19:17:36 +09002701 return true;
2702
satok47a44912010-10-06 16:03:58 +09002703 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002704 args = (SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09002705 showInputMethodAndSubtypeEnabler((String)args.arg1);
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002706 args.recycle();
satok217f5482010-12-15 05:19:19 +09002707 return true;
2708
2709 case MSG_SHOW_IM_CONFIG:
2710 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002711 return true;
2712
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002713 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002714
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002715 case MSG_UNBIND_INPUT:
2716 try {
2717 ((IInputMethod)msg.obj).unbindInput();
2718 } catch (RemoteException e) {
2719 // There is nothing interesting about the method dying.
2720 }
2721 return true;
2722 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002723 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002724 try {
2725 ((IInputMethod)args.arg1).bindInput((InputBinding)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;
2730 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002731 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002732 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002733 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002734 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002735 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002736 } catch (RemoteException e) {
2737 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002738 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002739 return true;
2740 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002741 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002742 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002743 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002744 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002745 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002746 } catch (RemoteException e) {
2747 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002748 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002749 return true;
2750 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002751 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002752 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002753 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002754 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2755 } catch (RemoteException e) {
2756 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002757 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002758 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002759 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002760 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002761 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002762 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002763 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002764 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002765 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002766 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002767 // Dispose the channel if the input method is not local to this process
2768 // because the remote proxy will get its own copy when unparceled.
2769 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002770 channel.dispose();
2771 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002772 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002773 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002774 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002775 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002776 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002777
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002778 case MSG_START_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002779 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002780 try {
2781 SessionState session = (SessionState)args.arg1;
2782 setEnabledSessionInMainThread(session);
2783 session.method.startInput((IInputContext)args.arg2,
2784 (EditorInfo)args.arg3);
2785 } catch (RemoteException e) {
2786 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002787 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002788 return true;
2789 case MSG_RESTART_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002790 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002791 try {
2792 SessionState session = (SessionState)args.arg1;
2793 setEnabledSessionInMainThread(session);
2794 session.method.restartInput((IInputContext)args.arg2,
2795 (EditorInfo)args.arg3);
2796 } catch (RemoteException e) {
2797 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002798 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002799 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002800
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002801 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002802
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002803 case MSG_UNBIND_METHOD:
2804 try {
2805 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
2806 } catch (RemoteException e) {
2807 // There is nothing interesting about the last client dying.
2808 }
2809 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002810 case MSG_BIND_METHOD: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002811 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002812 IInputMethodClient client = (IInputMethodClient)args.arg1;
2813 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002814 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002815 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002816 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002817 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002818 } finally {
2819 // Dispose the channel if the input method is not local to this process
2820 // because the remote proxy will get its own copy when unparceled.
2821 if (res.channel != null && Binder.isProxy(client)) {
2822 res.channel.dispose();
2823 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002824 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002825 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002826 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002827 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002828 case MSG_SET_ACTIVE:
2829 try {
2830 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2831 } catch (RemoteException e) {
2832 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2833 + ((ClientState)msg.obj).pid + " uid "
2834 + ((ClientState)msg.obj).uid);
2835 }
2836 return true;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002837 case MSG_SET_INTERACTIVE:
2838 handleSetInteractive(msg.arg1 != 0);
2839 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002840 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
2841 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07002842 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002843 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07002844 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002845 } catch (RemoteException e) {
2846 Slog.w(TAG, "Got RemoteException sending "
2847 + "setUserActionNotificationSequenceNumber("
2848 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07002849 + clientState.pid + " uid "
2850 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002851 }
2852 return true;
2853 }
satok01038492012-04-09 21:08:27 +09002854
2855 // --------------------------------------------------------------
2856 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07002857 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09002858 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002859 }
2860 return false;
2861 }
2862
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002863 private void handleSetInteractive(final boolean interactive) {
2864 synchronized (mMethodMap) {
2865 mIsInteractive = interactive;
2866 updateSystemUiLocked(mCurToken, interactive ? mImeWindowVis : 0, mBackDisposition);
2867
2868 // Inform the current client of the change in active status
2869 if (mCurClient != null && mCurClient.client != null) {
2870 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
2871 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, mCurClient));
2872 }
2873 }
2874 }
2875
satokdc9ddae2011-10-06 12:22:36 +09002876 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002877 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
2878 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09002879 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002880 if (DEBUG) {
2881 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2882 }
satok723a27e2010-11-11 14:58:11 +09002883 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002884 return true;
2885 }
2886
2887 return false;
2888 }
2889
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002890 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002891 HashMap<String, InputMethodInfo> map, boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002892 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002893 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Satoshi Kataoka0766eb02013-07-31 18:30:13 +09002894 + " \n ------ \n" + InputMethodUtils.getStackTrace());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002895 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002896 list.clear();
2897 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002898
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002899 // Use for queryIntentServicesAsUser
2900 final PackageManager pm = mContext.getPackageManager();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002901 String disabledSysImes = mSettings.getDisabledSystemInputMethods();
Amith Yamasanie861ec12010-03-24 21:39:27 -07002902 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002903
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002904 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002905 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002906 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2907 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002908
satoke7c6998e2011-06-03 17:57:59 +09002909 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2910 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002911 for (int i = 0; i < services.size(); ++i) {
2912 ResolveInfo ri = services.get(i);
2913 ServiceInfo si = ri.serviceInfo;
2914 ComponentName compName = new ComponentName(si.packageName, si.name);
2915 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2916 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002917 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002918 + ": it does not require the permission "
2919 + android.Manifest.permission.BIND_INPUT_METHOD);
2920 continue;
2921 }
2922
Joe Onorato8a9b2202010-02-26 18:56:32 -08002923 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002924
2925 try {
satoke7c6998e2011-06-03 17:57:59 +09002926 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002927 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002928 final String id = p.getId();
2929 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002930
2931 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002932 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002933 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002935 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002936 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002937 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002938 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002939 }
2940 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002941
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002942 if (resetDefaultEnabledIme) {
2943 final ArrayList<InputMethodInfo> defaultEnabledIme =
2944 InputMethodUtils.getDefaultEnabledImes(mContext, mSystemReady, list);
2945 for (int i = 0; i < defaultEnabledIme.size(); ++i) {
2946 final InputMethodInfo imi = defaultEnabledIme.get(i);
2947 if (DEBUG) {
2948 Slog.d(TAG, "--- enable ime = " + imi);
2949 }
2950 setInputMethodEnabledLocked(imi.getId(), true);
2951 }
2952 }
2953
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002954 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09002955 if (!TextUtils.isEmpty(defaultImiId)) {
2956 if (!map.containsKey(defaultImiId)) {
2957 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
2958 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07002959 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09002960 }
2961 } else {
2962 // Double check that the default IME is certainly enabled.
2963 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002964 }
2965 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09002966 // Here is not the perfect place to reset the switching controller. Ideally
2967 // mSwitchingController and mSettings should be able to share the same state.
2968 // TODO: Make sure that mSwitchingController and mSettings are sharing the
2969 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09002970 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002971 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002972
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002973 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002974
satok217f5482010-12-15 05:19:19 +09002975 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002976 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002977 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002978 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2979 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002980 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002981 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002982 }
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002983 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok217f5482010-12-15 05:19:19 +09002984 }
2985
2986 private void showConfigureInputMethods() {
2987 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2988 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2989 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2990 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002991 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09002992 }
2993
satok2c93efc2012-04-02 19:33:47 +09002994 private boolean isScreenLocked() {
2995 return mKeyguardManager != null
2996 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
2997 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002998
Seigo Nonaka14e13912015-05-06 21:04:13 -07002999 private void showInputMethodMenu(boolean showAuxSubtypes) {
3000 if (DEBUG) Slog.v(TAG, "Show switching menu. showAuxSubtypes=" + showAuxSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003001
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003002 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09003003 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003004
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003005 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003006 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003007 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003008
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003009 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09003010 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09003011 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
3012 mContext);
satok7f35c8c2010-10-07 21:13:11 +09003013 if (immis == null || immis.size() == 0) {
3014 return;
3015 }
3016
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003017 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003018
satok688bd472012-02-09 20:09:17 +09003019 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003020 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
Seigo Nonaka14e13912015-05-06 21:04:13 -07003021 true /* showSubtypes */, showAuxSubtypes, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09003022
satokc3690562012-01-10 20:14:43 +09003023 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003024 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09003025 if (currentSubtype != null) {
3026 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003027 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
3028 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09003029 }
3030 }
3031
Ken Wakasa761eb372011-03-04 19:06:18 +09003032 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09003033 mIms = new InputMethodInfo[N];
3034 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003035 int checkedItem = 0;
3036 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09003037 final ImeSubtypeListItem item = imList.get(i);
3038 mIms[i] = item.mImi;
3039 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003040 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09003041 int subtypeId = mSubtypeIds[i];
3042 if ((subtypeId == NOT_A_SUBTYPE_ID)
3043 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
3044 || (subtypeId == lastInputMethodSubtypeId)) {
3045 checkedItem = i;
3046 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003047 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003048 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08003049
3050 final Context settingsContext = new ContextThemeWrapper(context,
3051 com.android.internal.R.style.Theme_DeviceDefault_Settings);
3052
3053 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003054 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
3055 @Override
3056 public void onCancel(DialogInterface dialog) {
3057 hideInputMethodMenu();
3058 }
3059 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08003060
3061 final Context dialogContext = mDialogBuilder.getContext();
3062 final TypedArray a = dialogContext.obtainStyledAttributes(null,
3063 com.android.internal.R.styleable.DialogPreference,
3064 com.android.internal.R.attr.alertDialogStyle, 0);
3065 final Drawable dialogIcon = a.getDrawable(
3066 com.android.internal.R.styleable.DialogPreference_dialogIcon);
3067 a.recycle();
3068
3069 mDialogBuilder.setIcon(dialogIcon);
3070
3071 final LayoutInflater inflater = (LayoutInflater) dialogContext.getSystemService(
3072 Context.LAYOUT_INFLATER_SERVICE);
satok01038492012-04-09 21:08:27 +09003073 final View tv = inflater.inflate(
3074 com.android.internal.R.layout.input_method_switch_dialog_title, null);
3075 mDialogBuilder.setCustomTitle(tv);
3076
3077 // Setup layout for a toggle switch of the hardware keyboard
3078 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003079 mSwitchingDialogTitleView
3080 .findViewById(com.android.internal.R.id.hard_keyboard_section)
3081 .setVisibility(mWindowManagerService.isHardKeyboardAvailable()
3082 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003083 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003084 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07003085 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003086 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
3087 @Override
3088 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003089 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003090 // Ensure that the input method dialog is dismissed when changing
3091 // the hardware keyboard state.
3092 hideInputMethodMenu();
3093 }
3094 });
3095
Alan Viverette505e3ab2014-11-24 15:22:11 -08003096 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003097 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
3098 final OnClickListener choiceListener = new OnClickListener() {
3099 @Override
3100 public void onClick(final DialogInterface dialog, final int which) {
3101 synchronized (mMethodMap) {
3102 if (mIms == null || mIms.length <= which || mSubtypeIds == null
3103 || mSubtypeIds.length <= which) {
3104 return;
satok01038492012-04-09 21:08:27 +09003105 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003106 final InputMethodInfo im = mIms[which];
3107 int subtypeId = mSubtypeIds[which];
3108 adapter.mCheckedItem = which;
3109 adapter.notifyDataSetChanged();
3110 hideInputMethodMenu();
3111 if (im != null) {
3112 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
3113 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003114 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003115 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003116 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003117 }
3118 }
3119 };
3120 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003121
Seigo Nonakad4474cb2015-05-04 16:53:24 -07003122 if (!isScreenLocked) {
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003123 final OnClickListener positiveListener = new OnClickListener() {
3124 @Override
3125 public void onClick(DialogInterface dialog, int whichButton) {
3126 showConfigureInputMethods();
3127 }
3128 };
satok82beadf2010-12-27 19:03:06 +09003129 mDialogBuilder.setPositiveButton(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003130 com.android.internal.R.string.configure_input_methods, positiveListener);
satok7f35c8c2010-10-07 21:13:11 +09003131 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003132 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003133 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003134 mSwitchingDialog.getWindow().setType(
3135 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09003136 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
3137 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003138 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003139 updateSystemUi(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003140 mSwitchingDialog.show();
3141 }
3142 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003143
Ken Wakasa05dbb652011-08-22 15:22:43 +09003144 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
3145 private final LayoutInflater mInflater;
3146 private final int mTextViewResourceId;
3147 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09003148 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09003149 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
3150 List<ImeSubtypeListItem> itemsList, int checkedItem) {
3151 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003152
Ken Wakasa05dbb652011-08-22 15:22:43 +09003153 mTextViewResourceId = textViewResourceId;
3154 mItemsList = itemsList;
3155 mCheckedItem = checkedItem;
Alan Viverette505e3ab2014-11-24 15:22:11 -08003156 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Ken Wakasa05dbb652011-08-22 15:22:43 +09003157 }
3158
3159 @Override
3160 public View getView(int position, View convertView, ViewGroup parent) {
3161 final View view = convertView != null ? convertView
3162 : mInflater.inflate(mTextViewResourceId, null);
3163 if (position < 0 || position >= mItemsList.size()) return view;
3164 final ImeSubtypeListItem item = mItemsList.get(position);
3165 final CharSequence imeName = item.mImeName;
3166 final CharSequence subtypeName = item.mSubtypeName;
3167 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
3168 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
3169 if (TextUtils.isEmpty(subtypeName)) {
3170 firstTextView.setText(imeName);
3171 secondTextView.setVisibility(View.GONE);
3172 } else {
3173 firstTextView.setText(subtypeName);
3174 secondTextView.setText(imeName);
3175 secondTextView.setVisibility(View.VISIBLE);
3176 }
3177 final RadioButton radioButton =
3178 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
3179 radioButton.setChecked(position == mCheckedItem);
3180 return view;
3181 }
3182 }
3183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003184 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003185 synchronized (mMethodMap) {
3186 hideInputMethodMenuLocked();
3187 }
3188 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003189
The Android Open Source Project10592532009-03-18 17:39:46 -07003190 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003191 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003192
The Android Open Source Project10592532009-03-18 17:39:46 -07003193 if (mSwitchingDialog != null) {
3194 mSwitchingDialog.dismiss();
3195 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003196 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003197
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003198 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project10592532009-03-18 17:39:46 -07003199 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003200 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003201 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003202
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003203 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003204
satok42c5a162011-05-26 16:46:14 +09003205 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003206 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003207 // TODO: Make this work even for non-current users?
3208 if (!calledFromValidUser()) {
3209 return false;
3210 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003211 synchronized (mMethodMap) {
3212 if (mContext.checkCallingOrSelfPermission(
3213 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3214 != PackageManager.PERMISSION_GRANTED) {
3215 throw new SecurityException(
3216 "Requires permission "
3217 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3218 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003220 long ident = Binder.clearCallingIdentity();
3221 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003222 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003223 } finally {
3224 Binder.restoreCallingIdentity(ident);
3225 }
3226 }
3227 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003228
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003229 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3230 // Make sure this is a valid input method.
3231 InputMethodInfo imm = mMethodMap.get(id);
3232 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003233 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003234 }
3235
satokd87c2592010-09-29 11:52:06 +09003236 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3237 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003238
satokd87c2592010-09-29 11:52:06 +09003239 if (enabled) {
3240 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3241 if (pair.first.equals(id)) {
3242 // We are enabling this input method, but it is already enabled.
3243 // Nothing to do. The previous state was enabled.
3244 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003245 }
3246 }
satokd87c2592010-09-29 11:52:06 +09003247 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3248 // Previous state was disabled.
3249 return false;
3250 } else {
3251 StringBuilder builder = new StringBuilder();
3252 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3253 builder, enabledInputMethodsList, id)) {
3254 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003255 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003256 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3257 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3258 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003259 }
3260 // Previous state was enabled.
3261 return true;
3262 } else {
3263 // We are disabling the input method but it is already disabled.
3264 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003265 return false;
3266 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003267 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003268 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003269
satok723a27e2010-11-11 14:58:11 +09003270 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3271 boolean setSubtypeOnly) {
3272 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003273 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003274
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003275 mCurUserActionNotificationSequenceNumber =
3276 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3277 if (DEBUG) {
3278 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3279 + mCurUserActionNotificationSequenceNumber);
3280 }
3281
3282 if (mCurClient != null && mCurClient.client != null) {
3283 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3284 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003285 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003286 }
3287
satok723a27e2010-11-11 14:58:11 +09003288 // Set Subtype here
3289 if (imi == null || subtypeId < 0) {
3290 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003291 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003292 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003293 if (subtypeId < imi.getSubtypeCount()) {
3294 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3295 mSettings.putSelectedSubtype(subtype.hashCode());
3296 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003297 } else {
3298 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003299 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003300 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003301 }
satokab751aa2010-09-14 19:17:36 +09003302 }
satok723a27e2010-11-11 14:58:11 +09003303
satok4c0e7152012-06-20 20:08:44 +09003304 // Workaround.
3305 // ASEC is not ready in the IMMS constructor. Accordingly, forward-locked
3306 // IMEs are not recognized and considered uninstalled.
3307 // Actually, we can't move everything after SystemReady because
3308 // IMMS needs to run in the encryption lock screen. So, we just skip changing
3309 // the default IME here and try cheking the default IME again in systemReady().
3310 // TODO: Do nothing before system ready and implement a separated logic for
3311 // the encryption lock screen.
3312 // TODO: ASEC should be ready before IMMS is instantiated.
3313 if (mSystemReady && !setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003314 // Set InputMethod here
3315 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3316 }
3317 }
3318
3319 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3320 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3321 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3322 // newDefaultIme is empty when there is no candidate for the selected IME.
3323 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3324 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3325 if (subtypeHashCode != null) {
3326 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003327 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok723a27e2010-11-11 14:58:11 +09003328 imi, Integer.valueOf(subtypeHashCode));
3329 } catch (NumberFormatException e) {
3330 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3331 }
3332 }
3333 }
3334 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003335 }
3336
satok4e4569d2010-11-19 18:45:53 +09003337 // If there are no selected shortcuts, tries finding the most applicable ones.
3338 private Pair<InputMethodInfo, InputMethodSubtype>
3339 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3340 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3341 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003342 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003343 boolean foundInSystemIME = false;
3344
3345 // Search applicable subtype for each InputMethodInfo
3346 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003347 final String imiId = imi.getId();
3348 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3349 continue;
3350 }
satokcd7cd292010-11-20 15:46:23 +09003351 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003352 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003353 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003354 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003355 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003356 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003357 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003358 }
satokdf31ae62011-01-15 06:19:44 +09003359 // 2. Search by the system locale from enabledSubtypes.
3360 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003361 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003362 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003363 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003364 }
satoka86f5e42011-09-02 17:12:42 +09003365 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003366 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003367 final ArrayList<InputMethodSubtype> subtypesForSearch =
3368 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003369 ? InputMethodUtils.getSubtypes(imi)
3370 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003371 // 4. Search by the current subtype's locale from all subtypes.
3372 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003373 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003374 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003375 }
3376 // 5. Search by the system locale from all subtypes.
3377 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003378 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003379 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003380 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003381 }
satokcd7cd292010-11-20 15:46:23 +09003382 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003383 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003384 // The current input method is the most applicable IME.
3385 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003386 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003387 break;
satok7599a7f2010-12-22 13:45:23 +09003388 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003389 // The system input method is 2nd applicable IME.
3390 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003391 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003392 if ((imi.getServiceInfo().applicationInfo.flags
3393 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3394 foundInSystemIME = true;
3395 }
satok4e4569d2010-11-19 18:45:53 +09003396 }
3397 }
3398 }
3399 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003400 if (mostApplicableIMI != null) {
3401 Slog.w(TAG, "Most applicable shortcut input method was:"
3402 + mostApplicableIMI.getId());
3403 if (mostApplicableSubtype != null) {
3404 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3405 + "," + mostApplicableSubtype.getMode() + ","
3406 + mostApplicableSubtype.getLocale());
3407 }
3408 }
satok4e4569d2010-11-19 18:45:53 +09003409 }
satokcd7cd292010-11-20 15:46:23 +09003410 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09003411 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09003412 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003413 } else {
3414 return null;
3415 }
3416 }
3417
satokab751aa2010-09-14 19:17:36 +09003418 /**
3419 * @return Return the current subtype of this input method.
3420 */
satok42c5a162011-05-26 16:46:14 +09003421 @Override
satokab751aa2010-09-14 19:17:36 +09003422 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003423 // TODO: Make this work even for non-current users?
3424 if (!calledFromValidUser()) {
3425 return null;
3426 }
3427 synchronized (mMethodMap) {
3428 return getCurrentInputMethodSubtypeLocked();
3429 }
3430 }
3431
3432 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003433 if (mCurMethodId == null) {
3434 return null;
3435 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003436 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003437 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3438 if (imi == null || imi.getSubtypeCount() == 0) {
3439 return null;
satok4e4569d2010-11-19 18:45:53 +09003440 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003441 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003442 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3443 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003444 if (subtypeId == NOT_A_SUBTYPE_ID) {
3445 // If there are no selected subtypes, the framework will try to find
3446 // the most applicable subtype from explicitly or implicitly enabled
3447 // subtypes.
3448 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003449 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003450 // If there is only one explicitly or implicitly enabled subtype,
3451 // just returns it.
3452 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3453 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3454 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003455 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003456 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003457 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003458 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003459 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003460 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3461 true);
satok4e4569d2010-11-19 18:45:53 +09003462 }
satok3ef8b292010-11-23 06:06:29 +09003463 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003464 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003465 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003466 }
3467 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003468 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003469 }
3470
satokf3db1af2010-11-23 13:34:33 +09003471 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
3472 InputMethodSubtype subtype) {
3473 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
3474 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
3475 } else {
3476 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3477 subtypes.add(subtype);
3478 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
3479 }
3480 }
3481
satok4e4569d2010-11-19 18:45:53 +09003482 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003483 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003484 @Override
satok4e4569d2010-11-19 18:45:53 +09003485 public List getShortcutInputMethodsAndSubtypes() {
3486 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09003487 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09003488 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003489 // If there are no selected shortcut subtypes, the framework will try to find
3490 // the most applicable subtype from all subtypes whose mode is
3491 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003492 Pair<InputMethodInfo, InputMethodSubtype> info =
3493 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003494 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003495 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003496 ret.add(info.first);
3497 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003498 }
satok3da92232011-01-11 22:46:30 +09003499 return ret;
satokf3db1af2010-11-23 13:34:33 +09003500 }
satokf3db1af2010-11-23 13:34:33 +09003501 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3502 ret.add(imi);
3503 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3504 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003505 }
3506 }
satokf3db1af2010-11-23 13:34:33 +09003507 return ret;
satok4e4569d2010-11-19 18:45:53 +09003508 }
3509 }
3510
satok42c5a162011-05-26 16:46:14 +09003511 @Override
satokb66d2872010-11-10 01:04:04 +09003512 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003513 // TODO: Make this work even for non-current users?
3514 if (!calledFromValidUser()) {
3515 return false;
3516 }
satokb66d2872010-11-10 01:04:04 +09003517 synchronized (mMethodMap) {
3518 if (subtype != null && mCurMethodId != null) {
3519 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003520 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003521 if (subtypeId != NOT_A_SUBTYPE_ID) {
3522 setInputMethodLocked(mCurMethodId, subtypeId);
3523 return true;
3524 }
3525 }
3526 return false;
3527 }
3528 }
3529
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003530 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003531 private static class InputMethodFileManager {
3532 private static final String SYSTEM_PATH = "system";
3533 private static final String INPUT_METHOD_PATH = "inputmethod";
3534 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3535 private static final String NODE_SUBTYPES = "subtypes";
3536 private static final String NODE_SUBTYPE = "subtype";
3537 private static final String NODE_IMI = "imi";
3538 private static final String ATTR_ID = "id";
3539 private static final String ATTR_LABEL = "label";
3540 private static final String ATTR_ICON = "icon";
3541 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3542 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3543 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3544 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3545 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3546 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003547 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
satoke7c6998e2011-06-03 17:57:59 +09003548 new HashMap<String, List<InputMethodSubtype>>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003549 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003550 if (methodMap == null) {
3551 throw new NullPointerException("methodMap is null");
3552 }
3553 mMethodMap = methodMap;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003554 final File systemDir = userId == UserHandle.USER_OWNER
3555 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3556 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003557 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3558 if (!inputMethodDir.mkdirs()) {
3559 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3560 }
3561 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3562 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3563 if (!subtypeFile.exists()) {
3564 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003565 writeAdditionalInputMethodSubtypes(
3566 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003567 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003568 readAdditionalInputMethodSubtypes(
3569 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003570 }
3571 }
3572
3573 private void deleteAllInputMethodSubtypes(String imiId) {
3574 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003575 mAdditionalSubtypesMap.remove(imiId);
3576 writeAdditionalInputMethodSubtypes(
3577 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003578 }
3579 }
3580
3581 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003582 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003583 synchronized (mMethodMap) {
3584 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3585 final int N = additionalSubtypes.length;
3586 for (int i = 0; i < N; ++i) {
3587 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003588 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003589 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003590 } else {
3591 Slog.w(TAG, "Duplicated subtype definition found: "
3592 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003593 }
3594 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003595 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3596 writeAdditionalInputMethodSubtypes(
3597 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003598 }
3599 }
3600
3601 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3602 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003603 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003604 }
3605 }
3606
3607 private static void writeAdditionalInputMethodSubtypes(
3608 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3609 HashMap<String, InputMethodInfo> methodMap) {
3610 // Safety net for the case that this function is called before methodMap is set.
3611 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3612 FileOutputStream fos = null;
3613 try {
3614 fos = subtypesFile.startWrite();
3615 final XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003616 out.setOutput(fos, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003617 out.startDocument(null, true);
3618 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3619 out.startTag(null, NODE_SUBTYPES);
3620 for (String imiId : allSubtypes.keySet()) {
3621 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3622 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3623 continue;
3624 }
3625 out.startTag(null, NODE_IMI);
3626 out.attribute(null, ATTR_ID, imiId);
3627 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3628 final int N = subtypesList.size();
3629 for (int i = 0; i < N; ++i) {
3630 final InputMethodSubtype subtype = subtypesList.get(i);
3631 out.startTag(null, NODE_SUBTYPE);
3632 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3633 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3634 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3635 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3636 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3637 out.attribute(null, ATTR_IS_AUXILIARY,
3638 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3639 out.endTag(null, NODE_SUBTYPE);
3640 }
3641 out.endTag(null, NODE_IMI);
3642 }
3643 out.endTag(null, NODE_SUBTYPES);
3644 out.endDocument();
3645 subtypesFile.finishWrite(fos);
3646 } catch (java.io.IOException e) {
3647 Slog.w(TAG, "Error writing subtypes", e);
3648 if (fos != null) {
3649 subtypesFile.failWrite(fos);
3650 }
3651 }
3652 }
3653
3654 private static void readAdditionalInputMethodSubtypes(
3655 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3656 if (allSubtypes == null || subtypesFile == null) return;
3657 allSubtypes.clear();
3658 FileInputStream fis = null;
3659 try {
3660 fis = subtypesFile.openRead();
3661 final XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003662 parser.setInput(fis, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003663 int type = parser.getEventType();
3664 // Skip parsing until START_TAG
3665 while ((type = parser.next()) != XmlPullParser.START_TAG
3666 && type != XmlPullParser.END_DOCUMENT) {}
3667 String firstNodeName = parser.getName();
3668 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3669 throw new XmlPullParserException("Xml doesn't start with subtypes");
3670 }
3671 final int depth =parser.getDepth();
3672 String currentImiId = null;
3673 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3674 while (((type = parser.next()) != XmlPullParser.END_TAG
3675 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3676 if (type != XmlPullParser.START_TAG)
3677 continue;
3678 final String nodeName = parser.getName();
3679 if (NODE_IMI.equals(nodeName)) {
3680 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3681 if (TextUtils.isEmpty(currentImiId)) {
3682 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3683 continue;
3684 }
3685 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
3686 allSubtypes.put(currentImiId, tempSubtypesArray);
3687 } else if (NODE_SUBTYPE.equals(nodeName)) {
3688 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3689 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3690 continue;
3691 }
3692 final int icon = Integer.valueOf(
3693 parser.getAttributeValue(null, ATTR_ICON));
3694 final int label = Integer.valueOf(
3695 parser.getAttributeValue(null, ATTR_LABEL));
3696 final String imeSubtypeLocale =
3697 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3698 final String imeSubtypeMode =
3699 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3700 final String imeSubtypeExtraValue =
3701 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003702 final boolean isAuxiliary = "1".equals(String.valueOf(
3703 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003704 final InputMethodSubtype subtype = new InputMethodSubtypeBuilder()
3705 .setSubtypeNameResId(label)
3706 .setSubtypeIconResId(icon)
3707 .setSubtypeLocale(imeSubtypeLocale)
3708 .setSubtypeMode(imeSubtypeMode)
3709 .setSubtypeExtraValue(imeSubtypeExtraValue)
3710 .setIsAuxiliary(isAuxiliary)
3711 .build();
satoke7c6998e2011-06-03 17:57:59 +09003712 tempSubtypesArray.add(subtype);
3713 }
3714 }
3715 } catch (XmlPullParserException e) {
3716 Slog.w(TAG, "Error reading subtypes: " + e);
3717 return;
3718 } catch (java.io.IOException e) {
3719 Slog.w(TAG, "Error reading subtypes: " + e);
3720 return;
3721 } catch (NumberFormatException e) {
3722 Slog.w(TAG, "Error reading subtypes: " + e);
3723 return;
3724 } finally {
3725 if (fis != null) {
3726 try {
3727 fis.close();
3728 } catch (java.io.IOException e1) {
3729 Slog.w(TAG, "Failed to close.");
3730 }
3731 }
3732 }
3733 }
3734 }
3735
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003736 private static final class LocalServiceImpl implements InputMethodManagerInternal {
3737 @NonNull
3738 private final Handler mHandler;
3739
3740 LocalServiceImpl(@NonNull final Handler handler) {
3741 mHandler = handler;
3742 }
3743
3744 @Override
3745 public void setInteractive(boolean interactive) {
3746 // Do everything in handler so as not to block the caller.
3747 mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_INTERACTIVE,
3748 interactive ? 1 : 0, 0));
3749 }
3750 }
3751
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003752 @Override
3753 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3754 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3755 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003756
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003757 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3758 + Binder.getCallingPid()
3759 + ", uid=" + Binder.getCallingUid());
3760 return;
3761 }
3762
3763 IInputMethod method;
3764 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003765
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003766 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003767
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003768 synchronized (mMethodMap) {
3769 p.println("Current Input Method Manager state:");
3770 int N = mMethodList.size();
3771 p.println(" Input Methods:");
3772 for (int i=0; i<N; i++) {
3773 InputMethodInfo info = mMethodList.get(i);
3774 p.println(" InputMethod #" + i + ":");
3775 info.dump(p, " ");
3776 }
3777 p.println(" Clients:");
3778 for (ClientState ci : mClients.values()) {
3779 p.println(" Client " + ci + ":");
3780 p.println(" client=" + ci.client);
3781 p.println(" inputContext=" + ci.inputContext);
3782 p.println(" sessionRequested=" + ci.sessionRequested);
3783 p.println(" curSession=" + ci.curSession);
3784 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003785 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003786 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003787 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3788 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003789 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3790 + " mBoundToMethod=" + mBoundToMethod);
3791 p.println(" mCurToken=" + mCurToken);
3792 p.println(" mCurIntent=" + mCurIntent);
3793 method = mCurMethod;
3794 p.println(" mCurMethod=" + mCurMethod);
3795 p.println(" mEnabledSession=" + mEnabledSession);
3796 p.println(" mShowRequested=" + mShowRequested
3797 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3798 + " mShowForced=" + mShowForced
3799 + " mInputShown=" + mInputShown);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003800 p.println(" mCurUserActionNotificationSequenceNumber="
3801 + mCurUserActionNotificationSequenceNumber);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003802 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mIsInteractive);
Yohei Yukawa81482972015-06-04 00:58:59 -07003803 p.println(" mSettingsObserver=" + mSettingsObserver);
Yohei Yukawad7248862015-06-03 23:56:12 -07003804 p.println(" mSwitchingController:");
3805 mSwitchingController.dump(p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003806 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003807
Jeff Brownb88102f2010-09-08 11:49:43 -07003808 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003809 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003810 pw.flush();
3811 try {
3812 client.client.asBinder().dump(fd, args);
3813 } catch (RemoteException e) {
3814 p.println("Input method client dead: " + e);
3815 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003816 } else {
3817 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003818 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003819
Jeff Brownb88102f2010-09-08 11:49:43 -07003820 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003821 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003822 pw.flush();
3823 try {
3824 method.asBinder().dump(fd, args);
3825 } catch (RemoteException e) {
3826 p.println("Input method service dead: " + e);
3827 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003828 } else {
3829 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003830 }
3831 }
3832}