blob: e0992977155ee628c04a91b46ffeed80e3e84d82 [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;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080019import com.android.internal.content.PackageMonitor;
Satoshi Kataokad7443c82013-10-15 17:45:43 +090020import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController;
Satoshi Kataokad787f692013-10-26 04:44:21 +090021import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController.ImeSubtypeListItem;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +090022import com.android.internal.inputmethod.InputMethodUtils;
23import com.android.internal.inputmethod.InputMethodUtils.InputMethodSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import com.android.internal.os.HandlerCaller;
Svetoslav Ganov758143e2012-08-06 16:40:27 -070025import com.android.internal.os.SomeArgs;
satoke7c6998e2011-06-03 17:57:59 +090026import com.android.internal.util.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import com.android.internal.view.IInputContext;
28import com.android.internal.view.IInputMethod;
Michael Wright52a53522013-03-14 10:59:38 -070029import com.android.internal.view.IInputSessionCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import com.android.internal.view.IInputMethodClient;
31import com.android.internal.view.IInputMethodManager;
32import com.android.internal.view.IInputMethodSession;
33import com.android.internal.view.InputBindResult;
Adam Lesinskief2ea1f2013-12-05 16:48:06 -080034import com.android.server.statusbar.StatusBarManagerService;
satok01038492012-04-09 21:08:27 +090035import com.android.server.wm.WindowManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036
satoke7c6998e2011-06-03 17:57:59 +090037import org.xmlpull.v1.XmlPullParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import org.xmlpull.v1.XmlPullParserException;
satoke7c6998e2011-06-03 17:57:59 +090039import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040
41import android.app.ActivityManagerNative;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090042import android.app.AppGlobals;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.app.AlertDialog;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +090044import android.app.AppOpsManager;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090045import android.app.IUserSwitchObserver;
satokf90a33e2011-07-19 11:55:52 +090046import android.app.KeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +090047import android.app.Notification;
48import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070049import android.app.PendingIntent;
satok5b927c432012-05-01 20:09:34 +090050import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.content.ComponentName;
52import android.content.ContentResolver;
53import android.content.Context;
54import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.content.DialogInterface.OnCancelListener;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +090056import android.content.DialogInterface.OnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090058import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070060import android.content.pm.ApplicationInfo;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090061import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062import android.content.pm.PackageManager;
63import android.content.pm.ResolveInfo;
64import android.content.pm.ServiceInfo;
Amith Yamasani734983f2014-03-04 16:48:05 -080065import android.content.pm.UserInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070066import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.content.res.Resources;
68import android.content.res.TypedArray;
69import android.database.ContentObserver;
Alan Viverette505e3ab2014-11-24 15:22:11 -080070import android.graphics.drawable.Drawable;
Joe Onorato857fd9b2011-01-27 15:08:35 -080071import android.inputmethodservice.InputMethodService;
Michael Wright7b5a96b2014-08-09 19:28:42 -070072import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073import android.os.Binder;
satoke7c6998e2011-06-03 17:57:59 +090074import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.os.Handler;
76import android.os.IBinder;
77import android.os.IInterface;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090078import android.os.IRemoteCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079import android.os.Message;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090080import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081import android.os.Parcel;
82import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080083import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084import android.os.ServiceManager;
85import android.os.SystemClock;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090086import android.os.UserHandle;
Amith Yamasani734983f2014-03-04 16:48:05 -080087import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088import android.provider.Settings;
89import android.text.TextUtils;
Christopher Tate7b9a28c2015-03-18 13:06:16 -070090import android.text.TextUtils.SimpleStringSplitter;
satokf9f01002011-05-19 21:31:50 +090091import android.text.style.SuggestionSpan;
Christopher Tate7b9a28c2015-03-18 13:06:16 -070092import android.util.ArrayMap;
93import android.util.ArraySet;
Dianne Hackborn39606a02012-07-31 17:54:35 -070094import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +090096import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +090097import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098import android.util.PrintWriterPrinter;
99import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +0900100import android.util.Slog;
101import android.util.Xml;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +0900102import android.view.ContextThemeWrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103import android.view.IWindowManager;
Jeff Brownc28867a2013-03-26 15:42:39 -0700104import android.view.InputChannel;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900105import android.view.LayoutInflater;
106import android.view.View;
107import android.view.ViewGroup;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108import android.view.WindowManager;
satokab751aa2010-09-14 19:17:36 +0900109import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110import android.view.inputmethod.InputBinding;
111import android.view.inputmethod.InputMethod;
112import android.view.inputmethod.InputMethodInfo;
113import android.view.inputmethod.InputMethodManager;
satokab751aa2010-09-14 19:17:36 +0900114import android.view.inputmethod.InputMethodSubtype;
Yohei Yukawa443c2ba2014-09-10 14:10:30 +0900115import android.view.inputmethod.InputMethodSubtype.InputMethodSubtypeBuilder;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900116import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +0900117import android.widget.CompoundButton;
118import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900119import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +0900120import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900121import android.widget.TextView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122
satoke7c6998e2011-06-03 17:57:59 +0900123import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900125import java.io.FileInputStream;
126import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127import java.io.IOException;
128import java.io.PrintWriter;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100129import java.nio.charset.StandardCharsets;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900131import java.util.Collections;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132import java.util.HashMap;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700133import java.util.HashSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134import java.util.List;
satok5b927c432012-05-01 20:09:34 +0900135import java.util.Locale;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136
137/**
138 * This class provides a system service that manages input methods.
139 */
140public class InputMethodManagerService extends IInputMethodManager.Stub
141 implements ServiceConnection, Handler.Callback {
142 static final boolean DEBUG = false;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700143 static final boolean DEBUG_RESTORE = DEBUG || false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700144 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700146 private static final char INPUT_METHOD_SEPARATOR = ':';
147 private static final char INPUT_METHOD_SUBTYPE_SEPARATOR = ';';
148
Seigo Nonakad4474cb2015-05-04 16:53:24 -0700149 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 1;
150 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 2;
151 static final int MSG_SHOW_IM_CONFIG = 3;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 static final int MSG_UNBIND_INPUT = 1000;
154 static final int MSG_BIND_INPUT = 1010;
155 static final int MSG_SHOW_SOFT_INPUT = 1020;
156 static final int MSG_HIDE_SOFT_INPUT = 1030;
157 static final int MSG_ATTACH_TOKEN = 1040;
158 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 static final int MSG_START_INPUT = 2000;
161 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 static final int MSG_UNBIND_METHOD = 3000;
164 static final int MSG_BIND_METHOD = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700165 static final int MSG_SET_ACTIVE = 3020;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900166 static final int MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER = 3040;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800167
satok01038492012-04-09 21:08:27 +0900168 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
169
Satoshi Kataokabcacc322013-10-21 17:57:27 -0700170 static final long TIME_TO_RECONNECT = 3 * 1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800171
satokf9f01002011-05-19 21:31:50 +0900172 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
173
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900174 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
satokb6359412011-06-28 17:47:41 +0900175 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900176
satok4e4569d2010-11-19 18:45:53 +0900177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800179 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900181 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 final IWindowManager mIWindowManager;
184 final HandlerCaller mCaller;
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700185 final boolean mHasFeature;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900186 private InputMethodFileManager mFileManager;
satok01038492012-04-09 21:08:27 +0900187 private final HardKeyboardListener mHardKeyboardListener;
188 private final WindowManagerService mWindowManagerService;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900189 private final AppOpsManager mAppOpsManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800190
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900191 final InputBindResult mNoBinding = new InputBindResult(null, null, null, -1, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800192
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 // All known input methods. mMethodMap also serves as the global
194 // lock for this class.
satokd87c2592010-09-29 11:52:06 +0900195 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<InputMethodInfo>();
196 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<String, InputMethodInfo>();
satokf9f01002011-05-19 21:31:50 +0900197 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
198 new LruCache<SuggestionSpan, InputMethodInfo>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Satoshi Kataokad787f692013-10-26 04:44:21 +0900199 private final InputMethodSubtypeSwitchingController mSwitchingController;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800200
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700201 // Used to bring IME service up to visible adjustment while it is being shown.
202 final ServiceConnection mVisibleConnection = new ServiceConnection() {
203 @Override public void onServiceConnected(ComponentName name, IBinder service) {
204 }
205
206 @Override public void onServiceDisconnected(ComponentName name) {
207 }
208 };
209 boolean mVisibleBound = false;
210
satok7cfc0ed2011-06-20 21:29:36 +0900211 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700212 private NotificationManager mNotificationManager;
213 private KeyguardManager mKeyguardManager;
214 private StatusBarManagerService mStatusBar;
215 private Notification mImeSwitcherNotification;
216 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900217 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900218 private boolean mNotificationShown;
satok0a1bcf42012-05-16 19:26:31 +0900219 private final boolean mImeSelectedOnBoot;
satok7cfc0ed2011-06-20 21:29:36 +0900220
Tadashi G. Takaoka8c6d4772014-08-05 15:29:17 +0900221 static class SessionState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 final ClientState client;
223 final IInputMethod method;
Jeff Brownc28867a2013-03-26 15:42:39 -0700224
225 IInputMethodSession session;
226 InputChannel channel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 @Override
229 public String toString() {
230 return "SessionState{uid " + client.uid + " pid " + client.pid
231 + " method " + Integer.toHexString(
232 System.identityHashCode(method))
233 + " session " + Integer.toHexString(
234 System.identityHashCode(session))
Jeff Brownc28867a2013-03-26 15:42:39 -0700235 + " channel " + channel
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 + "}";
237 }
238
239 SessionState(ClientState _client, IInputMethod _method,
Jeff Brownc28867a2013-03-26 15:42:39 -0700240 IInputMethodSession _session, InputChannel _channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 client = _client;
242 method = _method;
243 session = _session;
Jeff Brownc28867a2013-03-26 15:42:39 -0700244 channel = _channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 }
246 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800247
Jeff Brownc28867a2013-03-26 15:42:39 -0700248 static final class ClientState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 final IInputMethodClient client;
250 final IInputContext inputContext;
251 final int uid;
252 final int pid;
253 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 boolean sessionRequested;
256 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 @Override
259 public String toString() {
260 return "ClientState{" + Integer.toHexString(
261 System.identityHashCode(this)) + " uid " + uid
262 + " pid " + pid + "}";
263 }
264
265 ClientState(IInputMethodClient _client, IInputContext _inputContext,
266 int _uid, int _pid) {
267 client = _client;
268 inputContext = _inputContext;
269 uid = _uid;
270 pid = _pid;
271 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
272 }
273 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800274
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 final HashMap<IBinder, ClientState> mClients
276 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700279 * Set once the system is ready to run third party code.
280 */
281 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800282
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700283 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 * Id of the currently selected input method.
285 */
286 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 /**
289 * The current binding sequence number, incremented every time there is
290 * a new bind performed.
291 */
292 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800293
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 /**
295 * The client that is currently bound to an input method.
296 */
297 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700300 * The last window token that gained focus.
301 */
302 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800303
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700304 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 * The input context last provided by the current client.
306 */
307 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309 /**
310 * The attributes last provided by the current client.
311 */
312 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800313
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 /**
315 * The input method ID of the input method service that we are currently
316 * connected to or in the process of connecting to.
317 */
318 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 /**
satokab751aa2010-09-14 19:17:36 +0900321 * The current subtype of the current input method.
322 */
323 private InputMethodSubtype mCurrentSubtype;
324
satok4e4569d2010-11-19 18:45:53 +0900325 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900326 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
327 mShortcutInputMethodsAndSubtypes =
328 new HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>();
satokab751aa2010-09-14 19:17:36 +0900329
John Spurlocke0980502013-10-25 11:59:29 -0400330 // Was the keyguard locked when this client became current?
331 private boolean mCurClientInKeyguard;
332
satokab751aa2010-09-14 19:17:36 +0900333 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 * Set to true if our ServiceConnection is currently actively bound to
335 * a service (whether or not we have gotten its IBinder back yet).
336 */
337 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 /**
340 * Set if the client has asked for the input method to be shown.
341 */
342 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 /**
345 * Set if we were explicitly told to show the input method.
346 */
347 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 /**
350 * Set if we were forced to be shown.
351 */
352 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 /**
355 * Set if we last told the input method to show itself.
356 */
357 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 /**
360 * The Intent used to connect to the current input method.
361 */
362 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 /**
365 * The token we have made for the currently active input method, to
366 * identify it in the future.
367 */
368 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 /**
371 * If non-null, this is the input method service we are currently connected
372 * to.
373 */
374 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 /**
377 * Time that we last initiated a bind to the input method, to determine
378 * if we should try to disconnect and reconnect to it.
379 */
380 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800381
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 /**
383 * Have we called mCurMethod.bindInput()?
384 */
385 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800386
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 /**
388 * Currently enabled session. Only touched by service thread, not
389 * protected by a lock.
390 */
391 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800392
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 /**
394 * True if the screen is on. The value is true initially.
395 */
396 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800397
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900398 int mCurUserActionNotificationSequenceNumber = 0;
399
Joe Onorato857fd9b2011-01-27 15:08:35 -0800400 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900401
402 /**
403 * A set of status bits regarding the active IME.
404 *
405 * <p>This value is a combination of following two bits:</p>
406 * <dl>
407 * <dt>{@link InputMethodService#IME_ACTIVE}</dt>
408 * <dd>
409 * If this bit is ON, connected IME is ready to accept touch/key events.
410 * </dd>
411 * <dt>{@link InputMethodService#IME_VISIBLE}</dt>
412 * <dd>
413 * If this bit is ON, some of IME view, e.g. software input, candidate view, is visible.
414 * </dd>
415 * </dl>
416 * <em>Do not update this value outside of setImeWindowStatus.</em>
417 */
Joe Onorato857fd9b2011-01-27 15:08:35 -0800418 int mImeWindowVis;
419
Ken Wakasa05dbb652011-08-22 15:22:43 +0900420 private AlertDialog.Builder mDialogBuilder;
421 private AlertDialog mSwitchingDialog;
satok01038492012-04-09 21:08:27 +0900422 private View mSwitchingDialogTitleView;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900423 private InputMethodInfo[] mIms;
424 private int[] mSubtypeIds;
satok5b927c432012-05-01 20:09:34 +0900425 private Locale mLastSystemLocale;
Michael Wright7b5a96b2014-08-09 19:28:42 -0700426 private boolean mShowImeWithHardKeyboard;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900427 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
428 private final IPackageManager mIPackageManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 class SettingsObserver extends ContentObserver {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800431 String mLastEnabled = "";
432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 SettingsObserver(Handler handler) {
434 super(handler);
435 ContentResolver resolver = mContext.getContentResolver();
436 resolver.registerContentObserver(Settings.Secure.getUriFor(
437 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
satokab751aa2010-09-14 19:17:36 +0900438 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokb6109bb2011-02-03 22:24:54 +0900439 Settings.Secure.ENABLED_INPUT_METHODS), false, this);
440 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokab751aa2010-09-14 19:17:36 +0900441 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
Michael Wright7b5a96b2014-08-09 19:28:42 -0700442 resolver.registerContentObserver(Settings.Secure.getUriFor(
443 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800445
Michael Wright7b5a96b2014-08-09 19:28:42 -0700446 @Override public void onChange(boolean selfChange, Uri uri) {
447 final Uri showImeUri =
448 Settings.Secure.getUriFor(Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 synchronized (mMethodMap) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700450 if (showImeUri.equals(uri)) {
451 updateKeyboardFromSettingsLocked();
452 } else {
453 boolean enabledChanged = false;
454 String newEnabled = mSettings.getEnabledInputMethodsStr();
455 if (!mLastEnabled.equals(newEnabled)) {
456 mLastEnabled = newEnabled;
457 enabledChanged = true;
458 }
459 updateInputMethodsFromSettingsLocked(enabledChanged);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800460 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 }
462 }
463 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800464
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900465 class ImmsBroadcastReceiver extends android.content.BroadcastReceiver {
466 private void updateActive() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 // Inform the current client of the change in active status
Dianne Hackborna6e41342012-05-22 16:30:34 -0700468 if (mCurClient != null && mCurClient.client != null) {
469 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
470 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471 }
472 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900473
474 @Override
475 public void onReceive(Context context, Intent intent) {
476 final String action = intent.getAction();
477 if (Intent.ACTION_SCREEN_ON.equals(action)) {
478 mScreenOn = true;
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900479 updateSystemUi(mCurToken, mImeWindowVis, mBackDisposition);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900480 updateActive();
481 return;
482 } else if (Intent.ACTION_SCREEN_OFF.equals(action)) {
483 mScreenOn = false;
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900484 updateSystemUi(mCurToken, 0 /* vis */, mBackDisposition);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900485 updateActive();
486 return;
487 } else if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
488 hideInputMethodMenu();
489 // No need to updateActive
490 return;
Amith Yamasani734983f2014-03-04 16:48:05 -0800491 } else if (Intent.ACTION_USER_ADDED.equals(action)
492 || Intent.ACTION_USER_REMOVED.equals(action)) {
Kenny Guy2a764942014-04-02 13:29:20 +0100493 updateCurrentProfileIds();
Amith Yamasani734983f2014-03-04 16:48:05 -0800494 return;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700495 } else if (Intent.ACTION_SETTING_RESTORED.equals(action)) {
496 final String name = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
497 if (Settings.Secure.ENABLED_INPUT_METHODS.equals(name)) {
498 final String prevValue = intent.getStringExtra(
499 Intent.EXTRA_SETTING_PREVIOUS_VALUE);
500 final String newValue = intent.getStringExtra(
501 Intent.EXTRA_SETTING_NEW_VALUE);
502 restoreEnabledInputMethods(mContext, prevValue, newValue);
503 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900504 } else {
505 Slog.w(TAG, "Unexpected intent " + intent);
506 }
507 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800509
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700510 // Apply the results of a restore operation to the set of enabled IMEs. Note that this
511 // does not attempt to validate on the fly with any installed device policy, so must only
512 // be run in the context of initial device setup.
513 //
514 // TODO: Move this method to InputMethodUtils with adding unit tests.
515 static void restoreEnabledInputMethods(Context context, String prevValue, String newValue) {
516 if (DEBUG_RESTORE) {
517 Slog.i(TAG, "Restoring enabled input methods:");
518 Slog.i(TAG, "prev=" + prevValue);
519 Slog.i(TAG, " new=" + newValue);
520 }
521 // 'new' is the just-restored state, 'prev' is what was in settings prior to the restore
522 ArrayMap<String, ArraySet<String>> prevMap = parseInputMethodsAndSubtypesString(prevValue);
523 ArrayMap<String, ArraySet<String>> newMap = parseInputMethodsAndSubtypesString(newValue);
524
525 // Merge the restored ime+subtype enabled states into the live state
526 for (ArrayMap.Entry<String, ArraySet<String>> entry : newMap.entrySet()) {
527 final String imeId = entry.getKey();
528 ArraySet<String> prevSubtypes = prevMap.get(imeId);
529 if (prevSubtypes == null) {
530 prevSubtypes = new ArraySet<String>(2);
531 prevMap.put(imeId, prevSubtypes);
532 }
533 prevSubtypes.addAll(entry.getValue());
534 }
535
536 final String mergedImesAndSubtypesString = buildInputMethodsAndSubtypesString(prevMap);
537 if (DEBUG_RESTORE) {
538 Slog.i(TAG, "Merged IME string:");
539 Slog.i(TAG, " " + mergedImesAndSubtypesString);
540 }
541 Settings.Secure.putString(context.getContentResolver(),
542 Settings.Secure.ENABLED_INPUT_METHODS, mergedImesAndSubtypesString);
543 }
544
545 // TODO: Move this method to InputMethodUtils with adding unit tests.
546 static String buildInputMethodsAndSubtypesString(ArrayMap<String, ArraySet<String>> map) {
547 // we want to use the canonical InputMethodSettings implementation,
548 // so we convert data structures first.
549 List<Pair<String, ArrayList<String>>> imeMap =
550 new ArrayList<Pair<String, ArrayList<String>>>(4);
551 for (ArrayMap.Entry<String, ArraySet<String>> entry : map.entrySet()) {
552 final String imeName = entry.getKey();
553 final ArraySet<String> subtypeSet = entry.getValue();
554 final ArrayList<String> subtypes = new ArrayList<String>(2);
555 if (subtypeSet != null) {
556 subtypes.addAll(subtypeSet);
557 }
558 imeMap.add(new Pair<String, ArrayList<String>>(imeName, subtypes));
559 }
560 return InputMethodSettings.buildInputMethodsSettingString(imeMap);
561 }
562
563 // TODO: Move this method to InputMethodUtils with adding unit tests.
564 static ArrayMap<String, ArraySet<String>> parseInputMethodsAndSubtypesString(
565 final String inputMethodsAndSubtypesString) {
566 final ArrayMap<String, ArraySet<String>> imeMap =
567 new ArrayMap<String, ArraySet<String>>();
568 if (TextUtils.isEmpty(inputMethodsAndSubtypesString)) {
569 return imeMap;
570 }
571
572 final SimpleStringSplitter typeSplitter =
573 new SimpleStringSplitter(INPUT_METHOD_SEPARATOR);
574 final SimpleStringSplitter subtypeSplitter =
575 new SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATOR);
576 List<Pair<String, ArrayList<String>>> allImeSettings =
577 InputMethodSettings.buildInputMethodsAndSubtypeList(inputMethodsAndSubtypesString,
578 typeSplitter,
579 subtypeSplitter);
580 for (Pair<String, ArrayList<String>> ime : allImeSettings) {
581 ArraySet<String> subtypes = new ArraySet<String>();
582 if (ime.second != null) {
583 subtypes.addAll(ime.second);
584 }
585 imeMap.put(ime.first, subtypes);
586 }
587 return imeMap;
588 }
589
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800590 class MyPackageMonitor extends PackageMonitor {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900591 private boolean isChangingPackagesOfCurrentUser() {
592 final int userId = getChangingUserId();
593 final boolean retval = userId == mSettings.getCurrentUserId();
594 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900595 if (!retval) {
596 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
597 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900598 }
599 return retval;
600 }
601
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800603 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900604 if (!isChangingPackagesOfCurrentUser()) {
605 return false;
606 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900608 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609 final int N = mMethodList.size();
610 if (curInputMethodId != null) {
611 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800612 InputMethodInfo imi = mMethodList.get(i);
613 if (imi.getId().equals(curInputMethodId)) {
614 for (String pkg : packages) {
615 if (imi.getPackageName().equals(pkg)) {
616 if (!doit) {
617 return true;
618 }
satok723a27e2010-11-11 14:58:11 +0900619 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800620 chooseNewDefaultIMELocked();
621 return true;
622 }
623 }
624 }
625 }
626 }
627 }
628 return false;
629 }
630
631 @Override
632 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900633 if (!isChangingPackagesOfCurrentUser()) {
634 return;
635 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800636 synchronized (mMethodMap) {
637 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900638 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800639 final int N = mMethodList.size();
640 if (curInputMethodId != null) {
641 for (int i=0; i<N; i++) {
642 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900643 final String imiId = imi.getId();
644 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800645 curIm = imi;
646 }
satoke7c6998e2011-06-03 17:57:59 +0900647
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800648 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900649 if (isPackageModified(imi.getPackageName())) {
650 mFileManager.deleteAllInputMethodSubtypes(imiId);
651 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800652 if (change == PACKAGE_TEMPORARY_CHANGE
653 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800654 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800655 + imi.getComponent());
656 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800657 }
658 }
659 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800660
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900661 buildInputMethodListLocked(
662 mMethodList, mMethodMap, false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800663
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800664 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800665
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800666 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800667 int change = isPackageDisappearing(curIm.getPackageName());
668 if (change == PACKAGE_TEMPORARY_CHANGE
669 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800670 ServiceInfo si = null;
671 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900672 si = mIPackageManager.getServiceInfo(
673 curIm.getComponent(), 0, mSettings.getCurrentUserId());
674 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800675 }
676 if (si == null) {
677 // Uh oh, current input method is no longer around!
678 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800679 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900680 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800681 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800682 changed = true;
683 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800684 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900685 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800686 }
687 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800688 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800689 }
satokab751aa2010-09-14 19:17:36 +0900690
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800691 if (curIm == null) {
692 // We currently don't have a default input method... is
693 // one now available?
694 changed = chooseNewDefaultIMELocked();
Yohei Yukawa54d512c2015-05-19 22:15:02 -0700695 } else if (!changed && isPackageModified(curIm.getPackageName())) {
696 // Even if the current input method is still available, mCurrentSubtype could
697 // be obsolete when the package is modified in practice.
698 changed = true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800699 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800700
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800701 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800702 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800703 }
704 }
705 }
706 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800707
Jeff Brownc28867a2013-03-26 15:42:39 -0700708 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900709 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -0700710 private final IInputMethod mMethod;
711 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800712
Jeff Brownc28867a2013-03-26 15:42:39 -0700713 MethodCallback(InputMethodManagerService imms, IInputMethod method,
714 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900715 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -0700716 mMethod = method;
717 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800718 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800719
satoke7c6998e2011-06-03 17:57:59 +0900720 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -0700721 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -0700722 long ident = Binder.clearCallingIdentity();
723 try {
724 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
725 } finally {
726 Binder.restoreCallingIdentity(ident);
727 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800728 }
729 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800730
satok01038492012-04-09 21:08:27 +0900731 private class HardKeyboardListener
732 implements WindowManagerService.OnHardKeyboardStatusChangeListener {
733 @Override
Michael Wright7b5a96b2014-08-09 19:28:42 -0700734 public void onHardKeyboardStatusChange(boolean available) {
735 mHandler.sendMessage(mHandler.obtainMessage(MSG_HARD_KEYBOARD_SWITCH_CHANGED,
736 available ? 1 : 0));
satok01038492012-04-09 21:08:27 +0900737 }
738
Michael Wright7b5a96b2014-08-09 19:28:42 -0700739 public void handleHardKeyboardStatusChange(boolean available) {
satok01038492012-04-09 21:08:27 +0900740 if (DEBUG) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700741 Slog.w(TAG, "HardKeyboardStatusChanged: available=" + available);
satok01038492012-04-09 21:08:27 +0900742 }
743 synchronized(mMethodMap) {
744 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
745 && mSwitchingDialog.isShowing()) {
746 mSwitchingDialogTitleView.findViewById(
747 com.android.internal.R.id.hard_keyboard_section).setVisibility(
748 available ? View.VISIBLE : View.GONE);
749 }
750 }
751 }
752 }
753
754 public InputMethodManagerService(Context context, WindowManagerService windowManager) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900755 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800756 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800757 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800758 mHandler = new Handler(this);
759 mIWindowManager = IWindowManager.Stub.asInterface(
760 ServiceManager.getService(Context.WINDOW_SERVICE));
Mita Yuned218c72012-12-06 17:18:25 -0800761 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900762 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800763 public void executeMessage(Message msg) {
764 handleMessage(msg);
765 }
Mita Yuned218c72012-12-06 17:18:25 -0800766 }, true /*asyncHandler*/);
satok01038492012-04-09 21:08:27 +0900767 mWindowManagerService = windowManager;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900768 mAppOpsManager = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE);
satok01038492012-04-09 21:08:27 +0900769 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700770 mHasFeature = context.getPackageManager().hasSystemFeature(
771 PackageManager.FEATURE_INPUT_METHODS);
satok7cfc0ed2011-06-20 21:29:36 +0900772
satok7cfc0ed2011-06-20 21:29:36 +0900773 mImeSwitcherNotification = new Notification();
774 mImeSwitcherNotification.icon = com.android.internal.R.drawable.ic_notification_ime_default;
775 mImeSwitcherNotification.when = 0;
776 mImeSwitcherNotification.flags = Notification.FLAG_ONGOING_EVENT;
777 mImeSwitcherNotification.tickerText = null;
778 mImeSwitcherNotification.defaults = 0; // please be quiet
779 mImeSwitcherNotification.sound = null;
780 mImeSwitcherNotification.vibrate = null;
Daniel Sandler590d5152012-06-14 16:10:13 -0400781
782 // Tag this notification specially so SystemUI knows it's important
John Spurlockfd7f1e02014-03-18 16:41:57 -0400783 mImeSwitcherNotification.extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
784 mImeSwitcherNotification.category = Notification.CATEGORY_SYSTEM;
Daniel Sandler590d5152012-06-14 16:10:13 -0400785
satok7cfc0ed2011-06-20 21:29:36 +0900786 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900787 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900788
789 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900790
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900791 final IntentFilter broadcastFilter = new IntentFilter();
792 broadcastFilter.addAction(Intent.ACTION_SCREEN_ON);
793 broadcastFilter.addAction(Intent.ACTION_SCREEN_OFF);
794 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Amith Yamasani734983f2014-03-04 16:48:05 -0800795 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
796 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700797 broadcastFilter.addAction(Intent.ACTION_SETTING_RESTORED);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900798 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800799
satok7cfc0ed2011-06-20 21:29:36 +0900800 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900801 int userId = 0;
802 try {
803 ActivityManagerNative.getDefault().registerUserSwitchObserver(
804 new IUserSwitchObserver.Stub() {
805 @Override
806 public void onUserSwitching(int newUserId, IRemoteCallback reply) {
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900807 synchronized(mMethodMap) {
808 switchUserLocked(newUserId);
809 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900810 if (reply != null) {
811 try {
812 reply.sendResult(null);
813 } catch (RemoteException e) {
814 }
815 }
816 }
817
818 @Override
819 public void onUserSwitchComplete(int newUserId) throws RemoteException {
820 }
Kenny Guy42979622015-04-13 18:03:05 +0000821
822 @Override
823 public void onForegroundProfileSwitch(int newProfileId) {
824 // Ignore.
825 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900826 });
827 userId = ActivityManagerNative.getDefault().getCurrentUser().id;
828 } catch (RemoteException e) {
829 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
830 }
satok81f8b7c2012-12-04 20:42:56 +0900831 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
satok913a8922010-08-26 21:53:41 +0900832
satokd87c2592010-09-29 11:52:06 +0900833 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900834 mSettings = new InputMethodSettings(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900835 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId);
Kenny Guy2a764942014-04-02 13:29:20 +0100836 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900837 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900838 synchronized (mMethodMap) {
839 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
840 mSettings, context);
841 }
satok0a1bcf42012-05-16 19:26:31 +0900842
843 // Just checking if defaultImiId is empty or not
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900844 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900845 if (DEBUG) {
846 Slog.d(TAG, "Initial default ime = " + defaultImiId);
847 }
satok0a1bcf42012-05-16 19:26:31 +0900848 mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
849
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900850 synchronized (mMethodMap) {
851 buildInputMethodListLocked(mMethodList, mMethodMap,
852 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
853 }
satokd87c2592010-09-29 11:52:06 +0900854 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800855
satok0a1bcf42012-05-16 19:26:31 +0900856 if (!mImeSelectedOnBoot) {
857 Slog.w(TAG, "No IME selected. Choose the most applicable IME.");
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900858 synchronized (mMethodMap) {
859 resetDefaultImeLocked(context);
860 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800861 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800862
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800863 mSettingsObserver = new SettingsObserver(mHandler);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900864 synchronized (mMethodMap) {
865 updateFromSettingsLocked(true);
866 }
satok5b927c432012-05-01 20:09:34 +0900867
868 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME
869 // according to the new system locale.
870 final IntentFilter filter = new IntentFilter();
871 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
872 mContext.registerReceiver(
873 new BroadcastReceiver() {
874 @Override
875 public void onReceive(Context context, Intent intent) {
876 synchronized(mMethodMap) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900877 resetStateIfCurrentLocaleChangedLocked();
satok5b927c432012-05-01 20:09:34 +0900878 }
879 }
880 }, filter);
881 }
882
satok5b927c432012-05-01 20:09:34 +0900883 private void resetDefaultImeLocked(Context context) {
884 // Do not reset the default (current) IME when it is a 3rd-party IME
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900885 if (mCurMethodId != null
886 && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +0900887 return;
888 }
889
890 InputMethodInfo defIm = null;
891 for (InputMethodInfo imi : mMethodList) {
Yohei Yukawa6aa03782015-02-21 03:00:22 +0900892 if (defIm == null && mSystemReady) {
893 final Locale systemLocale = context.getResources().getConfiguration().locale;
894 if (InputMethodUtils.isSystemImeThatHasSubtypeOf(imi, context,
895 true /* checkDefaultAttribute */, systemLocale, false /* checkCountry */,
896 InputMethodUtils.SUBTYPE_MODE_ANY)) {
satok5b927c432012-05-01 20:09:34 +0900897 defIm = imi;
898 Slog.i(TAG, "Selected default: " + imi.getId());
899 }
900 }
901 }
902 if (defIm == null && mMethodList.size() > 0) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900903 defIm = InputMethodUtils.getMostApplicableDefaultIME(
904 mSettings.getEnabledInputMethodListLocked());
Hyejin Kim5baaaac2014-10-27 17:17:06 +0900905 if (defIm != null) {
906 Slog.i(TAG, "Default found, using " + defIm.getId());
907 } else {
908 Slog.i(TAG, "No default found");
909 }
satok5b927c432012-05-01 20:09:34 +0900910 }
911 if (defIm != null) {
912 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
913 }
914 }
915
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900916 private void resetAllInternalStateLocked(final boolean updateOnlyWhenLocaleChanged,
917 final boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900918 if (!mSystemReady) {
919 // not system ready
920 return;
921 }
922 final Locale newLocale = mRes.getConfiguration().locale;
923 if (!updateOnlyWhenLocaleChanged
924 || (newLocale != null && !newLocale.equals(mLastSystemLocale))) {
925 if (!updateOnlyWhenLocaleChanged) {
926 hideCurrentInputLocked(0, null);
927 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -0700928 unbindCurrentMethodLocked(true, false);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900929 }
930 if (DEBUG) {
931 Slog.i(TAG, "Locale has been changed to " + newLocale);
932 }
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900933 buildInputMethodListLocked(mMethodList, mMethodMap, resetDefaultEnabledIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900934 if (!updateOnlyWhenLocaleChanged) {
935 final String selectedImiId = mSettings.getSelectedInputMethod();
936 if (TextUtils.isEmpty(selectedImiId)) {
937 // This is the first time of the user switch and
938 // set the current ime to the proper one.
939 resetDefaultImeLocked(mContext);
940 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +0900941 } else {
942 // If the locale is changed, needs to reset the default ime
943 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900944 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800945 updateFromSettingsLocked(true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900946 mLastSystemLocale = newLocale;
947 if (!updateOnlyWhenLocaleChanged) {
948 try {
949 startInputInnerLocked();
950 } catch (RuntimeException e) {
951 Slog.w(TAG, "Unexpected exception", e);
952 }
953 }
954 }
955 }
956
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900957 private void resetStateIfCurrentLocaleChangedLocked() {
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900958 resetAllInternalStateLocked(true /* updateOnlyWhenLocaleChanged */,
959 true /* resetDefaultImeLocked */);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900960 }
961
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900962 private void switchUserLocked(int newUserId) {
Yohei Yukawa90bf7082015-06-03 23:50:27 -0700963 if (DEBUG) Slog.d(TAG, "Switching user stage 1/3. newUserId=" + newUserId
964 + " currentUserId=" + mSettings.getCurrentUserId());
965
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900966 mSettings.setCurrentUserId(newUserId);
Kenny Guy2a764942014-04-02 13:29:20 +0100967 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900968 // InputMethodFileManager should be reset when the user is changed
969 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900970 final String defaultImiId = mSettings.getSelectedInputMethod();
Yohei Yukawa90bf7082015-06-03 23:50:27 -0700971
972 if (DEBUG) Slog.d(TAG, "Switching user stage 2/3. newUserId=" + newUserId
973 + " defaultImiId=" + defaultImiId);
974
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +0900975 // For secondary users, the list of enabled IMEs may not have been updated since the
976 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
977 // not be empty even if the IME has been uninstalled by the primary user.
978 // Even in such cases, IMMS works fine because it will find the most applicable
979 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900980 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900981 resetAllInternalStateLocked(false /* updateOnlyWhenLocaleChanged */,
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900982 initialUserSwitch /* needsToResetDefaultIme */);
983 if (initialUserSwitch) {
984 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mContext.getPackageManager(),
985 mSettings.getEnabledInputMethodListLocked());
986 }
Yohei Yukawa90bf7082015-06-03 23:50:27 -0700987
988 if (DEBUG) Slog.d(TAG, "Switching user stage 3/3. newUserId=" + newUserId
989 + " selectedIme=" + mSettings.getSelectedInputMethod());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900990 }
991
Kenny Guy2a764942014-04-02 13:29:20 +0100992 void updateCurrentProfileIds() {
993 List<UserInfo> profiles =
994 UserManager.get(mContext).getProfiles(mSettings.getCurrentUserId());
995 int[] currentProfileIds = new int[profiles.size()]; // profiles will not be null
996 for (int i = 0; i < currentProfileIds.length; i++) {
997 currentProfileIds[i] = profiles.get(i).id;
Amith Yamasani734983f2014-03-04 16:48:05 -0800998 }
Kenny Guy2a764942014-04-02 13:29:20 +0100999 mSettings.setCurrentProfileIds(currentProfileIds);
Amith Yamasani734983f2014-03-04 16:48:05 -08001000 }
1001
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001002 @Override
1003 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
1004 throws RemoteException {
1005 try {
1006 return super.onTransact(code, data, reply, flags);
1007 } catch (RuntimeException e) {
1008 // The input method manager only throws security exceptions, so let's
1009 // log all others.
1010 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -07001011 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012 }
1013 throw e;
1014 }
1015 }
1016
Svetoslav Ganova0027152013-06-25 14:59:53 -07001017 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001018 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001019 if (DEBUG) {
1020 Slog.d(TAG, "--- systemReady");
1021 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001022 if (!mSystemReady) {
1023 mSystemReady = true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001024 mKeyguardManager =
1025 (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001026 mNotificationManager = (NotificationManager)
1027 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
1028 mStatusBar = statusBar;
1029 statusBar.setIconVisibility("ime", false);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001030 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokb858c732011-07-22 19:54:34 +09001031 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
1032 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +09001033 if (mShowOngoingImeSwitcherForPhones) {
1034 mWindowManagerService.setOnHardKeyboardStatusChangeListener(
1035 mHardKeyboardListener);
1036 }
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09001037 buildInputMethodListLocked(mMethodList, mMethodMap,
1038 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satok0a1bcf42012-05-16 19:26:31 +09001039 if (!mImeSelectedOnBoot) {
1040 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001041 resetStateIfCurrentLocaleChangedLocked();
1042 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(
1043 mContext.getPackageManager(),
1044 mSettings.getEnabledInputMethodListLocked());
satok0a1bcf42012-05-16 19:26:31 +09001045 }
1046 mLastSystemLocale = mRes.getConfiguration().locale;
Dianne Hackborncc278702009-09-02 23:07:23 -07001047 try {
1048 startInputInnerLocked();
1049 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001050 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -07001051 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001052 }
1053 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001054 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001055
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001056 // ---------------------------------------------------------------------------------------
1057 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
1058 // 1) it comes from the system process
1059 // 2) the calling process' user id is identical to the current user id IMMS thinks.
1060 private boolean calledFromValidUser() {
1061 final int uid = Binder.getCallingUid();
1062 final int userId = UserHandle.getUserId(uid);
1063 if (DEBUG) {
1064 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
1065 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
1066 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +09001067 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
1068 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001069 }
Kenny Guy2a764942014-04-02 13:29:20 +01001070 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001071 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001072 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001073
1074 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
1075 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
1076 // must not manage background users' states in any functions.
1077 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
1078 // by a token.
1079 if (mContext.checkCallingOrSelfPermission(
1080 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1081 == PackageManager.PERMISSION_GRANTED) {
1082 if (DEBUG) {
1083 Slog.d(TAG, "--- Access granted because the calling process has "
1084 + "the INTERACT_ACROSS_USERS_FULL permission");
1085 }
1086 return true;
1087 }
Satoshi Kataoka0766eb02013-07-31 18:30:13 +09001088 Slog.w(TAG, "--- IPC called from background users. Ignore. \n"
1089 + InputMethodUtils.getStackTrace());
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001090 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001091 }
1092
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001093
1094 /**
1095 * Returns true iff the caller is identified to be the current input method with the token.
1096 * @param token The window token given to the input method when it was started.
1097 * @return true if and only if non-null valid token is specified.
1098 */
1099 private boolean calledWithValidToken(IBinder token) {
1100 if (token == null || mCurToken != token) {
1101 return false;
1102 }
1103 return true;
1104 }
1105
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001106 private boolean bindCurrentInputMethodService(
1107 Intent service, ServiceConnection conn, int flags) {
1108 if (service == null || conn == null) {
1109 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
1110 return false;
1111 }
Amith Yamasani27b89e62013-01-16 12:30:11 -08001112 return mContext.bindServiceAsUser(service, conn, flags,
1113 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001114 }
1115
satoke7c6998e2011-06-03 17:57:59 +09001116 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001117 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001118 // TODO: Make this work even for non-current users?
1119 if (!calledFromValidUser()) {
1120 return Collections.emptyList();
1121 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001122 synchronized (mMethodMap) {
1123 return new ArrayList<InputMethodInfo>(mMethodList);
1124 }
1125 }
1126
satoke7c6998e2011-06-03 17:57:59 +09001127 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001128 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001129 // TODO: Make this work even for non-current users?
1130 if (!calledFromValidUser()) {
1131 return Collections.emptyList();
1132 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001133 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001134 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135 }
1136 }
1137
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001138 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001139 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001140 * @return enabled subtypes of the specified imi
1141 */
satoke7c6998e2011-06-03 17:57:59 +09001142 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001143 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001144 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001145 // TODO: Make this work even for non-current users?
1146 if (!calledFromValidUser()) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001147 return Collections.<InputMethodSubtype>emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001148 }
satok67ddf9c2010-11-17 09:45:54 +09001149 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001150 final InputMethodInfo imi;
1151 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001152 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001153 } else {
1154 imi = mMethodMap.get(imiId);
1155 }
1156 if (imi == null) {
1157 return Collections.<InputMethodSubtype>emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001158 }
1159 return mSettings.getEnabledInputMethodSubtypeListLocked(
1160 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001161 }
1162 }
1163
satoke7c6998e2011-06-03 17:57:59 +09001164 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001165 public void addClient(IInputMethodClient client,
1166 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001167 if (!calledFromValidUser()) {
1168 return;
1169 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001170 synchronized (mMethodMap) {
1171 mClients.put(client.asBinder(), new ClientState(client,
1172 inputContext, uid, pid));
1173 }
1174 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001175
satoke7c6998e2011-06-03 17:57:59 +09001176 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001177 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001178 if (!calledFromValidUser()) {
1179 return;
1180 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001182 ClientState cs = mClients.remove(client.asBinder());
1183 if (cs != null) {
1184 clearClientSessionLocked(cs);
1185 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001186 }
1187 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 void executeOrSendMessage(IInterface target, Message msg) {
1190 if (target.asBinder() instanceof Binder) {
1191 mCaller.sendMessage(msg);
1192 } else {
1193 handleMessage(msg);
1194 msg.recycle();
1195 }
1196 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001197
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001198 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001199 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001200 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001201 + mCurClient.client.asBinder());
1202 if (mBoundToMethod) {
1203 mBoundToMethod = false;
1204 if (mCurMethod != null) {
1205 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1206 MSG_UNBIND_INPUT, mCurMethod));
1207 }
1208 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001209
1210 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1211 MSG_SET_ACTIVE, 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1213 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1214 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001216
The Android Open Source Project10592532009-03-18 17:39:46 -07001217 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001218 }
1219 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 private int getImeShowFlags() {
1222 int flags = 0;
1223 if (mShowForced) {
1224 flags |= InputMethod.SHOW_FORCED
1225 | InputMethod.SHOW_EXPLICIT;
1226 } else if (mShowExplicitlyRequested) {
1227 flags |= InputMethod.SHOW_EXPLICIT;
1228 }
1229 return flags;
1230 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001231
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001232 private int getAppShowFlags() {
1233 int flags = 0;
1234 if (mShowForced) {
1235 flags |= InputMethodManager.SHOW_FORCED;
1236 } else if (!mShowExplicitlyRequested) {
1237 flags |= InputMethodManager.SHOW_IMPLICIT;
1238 }
1239 return flags;
1240 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001241
Dianne Hackborn7663d802012-02-24 13:08:49 -08001242 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 if (!mBoundToMethod) {
1244 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1245 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1246 mBoundToMethod = true;
1247 }
1248 final SessionState session = mCurClient.curSession;
1249 if (initial) {
1250 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1251 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
1252 } else {
1253 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1254 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
1255 }
1256 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001257 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001258 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001259 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001260 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001261 (session.channel != null ? session.channel.dup() : null),
1262 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001264
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001265 InputBindResult startInputLocked(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001266 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001267 // If no method is currently selected, do nothing.
1268 if (mCurMethodId == null) {
1269 return mNoBinding;
1270 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001272 ClientState cs = mClients.get(client.asBinder());
1273 if (cs == null) {
1274 throw new IllegalArgumentException("unknown client "
1275 + client.asBinder());
1276 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001278 try {
1279 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1280 // Check with the window manager to make sure this client actually
1281 // has a window with focus. If not, reject. This is thread safe
1282 // because if the focus changes some time before or after, the
1283 // next client receiving focus that has any interest in input will
1284 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001285 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001286 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1287 return null;
1288 }
1289 } catch (RemoteException e) {
1290 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001291
Dianne Hackborn7663d802012-02-24 13:08:49 -08001292 return startInputUncheckedLocked(cs, inputContext, attribute, controlFlags);
1293 }
1294
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001295 InputBindResult startInputUncheckedLocked(@NonNull ClientState cs,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001296 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
1297 // If no method is currently selected, do nothing.
1298 if (mCurMethodId == null) {
1299 return mNoBinding;
1300 }
1301
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001302 if (attribute != null) {
1303 // We accept an empty package name as a valid data.
1304 if (!TextUtils.isEmpty(attribute.packageName) &&
1305 !InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.uid,
1306 attribute.packageName)) {
1307 Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
1308 + " uid=" + cs.uid + " package=" + attribute.packageName);
1309 return mNoBinding;
1310 }
1311 }
1312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001313 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001314 // Was the keyguard locked when switching over to the new client?
1315 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001316 // If the client is changing, we need to switch over to the new
1317 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001318 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001319 if (DEBUG) Slog.v(TAG, "switching to client: client = "
John Spurlocke0980502013-10-25 11:59:29 -04001320 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001321
1322 // If the screen is on, inform the new client it is active
1323 if (mScreenOn) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001324 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
1325 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326 }
1327 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001329 // Bump up the sequence for this client and attach it.
1330 mCurSeq++;
1331 if (mCurSeq <= 0) mCurSeq = 1;
1332 mCurClient = cs;
1333 mCurInputContext = inputContext;
1334 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001335
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001336 // Check if the input method is changing.
1337 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1338 if (cs.curSession != null) {
1339 // Fast case: if we are already connected to the input method,
1340 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001341 return attachNewInputLocked(
1342 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001343 }
1344 if (mHaveConnection) {
1345 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001346 // Return to client, and we will get back with it when
1347 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001348 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001349 return new InputBindResult(null, null, mCurId, mCurSeq,
1350 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001351 } else if (SystemClock.uptimeMillis()
1352 < (mLastBindTime+TIME_TO_RECONNECT)) {
1353 // In this case we have connected to the service, but
1354 // don't yet have its interface. If it hasn't been too
1355 // long since we did the connection, we'll return to
1356 // the client and wait to get the service interface so
1357 // we can report back. If it has been too long, we want
1358 // to fall through so we can try a disconnect/reconnect
1359 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001360 return new InputBindResult(null, null, mCurId, mCurSeq,
1361 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001362 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001363 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1364 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001365 }
1366 }
1367 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001368
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001369 return startInputInnerLocked();
1370 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001371
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001372 InputBindResult startInputInnerLocked() {
1373 if (mCurMethodId == null) {
1374 return mNoBinding;
1375 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001376
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001377 if (!mSystemReady) {
1378 // If the system is not yet ready, we shouldn't be running third
1379 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001380 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1381 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001382 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001383
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001384 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1385 if (info == null) {
1386 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1387 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001388
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001389 unbindCurrentMethodLocked(false, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001391 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1392 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001393 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1394 com.android.internal.R.string.input_method_binding_label);
1395 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1396 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001397 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001398 | Context.BIND_NOT_VISIBLE | Context.BIND_NOT_FOREGROUND
1399 | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001400 mLastBindTime = SystemClock.uptimeMillis();
1401 mHaveConnection = true;
1402 mCurId = info.getId();
1403 mCurToken = new Binder();
1404 try {
Craig Mautnerca0ac712013-03-14 09:43:02 -07001405 if (true || DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001406 mIWindowManager.addWindowToken(mCurToken,
1407 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1408 } catch (RemoteException e) {
1409 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001410 return new InputBindResult(null, null, mCurId, mCurSeq,
1411 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001412 } else {
1413 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001414 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001415 + mCurIntent);
1416 }
1417 return null;
1418 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001419
satoke7c6998e2011-06-03 17:57:59 +09001420 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001421 public InputBindResult startInput(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001422 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001423 if (!calledFromValidUser()) {
1424 return null;
1425 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001426 synchronized (mMethodMap) {
1427 final long ident = Binder.clearCallingIdentity();
1428 try {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001429 return startInputLocked(client, inputContext, attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001430 } finally {
1431 Binder.restoreCallingIdentity(ident);
1432 }
1433 }
1434 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001435
satoke7c6998e2011-06-03 17:57:59 +09001436 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001437 public void finishInput(IInputMethodClient client) {
1438 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001439
satoke7c6998e2011-06-03 17:57:59 +09001440 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001441 public void onServiceConnected(ComponentName name, IBinder service) {
1442 synchronized (mMethodMap) {
1443 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1444 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001445 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001446 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001447 unbindCurrentMethodLocked(false, false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001448 return;
1449 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001450 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001451 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1452 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001453 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001454 clearClientSessionLocked(mCurClient);
1455 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001456 }
1457 }
1458 }
1459 }
1460
Jeff Brownc28867a2013-03-26 15:42:39 -07001461 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1462 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001463 synchronized (mMethodMap) {
1464 if (mCurMethod != null && method != null
1465 && mCurMethod.asBinder() == method.asBinder()) {
1466 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001467 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001468 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001469 method, session, channel);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001470 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001471 if (res.method != null) {
1472 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
1473 MSG_BIND_METHOD, mCurClient.client, res));
1474 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001475 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001476 }
1477 }
1478 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001479
1480 // Session abandoned. Close its associated input channel.
1481 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001482 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001483
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001484 void unbindCurrentMethodLocked(boolean reportToClient, boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001485 if (mVisibleBound) {
1486 mContext.unbindService(mVisibleConnection);
1487 mVisibleBound = false;
1488 }
1489
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001490 if (mHaveConnection) {
1491 mContext.unbindService(this);
1492 mHaveConnection = false;
1493 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001494
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001495 if (mCurToken != null) {
1496 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001497 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001498 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001499 // The current IME is shown. Hence an IME switch (transition) is happening.
1500 mWindowManagerService.saveLastInputMethodWindowForTransition();
1501 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001502 mIWindowManager.removeWindowToken(mCurToken);
1503 } catch (RemoteException e) {
1504 }
1505 mCurToken = null;
1506 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001507
The Android Open Source Project10592532009-03-18 17:39:46 -07001508 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001509 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001510
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001511 if (reportToClient && mCurClient != null) {
1512 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1513 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1514 }
1515 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001516
1517 void requestClientSessionLocked(ClientState cs) {
1518 if (!cs.sessionRequested) {
1519 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1520 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1521 cs.sessionRequested = true;
1522 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1523 MSG_CREATE_SESSION, mCurMethod, channels[1],
1524 new MethodCallback(this, mCurMethod, channels[0])));
1525 }
1526 }
1527
1528 void clearClientSessionLocked(ClientState cs) {
1529 finishSessionLocked(cs.curSession);
1530 cs.curSession = null;
1531 cs.sessionRequested = false;
1532 }
1533
1534 private void finishSessionLocked(SessionState sessionState) {
1535 if (sessionState != null) {
1536 if (sessionState.session != null) {
1537 try {
1538 sessionState.session.finishSession();
1539 } catch (RemoteException e) {
1540 Slog.w(TAG, "Session failed to close due to remote exception", e);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001541 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Jeff Brownc28867a2013-03-26 15:42:39 -07001542 }
1543 sessionState.session = null;
1544 }
1545 if (sessionState.channel != null) {
1546 sessionState.channel.dispose();
1547 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001548 }
1549 }
1550 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001551
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001552 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001553 if (mCurMethod != null) {
1554 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001555 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001556 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001557
Jeff Brownc28867a2013-03-26 15:42:39 -07001558 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001559 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001560 mCurMethod = null;
1561 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001562 if (mStatusBar != null) {
1563 mStatusBar.setIconVisibility("ime", false);
1564 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001565 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001566
satoke7c6998e2011-06-03 17:57:59 +09001567 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001568 public void onServiceDisconnected(ComponentName name) {
1569 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001570 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001571 + " mCurIntent=" + mCurIntent);
1572 if (mCurMethod != null && mCurIntent != null
1573 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001574 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001575 // We consider this to be a new bind attempt, since the system
1576 // should now try to restart the service for us.
1577 mLastBindTime = SystemClock.uptimeMillis();
1578 mShowRequested = mInputShown;
1579 mInputShown = false;
1580 if (mCurClient != null) {
1581 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1582 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1583 }
1584 }
1585 }
1586 }
1587
satokf9f01002011-05-19 21:31:50 +09001588 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001589 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
1590 long ident = Binder.clearCallingIdentity();
1591 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001592 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09001593 if (!calledWithValidToken(token)) {
1594 final int uid = Binder.getCallingUid();
1595 Slog.e(TAG, "Ignoring updateStatusIcon due to an invalid token. uid:" + uid
1596 + " token:" + token);
1597 return;
1598 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001599 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001600 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001601 if (mStatusBar != null) {
1602 mStatusBar.setIconVisibility("ime", false);
1603 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001604 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001605 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001606 CharSequence contentDescription = null;
1607 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001608 // Use PackageManager to load label
1609 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001610 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001611 mIPackageManager.getApplicationInfo(packageName, 0,
1612 mSettings.getCurrentUserId()));
1613 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001614 /* ignore */
1615 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001616 if (mStatusBar != null) {
1617 mStatusBar.setIcon("ime", packageName, iconId, 0,
1618 contentDescription != null
1619 ? contentDescription.toString() : null);
1620 mStatusBar.setIconVisibility("ime", true);
1621 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001622 }
1623 }
1624 } finally {
1625 Binder.restoreCallingIdentity(ident);
1626 }
1627 }
1628
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001629 private boolean shouldShowImeSwitcherLocked(int visibility) {
satok7cfc0ed2011-06-20 21:29:36 +09001630 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04001631 if (mSwitchingDialog != null) return false;
satok2c93efc2012-04-02 19:33:47 +09001632 if (isScreenLocked()) return false;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001633 if ((visibility & InputMethodService.IME_ACTIVE) == 0) return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001634 if (mWindowManagerService.isHardKeyboardAvailable()) {
1635 // When physical keyboard is attached, we show the ime switcher (or notification if
1636 // NavBar is not available) because SHOW_IME_WITH_HARD_KEYBOARD settings currently
1637 // exists in the IME switcher dialog. Might be OK to remove this condition once
1638 // SHOW_IME_WITH_HARD_KEYBOARD settings finds a good place to live.
1639 return true;
1640 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001641 if ((visibility & InputMethodService.IME_VISIBLE) == 0) return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001642
1643 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1644 final int N = imis.size();
1645 if (N > 2) return true;
1646 if (N < 1) return false;
1647 int nonAuxCount = 0;
1648 int auxCount = 0;
1649 InputMethodSubtype nonAuxSubtype = null;
1650 InputMethodSubtype auxSubtype = null;
1651 for(int i = 0; i < N; ++i) {
1652 final InputMethodInfo imi = imis.get(i);
1653 final List<InputMethodSubtype> subtypes =
1654 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
1655 final int subtypeCount = subtypes.size();
1656 if (subtypeCount == 0) {
1657 ++nonAuxCount;
1658 } else {
1659 for (int j = 0; j < subtypeCount; ++j) {
1660 final InputMethodSubtype subtype = subtypes.get(j);
1661 if (!subtype.isAuxiliary()) {
1662 ++nonAuxCount;
1663 nonAuxSubtype = subtype;
1664 } else {
1665 ++auxCount;
1666 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001667 }
1668 }
satok7cfc0ed2011-06-20 21:29:36 +09001669 }
1670 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001671 if (nonAuxCount > 1 || auxCount > 1) {
1672 return true;
1673 } else if (nonAuxCount == 1 && auxCount == 1) {
1674 if (nonAuxSubtype != null && auxSubtype != null
1675 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1676 || auxSubtype.overridesImplicitlyEnabledSubtype()
1677 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
1678 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1679 return false;
1680 }
1681 return true;
1682 }
1683 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001684 }
1685
John Spurlocke0980502013-10-25 11:59:29 -04001686 private boolean isKeyguardLocked() {
1687 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1688 }
1689
satokdbf29502011-08-25 15:28:23 +09001690 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001691 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001692 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001693 if (!calledWithValidToken(token)) {
1694 final int uid = Binder.getCallingUid();
1695 Slog.e(TAG, "Ignoring setImeWindowStatus due to an invalid token. uid:" + uid
1696 + " token:" + token);
1697 return;
1698 }
1699
1700 synchronized (mMethodMap) {
1701 mImeWindowVis = vis;
1702 mBackDisposition = backDisposition;
1703 updateSystemUiLocked(token, vis, backDisposition);
1704 }
1705 }
1706
1707 private void updateSystemUi(IBinder token, int vis, int backDisposition) {
1708 synchronized (mMethodMap) {
1709 updateSystemUiLocked(token, vis, backDisposition);
1710 }
1711 }
1712
1713 // Caution! This method is called in this class. Handle multi-user carefully
1714 private void updateSystemUiLocked(IBinder token, int vis, int backDisposition) {
1715 if (!calledWithValidToken(token)) {
1716 final int uid = Binder.getCallingUid();
1717 Slog.e(TAG, "Ignoring updateSystemUiLocked due to an invalid token. uid:" + uid
1718 + " token:" + token);
1719 return;
1720 }
1721
1722 // TODO: Move this clearing calling identity block to setImeWindowStatus after making sure
1723 // all updateSystemUi happens on system previlege.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001724 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001725 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001726 // apply policy for binder calls
1727 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
1728 vis = 0;
satok06487a52010-10-29 11:37:18 +09001729 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001730 // mImeWindowVis should be updated before calling shouldShowImeSwitcherLocked().
1731 final boolean needsToShowImeSwitcher = shouldShowImeSwitcherLocked(vis);
1732 if (mStatusBar != null) {
1733 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
1734 needsToShowImeSwitcher);
1735 }
1736 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1737 if (imi != null && needsToShowImeSwitcher) {
1738 // Used to load label
1739 final CharSequence title = mRes.getText(
1740 com.android.internal.R.string.select_input_method);
1741 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
1742 mContext, imi, mCurrentSubtype);
satok5bc8e732011-07-22 21:07:23 +09001743
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001744 mImeSwitcherNotification.color = mContext.getColor(
1745 com.android.internal.R.color.system_notification_accent_color);
1746 mImeSwitcherNotification.setLatestEventInfo(
1747 mContext, title, summary, mImeSwitchPendingIntent);
1748 if ((mNotificationManager != null)
1749 && !mWindowManagerService.hasNavigationBar()) {
1750 if (DEBUG) {
1751 Slog.d(TAG, "--- show notification: label = " + summary);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001752 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001753 mNotificationManager.notifyAsUser(null,
1754 com.android.internal.R.string.select_input_method,
1755 mImeSwitcherNotification, UserHandle.ALL);
1756 mNotificationShown = true;
1757 }
1758 } else {
1759 if (mNotificationShown && mNotificationManager != null) {
1760 if (DEBUG) {
1761 Slog.d(TAG, "--- hide notification");
satok7cfc0ed2011-06-20 21:29:36 +09001762 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001763 mNotificationManager.cancelAsUser(null,
1764 com.android.internal.R.string.select_input_method, UserHandle.ALL);
1765 mNotificationShown = false;
satok7cfc0ed2011-06-20 21:29:36 +09001766 }
satok06487a52010-10-29 11:37:18 +09001767 }
1768 } finally {
1769 Binder.restoreCallingIdentity(ident);
1770 }
1771 }
1772
satoke7c6998e2011-06-03 17:57:59 +09001773 @Override
satokf9f01002011-05-19 21:31:50 +09001774 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001775 if (!calledFromValidUser()) {
1776 return;
1777 }
satokf9f01002011-05-19 21:31:50 +09001778 synchronized (mMethodMap) {
1779 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1780 for (int i = 0; i < spans.length; ++i) {
1781 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001782 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001783 mSecureSuggestionSpans.put(ss, currentImi);
1784 }
1785 }
1786 }
1787 }
1788
satoke7c6998e2011-06-03 17:57:59 +09001789 @Override
satokf9f01002011-05-19 21:31:50 +09001790 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001791 if (!calledFromValidUser()) {
1792 return false;
1793 }
satokf9f01002011-05-19 21:31:50 +09001794 synchronized (mMethodMap) {
1795 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1796 // TODO: Do not send the intent if the process of the targetImi is already dead.
1797 if (targetImi != null) {
1798 final String[] suggestions = span.getSuggestions();
1799 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001800 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001801 final Intent intent = new Intent();
1802 // Ensures that only a class in the original IME package will receive the
1803 // notification.
satok42c5a162011-05-26 16:46:14 +09001804 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001805 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1806 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1807 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1808 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001809 final long ident = Binder.clearCallingIdentity();
1810 try {
1811 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1812 } finally {
1813 Binder.restoreCallingIdentity(ident);
1814 }
satokf9f01002011-05-19 21:31:50 +09001815 return true;
1816 }
1817 }
1818 return false;
1819 }
1820
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001821 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001822 updateInputMethodsFromSettingsLocked(enabledMayChange);
1823 updateKeyboardFromSettingsLocked();
1824 }
1825
1826 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001827 if (enabledMayChange) {
1828 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1829 for (int i=0; i<enabled.size(); i++) {
1830 // We allow the user to select "disabled until used" apps, so if they
1831 // are enabling one of those here we now need to make it enabled.
1832 InputMethodInfo imm = enabled.get(i);
1833 try {
1834 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1835 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1836 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09001837 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001838 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001839 if (DEBUG) {
1840 Slog.d(TAG, "Update state(" + imm.getId()
1841 + "): DISABLED_UNTIL_USED -> DEFAULT");
1842 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001843 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1844 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001845 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
1846 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001847 }
1848 } catch (RemoteException e) {
1849 }
1850 }
1851 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001852 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1853 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1854 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1855 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001856 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001857 // There is no input method selected, try to choose new applicable input method.
1858 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001859 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001860 }
1861 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001862 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001863 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001864 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001865 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001866 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001867 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001868 }
satokf3db1af2010-11-23 13:34:33 +09001869 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001870 } else {
1871 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001872 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001873 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001874 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09001875 // Here is not the perfect place to reset the switching controller. Ideally
1876 // mSwitchingController and mSettings should be able to share the same state.
1877 // TODO: Make sure that mSwitchingController and mSettings are sharing the
1878 // the same enabled IMEs list.
1879 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07001880
1881 }
1882
1883 public void updateKeyboardFromSettingsLocked() {
1884 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
1885 if (mSwitchingDialog != null
1886 && mSwitchingDialogTitleView != null
1887 && mSwitchingDialog.isShowing()) {
1888 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
1889 com.android.internal.R.id.hard_keyboard_switch);
1890 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
1891 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001892 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001893
satokab751aa2010-09-14 19:17:36 +09001894 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001895 InputMethodInfo info = mMethodMap.get(id);
1896 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001897 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001898 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001899
Yohei Yukawae63b5fa2014-09-19 13:14:55 +09001900 if (mCurClient != null && mCurAttribute != null) {
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001901 // We have already made sure that the package name belongs to the application's UID.
1902 // No further UID check is required.
1903 if (SystemConfig.getInstance().getFixedImeApps().contains(mCurAttribute.packageName)) {
1904 return;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +09001905 }
1906 }
1907
satokd81e9502012-05-21 12:58:45 +09001908 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001909 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09001910 final int subtypeCount = info.getSubtypeCount();
1911 if (subtypeCount <= 0) {
1912 return;
satokcd7cd292010-11-20 15:46:23 +09001913 }
satokd81e9502012-05-21 12:58:45 +09001914 final InputMethodSubtype oldSubtype = mCurrentSubtype;
1915 final InputMethodSubtype newSubtype;
1916 if (subtypeId >= 0 && subtypeId < subtypeCount) {
1917 newSubtype = info.getSubtypeAt(subtypeId);
1918 } else {
1919 // If subtype is null, try to find the most applicable one from
1920 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001921 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09001922 }
1923 if (newSubtype == null || oldSubtype == null) {
1924 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
1925 + ", new subtype = " + newSubtype);
1926 return;
1927 }
1928 if (newSubtype != oldSubtype) {
1929 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1930 if (mCurMethod != null) {
1931 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001932 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokd81e9502012-05-21 12:58:45 +09001933 mCurMethod.changeInputMethodSubtype(newSubtype);
1934 } catch (RemoteException e) {
1935 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
satokab751aa2010-09-14 19:17:36 +09001936 }
1937 }
1938 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001939 return;
1940 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001941
satokd81e9502012-05-21 12:58:45 +09001942 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001943 final long ident = Binder.clearCallingIdentity();
1944 try {
satokab751aa2010-09-14 19:17:36 +09001945 // Set a subtype to this input method.
1946 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001947 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1948 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1949 // because mCurMethodId is stored as a history in
1950 // setSelectedInputMethodAndSubtypeLocked().
1951 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001952
1953 if (ActivityManagerNative.isSystemReady()) {
1954 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001955 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001956 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07001957 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001958 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001959 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001960 } finally {
1961 Binder.restoreCallingIdentity(ident);
1962 }
1963 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001964
satok42c5a162011-05-26 16:46:14 +09001965 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001966 public boolean showSoftInput(IInputMethodClient client, int flags,
1967 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001968 if (!calledFromValidUser()) {
1969 return false;
1970 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001971 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001972 long ident = Binder.clearCallingIdentity();
1973 try {
1974 synchronized (mMethodMap) {
1975 if (mCurClient == null || client == null
1976 || mCurClient.client.asBinder() != client.asBinder()) {
1977 try {
1978 // We need to check if this is the current client with
1979 // focus in the window manager, to allow this call to
1980 // be made before input is started in it.
1981 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001982 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001983 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001984 }
1985 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001986 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001987 }
1988 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001989
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001990 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001991 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001992 }
1993 } finally {
1994 Binder.restoreCallingIdentity(ident);
1995 }
1996 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001997
The Android Open Source Project4df24232009-03-05 14:34:35 -08001998 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001999 mShowRequested = true;
2000 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
2001 mShowExplicitlyRequested = true;
2002 }
2003 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
2004 mShowExplicitlyRequested = true;
2005 mShowForced = true;
2006 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002007
Dianne Hackborncc278702009-09-02 23:07:23 -07002008 if (!mSystemReady) {
2009 return false;
2010 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002011
The Android Open Source Project4df24232009-03-05 14:34:35 -08002012 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002013 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002014 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002015 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
2016 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
2017 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002018 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002019 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002020 bindCurrentInputMethodService(
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07002021 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE
Dianne Hackbornd69e4c12015-04-24 09:54:54 -07002022 | Context.BIND_TREAT_LIKE_ACTIVITY
2023 | Context.BIND_FOREGROUND_SERVICE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002024 mVisibleBound = true;
2025 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002026 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002027 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09002028 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002029 // The client has asked to have the input method shown, but
2030 // we have been sitting here too long with a connection to the
2031 // service and no interface received, so let's disconnect/connect
2032 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002033 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002034 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09002035 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002036 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002037 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002038 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002039 } else {
2040 if (DEBUG) {
2041 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
2042 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
2043 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002044 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002045
The Android Open Source Project4df24232009-03-05 14:34:35 -08002046 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002047 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002048
satok42c5a162011-05-26 16:46:14 +09002049 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002050 public boolean hideSoftInput(IInputMethodClient client, int flags,
2051 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002052 if (!calledFromValidUser()) {
2053 return false;
2054 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002055 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002056 long ident = Binder.clearCallingIdentity();
2057 try {
2058 synchronized (mMethodMap) {
2059 if (mCurClient == null || client == null
2060 || mCurClient.client.asBinder() != client.asBinder()) {
2061 try {
2062 // We need to check if this is the current client with
2063 // focus in the window manager, to allow this call to
2064 // be made before input is started in it.
2065 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002066 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
2067 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002068 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002069 }
2070 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002071 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002072 }
2073 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002074
Joe Onorato8a9b2202010-02-26 18:56:32 -08002075 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002076 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002077 }
2078 } finally {
2079 Binder.restoreCallingIdentity(ident);
2080 }
2081 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002082
The Android Open Source Project4df24232009-03-05 14:34:35 -08002083 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002084 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
2085 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002086 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 -08002087 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002088 }
2089 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002090 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 -08002091 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002092 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002093 boolean res;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002094 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002095 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2096 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
2097 res = true;
2098 } else {
2099 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002100 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002101 if (mHaveConnection && mVisibleBound) {
2102 mContext.unbindService(mVisibleConnection);
2103 mVisibleBound = false;
2104 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002105 mInputShown = false;
2106 mShowRequested = false;
2107 mShowExplicitlyRequested = false;
2108 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002109 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002110 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002111
satok42c5a162011-05-26 16:46:14 +09002112 @Override
Dianne Hackborn7663d802012-02-24 13:08:49 -08002113 public InputBindResult windowGainedFocus(IInputMethodClient client, IBinder windowToken,
2114 int controlFlags, int softInputMode, int windowFlags,
2115 EditorInfo attribute, IInputContext inputContext) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002116 // Needs to check the validity before clearing calling identity
2117 final boolean calledFromValidUser = calledFromValidUser();
2118
Dianne Hackborn7663d802012-02-24 13:08:49 -08002119 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002120 long ident = Binder.clearCallingIdentity();
2121 try {
2122 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002123 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
Dianne Hackborn7663d802012-02-24 13:08:49 -08002124 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002125 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08002126 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002127
Dianne Hackborn7663d802012-02-24 13:08:49 -08002128 ClientState cs = mClients.get(client.asBinder());
2129 if (cs == null) {
2130 throw new IllegalArgumentException("unknown client "
2131 + client.asBinder());
2132 }
2133
2134 try {
2135 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2136 // Check with the window manager to make sure this client actually
2137 // has a window with focus. If not, reject. This is thread safe
2138 // because if the focus changes some time before or after, the
2139 // next client receiving focus that has any interest in input will
2140 // be calling through here after that change happens.
2141 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2142 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2143 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002144 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002145 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002146 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002147
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002148 if (!calledFromValidUser) {
2149 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2150 Slog.w(TAG, "If you want to interect with IME, you need "
2151 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2152 hideCurrentInputLocked(0, null);
2153 return null;
2154 }
2155
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002156 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07002157 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09002158 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002159 if (attribute != null) {
2160 return startInputUncheckedLocked(cs, inputContext, attribute,
2161 controlFlags);
2162 }
2163 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002164 }
2165 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002166
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002167 // Should we auto-show the IME even if the caller has not
2168 // specified what should be done with it?
2169 // We only do this automatically if the window can resize
2170 // to accommodate the IME (so what the user sees will give
2171 // them good context without input information being obscured
2172 // by the IME) or if running on a large screen where there
2173 // is more room for the target window + IME.
2174 final boolean doAutoShow =
2175 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2176 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2177 || mRes.getConfiguration().isLayoutSizeAtLeast(
2178 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002179 final boolean isTextEditor =
2180 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2181
2182 // We want to start input before showing the IME, but after closing
2183 // it. We want to do this after closing it to help the IME disappear
2184 // more quickly (not get stuck behind it initializing itself for the
2185 // new focused input, even if its window wants to hide the IME).
2186 boolean didStart = false;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002188 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2189 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002190 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002191 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2192 // There is no focus view, and this window will
2193 // be behind any soft input window, so hide the
2194 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002195 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002196 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002197 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002198 } else if (isTextEditor && doAutoShow && (softInputMode &
2199 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002200 // There is a focus view, and we are navigating forward
2201 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002202 // We only do this automatically if the window can resize
2203 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002204 // them good context without input information being obscured
2205 // by the IME) or if running on a large screen where there
2206 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002207 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002208 if (attribute != null) {
2209 res = startInputUncheckedLocked(cs, inputContext, attribute,
2210 controlFlags);
2211 didStart = true;
2212 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002213 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002214 }
2215 break;
2216 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2217 // Do nothing.
2218 break;
2219 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2220 if ((softInputMode &
2221 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002222 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002223 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002224 }
2225 break;
2226 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002227 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002228 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002229 break;
2230 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2231 if ((softInputMode &
2232 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002233 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002234 if (attribute != null) {
2235 res = startInputUncheckedLocked(cs, inputContext, attribute,
2236 controlFlags);
2237 didStart = true;
2238 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002239 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002240 }
2241 break;
2242 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002243 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002244 if (attribute != null) {
2245 res = startInputUncheckedLocked(cs, inputContext, attribute,
2246 controlFlags);
2247 didStart = true;
2248 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002249 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002250 break;
2251 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002252
2253 if (!didStart && attribute != null) {
2254 res = startInputUncheckedLocked(cs, inputContext, attribute,
2255 controlFlags);
2256 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002257 }
2258 } finally {
2259 Binder.restoreCallingIdentity(ident);
2260 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002261
2262 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002263 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002264
satok42c5a162011-05-26 16:46:14 +09002265 @Override
Seigo Nonaka14e13912015-05-06 21:04:13 -07002266 public void showInputMethodPickerFromClient(
2267 IInputMethodClient client, int auxiliarySubtypeMode) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002268 if (!calledFromValidUser()) {
2269 return;
2270 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002271 synchronized (mMethodMap) {
2272 if (mCurClient == null || client == null
2273 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002274 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002275 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002276 }
2277
satok440aab52010-11-25 09:43:11 +09002278 // Always call subtype picker, because subtype picker is a superset of input method
2279 // picker.
Seigo Nonaka14e13912015-05-06 21:04:13 -07002280 mHandler.sendMessage(mCaller.obtainMessageI(
2281 MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode));
satokab751aa2010-09-14 19:17:36 +09002282 }
2283 }
2284
satok42c5a162011-05-26 16:46:14 +09002285 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002286 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002287 if (!calledFromValidUser()) {
2288 return;
2289 }
satok28203512010-11-24 11:06:49 +09002290 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2291 }
2292
satok42c5a162011-05-26 16:46:14 +09002293 @Override
satok28203512010-11-24 11:06:49 +09002294 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002295 if (!calledFromValidUser()) {
2296 return;
2297 }
satok28203512010-11-24 11:06:49 +09002298 synchronized (mMethodMap) {
2299 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002300 setInputMethodWithSubtypeIdLocked(token, id,
2301 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2302 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002303 } else {
2304 setInputMethod(token, id);
2305 }
2306 }
satokab751aa2010-09-14 19:17:36 +09002307 }
2308
satok42c5a162011-05-26 16:46:14 +09002309 @Override
satokb416a712010-11-25 20:42:14 +09002310 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002311 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002312 if (!calledFromValidUser()) {
2313 return;
2314 }
satokb416a712010-11-25 20:42:14 +09002315 synchronized (mMethodMap) {
2316 if (mCurClient == null || client == null
2317 || mCurClient.client.asBinder() != client.asBinder()) {
2318 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
2319 }
satok7fee71f2010-12-17 18:54:26 +09002320 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2321 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002322 }
2323 }
2324
satok4fc87d62011-05-20 16:13:43 +09002325 @Override
satok735cf382010-11-11 20:40:09 +09002326 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002327 if (!calledFromValidUser()) {
2328 return false;
2329 }
satok735cf382010-11-11 20:40:09 +09002330 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002331 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002332 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002333 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002334 lastImi = mMethodMap.get(lastIme.first);
2335 } else {
2336 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002337 }
satok4fc87d62011-05-20 16:13:43 +09002338 String targetLastImiId = null;
2339 int subtypeId = NOT_A_SUBTYPE_ID;
2340 if (lastIme != null && lastImi != null) {
2341 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
2342 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
2343 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2344 : mCurrentSubtype.hashCode();
2345 // If the last IME is the same as the current IME and the last subtype is not
2346 // defined, there is no need to switch to the last IME.
2347 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2348 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002349 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002350 }
2351 }
2352
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002353 if (TextUtils.isEmpty(targetLastImiId)
2354 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002355 // This is a safety net. If the currentSubtype can't be added to the history
2356 // and the framework couldn't find the last ime, we will make the last ime be
2357 // the most applicable enabled keyboard subtype of the system imes.
2358 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2359 if (enabled != null) {
2360 final int N = enabled.size();
2361 final String locale = mCurrentSubtype == null
2362 ? mRes.getConfiguration().locale.toString()
2363 : mCurrentSubtype.getLocale();
2364 for (int i = 0; i < N; ++i) {
2365 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002366 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002367 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002368 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2369 InputMethodUtils.getSubtypes(imi),
2370 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002371 if (keyboardSubtype != null) {
2372 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002373 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002374 imi, keyboardSubtype.hashCode());
2375 if(keyboardSubtype.getLocale().equals(locale)) {
2376 break;
2377 }
2378 }
2379 }
2380 }
2381 }
2382 }
2383
2384 if (!TextUtils.isEmpty(targetLastImiId)) {
2385 if (DEBUG) {
2386 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2387 + ", from: " + mCurMethodId + ", " + subtypeId);
2388 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002389 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002390 return true;
2391 } else {
2392 return false;
2393 }
satok735cf382010-11-11 20:40:09 +09002394 }
2395 }
2396
satoke7c6998e2011-06-03 17:57:59 +09002397 @Override
satok688bd472012-02-09 20:09:17 +09002398 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002399 if (!calledFromValidUser()) {
2400 return false;
2401 }
satok688bd472012-02-09 20:09:17 +09002402 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002403 if (!calledWithValidToken(token)) {
2404 final int uid = Binder.getCallingUid();
2405 Slog.e(TAG, "Ignoring switchToNextInputMethod due to an invalid token. uid:" + uid
2406 + " token:" + token);
2407 return false;
2408 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002409 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
satok688bd472012-02-09 20:09:17 +09002410 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2411 if (nextSubtype == null) {
2412 return false;
2413 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002414 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2415 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002416 return true;
2417 }
2418 }
2419
2420 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002421 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2422 if (!calledFromValidUser()) {
2423 return false;
2424 }
2425 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002426 if (!calledWithValidToken(token)) {
2427 final int uid = Binder.getCallingUid();
2428 Slog.e(TAG, "Ignoring shouldOfferSwitchingToNextInputMethod due to an invalid "
2429 + "token. uid:" + uid + " token:" + token);
2430 return false;
2431 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002432 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002433 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2434 if (nextSubtype == null) {
2435 return false;
2436 }
2437 return true;
2438 }
2439 }
2440
2441 @Override
satok68f1b782011-04-11 14:26:04 +09002442 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002443 if (!calledFromValidUser()) {
2444 return null;
2445 }
satok68f1b782011-04-11 14:26:04 +09002446 synchronized (mMethodMap) {
2447 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2448 // TODO: Handle the case of the last IME with no subtypes
2449 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2450 || TextUtils.isEmpty(lastIme.second)) return null;
2451 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2452 if (lastImi == null) return null;
2453 try {
2454 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002455 final int lastSubtypeId =
2456 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002457 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2458 return null;
2459 }
2460 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002461 } catch (NumberFormatException e) {
2462 return null;
2463 }
2464 }
2465 }
2466
satoke7c6998e2011-06-03 17:57:59 +09002467 @Override
satokee5e77c2011-09-02 18:50:15 +09002468 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002469 if (!calledFromValidUser()) {
2470 return;
2471 }
satok91e88122011-07-18 11:11:42 +09002472 // By this IPC call, only a process which shares the same uid with the IME can add
2473 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09002474 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09002475 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002476 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002477 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002478 final String[] packageInfos;
2479 try {
2480 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2481 } catch (RemoteException e) {
2482 Slog.e(TAG, "Failed to get package infos");
2483 return;
2484 }
satok91e88122011-07-18 11:11:42 +09002485 if (packageInfos != null) {
2486 final int packageNum = packageInfos.length;
2487 for (int i = 0; i < packageNum; ++i) {
2488 if (packageInfos[i].equals(imi.getPackageName())) {
2489 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002490 final long ident = Binder.clearCallingIdentity();
2491 try {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002492 buildInputMethodListLocked(mMethodList, mMethodMap,
2493 false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002494 } finally {
2495 Binder.restoreCallingIdentity(ident);
2496 }
satokee5e77c2011-09-02 18:50:15 +09002497 return;
satok91e88122011-07-18 11:11:42 +09002498 }
2499 }
2500 }
satoke7c6998e2011-06-03 17:57:59 +09002501 }
satokee5e77c2011-09-02 18:50:15 +09002502 return;
satoke7c6998e2011-06-03 17:57:59 +09002503 }
2504
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002505 @Override
2506 public int getInputMethodWindowVisibleHeight() {
2507 return mWindowManagerService.getInputMethodWindowVisibleHeight();
2508 }
2509
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002510 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002511 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002512 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002513 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002514 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002515 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002516 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
2517 if (DEBUG) {
2518 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
2519 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
2520 + " actual: " + sequenceNumber);
2521 }
2522 return;
2523 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002524 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2525 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09002526 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002527 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002528 }
2529 }
2530
satok28203512010-11-24 11:06:49 +09002531 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002532 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002533 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
2534 }
2535 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002536
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002537 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
2538 if (token == null) {
2539 if (mContext.checkCallingOrSelfPermission(
2540 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2541 != PackageManager.PERMISSION_GRANTED) {
2542 throw new SecurityException(
2543 "Using null token requires permission "
2544 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002545 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002546 } else if (mCurToken != token) {
2547 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2548 + " token: " + token);
2549 return;
2550 }
2551
2552 final long ident = Binder.clearCallingIdentity();
2553 try {
2554 setInputMethodLocked(id, subtypeId);
2555 } finally {
2556 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002557 }
2558 }
2559
satok42c5a162011-05-26 16:46:14 +09002560 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002561 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002562 if (!calledFromValidUser()) {
2563 return;
2564 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002565 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002566 if (!calledWithValidToken(token)) {
2567 final int uid = Binder.getCallingUid();
2568 Slog.e(TAG, "Ignoring hideInputMethod due to an invalid token. uid:"
2569 + uid + " token:" + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002570 return;
2571 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002572 long ident = Binder.clearCallingIdentity();
2573 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002574 hideCurrentInputLocked(flags, null);
2575 } finally {
2576 Binder.restoreCallingIdentity(ident);
2577 }
2578 }
2579 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002580
satok42c5a162011-05-26 16:46:14 +09002581 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002582 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002583 if (!calledFromValidUser()) {
2584 return;
2585 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002586 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002587 if (!calledWithValidToken(token)) {
2588 final int uid = Binder.getCallingUid();
2589 Slog.e(TAG, "Ignoring showMySoftInput due to an invalid token. uid:"
2590 + uid + " token:" + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002591 return;
2592 }
2593 long ident = Binder.clearCallingIdentity();
2594 try {
2595 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002596 } finally {
2597 Binder.restoreCallingIdentity(ident);
2598 }
2599 }
2600 }
2601
2602 void setEnabledSessionInMainThread(SessionState session) {
2603 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002604 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002605 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002606 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002607 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002608 } catch (RemoteException e) {
2609 }
2610 }
2611 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002612 if (mEnabledSession != null && mEnabledSession.session != null) {
2613 try {
2614 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
2615 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
2616 } catch (RemoteException e) {
2617 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002618 }
2619 }
2620 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002621
satok42c5a162011-05-26 16:46:14 +09002622 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002623 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002624 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002625 switch (msg.what) {
satokab751aa2010-09-14 19:17:36 +09002626 case MSG_SHOW_IM_SUBTYPE_PICKER:
Seigo Nonaka14e13912015-05-06 21:04:13 -07002627 final boolean showAuxSubtypes;
2628 switch (msg.arg1) {
2629 case InputMethodManager.SHOW_IM_PICKER_MODE_AUTO:
2630 // This is undocumented so far, but IMM#showInputMethodPicker() has been
2631 // implemented so that auxiliary subtypes will be excluded when the soft
2632 // keyboard is invisible.
2633 showAuxSubtypes = mInputShown;
2634 break;
2635 case InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES:
2636 showAuxSubtypes = true;
2637 break;
2638 case InputMethodManager.SHOW_IM_PICKER_MODE_EXCLUDE_AUXILIARY_SUBTYPES:
2639 showAuxSubtypes = false;
2640 break;
2641 default:
2642 Slog.e(TAG, "Unknown subtype picker mode = " + msg.arg1);
2643 return false;
2644 }
2645 showInputMethodMenu(showAuxSubtypes);
satokab751aa2010-09-14 19:17:36 +09002646 return true;
2647
satok47a44912010-10-06 16:03:58 +09002648 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002649 args = (SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09002650 showInputMethodAndSubtypeEnabler((String)args.arg1);
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002651 args.recycle();
satok217f5482010-12-15 05:19:19 +09002652 return true;
2653
2654 case MSG_SHOW_IM_CONFIG:
2655 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002656 return true;
2657
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002658 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002659
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002660 case MSG_UNBIND_INPUT:
2661 try {
2662 ((IInputMethod)msg.obj).unbindInput();
2663 } catch (RemoteException e) {
2664 // There is nothing interesting about the method dying.
2665 }
2666 return true;
2667 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002668 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002669 try {
2670 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2671 } catch (RemoteException e) {
2672 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002673 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002674 return true;
2675 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002676 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002677 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002678 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002679 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002680 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002681 } catch (RemoteException e) {
2682 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002683 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002684 return true;
2685 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002686 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002687 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002688 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002689 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002690 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002691 } catch (RemoteException e) {
2692 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002693 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002694 return true;
2695 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002696 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002697 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002698 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002699 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2700 } catch (RemoteException e) {
2701 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002702 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002703 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002704 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002705 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002706 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002707 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002708 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002709 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002710 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002711 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002712 // Dispose the channel if the input method is not local to this process
2713 // because the remote proxy will get its own copy when unparceled.
2714 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002715 channel.dispose();
2716 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002717 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002718 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002719 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002720 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002721 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002722
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002723 case MSG_START_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002724 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002725 try {
2726 SessionState session = (SessionState)args.arg1;
2727 setEnabledSessionInMainThread(session);
2728 session.method.startInput((IInputContext)args.arg2,
2729 (EditorInfo)args.arg3);
2730 } catch (RemoteException e) {
2731 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002732 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002733 return true;
2734 case MSG_RESTART_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002735 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002736 try {
2737 SessionState session = (SessionState)args.arg1;
2738 setEnabledSessionInMainThread(session);
2739 session.method.restartInput((IInputContext)args.arg2,
2740 (EditorInfo)args.arg3);
2741 } catch (RemoteException e) {
2742 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002743 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002744 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002746 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002747
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002748 case MSG_UNBIND_METHOD:
2749 try {
2750 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
2751 } catch (RemoteException e) {
2752 // There is nothing interesting about the last client dying.
2753 }
2754 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002755 case MSG_BIND_METHOD: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002756 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002757 IInputMethodClient client = (IInputMethodClient)args.arg1;
2758 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002759 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002760 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002761 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002762 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002763 } finally {
2764 // Dispose the channel if the input method is not local to this process
2765 // because the remote proxy will get its own copy when unparceled.
2766 if (res.channel != null && Binder.isProxy(client)) {
2767 res.channel.dispose();
2768 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002769 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002770 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002771 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002772 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002773 case MSG_SET_ACTIVE:
2774 try {
2775 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2776 } catch (RemoteException e) {
2777 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2778 + ((ClientState)msg.obj).pid + " uid "
2779 + ((ClientState)msg.obj).uid);
2780 }
2781 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002782 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
2783 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07002784 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002785 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07002786 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002787 } catch (RemoteException e) {
2788 Slog.w(TAG, "Got RemoteException sending "
2789 + "setUserActionNotificationSequenceNumber("
2790 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07002791 + clientState.pid + " uid "
2792 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002793 }
2794 return true;
2795 }
satok01038492012-04-09 21:08:27 +09002796
2797 // --------------------------------------------------------------
2798 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07002799 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09002800 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002801 }
2802 return false;
2803 }
2804
satokdc9ddae2011-10-06 12:22:36 +09002805 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002806 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
2807 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09002808 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002809 if (DEBUG) {
2810 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2811 }
satok723a27e2010-11-11 14:58:11 +09002812 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002813 return true;
2814 }
2815
2816 return false;
2817 }
2818
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002819 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002820 HashMap<String, InputMethodInfo> map, boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002821 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002822 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Satoshi Kataoka0766eb02013-07-31 18:30:13 +09002823 + " \n ------ \n" + InputMethodUtils.getStackTrace());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002824 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002825 list.clear();
2826 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002827
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002828 // Use for queryIntentServicesAsUser
2829 final PackageManager pm = mContext.getPackageManager();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002830 String disabledSysImes = mSettings.getDisabledSystemInputMethods();
Amith Yamasanie861ec12010-03-24 21:39:27 -07002831 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002832
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002833 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002834 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002835 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2836 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002837
satoke7c6998e2011-06-03 17:57:59 +09002838 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2839 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002840 for (int i = 0; i < services.size(); ++i) {
2841 ResolveInfo ri = services.get(i);
2842 ServiceInfo si = ri.serviceInfo;
2843 ComponentName compName = new ComponentName(si.packageName, si.name);
2844 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2845 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002846 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002847 + ": it does not require the permission "
2848 + android.Manifest.permission.BIND_INPUT_METHOD);
2849 continue;
2850 }
2851
Joe Onorato8a9b2202010-02-26 18:56:32 -08002852 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002853
2854 try {
satoke7c6998e2011-06-03 17:57:59 +09002855 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002856 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002857 final String id = p.getId();
2858 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002859
2860 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002861 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002862 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002863
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002864 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002865 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002866 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002867 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002868 }
2869 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002870
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002871 if (resetDefaultEnabledIme) {
2872 final ArrayList<InputMethodInfo> defaultEnabledIme =
2873 InputMethodUtils.getDefaultEnabledImes(mContext, mSystemReady, list);
2874 for (int i = 0; i < defaultEnabledIme.size(); ++i) {
2875 final InputMethodInfo imi = defaultEnabledIme.get(i);
2876 if (DEBUG) {
2877 Slog.d(TAG, "--- enable ime = " + imi);
2878 }
2879 setInputMethodEnabledLocked(imi.getId(), true);
2880 }
2881 }
2882
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002883 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09002884 if (!TextUtils.isEmpty(defaultImiId)) {
2885 if (!map.containsKey(defaultImiId)) {
2886 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
2887 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07002888 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09002889 }
2890 } else {
2891 // Double check that the default IME is certainly enabled.
2892 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002893 }
2894 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09002895 // Here is not the perfect place to reset the switching controller. Ideally
2896 // mSwitchingController and mSettings should be able to share the same state.
2897 // TODO: Make sure that mSwitchingController and mSettings are sharing the
2898 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09002899 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002900 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002901
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002902 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002903
satok217f5482010-12-15 05:19:19 +09002904 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002905 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002906 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002907 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2908 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002909 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002910 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002911 }
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002912 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok217f5482010-12-15 05:19:19 +09002913 }
2914
2915 private void showConfigureInputMethods() {
2916 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2917 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2918 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2919 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002920 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09002921 }
2922
satok2c93efc2012-04-02 19:33:47 +09002923 private boolean isScreenLocked() {
2924 return mKeyguardManager != null
2925 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
2926 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002927
Seigo Nonaka14e13912015-05-06 21:04:13 -07002928 private void showInputMethodMenu(boolean showAuxSubtypes) {
2929 if (DEBUG) Slog.v(TAG, "Show switching menu. showAuxSubtypes=" + showAuxSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002930
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002931 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09002932 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002933
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002934 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002935 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002936 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002937
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002938 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002939 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09002940 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
2941 mContext);
satok7f35c8c2010-10-07 21:13:11 +09002942 if (immis == null || immis.size() == 0) {
2943 return;
2944 }
2945
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002946 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002947
satok688bd472012-02-09 20:09:17 +09002948 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002949 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
Seigo Nonaka14e13912015-05-06 21:04:13 -07002950 true /* showSubtypes */, showAuxSubtypes, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09002951
satokc3690562012-01-10 20:14:43 +09002952 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002953 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09002954 if (currentSubtype != null) {
2955 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002956 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
2957 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09002958 }
2959 }
2960
Ken Wakasa761eb372011-03-04 19:06:18 +09002961 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09002962 mIms = new InputMethodInfo[N];
2963 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002964 int checkedItem = 0;
2965 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002966 final ImeSubtypeListItem item = imList.get(i);
2967 mIms[i] = item.mImi;
2968 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002969 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002970 int subtypeId = mSubtypeIds[i];
2971 if ((subtypeId == NOT_A_SUBTYPE_ID)
2972 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2973 || (subtypeId == lastInputMethodSubtypeId)) {
2974 checkedItem = i;
2975 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002976 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002977 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08002978
2979 final Context settingsContext = new ContextThemeWrapper(context,
2980 com.android.internal.R.style.Theme_DeviceDefault_Settings);
2981
2982 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002983 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
2984 @Override
2985 public void onCancel(DialogInterface dialog) {
2986 hideInputMethodMenu();
2987 }
2988 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08002989
2990 final Context dialogContext = mDialogBuilder.getContext();
2991 final TypedArray a = dialogContext.obtainStyledAttributes(null,
2992 com.android.internal.R.styleable.DialogPreference,
2993 com.android.internal.R.attr.alertDialogStyle, 0);
2994 final Drawable dialogIcon = a.getDrawable(
2995 com.android.internal.R.styleable.DialogPreference_dialogIcon);
2996 a.recycle();
2997
2998 mDialogBuilder.setIcon(dialogIcon);
2999
3000 final LayoutInflater inflater = (LayoutInflater) dialogContext.getSystemService(
3001 Context.LAYOUT_INFLATER_SERVICE);
satok01038492012-04-09 21:08:27 +09003002 final View tv = inflater.inflate(
3003 com.android.internal.R.layout.input_method_switch_dialog_title, null);
3004 mDialogBuilder.setCustomTitle(tv);
3005
3006 // Setup layout for a toggle switch of the hardware keyboard
3007 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003008 mSwitchingDialogTitleView
3009 .findViewById(com.android.internal.R.id.hard_keyboard_section)
3010 .setVisibility(mWindowManagerService.isHardKeyboardAvailable()
3011 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003012 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003013 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07003014 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003015 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
3016 @Override
3017 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003018 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003019 // Ensure that the input method dialog is dismissed when changing
3020 // the hardware keyboard state.
3021 hideInputMethodMenu();
3022 }
3023 });
3024
Alan Viverette505e3ab2014-11-24 15:22:11 -08003025 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003026 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
3027 final OnClickListener choiceListener = new OnClickListener() {
3028 @Override
3029 public void onClick(final DialogInterface dialog, final int which) {
3030 synchronized (mMethodMap) {
3031 if (mIms == null || mIms.length <= which || mSubtypeIds == null
3032 || mSubtypeIds.length <= which) {
3033 return;
satok01038492012-04-09 21:08:27 +09003034 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003035 final InputMethodInfo im = mIms[which];
3036 int subtypeId = mSubtypeIds[which];
3037 adapter.mCheckedItem = which;
3038 adapter.notifyDataSetChanged();
3039 hideInputMethodMenu();
3040 if (im != null) {
3041 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
3042 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003043 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003044 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003045 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003046 }
3047 }
3048 };
3049 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003050
Seigo Nonakad4474cb2015-05-04 16:53:24 -07003051 if (!isScreenLocked) {
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003052 final OnClickListener positiveListener = new OnClickListener() {
3053 @Override
3054 public void onClick(DialogInterface dialog, int whichButton) {
3055 showConfigureInputMethods();
3056 }
3057 };
satok82beadf2010-12-27 19:03:06 +09003058 mDialogBuilder.setPositiveButton(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003059 com.android.internal.R.string.configure_input_methods, positiveListener);
satok7f35c8c2010-10-07 21:13:11 +09003060 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003061 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003062 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003063 mSwitchingDialog.getWindow().setType(
3064 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09003065 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
3066 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003067 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003068 updateSystemUi(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003069 mSwitchingDialog.show();
3070 }
3071 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003072
Ken Wakasa05dbb652011-08-22 15:22:43 +09003073 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
3074 private final LayoutInflater mInflater;
3075 private final int mTextViewResourceId;
3076 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09003077 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09003078 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
3079 List<ImeSubtypeListItem> itemsList, int checkedItem) {
3080 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003081
Ken Wakasa05dbb652011-08-22 15:22:43 +09003082 mTextViewResourceId = textViewResourceId;
3083 mItemsList = itemsList;
3084 mCheckedItem = checkedItem;
Alan Viverette505e3ab2014-11-24 15:22:11 -08003085 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Ken Wakasa05dbb652011-08-22 15:22:43 +09003086 }
3087
3088 @Override
3089 public View getView(int position, View convertView, ViewGroup parent) {
3090 final View view = convertView != null ? convertView
3091 : mInflater.inflate(mTextViewResourceId, null);
3092 if (position < 0 || position >= mItemsList.size()) return view;
3093 final ImeSubtypeListItem item = mItemsList.get(position);
3094 final CharSequence imeName = item.mImeName;
3095 final CharSequence subtypeName = item.mSubtypeName;
3096 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
3097 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
3098 if (TextUtils.isEmpty(subtypeName)) {
3099 firstTextView.setText(imeName);
3100 secondTextView.setVisibility(View.GONE);
3101 } else {
3102 firstTextView.setText(subtypeName);
3103 secondTextView.setText(imeName);
3104 secondTextView.setVisibility(View.VISIBLE);
3105 }
3106 final RadioButton radioButton =
3107 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
3108 radioButton.setChecked(position == mCheckedItem);
3109 return view;
3110 }
3111 }
3112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003113 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003114 synchronized (mMethodMap) {
3115 hideInputMethodMenuLocked();
3116 }
3117 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003118
The Android Open Source Project10592532009-03-18 17:39:46 -07003119 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003120 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003121
The Android Open Source Project10592532009-03-18 17:39:46 -07003122 if (mSwitchingDialog != null) {
3123 mSwitchingDialog.dismiss();
3124 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003125 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003126
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003127 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project10592532009-03-18 17:39:46 -07003128 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003129 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003130 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003132 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003133
satok42c5a162011-05-26 16:46:14 +09003134 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003135 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003136 // TODO: Make this work even for non-current users?
3137 if (!calledFromValidUser()) {
3138 return false;
3139 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003140 synchronized (mMethodMap) {
3141 if (mContext.checkCallingOrSelfPermission(
3142 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3143 != PackageManager.PERMISSION_GRANTED) {
3144 throw new SecurityException(
3145 "Requires permission "
3146 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3147 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003149 long ident = Binder.clearCallingIdentity();
3150 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003151 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003152 } finally {
3153 Binder.restoreCallingIdentity(ident);
3154 }
3155 }
3156 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003157
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003158 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3159 // Make sure this is a valid input method.
3160 InputMethodInfo imm = mMethodMap.get(id);
3161 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003162 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003163 }
3164
satokd87c2592010-09-29 11:52:06 +09003165 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3166 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003167
satokd87c2592010-09-29 11:52:06 +09003168 if (enabled) {
3169 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3170 if (pair.first.equals(id)) {
3171 // We are enabling this input method, but it is already enabled.
3172 // Nothing to do. The previous state was enabled.
3173 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003174 }
3175 }
satokd87c2592010-09-29 11:52:06 +09003176 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3177 // Previous state was disabled.
3178 return false;
3179 } else {
3180 StringBuilder builder = new StringBuilder();
3181 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3182 builder, enabledInputMethodsList, id)) {
3183 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003184 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003185 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3186 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3187 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003188 }
3189 // Previous state was enabled.
3190 return true;
3191 } else {
3192 // We are disabling the input method but it is already disabled.
3193 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003194 return false;
3195 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003196 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003197 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003198
satok723a27e2010-11-11 14:58:11 +09003199 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3200 boolean setSubtypeOnly) {
3201 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003202 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003203
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003204 mCurUserActionNotificationSequenceNumber =
3205 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3206 if (DEBUG) {
3207 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3208 + mCurUserActionNotificationSequenceNumber);
3209 }
3210
3211 if (mCurClient != null && mCurClient.client != null) {
3212 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3213 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003214 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003215 }
3216
satok723a27e2010-11-11 14:58:11 +09003217 // Set Subtype here
3218 if (imi == null || subtypeId < 0) {
3219 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003220 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003221 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003222 if (subtypeId < imi.getSubtypeCount()) {
3223 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3224 mSettings.putSelectedSubtype(subtype.hashCode());
3225 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003226 } else {
3227 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003228 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003229 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003230 }
satokab751aa2010-09-14 19:17:36 +09003231 }
satok723a27e2010-11-11 14:58:11 +09003232
satok4c0e7152012-06-20 20:08:44 +09003233 // Workaround.
3234 // ASEC is not ready in the IMMS constructor. Accordingly, forward-locked
3235 // IMEs are not recognized and considered uninstalled.
3236 // Actually, we can't move everything after SystemReady because
3237 // IMMS needs to run in the encryption lock screen. So, we just skip changing
3238 // the default IME here and try cheking the default IME again in systemReady().
3239 // TODO: Do nothing before system ready and implement a separated logic for
3240 // the encryption lock screen.
3241 // TODO: ASEC should be ready before IMMS is instantiated.
3242 if (mSystemReady && !setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003243 // Set InputMethod here
3244 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3245 }
3246 }
3247
3248 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3249 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3250 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3251 // newDefaultIme is empty when there is no candidate for the selected IME.
3252 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3253 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3254 if (subtypeHashCode != null) {
3255 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003256 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok723a27e2010-11-11 14:58:11 +09003257 imi, Integer.valueOf(subtypeHashCode));
3258 } catch (NumberFormatException e) {
3259 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3260 }
3261 }
3262 }
3263 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003264 }
3265
satok4e4569d2010-11-19 18:45:53 +09003266 // If there are no selected shortcuts, tries finding the most applicable ones.
3267 private Pair<InputMethodInfo, InputMethodSubtype>
3268 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3269 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3270 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003271 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003272 boolean foundInSystemIME = false;
3273
3274 // Search applicable subtype for each InputMethodInfo
3275 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003276 final String imiId = imi.getId();
3277 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3278 continue;
3279 }
satokcd7cd292010-11-20 15:46:23 +09003280 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003281 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003282 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003283 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003284 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003285 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003286 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003287 }
satokdf31ae62011-01-15 06:19:44 +09003288 // 2. Search by the system locale from enabledSubtypes.
3289 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003290 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003291 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003292 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003293 }
satoka86f5e42011-09-02 17:12:42 +09003294 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003295 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003296 final ArrayList<InputMethodSubtype> subtypesForSearch =
3297 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003298 ? InputMethodUtils.getSubtypes(imi)
3299 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003300 // 4. Search by the current subtype's locale from all subtypes.
3301 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003302 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003303 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003304 }
3305 // 5. Search by the system locale from all subtypes.
3306 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003307 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003308 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003309 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003310 }
satokcd7cd292010-11-20 15:46:23 +09003311 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003312 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003313 // The current input method is the most applicable IME.
3314 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003315 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003316 break;
satok7599a7f2010-12-22 13:45:23 +09003317 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003318 // The system input method is 2nd applicable IME.
3319 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003320 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003321 if ((imi.getServiceInfo().applicationInfo.flags
3322 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3323 foundInSystemIME = true;
3324 }
satok4e4569d2010-11-19 18:45:53 +09003325 }
3326 }
3327 }
3328 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003329 if (mostApplicableIMI != null) {
3330 Slog.w(TAG, "Most applicable shortcut input method was:"
3331 + mostApplicableIMI.getId());
3332 if (mostApplicableSubtype != null) {
3333 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3334 + "," + mostApplicableSubtype.getMode() + ","
3335 + mostApplicableSubtype.getLocale());
3336 }
3337 }
satok4e4569d2010-11-19 18:45:53 +09003338 }
satokcd7cd292010-11-20 15:46:23 +09003339 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09003340 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09003341 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003342 } else {
3343 return null;
3344 }
3345 }
3346
satokab751aa2010-09-14 19:17:36 +09003347 /**
3348 * @return Return the current subtype of this input method.
3349 */
satok42c5a162011-05-26 16:46:14 +09003350 @Override
satokab751aa2010-09-14 19:17:36 +09003351 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003352 // TODO: Make this work even for non-current users?
3353 if (!calledFromValidUser()) {
3354 return null;
3355 }
3356 synchronized (mMethodMap) {
3357 return getCurrentInputMethodSubtypeLocked();
3358 }
3359 }
3360
3361 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003362 if (mCurMethodId == null) {
3363 return null;
3364 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003365 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003366 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3367 if (imi == null || imi.getSubtypeCount() == 0) {
3368 return null;
satok4e4569d2010-11-19 18:45:53 +09003369 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003370 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003371 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3372 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003373 if (subtypeId == NOT_A_SUBTYPE_ID) {
3374 // If there are no selected subtypes, the framework will try to find
3375 // the most applicable subtype from explicitly or implicitly enabled
3376 // subtypes.
3377 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003378 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003379 // If there is only one explicitly or implicitly enabled subtype,
3380 // just returns it.
3381 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3382 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3383 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003384 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003385 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003386 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003387 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003388 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003389 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3390 true);
satok4e4569d2010-11-19 18:45:53 +09003391 }
satok3ef8b292010-11-23 06:06:29 +09003392 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003393 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003394 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003395 }
3396 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003397 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003398 }
3399
satokf3db1af2010-11-23 13:34:33 +09003400 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
3401 InputMethodSubtype subtype) {
3402 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
3403 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
3404 } else {
3405 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3406 subtypes.add(subtype);
3407 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
3408 }
3409 }
3410
satok4e4569d2010-11-19 18:45:53 +09003411 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003412 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003413 @Override
satok4e4569d2010-11-19 18:45:53 +09003414 public List getShortcutInputMethodsAndSubtypes() {
3415 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09003416 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09003417 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003418 // If there are no selected shortcut subtypes, the framework will try to find
3419 // the most applicable subtype from all subtypes whose mode is
3420 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003421 Pair<InputMethodInfo, InputMethodSubtype> info =
3422 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003423 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003424 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003425 ret.add(info.first);
3426 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003427 }
satok3da92232011-01-11 22:46:30 +09003428 return ret;
satokf3db1af2010-11-23 13:34:33 +09003429 }
satokf3db1af2010-11-23 13:34:33 +09003430 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3431 ret.add(imi);
3432 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3433 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003434 }
3435 }
satokf3db1af2010-11-23 13:34:33 +09003436 return ret;
satok4e4569d2010-11-19 18:45:53 +09003437 }
3438 }
3439
satok42c5a162011-05-26 16:46:14 +09003440 @Override
satokb66d2872010-11-10 01:04:04 +09003441 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003442 // TODO: Make this work even for non-current users?
3443 if (!calledFromValidUser()) {
3444 return false;
3445 }
satokb66d2872010-11-10 01:04:04 +09003446 synchronized (mMethodMap) {
3447 if (subtype != null && mCurMethodId != null) {
3448 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003449 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003450 if (subtypeId != NOT_A_SUBTYPE_ID) {
3451 setInputMethodLocked(mCurMethodId, subtypeId);
3452 return true;
3453 }
3454 }
3455 return false;
3456 }
3457 }
3458
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003459 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003460 private static class InputMethodFileManager {
3461 private static final String SYSTEM_PATH = "system";
3462 private static final String INPUT_METHOD_PATH = "inputmethod";
3463 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3464 private static final String NODE_SUBTYPES = "subtypes";
3465 private static final String NODE_SUBTYPE = "subtype";
3466 private static final String NODE_IMI = "imi";
3467 private static final String ATTR_ID = "id";
3468 private static final String ATTR_LABEL = "label";
3469 private static final String ATTR_ICON = "icon";
3470 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3471 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3472 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3473 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3474 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3475 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003476 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
satoke7c6998e2011-06-03 17:57:59 +09003477 new HashMap<String, List<InputMethodSubtype>>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003478 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003479 if (methodMap == null) {
3480 throw new NullPointerException("methodMap is null");
3481 }
3482 mMethodMap = methodMap;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003483 final File systemDir = userId == UserHandle.USER_OWNER
3484 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3485 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003486 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3487 if (!inputMethodDir.mkdirs()) {
3488 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3489 }
3490 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3491 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3492 if (!subtypeFile.exists()) {
3493 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003494 writeAdditionalInputMethodSubtypes(
3495 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003496 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003497 readAdditionalInputMethodSubtypes(
3498 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003499 }
3500 }
3501
3502 private void deleteAllInputMethodSubtypes(String imiId) {
3503 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003504 mAdditionalSubtypesMap.remove(imiId);
3505 writeAdditionalInputMethodSubtypes(
3506 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003507 }
3508 }
3509
3510 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003511 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003512 synchronized (mMethodMap) {
3513 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3514 final int N = additionalSubtypes.length;
3515 for (int i = 0; i < N; ++i) {
3516 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003517 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003518 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003519 } else {
3520 Slog.w(TAG, "Duplicated subtype definition found: "
3521 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003522 }
3523 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003524 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3525 writeAdditionalInputMethodSubtypes(
3526 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003527 }
3528 }
3529
3530 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3531 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003532 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003533 }
3534 }
3535
3536 private static void writeAdditionalInputMethodSubtypes(
3537 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3538 HashMap<String, InputMethodInfo> methodMap) {
3539 // Safety net for the case that this function is called before methodMap is set.
3540 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3541 FileOutputStream fos = null;
3542 try {
3543 fos = subtypesFile.startWrite();
3544 final XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003545 out.setOutput(fos, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003546 out.startDocument(null, true);
3547 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3548 out.startTag(null, NODE_SUBTYPES);
3549 for (String imiId : allSubtypes.keySet()) {
3550 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3551 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3552 continue;
3553 }
3554 out.startTag(null, NODE_IMI);
3555 out.attribute(null, ATTR_ID, imiId);
3556 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3557 final int N = subtypesList.size();
3558 for (int i = 0; i < N; ++i) {
3559 final InputMethodSubtype subtype = subtypesList.get(i);
3560 out.startTag(null, NODE_SUBTYPE);
3561 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3562 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3563 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3564 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3565 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3566 out.attribute(null, ATTR_IS_AUXILIARY,
3567 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3568 out.endTag(null, NODE_SUBTYPE);
3569 }
3570 out.endTag(null, NODE_IMI);
3571 }
3572 out.endTag(null, NODE_SUBTYPES);
3573 out.endDocument();
3574 subtypesFile.finishWrite(fos);
3575 } catch (java.io.IOException e) {
3576 Slog.w(TAG, "Error writing subtypes", e);
3577 if (fos != null) {
3578 subtypesFile.failWrite(fos);
3579 }
3580 }
3581 }
3582
3583 private static void readAdditionalInputMethodSubtypes(
3584 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3585 if (allSubtypes == null || subtypesFile == null) return;
3586 allSubtypes.clear();
3587 FileInputStream fis = null;
3588 try {
3589 fis = subtypesFile.openRead();
3590 final XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003591 parser.setInput(fis, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003592 int type = parser.getEventType();
3593 // Skip parsing until START_TAG
3594 while ((type = parser.next()) != XmlPullParser.START_TAG
3595 && type != XmlPullParser.END_DOCUMENT) {}
3596 String firstNodeName = parser.getName();
3597 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3598 throw new XmlPullParserException("Xml doesn't start with subtypes");
3599 }
3600 final int depth =parser.getDepth();
3601 String currentImiId = null;
3602 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3603 while (((type = parser.next()) != XmlPullParser.END_TAG
3604 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3605 if (type != XmlPullParser.START_TAG)
3606 continue;
3607 final String nodeName = parser.getName();
3608 if (NODE_IMI.equals(nodeName)) {
3609 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3610 if (TextUtils.isEmpty(currentImiId)) {
3611 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3612 continue;
3613 }
3614 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
3615 allSubtypes.put(currentImiId, tempSubtypesArray);
3616 } else if (NODE_SUBTYPE.equals(nodeName)) {
3617 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3618 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3619 continue;
3620 }
3621 final int icon = Integer.valueOf(
3622 parser.getAttributeValue(null, ATTR_ICON));
3623 final int label = Integer.valueOf(
3624 parser.getAttributeValue(null, ATTR_LABEL));
3625 final String imeSubtypeLocale =
3626 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3627 final String imeSubtypeMode =
3628 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3629 final String imeSubtypeExtraValue =
3630 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003631 final boolean isAuxiliary = "1".equals(String.valueOf(
3632 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003633 final InputMethodSubtype subtype = new InputMethodSubtypeBuilder()
3634 .setSubtypeNameResId(label)
3635 .setSubtypeIconResId(icon)
3636 .setSubtypeLocale(imeSubtypeLocale)
3637 .setSubtypeMode(imeSubtypeMode)
3638 .setSubtypeExtraValue(imeSubtypeExtraValue)
3639 .setIsAuxiliary(isAuxiliary)
3640 .build();
satoke7c6998e2011-06-03 17:57:59 +09003641 tempSubtypesArray.add(subtype);
3642 }
3643 }
3644 } catch (XmlPullParserException e) {
3645 Slog.w(TAG, "Error reading subtypes: " + e);
3646 return;
3647 } catch (java.io.IOException e) {
3648 Slog.w(TAG, "Error reading subtypes: " + e);
3649 return;
3650 } catch (NumberFormatException e) {
3651 Slog.w(TAG, "Error reading subtypes: " + e);
3652 return;
3653 } finally {
3654 if (fis != null) {
3655 try {
3656 fis.close();
3657 } catch (java.io.IOException e1) {
3658 Slog.w(TAG, "Failed to close.");
3659 }
3660 }
3661 }
3662 }
3663 }
3664
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003665 @Override
3666 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3667 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3668 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003669
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003670 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3671 + Binder.getCallingPid()
3672 + ", uid=" + Binder.getCallingUid());
3673 return;
3674 }
3675
3676 IInputMethod method;
3677 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003678
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003679 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003680
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003681 synchronized (mMethodMap) {
3682 p.println("Current Input Method Manager state:");
3683 int N = mMethodList.size();
3684 p.println(" Input Methods:");
3685 for (int i=0; i<N; i++) {
3686 InputMethodInfo info = mMethodList.get(i);
3687 p.println(" InputMethod #" + i + ":");
3688 info.dump(p, " ");
3689 }
3690 p.println(" Clients:");
3691 for (ClientState ci : mClients.values()) {
3692 p.println(" Client " + ci + ":");
3693 p.println(" client=" + ci.client);
3694 p.println(" inputContext=" + ci.inputContext);
3695 p.println(" sessionRequested=" + ci.sessionRequested);
3696 p.println(" curSession=" + ci.curSession);
3697 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003698 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003699 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003700 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3701 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003702 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3703 + " mBoundToMethod=" + mBoundToMethod);
3704 p.println(" mCurToken=" + mCurToken);
3705 p.println(" mCurIntent=" + mCurIntent);
3706 method = mCurMethod;
3707 p.println(" mCurMethod=" + mCurMethod);
3708 p.println(" mEnabledSession=" + mEnabledSession);
3709 p.println(" mShowRequested=" + mShowRequested
3710 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3711 + " mShowForced=" + mShowForced
3712 + " mInputShown=" + mInputShown);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003713 p.println(" mCurUserActionNotificationSequenceNumber="
3714 + mCurUserActionNotificationSequenceNumber);
Dianne Hackbornd4e6d462014-05-16 16:32:37 -07003715 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003716 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003717
Jeff Brownb88102f2010-09-08 11:49:43 -07003718 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003719 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003720 pw.flush();
3721 try {
3722 client.client.asBinder().dump(fd, args);
3723 } catch (RemoteException e) {
3724 p.println("Input method client dead: " + e);
3725 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003726 } else {
3727 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003728 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003729
Jeff Brownb88102f2010-09-08 11:49:43 -07003730 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003731 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003732 pw.flush();
3733 try {
3734 method.asBinder().dump(fd, args);
3735 } catch (RemoteException e) {
3736 p.println("Input method service dead: " + e);
3737 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003738 } else {
3739 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003740 }
3741 }
3742}