blob: d92a89f976cde2d1b386a97786d3c858ce4e51ce [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;
129import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900130import java.util.Collections;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131import java.util.HashMap;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700132import java.util.HashSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133import java.util.List;
satok5b927c432012-05-01 20:09:34 +0900134import java.util.Locale;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135
136/**
137 * This class provides a system service that manages input methods.
138 */
139public class InputMethodManagerService extends IInputMethodManager.Stub
140 implements ServiceConnection, Handler.Callback {
141 static final boolean DEBUG = false;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700142 static final boolean DEBUG_RESTORE = DEBUG || false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700143 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700145 private static final char INPUT_METHOD_SEPARATOR = ':';
146 private static final char INPUT_METHOD_SUBTYPE_SEPARATOR = ';';
147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 static final int MSG_SHOW_IM_PICKER = 1;
satokab751aa2010-09-14 19:17:36 +0900149 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 2;
satok47a44912010-10-06 16:03:58 +0900150 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 3;
satok217f5482010-12-15 05:19:19 +0900151 static final int MSG_SHOW_IM_CONFIG = 4;
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;
401 int mImeWindowVis;
402
Ken Wakasa05dbb652011-08-22 15:22:43 +0900403 private AlertDialog.Builder mDialogBuilder;
404 private AlertDialog mSwitchingDialog;
satok01038492012-04-09 21:08:27 +0900405 private View mSwitchingDialogTitleView;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900406 private InputMethodInfo[] mIms;
407 private int[] mSubtypeIds;
satok5b927c432012-05-01 20:09:34 +0900408 private Locale mLastSystemLocale;
Michael Wright7b5a96b2014-08-09 19:28:42 -0700409 private boolean mShowImeWithHardKeyboard;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900410 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
411 private final IPackageManager mIPackageManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 class SettingsObserver extends ContentObserver {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800414 String mLastEnabled = "";
415
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 SettingsObserver(Handler handler) {
417 super(handler);
418 ContentResolver resolver = mContext.getContentResolver();
419 resolver.registerContentObserver(Settings.Secure.getUriFor(
420 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
satokab751aa2010-09-14 19:17:36 +0900421 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokb6109bb2011-02-03 22:24:54 +0900422 Settings.Secure.ENABLED_INPUT_METHODS), false, this);
423 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokab751aa2010-09-14 19:17:36 +0900424 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
Michael Wright7b5a96b2014-08-09 19:28:42 -0700425 resolver.registerContentObserver(Settings.Secure.getUriFor(
426 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800427 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800428
Michael Wright7b5a96b2014-08-09 19:28:42 -0700429 @Override public void onChange(boolean selfChange, Uri uri) {
430 final Uri showImeUri =
431 Settings.Secure.getUriFor(Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 synchronized (mMethodMap) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700433 if (showImeUri.equals(uri)) {
434 updateKeyboardFromSettingsLocked();
435 } else {
436 boolean enabledChanged = false;
437 String newEnabled = mSettings.getEnabledInputMethodsStr();
438 if (!mLastEnabled.equals(newEnabled)) {
439 mLastEnabled = newEnabled;
440 enabledChanged = true;
441 }
442 updateInputMethodsFromSettingsLocked(enabledChanged);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800443 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 }
445 }
446 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800447
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900448 class ImmsBroadcastReceiver extends android.content.BroadcastReceiver {
449 private void updateActive() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 // Inform the current client of the change in active status
Dianne Hackborna6e41342012-05-22 16:30:34 -0700451 if (mCurClient != null && mCurClient.client != null) {
452 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
453 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 }
455 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900456
457 @Override
458 public void onReceive(Context context, Intent intent) {
459 final String action = intent.getAction();
460 if (Intent.ACTION_SCREEN_ON.equals(action)) {
461 mScreenOn = true;
462 refreshImeWindowVisibilityLocked();
463 updateActive();
464 return;
465 } else if (Intent.ACTION_SCREEN_OFF.equals(action)) {
466 mScreenOn = false;
467 setImeWindowVisibilityStatusHiddenLocked();
468 updateActive();
469 return;
470 } else if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
471 hideInputMethodMenu();
472 // No need to updateActive
473 return;
Amith Yamasani734983f2014-03-04 16:48:05 -0800474 } else if (Intent.ACTION_USER_ADDED.equals(action)
475 || Intent.ACTION_USER_REMOVED.equals(action)) {
Kenny Guy2a764942014-04-02 13:29:20 +0100476 updateCurrentProfileIds();
Amith Yamasani734983f2014-03-04 16:48:05 -0800477 return;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700478 } else if (Intent.ACTION_SETTING_RESTORED.equals(action)) {
479 final String name = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
480 if (Settings.Secure.ENABLED_INPUT_METHODS.equals(name)) {
481 final String prevValue = intent.getStringExtra(
482 Intent.EXTRA_SETTING_PREVIOUS_VALUE);
483 final String newValue = intent.getStringExtra(
484 Intent.EXTRA_SETTING_NEW_VALUE);
485 restoreEnabledInputMethods(mContext, prevValue, newValue);
486 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900487 } else {
488 Slog.w(TAG, "Unexpected intent " + intent);
489 }
490 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800492
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700493 // Apply the results of a restore operation to the set of enabled IMEs. Note that this
494 // does not attempt to validate on the fly with any installed device policy, so must only
495 // be run in the context of initial device setup.
496 //
497 // TODO: Move this method to InputMethodUtils with adding unit tests.
498 static void restoreEnabledInputMethods(Context context, String prevValue, String newValue) {
499 if (DEBUG_RESTORE) {
500 Slog.i(TAG, "Restoring enabled input methods:");
501 Slog.i(TAG, "prev=" + prevValue);
502 Slog.i(TAG, " new=" + newValue);
503 }
504 // 'new' is the just-restored state, 'prev' is what was in settings prior to the restore
505 ArrayMap<String, ArraySet<String>> prevMap = parseInputMethodsAndSubtypesString(prevValue);
506 ArrayMap<String, ArraySet<String>> newMap = parseInputMethodsAndSubtypesString(newValue);
507
508 // Merge the restored ime+subtype enabled states into the live state
509 for (ArrayMap.Entry<String, ArraySet<String>> entry : newMap.entrySet()) {
510 final String imeId = entry.getKey();
511 ArraySet<String> prevSubtypes = prevMap.get(imeId);
512 if (prevSubtypes == null) {
513 prevSubtypes = new ArraySet<String>(2);
514 prevMap.put(imeId, prevSubtypes);
515 }
516 prevSubtypes.addAll(entry.getValue());
517 }
518
519 final String mergedImesAndSubtypesString = buildInputMethodsAndSubtypesString(prevMap);
520 if (DEBUG_RESTORE) {
521 Slog.i(TAG, "Merged IME string:");
522 Slog.i(TAG, " " + mergedImesAndSubtypesString);
523 }
524 Settings.Secure.putString(context.getContentResolver(),
525 Settings.Secure.ENABLED_INPUT_METHODS, mergedImesAndSubtypesString);
526 }
527
528 // TODO: Move this method to InputMethodUtils with adding unit tests.
529 static String buildInputMethodsAndSubtypesString(ArrayMap<String, ArraySet<String>> map) {
530 // we want to use the canonical InputMethodSettings implementation,
531 // so we convert data structures first.
532 List<Pair<String, ArrayList<String>>> imeMap =
533 new ArrayList<Pair<String, ArrayList<String>>>(4);
534 for (ArrayMap.Entry<String, ArraySet<String>> entry : map.entrySet()) {
535 final String imeName = entry.getKey();
536 final ArraySet<String> subtypeSet = entry.getValue();
537 final ArrayList<String> subtypes = new ArrayList<String>(2);
538 if (subtypeSet != null) {
539 subtypes.addAll(subtypeSet);
540 }
541 imeMap.add(new Pair<String, ArrayList<String>>(imeName, subtypes));
542 }
543 return InputMethodSettings.buildInputMethodsSettingString(imeMap);
544 }
545
546 // TODO: Move this method to InputMethodUtils with adding unit tests.
547 static ArrayMap<String, ArraySet<String>> parseInputMethodsAndSubtypesString(
548 final String inputMethodsAndSubtypesString) {
549 final ArrayMap<String, ArraySet<String>> imeMap =
550 new ArrayMap<String, ArraySet<String>>();
551 if (TextUtils.isEmpty(inputMethodsAndSubtypesString)) {
552 return imeMap;
553 }
554
555 final SimpleStringSplitter typeSplitter =
556 new SimpleStringSplitter(INPUT_METHOD_SEPARATOR);
557 final SimpleStringSplitter subtypeSplitter =
558 new SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATOR);
559 List<Pair<String, ArrayList<String>>> allImeSettings =
560 InputMethodSettings.buildInputMethodsAndSubtypeList(inputMethodsAndSubtypesString,
561 typeSplitter,
562 subtypeSplitter);
563 for (Pair<String, ArrayList<String>> ime : allImeSettings) {
564 ArraySet<String> subtypes = new ArraySet<String>();
565 if (ime.second != null) {
566 subtypes.addAll(ime.second);
567 }
568 imeMap.put(ime.first, subtypes);
569 }
570 return imeMap;
571 }
572
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800573 class MyPackageMonitor extends PackageMonitor {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900574 private boolean isChangingPackagesOfCurrentUser() {
575 final int userId = getChangingUserId();
576 final boolean retval = userId == mSettings.getCurrentUserId();
577 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900578 if (!retval) {
579 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
580 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900581 }
582 return retval;
583 }
584
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800586 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900587 if (!isChangingPackagesOfCurrentUser()) {
588 return false;
589 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800590 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900591 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800592 final int N = mMethodList.size();
593 if (curInputMethodId != null) {
594 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800595 InputMethodInfo imi = mMethodList.get(i);
596 if (imi.getId().equals(curInputMethodId)) {
597 for (String pkg : packages) {
598 if (imi.getPackageName().equals(pkg)) {
599 if (!doit) {
600 return true;
601 }
satok723a27e2010-11-11 14:58:11 +0900602 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800603 chooseNewDefaultIMELocked();
604 return true;
605 }
606 }
607 }
608 }
609 }
610 }
611 return false;
612 }
613
614 @Override
615 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900616 if (!isChangingPackagesOfCurrentUser()) {
617 return;
618 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800619 synchronized (mMethodMap) {
620 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900621 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800622 final int N = mMethodList.size();
623 if (curInputMethodId != null) {
624 for (int i=0; i<N; i++) {
625 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900626 final String imiId = imi.getId();
627 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800628 curIm = imi;
629 }
satoke7c6998e2011-06-03 17:57:59 +0900630
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800631 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900632 if (isPackageModified(imi.getPackageName())) {
633 mFileManager.deleteAllInputMethodSubtypes(imiId);
634 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800635 if (change == PACKAGE_TEMPORARY_CHANGE
636 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800637 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800638 + imi.getComponent());
639 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640 }
641 }
642 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800643
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900644 buildInputMethodListLocked(
645 mMethodList, mMethodMap, false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800646
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800647 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800648
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800649 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800650 int change = isPackageDisappearing(curIm.getPackageName());
651 if (change == PACKAGE_TEMPORARY_CHANGE
652 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800653 ServiceInfo si = null;
654 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900655 si = mIPackageManager.getServiceInfo(
656 curIm.getComponent(), 0, mSettings.getCurrentUserId());
657 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800658 }
659 if (si == null) {
660 // Uh oh, current input method is no longer around!
661 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800662 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
satok15452a42011-10-28 17:58:28 +0900663 setImeWindowVisibilityStatusHiddenLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800664 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800665 changed = true;
666 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800667 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900668 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800669 }
670 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800671 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800672 }
satokab751aa2010-09-14 19:17:36 +0900673
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800674 if (curIm == null) {
675 // We currently don't have a default input method... is
676 // one now available?
677 changed = chooseNewDefaultIMELocked();
678 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800679
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800680 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800681 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682 }
683 }
684 }
685 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800686
Jeff Brownc28867a2013-03-26 15:42:39 -0700687 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900688 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -0700689 private final IInputMethod mMethod;
690 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800691
Jeff Brownc28867a2013-03-26 15:42:39 -0700692 MethodCallback(InputMethodManagerService imms, IInputMethod method,
693 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900694 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -0700695 mMethod = method;
696 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800697 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800698
satoke7c6998e2011-06-03 17:57:59 +0900699 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -0700700 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -0700701 long ident = Binder.clearCallingIdentity();
702 try {
703 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
704 } finally {
705 Binder.restoreCallingIdentity(ident);
706 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800707 }
708 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800709
satok01038492012-04-09 21:08:27 +0900710 private class HardKeyboardListener
711 implements WindowManagerService.OnHardKeyboardStatusChangeListener {
712 @Override
Michael Wright7b5a96b2014-08-09 19:28:42 -0700713 public void onHardKeyboardStatusChange(boolean available) {
714 mHandler.sendMessage(mHandler.obtainMessage(MSG_HARD_KEYBOARD_SWITCH_CHANGED,
715 available ? 1 : 0));
satok01038492012-04-09 21:08:27 +0900716 }
717
Michael Wright7b5a96b2014-08-09 19:28:42 -0700718 public void handleHardKeyboardStatusChange(boolean available) {
satok01038492012-04-09 21:08:27 +0900719 if (DEBUG) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700720 Slog.w(TAG, "HardKeyboardStatusChanged: available=" + available);
satok01038492012-04-09 21:08:27 +0900721 }
722 synchronized(mMethodMap) {
723 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
724 && mSwitchingDialog.isShowing()) {
725 mSwitchingDialogTitleView.findViewById(
726 com.android.internal.R.id.hard_keyboard_section).setVisibility(
727 available ? View.VISIBLE : View.GONE);
728 }
729 }
730 }
731 }
732
733 public InputMethodManagerService(Context context, WindowManagerService windowManager) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900734 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800735 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800736 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800737 mHandler = new Handler(this);
738 mIWindowManager = IWindowManager.Stub.asInterface(
739 ServiceManager.getService(Context.WINDOW_SERVICE));
Mita Yuned218c72012-12-06 17:18:25 -0800740 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900741 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800742 public void executeMessage(Message msg) {
743 handleMessage(msg);
744 }
Mita Yuned218c72012-12-06 17:18:25 -0800745 }, true /*asyncHandler*/);
satok01038492012-04-09 21:08:27 +0900746 mWindowManagerService = windowManager;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900747 mAppOpsManager = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE);
satok01038492012-04-09 21:08:27 +0900748 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700749 mHasFeature = context.getPackageManager().hasSystemFeature(
750 PackageManager.FEATURE_INPUT_METHODS);
satok7cfc0ed2011-06-20 21:29:36 +0900751
satok7cfc0ed2011-06-20 21:29:36 +0900752 mImeSwitcherNotification = new Notification();
753 mImeSwitcherNotification.icon = com.android.internal.R.drawable.ic_notification_ime_default;
754 mImeSwitcherNotification.when = 0;
755 mImeSwitcherNotification.flags = Notification.FLAG_ONGOING_EVENT;
756 mImeSwitcherNotification.tickerText = null;
757 mImeSwitcherNotification.defaults = 0; // please be quiet
758 mImeSwitcherNotification.sound = null;
759 mImeSwitcherNotification.vibrate = null;
Daniel Sandler590d5152012-06-14 16:10:13 -0400760
761 // Tag this notification specially so SystemUI knows it's important
John Spurlockfd7f1e02014-03-18 16:41:57 -0400762 mImeSwitcherNotification.extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
763 mImeSwitcherNotification.category = Notification.CATEGORY_SYSTEM;
Daniel Sandler590d5152012-06-14 16:10:13 -0400764
satok7cfc0ed2011-06-20 21:29:36 +0900765 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900766 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900767
768 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900769
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900770 final IntentFilter broadcastFilter = new IntentFilter();
771 broadcastFilter.addAction(Intent.ACTION_SCREEN_ON);
772 broadcastFilter.addAction(Intent.ACTION_SCREEN_OFF);
773 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Amith Yamasani734983f2014-03-04 16:48:05 -0800774 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
775 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700776 broadcastFilter.addAction(Intent.ACTION_SETTING_RESTORED);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900777 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800778
satok7cfc0ed2011-06-20 21:29:36 +0900779 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900780 int userId = 0;
781 try {
782 ActivityManagerNative.getDefault().registerUserSwitchObserver(
783 new IUserSwitchObserver.Stub() {
784 @Override
785 public void onUserSwitching(int newUserId, IRemoteCallback reply) {
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900786 synchronized(mMethodMap) {
787 switchUserLocked(newUserId);
788 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900789 if (reply != null) {
790 try {
791 reply.sendResult(null);
792 } catch (RemoteException e) {
793 }
794 }
795 }
796
797 @Override
798 public void onUserSwitchComplete(int newUserId) throws RemoteException {
799 }
800 });
801 userId = ActivityManagerNative.getDefault().getCurrentUser().id;
802 } catch (RemoteException e) {
803 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
804 }
satok81f8b7c2012-12-04 20:42:56 +0900805 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
satok913a8922010-08-26 21:53:41 +0900806
satokd87c2592010-09-29 11:52:06 +0900807 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900808 mSettings = new InputMethodSettings(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900809 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId);
Kenny Guy2a764942014-04-02 13:29:20 +0100810 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900811 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900812 synchronized (mMethodMap) {
813 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
814 mSettings, context);
815 }
satok0a1bcf42012-05-16 19:26:31 +0900816
817 // Just checking if defaultImiId is empty or not
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900818 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900819 if (DEBUG) {
820 Slog.d(TAG, "Initial default ime = " + defaultImiId);
821 }
satok0a1bcf42012-05-16 19:26:31 +0900822 mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
823
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900824 synchronized (mMethodMap) {
825 buildInputMethodListLocked(mMethodList, mMethodMap,
826 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
827 }
satokd87c2592010-09-29 11:52:06 +0900828 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800829
satok0a1bcf42012-05-16 19:26:31 +0900830 if (!mImeSelectedOnBoot) {
831 Slog.w(TAG, "No IME selected. Choose the most applicable IME.");
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900832 synchronized (mMethodMap) {
833 resetDefaultImeLocked(context);
834 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800835 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800836
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800837 mSettingsObserver = new SettingsObserver(mHandler);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900838 synchronized (mMethodMap) {
839 updateFromSettingsLocked(true);
840 }
satok5b927c432012-05-01 20:09:34 +0900841
842 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME
843 // according to the new system locale.
844 final IntentFilter filter = new IntentFilter();
845 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
846 mContext.registerReceiver(
847 new BroadcastReceiver() {
848 @Override
849 public void onReceive(Context context, Intent intent) {
850 synchronized(mMethodMap) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900851 resetStateIfCurrentLocaleChangedLocked();
satok5b927c432012-05-01 20:09:34 +0900852 }
853 }
854 }, filter);
855 }
856
satok5b927c432012-05-01 20:09:34 +0900857 private void resetDefaultImeLocked(Context context) {
858 // Do not reset the default (current) IME when it is a 3rd-party IME
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900859 if (mCurMethodId != null
860 && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +0900861 return;
862 }
863
864 InputMethodInfo defIm = null;
865 for (InputMethodInfo imi : mMethodList) {
Yohei Yukawa6aa03782015-02-21 03:00:22 +0900866 if (defIm == null && mSystemReady) {
867 final Locale systemLocale = context.getResources().getConfiguration().locale;
868 if (InputMethodUtils.isSystemImeThatHasSubtypeOf(imi, context,
869 true /* checkDefaultAttribute */, systemLocale, false /* checkCountry */,
870 InputMethodUtils.SUBTYPE_MODE_ANY)) {
satok5b927c432012-05-01 20:09:34 +0900871 defIm = imi;
872 Slog.i(TAG, "Selected default: " + imi.getId());
873 }
874 }
875 }
876 if (defIm == null && mMethodList.size() > 0) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900877 defIm = InputMethodUtils.getMostApplicableDefaultIME(
878 mSettings.getEnabledInputMethodListLocked());
Hyejin Kim5baaaac2014-10-27 17:17:06 +0900879 if (defIm != null) {
880 Slog.i(TAG, "Default found, using " + defIm.getId());
881 } else {
882 Slog.i(TAG, "No default found");
883 }
satok5b927c432012-05-01 20:09:34 +0900884 }
885 if (defIm != null) {
886 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
887 }
888 }
889
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900890 private void resetAllInternalStateLocked(final boolean updateOnlyWhenLocaleChanged,
891 final boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900892 if (!mSystemReady) {
893 // not system ready
894 return;
895 }
896 final Locale newLocale = mRes.getConfiguration().locale;
897 if (!updateOnlyWhenLocaleChanged
898 || (newLocale != null && !newLocale.equals(mLastSystemLocale))) {
899 if (!updateOnlyWhenLocaleChanged) {
900 hideCurrentInputLocked(0, null);
901 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -0700902 unbindCurrentMethodLocked(true, false);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900903 }
904 if (DEBUG) {
905 Slog.i(TAG, "Locale has been changed to " + newLocale);
906 }
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900907 buildInputMethodListLocked(mMethodList, mMethodMap, resetDefaultEnabledIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900908 if (!updateOnlyWhenLocaleChanged) {
909 final String selectedImiId = mSettings.getSelectedInputMethod();
910 if (TextUtils.isEmpty(selectedImiId)) {
911 // This is the first time of the user switch and
912 // set the current ime to the proper one.
913 resetDefaultImeLocked(mContext);
914 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +0900915 } else {
916 // If the locale is changed, needs to reset the default ime
917 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900918 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800919 updateFromSettingsLocked(true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900920 mLastSystemLocale = newLocale;
921 if (!updateOnlyWhenLocaleChanged) {
922 try {
923 startInputInnerLocked();
924 } catch (RuntimeException e) {
925 Slog.w(TAG, "Unexpected exception", e);
926 }
927 }
928 }
929 }
930
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900931 private void resetStateIfCurrentLocaleChangedLocked() {
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900932 resetAllInternalStateLocked(true /* updateOnlyWhenLocaleChanged */,
933 true /* resetDefaultImeLocked */);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900934 }
935
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900936 private void switchUserLocked(int newUserId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900937 mSettings.setCurrentUserId(newUserId);
Kenny Guy2a764942014-04-02 13:29:20 +0100938 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900939 // InputMethodFileManager should be reset when the user is changed
940 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900941 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +0900942 // For secondary users, the list of enabled IMEs may not have been updated since the
943 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
944 // not be empty even if the IME has been uninstalled by the primary user.
945 // Even in such cases, IMMS works fine because it will find the most applicable
946 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900947 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900948 if (DEBUG) {
949 Slog.d(TAG, "Switch user: " + newUserId + " current ime = " + defaultImiId);
950 }
951 resetAllInternalStateLocked(false /* updateOnlyWhenLocaleChanged */,
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900952 initialUserSwitch /* needsToResetDefaultIme */);
953 if (initialUserSwitch) {
954 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mContext.getPackageManager(),
955 mSettings.getEnabledInputMethodListLocked());
956 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900957 }
958
Kenny Guy2a764942014-04-02 13:29:20 +0100959 void updateCurrentProfileIds() {
960 List<UserInfo> profiles =
961 UserManager.get(mContext).getProfiles(mSettings.getCurrentUserId());
962 int[] currentProfileIds = new int[profiles.size()]; // profiles will not be null
963 for (int i = 0; i < currentProfileIds.length; i++) {
964 currentProfileIds[i] = profiles.get(i).id;
Amith Yamasani734983f2014-03-04 16:48:05 -0800965 }
Kenny Guy2a764942014-04-02 13:29:20 +0100966 mSettings.setCurrentProfileIds(currentProfileIds);
Amith Yamasani734983f2014-03-04 16:48:05 -0800967 }
968
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800969 @Override
970 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
971 throws RemoteException {
972 try {
973 return super.onTransact(code, data, reply, flags);
974 } catch (RuntimeException e) {
975 // The input method manager only throws security exceptions, so let's
976 // log all others.
977 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -0700978 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800979 }
980 throw e;
981 }
982 }
983
Svetoslav Ganova0027152013-06-25 14:59:53 -0700984 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700985 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900986 if (DEBUG) {
987 Slog.d(TAG, "--- systemReady");
988 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700989 if (!mSystemReady) {
990 mSystemReady = true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900991 mKeyguardManager =
992 (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700993 mNotificationManager = (NotificationManager)
994 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
995 mStatusBar = statusBar;
996 statusBar.setIconVisibility("ime", false);
997 updateImeWindowStatusLocked();
satokb858c732011-07-22 19:54:34 +0900998 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
999 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +09001000 if (mShowOngoingImeSwitcherForPhones) {
1001 mWindowManagerService.setOnHardKeyboardStatusChangeListener(
1002 mHardKeyboardListener);
1003 }
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09001004 buildInputMethodListLocked(mMethodList, mMethodMap,
1005 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satok0a1bcf42012-05-16 19:26:31 +09001006 if (!mImeSelectedOnBoot) {
1007 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001008 resetStateIfCurrentLocaleChangedLocked();
1009 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(
1010 mContext.getPackageManager(),
1011 mSettings.getEnabledInputMethodListLocked());
satok0a1bcf42012-05-16 19:26:31 +09001012 }
1013 mLastSystemLocale = mRes.getConfiguration().locale;
Dianne Hackborncc278702009-09-02 23:07:23 -07001014 try {
1015 startInputInnerLocked();
1016 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001017 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -07001018 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001019 }
1020 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001022
satok15452a42011-10-28 17:58:28 +09001023 private void setImeWindowVisibilityStatusHiddenLocked() {
1024 mImeWindowVis = 0;
1025 updateImeWindowStatusLocked();
1026 }
1027
satok3afd6c02011-11-18 08:38:19 +09001028 private void refreshImeWindowVisibilityLocked() {
1029 final Configuration conf = mRes.getConfiguration();
1030 final boolean haveHardKeyboard = conf.keyboard
1031 != Configuration.KEYBOARD_NOKEYS;
1032 final boolean hardKeyShown = haveHardKeyboard
1033 && conf.hardKeyboardHidden
1034 != Configuration.HARDKEYBOARDHIDDEN_YES;
John Spurlocke0980502013-10-25 11:59:29 -04001035
1036 final boolean isScreenLocked = isKeyguardLocked();
1037 final boolean inputActive = !isScreenLocked && (mInputShown || hardKeyShown);
John Spurlock36439b42013-10-23 16:36:47 -04001038 // We assume the softkeyboard is shown when the input is active as long as the
1039 // hard keyboard is not shown.
1040 final boolean inputVisible = inputActive && !hardKeyShown;
1041 mImeWindowVis = (inputActive ? InputMethodService.IME_ACTIVE : 0)
1042 | (inputVisible ? InputMethodService.IME_VISIBLE : 0);
satok3afd6c02011-11-18 08:38:19 +09001043 updateImeWindowStatusLocked();
1044 }
1045
satok15452a42011-10-28 17:58:28 +09001046 private void updateImeWindowStatusLocked() {
satokdbf29502011-08-25 15:28:23 +09001047 setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001048 }
1049
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001050 // ---------------------------------------------------------------------------------------
1051 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
1052 // 1) it comes from the system process
1053 // 2) the calling process' user id is identical to the current user id IMMS thinks.
1054 private boolean calledFromValidUser() {
1055 final int uid = Binder.getCallingUid();
1056 final int userId = UserHandle.getUserId(uid);
1057 if (DEBUG) {
1058 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
1059 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
1060 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +09001061 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
1062 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001063 }
Kenny Guy2a764942014-04-02 13:29:20 +01001064 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001065 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001066 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001067
1068 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
1069 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
1070 // must not manage background users' states in any functions.
1071 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
1072 // by a token.
1073 if (mContext.checkCallingOrSelfPermission(
1074 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1075 == PackageManager.PERMISSION_GRANTED) {
1076 if (DEBUG) {
1077 Slog.d(TAG, "--- Access granted because the calling process has "
1078 + "the INTERACT_ACROSS_USERS_FULL permission");
1079 }
1080 return true;
1081 }
Satoshi Kataoka0766eb02013-07-31 18:30:13 +09001082 Slog.w(TAG, "--- IPC called from background users. Ignore. \n"
1083 + InputMethodUtils.getStackTrace());
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001084 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001085 }
1086
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001087
1088 /**
1089 * Returns true iff the caller is identified to be the current input method with the token.
1090 * @param token The window token given to the input method when it was started.
1091 * @return true if and only if non-null valid token is specified.
1092 */
1093 private boolean calledWithValidToken(IBinder token) {
1094 if (token == null || mCurToken != token) {
1095 return false;
1096 }
1097 return true;
1098 }
1099
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001100 private boolean bindCurrentInputMethodService(
1101 Intent service, ServiceConnection conn, int flags) {
1102 if (service == null || conn == null) {
1103 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
1104 return false;
1105 }
Amith Yamasani27b89e62013-01-16 12:30:11 -08001106 return mContext.bindServiceAsUser(service, conn, flags,
1107 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001108 }
1109
satoke7c6998e2011-06-03 17:57:59 +09001110 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001112 // TODO: Make this work even for non-current users?
1113 if (!calledFromValidUser()) {
1114 return Collections.emptyList();
1115 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001116 synchronized (mMethodMap) {
1117 return new ArrayList<InputMethodInfo>(mMethodList);
1118 }
1119 }
1120
satoke7c6998e2011-06-03 17:57:59 +09001121 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001122 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001123 // TODO: Make this work even for non-current users?
1124 if (!calledFromValidUser()) {
1125 return Collections.emptyList();
1126 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001127 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001128 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001129 }
1130 }
1131
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001132 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001133 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001134 * @return enabled subtypes of the specified imi
1135 */
satoke7c6998e2011-06-03 17:57:59 +09001136 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001137 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001138 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001139 // TODO: Make this work even for non-current users?
1140 if (!calledFromValidUser()) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001141 return Collections.<InputMethodSubtype>emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001142 }
satok67ddf9c2010-11-17 09:45:54 +09001143 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001144 final InputMethodInfo imi;
1145 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001146 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001147 } else {
1148 imi = mMethodMap.get(imiId);
1149 }
1150 if (imi == null) {
1151 return Collections.<InputMethodSubtype>emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001152 }
1153 return mSettings.getEnabledInputMethodSubtypeListLocked(
1154 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001155 }
1156 }
1157
satoke7c6998e2011-06-03 17:57:59 +09001158 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 public void addClient(IInputMethodClient client,
1160 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001161 if (!calledFromValidUser()) {
1162 return;
1163 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001164 synchronized (mMethodMap) {
1165 mClients.put(client.asBinder(), new ClientState(client,
1166 inputContext, uid, pid));
1167 }
1168 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001169
satoke7c6998e2011-06-03 17:57:59 +09001170 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001172 if (!calledFromValidUser()) {
1173 return;
1174 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001175 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001176 ClientState cs = mClients.remove(client.asBinder());
1177 if (cs != null) {
1178 clearClientSessionLocked(cs);
1179 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 }
1181 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001183 void executeOrSendMessage(IInterface target, Message msg) {
1184 if (target.asBinder() instanceof Binder) {
1185 mCaller.sendMessage(msg);
1186 } else {
1187 handleMessage(msg);
1188 msg.recycle();
1189 }
1190 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001191
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001192 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001193 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001194 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001195 + mCurClient.client.asBinder());
1196 if (mBoundToMethod) {
1197 mBoundToMethod = false;
1198 if (mCurMethod != null) {
1199 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1200 MSG_UNBIND_INPUT, mCurMethod));
1201 }
1202 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001203
1204 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1205 MSG_SET_ACTIVE, 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1207 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1208 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001210
The Android Open Source Project10592532009-03-18 17:39:46 -07001211 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 }
1213 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 private int getImeShowFlags() {
1216 int flags = 0;
1217 if (mShowForced) {
1218 flags |= InputMethod.SHOW_FORCED
1219 | InputMethod.SHOW_EXPLICIT;
1220 } else if (mShowExplicitlyRequested) {
1221 flags |= InputMethod.SHOW_EXPLICIT;
1222 }
1223 return flags;
1224 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 private int getAppShowFlags() {
1227 int flags = 0;
1228 if (mShowForced) {
1229 flags |= InputMethodManager.SHOW_FORCED;
1230 } else if (!mShowExplicitlyRequested) {
1231 flags |= InputMethodManager.SHOW_IMPLICIT;
1232 }
1233 return flags;
1234 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001235
Dianne Hackborn7663d802012-02-24 13:08:49 -08001236 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237 if (!mBoundToMethod) {
1238 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1239 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1240 mBoundToMethod = true;
1241 }
1242 final SessionState session = mCurClient.curSession;
1243 if (initial) {
1244 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1245 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
1246 } else {
1247 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1248 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
1249 }
1250 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001251 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001252 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001253 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001254 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001255 (session.channel != null ? session.channel.dup() : null),
1256 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001257 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001259 InputBindResult startInputLocked(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001260 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001261 // If no method is currently selected, do nothing.
1262 if (mCurMethodId == null) {
1263 return mNoBinding;
1264 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001265
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001266 ClientState cs = mClients.get(client.asBinder());
1267 if (cs == null) {
1268 throw new IllegalArgumentException("unknown client "
1269 + client.asBinder());
1270 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001272 try {
1273 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1274 // Check with the window manager to make sure this client actually
1275 // has a window with focus. If not, reject. This is thread safe
1276 // because if the focus changes some time before or after, the
1277 // next client receiving focus that has any interest in input will
1278 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001279 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001280 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1281 return null;
1282 }
1283 } catch (RemoteException e) {
1284 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001285
Dianne Hackborn7663d802012-02-24 13:08:49 -08001286 return startInputUncheckedLocked(cs, inputContext, attribute, controlFlags);
1287 }
1288
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001289 InputBindResult startInputUncheckedLocked(@NonNull ClientState cs,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001290 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
1291 // If no method is currently selected, do nothing.
1292 if (mCurMethodId == null) {
1293 return mNoBinding;
1294 }
1295
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001296 if (attribute != null) {
1297 // We accept an empty package name as a valid data.
1298 if (!TextUtils.isEmpty(attribute.packageName) &&
1299 !InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.uid,
1300 attribute.packageName)) {
1301 Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
1302 + " uid=" + cs.uid + " package=" + attribute.packageName);
1303 return mNoBinding;
1304 }
1305 }
1306
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001307 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001308 // Was the keyguard locked when switching over to the new client?
1309 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310 // If the client is changing, we need to switch over to the new
1311 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001312 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001313 if (DEBUG) Slog.v(TAG, "switching to client: client = "
John Spurlocke0980502013-10-25 11:59:29 -04001314 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001315
1316 // If the screen is on, inform the new client it is active
1317 if (mScreenOn) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001318 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
1319 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320 }
1321 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001323 // Bump up the sequence for this client and attach it.
1324 mCurSeq++;
1325 if (mCurSeq <= 0) mCurSeq = 1;
1326 mCurClient = cs;
1327 mCurInputContext = inputContext;
1328 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001329
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001330 // Check if the input method is changing.
1331 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1332 if (cs.curSession != null) {
1333 // Fast case: if we are already connected to the input method,
1334 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001335 return attachNewInputLocked(
1336 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001337 }
1338 if (mHaveConnection) {
1339 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001340 // Return to client, and we will get back with it when
1341 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001342 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001343 return new InputBindResult(null, null, mCurId, mCurSeq,
1344 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001345 } else if (SystemClock.uptimeMillis()
1346 < (mLastBindTime+TIME_TO_RECONNECT)) {
1347 // In this case we have connected to the service, but
1348 // don't yet have its interface. If it hasn't been too
1349 // long since we did the connection, we'll return to
1350 // the client and wait to get the service interface so
1351 // we can report back. If it has been too long, we want
1352 // to fall through so we can try a disconnect/reconnect
1353 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001354 return new InputBindResult(null, null, mCurId, mCurSeq,
1355 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001356 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001357 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1358 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001359 }
1360 }
1361 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001362
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001363 return startInputInnerLocked();
1364 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001365
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001366 InputBindResult startInputInnerLocked() {
1367 if (mCurMethodId == null) {
1368 return mNoBinding;
1369 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001370
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001371 if (!mSystemReady) {
1372 // If the system is not yet ready, we shouldn't be running third
1373 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001374 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1375 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001376 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001377
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001378 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1379 if (info == null) {
1380 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1381 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001382
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001383 unbindCurrentMethodLocked(false, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001384
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001385 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1386 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001387 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1388 com.android.internal.R.string.input_method_binding_label);
1389 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1390 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001391 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001392 | Context.BIND_NOT_VISIBLE | Context.BIND_NOT_FOREGROUND
1393 | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001394 mLastBindTime = SystemClock.uptimeMillis();
1395 mHaveConnection = true;
1396 mCurId = info.getId();
1397 mCurToken = new Binder();
1398 try {
Craig Mautnerca0ac712013-03-14 09:43:02 -07001399 if (true || DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001400 mIWindowManager.addWindowToken(mCurToken,
1401 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1402 } catch (RemoteException e) {
1403 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001404 return new InputBindResult(null, null, mCurId, mCurSeq,
1405 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001406 } else {
1407 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001408 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001409 + mCurIntent);
1410 }
1411 return null;
1412 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001413
satoke7c6998e2011-06-03 17:57:59 +09001414 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001415 public InputBindResult startInput(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001416 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001417 if (!calledFromValidUser()) {
1418 return null;
1419 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001420 synchronized (mMethodMap) {
1421 final long ident = Binder.clearCallingIdentity();
1422 try {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001423 return startInputLocked(client, inputContext, attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424 } finally {
1425 Binder.restoreCallingIdentity(ident);
1426 }
1427 }
1428 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001429
satoke7c6998e2011-06-03 17:57:59 +09001430 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001431 public void finishInput(IInputMethodClient client) {
1432 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001433
satoke7c6998e2011-06-03 17:57:59 +09001434 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001435 public void onServiceConnected(ComponentName name, IBinder service) {
1436 synchronized (mMethodMap) {
1437 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1438 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001439 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001440 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001441 unbindCurrentMethodLocked(false, false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001442 return;
1443 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001444 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001445 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1446 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001447 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001448 clearClientSessionLocked(mCurClient);
1449 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001450 }
1451 }
1452 }
1453 }
1454
Jeff Brownc28867a2013-03-26 15:42:39 -07001455 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1456 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001457 synchronized (mMethodMap) {
1458 if (mCurMethod != null && method != null
1459 && mCurMethod.asBinder() == method.asBinder()) {
1460 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001461 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001462 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001463 method, session, channel);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001464 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001465 if (res.method != null) {
1466 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
1467 MSG_BIND_METHOD, mCurClient.client, res));
1468 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001469 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001470 }
1471 }
1472 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001473
1474 // Session abandoned. Close its associated input channel.
1475 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001476 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001477
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001478 void unbindCurrentMethodLocked(boolean reportToClient, boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001479 if (mVisibleBound) {
1480 mContext.unbindService(mVisibleConnection);
1481 mVisibleBound = false;
1482 }
1483
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001484 if (mHaveConnection) {
1485 mContext.unbindService(this);
1486 mHaveConnection = false;
1487 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001488
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001489 if (mCurToken != null) {
1490 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001491 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001492 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001493 // The current IME is shown. Hence an IME switch (transition) is happening.
1494 mWindowManagerService.saveLastInputMethodWindowForTransition();
1495 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001496 mIWindowManager.removeWindowToken(mCurToken);
1497 } catch (RemoteException e) {
1498 }
1499 mCurToken = null;
1500 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001501
The Android Open Source Project10592532009-03-18 17:39:46 -07001502 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001503 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001504
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001505 if (reportToClient && mCurClient != null) {
1506 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1507 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1508 }
1509 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001510
1511 void requestClientSessionLocked(ClientState cs) {
1512 if (!cs.sessionRequested) {
1513 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1514 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1515 cs.sessionRequested = true;
1516 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1517 MSG_CREATE_SESSION, mCurMethod, channels[1],
1518 new MethodCallback(this, mCurMethod, channels[0])));
1519 }
1520 }
1521
1522 void clearClientSessionLocked(ClientState cs) {
1523 finishSessionLocked(cs.curSession);
1524 cs.curSession = null;
1525 cs.sessionRequested = false;
1526 }
1527
1528 private void finishSessionLocked(SessionState sessionState) {
1529 if (sessionState != null) {
1530 if (sessionState.session != null) {
1531 try {
1532 sessionState.session.finishSession();
1533 } catch (RemoteException e) {
1534 Slog.w(TAG, "Session failed to close due to remote exception", e);
1535 setImeWindowVisibilityStatusHiddenLocked();
1536 }
1537 sessionState.session = null;
1538 }
1539 if (sessionState.channel != null) {
1540 sessionState.channel.dispose();
1541 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001542 }
1543 }
1544 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001545
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001546 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001547 if (mCurMethod != null) {
1548 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001549 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001550 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001551
Jeff Brownc28867a2013-03-26 15:42:39 -07001552 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001553 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001554 mCurMethod = null;
1555 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001556 if (mStatusBar != null) {
1557 mStatusBar.setIconVisibility("ime", false);
1558 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001559 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001560
satoke7c6998e2011-06-03 17:57:59 +09001561 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001562 public void onServiceDisconnected(ComponentName name) {
1563 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001564 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001565 + " mCurIntent=" + mCurIntent);
1566 if (mCurMethod != null && mCurIntent != null
1567 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001568 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001569 // We consider this to be a new bind attempt, since the system
1570 // should now try to restart the service for us.
1571 mLastBindTime = SystemClock.uptimeMillis();
1572 mShowRequested = mInputShown;
1573 mInputShown = false;
1574 if (mCurClient != null) {
1575 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1576 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1577 }
1578 }
1579 }
1580 }
1581
satokf9f01002011-05-19 21:31:50 +09001582 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001583 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
1584 long ident = Binder.clearCallingIdentity();
1585 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001586 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09001587 if (!calledWithValidToken(token)) {
1588 final int uid = Binder.getCallingUid();
1589 Slog.e(TAG, "Ignoring updateStatusIcon due to an invalid token. uid:" + uid
1590 + " token:" + token);
1591 return;
1592 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001593 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001594 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001595 if (mStatusBar != null) {
1596 mStatusBar.setIconVisibility("ime", false);
1597 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001598 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001599 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001600 CharSequence contentDescription = null;
1601 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001602 // Use PackageManager to load label
1603 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001604 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001605 mIPackageManager.getApplicationInfo(packageName, 0,
1606 mSettings.getCurrentUserId()));
1607 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001608 /* ignore */
1609 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001610 if (mStatusBar != null) {
1611 mStatusBar.setIcon("ime", packageName, iconId, 0,
1612 contentDescription != null
1613 ? contentDescription.toString() : null);
1614 mStatusBar.setIconVisibility("ime", true);
1615 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001616 }
1617 }
1618 } finally {
1619 Binder.restoreCallingIdentity(ident);
1620 }
1621 }
1622
satok7cfc0ed2011-06-20 21:29:36 +09001623 private boolean needsToShowImeSwitchOngoingNotification() {
1624 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04001625 if (mSwitchingDialog != null) return false;
satok2c93efc2012-04-02 19:33:47 +09001626 if (isScreenLocked()) return false;
satok7cfc0ed2011-06-20 21:29:36 +09001627 synchronized (mMethodMap) {
1628 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1629 final int N = imis.size();
satokb6359412011-06-28 17:47:41 +09001630 if (N > 2) return true;
1631 if (N < 1) return false;
1632 int nonAuxCount = 0;
1633 int auxCount = 0;
1634 InputMethodSubtype nonAuxSubtype = null;
1635 InputMethodSubtype auxSubtype = null;
satok7cfc0ed2011-06-20 21:29:36 +09001636 for(int i = 0; i < N; ++i) {
1637 final InputMethodInfo imi = imis.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001638 final List<InputMethodSubtype> subtypes =
1639 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satok7cfc0ed2011-06-20 21:29:36 +09001640 final int subtypeCount = subtypes.size();
1641 if (subtypeCount == 0) {
satokb6359412011-06-28 17:47:41 +09001642 ++nonAuxCount;
satok7cfc0ed2011-06-20 21:29:36 +09001643 } else {
1644 for (int j = 0; j < subtypeCount; ++j) {
satokb6359412011-06-28 17:47:41 +09001645 final InputMethodSubtype subtype = subtypes.get(j);
1646 if (!subtype.isAuxiliary()) {
1647 ++nonAuxCount;
1648 nonAuxSubtype = subtype;
1649 } else {
1650 ++auxCount;
1651 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001652 }
1653 }
1654 }
satok7cfc0ed2011-06-20 21:29:36 +09001655 }
satokb6359412011-06-28 17:47:41 +09001656 if (nonAuxCount > 1 || auxCount > 1) {
1657 return true;
1658 } else if (nonAuxCount == 1 && auxCount == 1) {
1659 if (nonAuxSubtype != null && auxSubtype != null
satok9747f892011-09-12 15:56:40 +09001660 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1661 || auxSubtype.overridesImplicitlyEnabledSubtype()
1662 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
satokb6359412011-06-28 17:47:41 +09001663 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1664 return false;
1665 }
1666 return true;
1667 }
1668 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001669 }
satok7cfc0ed2011-06-20 21:29:36 +09001670 }
1671
John Spurlocke0980502013-10-25 11:59:29 -04001672 private boolean isKeyguardLocked() {
1673 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1674 }
1675
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001676 // Caution! This method is called in this class. Handle multi-user carefully
satokdbf29502011-08-25 15:28:23 +09001677 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001678 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001679 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001680 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001681 try {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001682 if (!calledWithValidToken(token)) {
1683 final int uid = Binder.getCallingUid();
1684 Slog.e(TAG, "Ignoring setImeWindowStatus due to an invalid token. uid:" + uid
1685 + " token:" + token);
satok06487a52010-10-29 11:37:18 +09001686 return;
1687 }
satok06487a52010-10-29 11:37:18 +09001688 synchronized (mMethodMap) {
John Spurlocke0980502013-10-25 11:59:29 -04001689 // apply policy for binder calls
1690 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
1691 vis = 0;
1692 }
Joe Onorato857fd9b2011-01-27 15:08:35 -08001693 mImeWindowVis = vis;
1694 mBackDisposition = backDisposition;
jungheang.lee217fd292013-02-26 16:53:22 +09001695 final boolean iconVisibility = ((vis & (InputMethodService.IME_ACTIVE)) != 0)
1696 && (mWindowManagerService.isHardKeyboardAvailable()
1697 || (vis & (InputMethodService.IME_VISIBLE)) != 0);
Jason Monkb605fec2014-05-02 17:04:10 -04001698 final boolean needsToShowImeSwitcher = iconVisibility
1699 && needsToShowImeSwitchOngoingNotification();
1700 if (mStatusBar != null) {
1701 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
1702 needsToShowImeSwitcher);
1703 }
satok5bc8e732011-07-22 21:07:23 +09001704 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Jason Monkb605fec2014-05-02 17:04:10 -04001705 if (imi != null && needsToShowImeSwitcher) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001706 // Used to load label
satok7cfc0ed2011-06-20 21:29:36 +09001707 final CharSequence title = mRes.getText(
1708 com.android.internal.R.string.select_input_method);
Satoshi Kataokab2827262013-07-04 19:43:14 +09001709 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
1710 mContext, imi, mCurrentSubtype);
satok5bc8e732011-07-22 21:07:23 +09001711
Alan Viverette4a357cd2015-03-18 18:37:18 -07001712 mImeSwitcherNotification.color = mContext.getColor(
Selim Cinek255dd042014-08-19 22:29:02 +02001713 com.android.internal.R.color.system_notification_accent_color);
satok7cfc0ed2011-06-20 21:29:36 +09001714 mImeSwitcherNotification.setLatestEventInfo(
satok5bc8e732011-07-22 21:07:23 +09001715 mContext, title, summary, mImeSwitchPendingIntent);
Jason Monke463c742014-05-02 12:31:45 -04001716 if ((mNotificationManager != null)
1717 && !mWindowManagerService.hasNavigationBar()) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001718 if (DEBUG) {
Satoshi Kataokab2827262013-07-04 19:43:14 +09001719 Slog.d(TAG, "--- show notification: label = " + summary);
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001720 }
1721 mNotificationManager.notifyAsUser(null,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001722 com.android.internal.R.string.select_input_method,
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001723 mImeSwitcherNotification, UserHandle.ALL);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001724 mNotificationShown = true;
1725 }
satok7cfc0ed2011-06-20 21:29:36 +09001726 } else {
Dianne Hackborn661cd522011-08-22 00:26:20 -07001727 if (mNotificationShown && mNotificationManager != null) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001728 if (DEBUG) {
1729 Slog.d(TAG, "--- hide notification");
1730 }
1731 mNotificationManager.cancelAsUser(null,
1732 com.android.internal.R.string.select_input_method, UserHandle.ALL);
satok7cfc0ed2011-06-20 21:29:36 +09001733 mNotificationShown = false;
1734 }
1735 }
satok06487a52010-10-29 11:37:18 +09001736 }
1737 } finally {
1738 Binder.restoreCallingIdentity(ident);
1739 }
1740 }
1741
satoke7c6998e2011-06-03 17:57:59 +09001742 @Override
satokf9f01002011-05-19 21:31:50 +09001743 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001744 if (!calledFromValidUser()) {
1745 return;
1746 }
satokf9f01002011-05-19 21:31:50 +09001747 synchronized (mMethodMap) {
1748 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1749 for (int i = 0; i < spans.length; ++i) {
1750 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001751 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001752 mSecureSuggestionSpans.put(ss, currentImi);
1753 }
1754 }
1755 }
1756 }
1757
satoke7c6998e2011-06-03 17:57:59 +09001758 @Override
satokf9f01002011-05-19 21:31:50 +09001759 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001760 if (!calledFromValidUser()) {
1761 return false;
1762 }
satokf9f01002011-05-19 21:31:50 +09001763 synchronized (mMethodMap) {
1764 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1765 // TODO: Do not send the intent if the process of the targetImi is already dead.
1766 if (targetImi != null) {
1767 final String[] suggestions = span.getSuggestions();
1768 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001769 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001770 final Intent intent = new Intent();
1771 // Ensures that only a class in the original IME package will receive the
1772 // notification.
satok42c5a162011-05-26 16:46:14 +09001773 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001774 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1775 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1776 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1777 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001778 final long ident = Binder.clearCallingIdentity();
1779 try {
1780 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1781 } finally {
1782 Binder.restoreCallingIdentity(ident);
1783 }
satokf9f01002011-05-19 21:31:50 +09001784 return true;
1785 }
1786 }
1787 return false;
1788 }
1789
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001790 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001791 updateInputMethodsFromSettingsLocked(enabledMayChange);
1792 updateKeyboardFromSettingsLocked();
1793 }
1794
1795 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001796 if (enabledMayChange) {
1797 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1798 for (int i=0; i<enabled.size(); i++) {
1799 // We allow the user to select "disabled until used" apps, so if they
1800 // are enabling one of those here we now need to make it enabled.
1801 InputMethodInfo imm = enabled.get(i);
1802 try {
1803 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1804 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1805 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09001806 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001807 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001808 if (DEBUG) {
1809 Slog.d(TAG, "Update state(" + imm.getId()
1810 + "): DISABLED_UNTIL_USED -> DEFAULT");
1811 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001812 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1813 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001814 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
1815 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001816 }
1817 } catch (RemoteException e) {
1818 }
1819 }
1820 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001821 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1822 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1823 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1824 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001825 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001826 // There is no input method selected, try to choose new applicable input method.
1827 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001828 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001829 }
1830 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001831 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001832 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001833 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001834 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001835 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001836 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001837 }
satokf3db1af2010-11-23 13:34:33 +09001838 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001839 } else {
1840 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001841 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001842 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001843 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09001844 // Here is not the perfect place to reset the switching controller. Ideally
1845 // mSwitchingController and mSettings should be able to share the same state.
1846 // TODO: Make sure that mSwitchingController and mSettings are sharing the
1847 // the same enabled IMEs list.
1848 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07001849
1850 }
1851
1852 public void updateKeyboardFromSettingsLocked() {
1853 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
1854 if (mSwitchingDialog != null
1855 && mSwitchingDialogTitleView != null
1856 && mSwitchingDialog.isShowing()) {
1857 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
1858 com.android.internal.R.id.hard_keyboard_switch);
1859 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
1860 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001861 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001862
satokab751aa2010-09-14 19:17:36 +09001863 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001864 InputMethodInfo info = mMethodMap.get(id);
1865 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001866 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001867 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001868
Yohei Yukawae63b5fa2014-09-19 13:14:55 +09001869 if (mCurClient != null && mCurAttribute != null) {
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001870 // We have already made sure that the package name belongs to the application's UID.
1871 // No further UID check is required.
1872 if (SystemConfig.getInstance().getFixedImeApps().contains(mCurAttribute.packageName)) {
1873 return;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +09001874 }
1875 }
1876
satokd81e9502012-05-21 12:58:45 +09001877 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001878 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09001879 final int subtypeCount = info.getSubtypeCount();
1880 if (subtypeCount <= 0) {
1881 return;
satokcd7cd292010-11-20 15:46:23 +09001882 }
satokd81e9502012-05-21 12:58:45 +09001883 final InputMethodSubtype oldSubtype = mCurrentSubtype;
1884 final InputMethodSubtype newSubtype;
1885 if (subtypeId >= 0 && subtypeId < subtypeCount) {
1886 newSubtype = info.getSubtypeAt(subtypeId);
1887 } else {
1888 // If subtype is null, try to find the most applicable one from
1889 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001890 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09001891 }
1892 if (newSubtype == null || oldSubtype == null) {
1893 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
1894 + ", new subtype = " + newSubtype);
1895 return;
1896 }
1897 if (newSubtype != oldSubtype) {
1898 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1899 if (mCurMethod != null) {
1900 try {
1901 refreshImeWindowVisibilityLocked();
1902 mCurMethod.changeInputMethodSubtype(newSubtype);
1903 } catch (RemoteException e) {
1904 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
satokab751aa2010-09-14 19:17:36 +09001905 }
1906 }
1907 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001908 return;
1909 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001910
satokd81e9502012-05-21 12:58:45 +09001911 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001912 final long ident = Binder.clearCallingIdentity();
1913 try {
satokab751aa2010-09-14 19:17:36 +09001914 // Set a subtype to this input method.
1915 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001916 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1917 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1918 // because mCurMethodId is stored as a history in
1919 // setSelectedInputMethodAndSubtypeLocked().
1920 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001921
1922 if (ActivityManagerNative.isSystemReady()) {
1923 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001924 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001925 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07001926 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001927 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001928 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001929 } finally {
1930 Binder.restoreCallingIdentity(ident);
1931 }
1932 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001933
satok42c5a162011-05-26 16:46:14 +09001934 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001935 public boolean showSoftInput(IInputMethodClient client, int flags,
1936 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001937 if (!calledFromValidUser()) {
1938 return false;
1939 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001940 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001941 long ident = Binder.clearCallingIdentity();
1942 try {
1943 synchronized (mMethodMap) {
1944 if (mCurClient == null || client == null
1945 || mCurClient.client.asBinder() != client.asBinder()) {
1946 try {
1947 // We need to check if this is the current client with
1948 // focus in the window manager, to allow this call to
1949 // be made before input is started in it.
1950 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001951 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001952 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001953 }
1954 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001955 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001956 }
1957 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001958
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001959 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001960 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001961 }
1962 } finally {
1963 Binder.restoreCallingIdentity(ident);
1964 }
1965 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001966
The Android Open Source Project4df24232009-03-05 14:34:35 -08001967 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001968 mShowRequested = true;
1969 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1970 mShowExplicitlyRequested = true;
1971 }
1972 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1973 mShowExplicitlyRequested = true;
1974 mShowForced = true;
1975 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001976
Dianne Hackborncc278702009-09-02 23:07:23 -07001977 if (!mSystemReady) {
1978 return false;
1979 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001980
The Android Open Source Project4df24232009-03-05 14:34:35 -08001981 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001982 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001983 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001984 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1985 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1986 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001987 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001988 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001989 bindCurrentInputMethodService(
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001990 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE
1991 | Context.BIND_TREAT_LIKE_ACTIVITY);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001992 mVisibleBound = true;
1993 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001994 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001995 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09001996 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001997 // The client has asked to have the input method shown, but
1998 // we have been sitting here too long with a connection to the
1999 // service and no interface received, so let's disconnect/connect
2000 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002001 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002002 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09002003 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002004 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002005 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002006 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002007 } else {
2008 if (DEBUG) {
2009 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
2010 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
2011 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002012 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002013
The Android Open Source Project4df24232009-03-05 14:34:35 -08002014 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002015 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002016
satok42c5a162011-05-26 16:46:14 +09002017 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002018 public boolean hideSoftInput(IInputMethodClient client, int flags,
2019 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002020 if (!calledFromValidUser()) {
2021 return false;
2022 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002023 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002024 long ident = Binder.clearCallingIdentity();
2025 try {
2026 synchronized (mMethodMap) {
2027 if (mCurClient == null || client == null
2028 || mCurClient.client.asBinder() != client.asBinder()) {
2029 try {
2030 // We need to check if this is the current client with
2031 // focus in the window manager, to allow this call to
2032 // be made before input is started in it.
2033 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002034 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
2035 + uid + ": " + client);
satok15452a42011-10-28 17:58:28 +09002036 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08002037 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002038 }
2039 } catch (RemoteException e) {
satok15452a42011-10-28 17:58:28 +09002040 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08002041 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002042 }
2043 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002044
Joe Onorato8a9b2202010-02-26 18:56:32 -08002045 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002046 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002047 }
2048 } finally {
2049 Binder.restoreCallingIdentity(ident);
2050 }
2051 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002052
The Android Open Source Project4df24232009-03-05 14:34:35 -08002053 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002054 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
2055 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002056 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 -08002057 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002058 }
2059 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002060 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 -08002061 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002062 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002063 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002064 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002065 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2066 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
2067 res = true;
2068 } else {
2069 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002070 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002071 if (mHaveConnection && mVisibleBound) {
2072 mContext.unbindService(mVisibleConnection);
2073 mVisibleBound = false;
2074 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002075 mInputShown = false;
2076 mShowRequested = false;
2077 mShowExplicitlyRequested = false;
2078 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002079 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002080 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002081
satok42c5a162011-05-26 16:46:14 +09002082 @Override
Dianne Hackborn7663d802012-02-24 13:08:49 -08002083 public InputBindResult windowGainedFocus(IInputMethodClient client, IBinder windowToken,
2084 int controlFlags, int softInputMode, int windowFlags,
2085 EditorInfo attribute, IInputContext inputContext) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002086 // Needs to check the validity before clearing calling identity
2087 final boolean calledFromValidUser = calledFromValidUser();
2088
Dianne Hackborn7663d802012-02-24 13:08:49 -08002089 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002090 long ident = Binder.clearCallingIdentity();
2091 try {
2092 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002093 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
Dianne Hackborn7663d802012-02-24 13:08:49 -08002094 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002095 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08002096 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002097
Dianne Hackborn7663d802012-02-24 13:08:49 -08002098 ClientState cs = mClients.get(client.asBinder());
2099 if (cs == null) {
2100 throw new IllegalArgumentException("unknown client "
2101 + client.asBinder());
2102 }
2103
2104 try {
2105 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2106 // Check with the window manager to make sure this client actually
2107 // has a window with focus. If not, reject. This is thread safe
2108 // because if the focus changes some time before or after, the
2109 // next client receiving focus that has any interest in input will
2110 // be calling through here after that change happens.
2111 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2112 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2113 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002114 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002115 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002116 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002117
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002118 if (!calledFromValidUser) {
2119 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2120 Slog.w(TAG, "If you want to interect with IME, you need "
2121 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2122 hideCurrentInputLocked(0, null);
2123 return null;
2124 }
2125
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002126 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07002127 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09002128 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002129 if (attribute != null) {
2130 return startInputUncheckedLocked(cs, inputContext, attribute,
2131 controlFlags);
2132 }
2133 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002134 }
2135 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002136
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002137 // Should we auto-show the IME even if the caller has not
2138 // specified what should be done with it?
2139 // We only do this automatically if the window can resize
2140 // to accommodate the IME (so what the user sees will give
2141 // them good context without input information being obscured
2142 // by the IME) or if running on a large screen where there
2143 // is more room for the target window + IME.
2144 final boolean doAutoShow =
2145 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2146 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2147 || mRes.getConfiguration().isLayoutSizeAtLeast(
2148 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002149 final boolean isTextEditor =
2150 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2151
2152 // We want to start input before showing the IME, but after closing
2153 // it. We want to do this after closing it to help the IME disappear
2154 // more quickly (not get stuck behind it initializing itself for the
2155 // new focused input, even if its window wants to hide the IME).
2156 boolean didStart = false;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002158 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2159 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002160 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002161 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2162 // There is no focus view, and this window will
2163 // be behind any soft input window, so hide the
2164 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002165 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002166 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002167 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002168 } else if (isTextEditor && doAutoShow && (softInputMode &
2169 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002170 // There is a focus view, and we are navigating forward
2171 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002172 // We only do this automatically if the window can resize
2173 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002174 // them good context without input information being obscured
2175 // by the IME) or if running on a large screen where there
2176 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002177 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002178 if (attribute != null) {
2179 res = startInputUncheckedLocked(cs, inputContext, attribute,
2180 controlFlags);
2181 didStart = true;
2182 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002183 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002184 }
2185 break;
2186 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2187 // Do nothing.
2188 break;
2189 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2190 if ((softInputMode &
2191 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002192 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002193 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002194 }
2195 break;
2196 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002197 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002198 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002199 break;
2200 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2201 if ((softInputMode &
2202 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002203 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002204 if (attribute != null) {
2205 res = startInputUncheckedLocked(cs, inputContext, attribute,
2206 controlFlags);
2207 didStart = true;
2208 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002209 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002210 }
2211 break;
2212 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002213 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002214 if (attribute != null) {
2215 res = startInputUncheckedLocked(cs, inputContext, attribute,
2216 controlFlags);
2217 didStart = true;
2218 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002219 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002220 break;
2221 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002222
2223 if (!didStart && attribute != null) {
2224 res = startInputUncheckedLocked(cs, inputContext, attribute,
2225 controlFlags);
2226 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002227 }
2228 } finally {
2229 Binder.restoreCallingIdentity(ident);
2230 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002231
2232 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002233 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002234
satok42c5a162011-05-26 16:46:14 +09002235 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002236 public void showInputMethodPickerFromClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002237 if (!calledFromValidUser()) {
2238 return;
2239 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002240 synchronized (mMethodMap) {
2241 if (mCurClient == null || client == null
2242 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002243 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002244 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002245 }
2246
satok440aab52010-11-25 09:43:11 +09002247 // Always call subtype picker, because subtype picker is a superset of input method
2248 // picker.
satokab751aa2010-09-14 19:17:36 +09002249 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
2250 }
2251 }
2252
satok42c5a162011-05-26 16:46:14 +09002253 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002254 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002255 if (!calledFromValidUser()) {
2256 return;
2257 }
satok28203512010-11-24 11:06:49 +09002258 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2259 }
2260
satok42c5a162011-05-26 16:46:14 +09002261 @Override
satok28203512010-11-24 11:06:49 +09002262 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002263 if (!calledFromValidUser()) {
2264 return;
2265 }
satok28203512010-11-24 11:06:49 +09002266 synchronized (mMethodMap) {
2267 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002268 setInputMethodWithSubtypeIdLocked(token, id,
2269 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2270 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002271 } else {
2272 setInputMethod(token, id);
2273 }
2274 }
satokab751aa2010-09-14 19:17:36 +09002275 }
2276
satok42c5a162011-05-26 16:46:14 +09002277 @Override
satokb416a712010-11-25 20:42:14 +09002278 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002279 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002280 if (!calledFromValidUser()) {
2281 return;
2282 }
satokb416a712010-11-25 20:42:14 +09002283 synchronized (mMethodMap) {
2284 if (mCurClient == null || client == null
2285 || mCurClient.client.asBinder() != client.asBinder()) {
2286 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
2287 }
satok7fee71f2010-12-17 18:54:26 +09002288 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2289 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002290 }
2291 }
2292
satok4fc87d62011-05-20 16:13:43 +09002293 @Override
satok735cf382010-11-11 20:40:09 +09002294 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002295 if (!calledFromValidUser()) {
2296 return false;
2297 }
satok735cf382010-11-11 20:40:09 +09002298 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002299 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002300 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002301 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002302 lastImi = mMethodMap.get(lastIme.first);
2303 } else {
2304 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002305 }
satok4fc87d62011-05-20 16:13:43 +09002306 String targetLastImiId = null;
2307 int subtypeId = NOT_A_SUBTYPE_ID;
2308 if (lastIme != null && lastImi != null) {
2309 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
2310 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
2311 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2312 : mCurrentSubtype.hashCode();
2313 // If the last IME is the same as the current IME and the last subtype is not
2314 // defined, there is no need to switch to the last IME.
2315 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2316 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002317 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002318 }
2319 }
2320
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002321 if (TextUtils.isEmpty(targetLastImiId)
2322 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002323 // This is a safety net. If the currentSubtype can't be added to the history
2324 // and the framework couldn't find the last ime, we will make the last ime be
2325 // the most applicable enabled keyboard subtype of the system imes.
2326 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2327 if (enabled != null) {
2328 final int N = enabled.size();
2329 final String locale = mCurrentSubtype == null
2330 ? mRes.getConfiguration().locale.toString()
2331 : mCurrentSubtype.getLocale();
2332 for (int i = 0; i < N; ++i) {
2333 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002334 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002335 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002336 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2337 InputMethodUtils.getSubtypes(imi),
2338 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002339 if (keyboardSubtype != null) {
2340 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002341 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002342 imi, keyboardSubtype.hashCode());
2343 if(keyboardSubtype.getLocale().equals(locale)) {
2344 break;
2345 }
2346 }
2347 }
2348 }
2349 }
2350 }
2351
2352 if (!TextUtils.isEmpty(targetLastImiId)) {
2353 if (DEBUG) {
2354 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2355 + ", from: " + mCurMethodId + ", " + subtypeId);
2356 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002357 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002358 return true;
2359 } else {
2360 return false;
2361 }
satok735cf382010-11-11 20:40:09 +09002362 }
2363 }
2364
satoke7c6998e2011-06-03 17:57:59 +09002365 @Override
satok688bd472012-02-09 20:09:17 +09002366 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002367 if (!calledFromValidUser()) {
2368 return false;
2369 }
satok688bd472012-02-09 20:09:17 +09002370 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002371 if (!calledWithValidToken(token)) {
2372 final int uid = Binder.getCallingUid();
2373 Slog.e(TAG, "Ignoring switchToNextInputMethod due to an invalid token. uid:" + uid
2374 + " token:" + token);
2375 return false;
2376 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002377 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
satok688bd472012-02-09 20:09:17 +09002378 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2379 if (nextSubtype == null) {
2380 return false;
2381 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002382 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2383 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002384 return true;
2385 }
2386 }
2387
2388 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002389 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2390 if (!calledFromValidUser()) {
2391 return false;
2392 }
2393 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002394 if (!calledWithValidToken(token)) {
2395 final int uid = Binder.getCallingUid();
2396 Slog.e(TAG, "Ignoring shouldOfferSwitchingToNextInputMethod due to an invalid "
2397 + "token. uid:" + uid + " token:" + token);
2398 return false;
2399 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002400 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002401 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2402 if (nextSubtype == null) {
2403 return false;
2404 }
2405 return true;
2406 }
2407 }
2408
2409 @Override
satok68f1b782011-04-11 14:26:04 +09002410 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002411 if (!calledFromValidUser()) {
2412 return null;
2413 }
satok68f1b782011-04-11 14:26:04 +09002414 synchronized (mMethodMap) {
2415 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2416 // TODO: Handle the case of the last IME with no subtypes
2417 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2418 || TextUtils.isEmpty(lastIme.second)) return null;
2419 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2420 if (lastImi == null) return null;
2421 try {
2422 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002423 final int lastSubtypeId =
2424 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002425 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2426 return null;
2427 }
2428 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002429 } catch (NumberFormatException e) {
2430 return null;
2431 }
2432 }
2433 }
2434
satoke7c6998e2011-06-03 17:57:59 +09002435 @Override
satokee5e77c2011-09-02 18:50:15 +09002436 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002437 if (!calledFromValidUser()) {
2438 return;
2439 }
satok91e88122011-07-18 11:11:42 +09002440 // By this IPC call, only a process which shares the same uid with the IME can add
2441 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09002442 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09002443 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002444 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002445 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002446 final String[] packageInfos;
2447 try {
2448 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2449 } catch (RemoteException e) {
2450 Slog.e(TAG, "Failed to get package infos");
2451 return;
2452 }
satok91e88122011-07-18 11:11:42 +09002453 if (packageInfos != null) {
2454 final int packageNum = packageInfos.length;
2455 for (int i = 0; i < packageNum; ++i) {
2456 if (packageInfos[i].equals(imi.getPackageName())) {
2457 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002458 final long ident = Binder.clearCallingIdentity();
2459 try {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002460 buildInputMethodListLocked(mMethodList, mMethodMap,
2461 false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002462 } finally {
2463 Binder.restoreCallingIdentity(ident);
2464 }
satokee5e77c2011-09-02 18:50:15 +09002465 return;
satok91e88122011-07-18 11:11:42 +09002466 }
2467 }
2468 }
satoke7c6998e2011-06-03 17:57:59 +09002469 }
satokee5e77c2011-09-02 18:50:15 +09002470 return;
satoke7c6998e2011-06-03 17:57:59 +09002471 }
2472
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002473 @Override
2474 public int getInputMethodWindowVisibleHeight() {
2475 return mWindowManagerService.getInputMethodWindowVisibleHeight();
2476 }
2477
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002478 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002479 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002480 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002481 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002482 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002483 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002484 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
2485 if (DEBUG) {
2486 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
2487 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
2488 + " actual: " + sequenceNumber);
2489 }
2490 return;
2491 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002492 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2493 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09002494 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002495 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002496 }
2497 }
2498
satok28203512010-11-24 11:06:49 +09002499 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002500 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002501 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
2502 }
2503 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002504
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002505 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
2506 if (token == null) {
2507 if (mContext.checkCallingOrSelfPermission(
2508 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2509 != PackageManager.PERMISSION_GRANTED) {
2510 throw new SecurityException(
2511 "Using null token requires permission "
2512 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002513 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002514 } else if (mCurToken != token) {
2515 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2516 + " token: " + token);
2517 return;
2518 }
2519
2520 final long ident = Binder.clearCallingIdentity();
2521 try {
2522 setInputMethodLocked(id, subtypeId);
2523 } finally {
2524 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002525 }
2526 }
2527
satok42c5a162011-05-26 16:46:14 +09002528 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002529 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002530 if (!calledFromValidUser()) {
2531 return;
2532 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002533 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002534 if (!calledWithValidToken(token)) {
2535 final int uid = Binder.getCallingUid();
2536 Slog.e(TAG, "Ignoring hideInputMethod due to an invalid token. uid:"
2537 + uid + " token:" + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002538 return;
2539 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002540 long ident = Binder.clearCallingIdentity();
2541 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002542 hideCurrentInputLocked(flags, null);
2543 } finally {
2544 Binder.restoreCallingIdentity(ident);
2545 }
2546 }
2547 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002548
satok42c5a162011-05-26 16:46:14 +09002549 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002550 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002551 if (!calledFromValidUser()) {
2552 return;
2553 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002554 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002555 if (!calledWithValidToken(token)) {
2556 final int uid = Binder.getCallingUid();
2557 Slog.e(TAG, "Ignoring showMySoftInput due to an invalid token. uid:"
2558 + uid + " token:" + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002559 return;
2560 }
2561 long ident = Binder.clearCallingIdentity();
2562 try {
2563 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002564 } finally {
2565 Binder.restoreCallingIdentity(ident);
2566 }
2567 }
2568 }
2569
2570 void setEnabledSessionInMainThread(SessionState session) {
2571 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002572 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002573 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002574 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002575 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002576 } catch (RemoteException e) {
2577 }
2578 }
2579 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002580 if (mEnabledSession != null && mEnabledSession.session != null) {
2581 try {
2582 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
2583 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
2584 } catch (RemoteException e) {
2585 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002586 }
2587 }
2588 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002589
satok42c5a162011-05-26 16:46:14 +09002590 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002591 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002592 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002593 switch (msg.what) {
2594 case MSG_SHOW_IM_PICKER:
2595 showInputMethodMenu();
2596 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002597
satokab751aa2010-09-14 19:17:36 +09002598 case MSG_SHOW_IM_SUBTYPE_PICKER:
2599 showInputMethodSubtypeMenu();
2600 return true;
2601
satok47a44912010-10-06 16:03:58 +09002602 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002603 args = (SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09002604 showInputMethodAndSubtypeEnabler((String)args.arg1);
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002605 args.recycle();
satok217f5482010-12-15 05:19:19 +09002606 return true;
2607
2608 case MSG_SHOW_IM_CONFIG:
2609 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002610 return true;
2611
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002612 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002613
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002614 case MSG_UNBIND_INPUT:
2615 try {
2616 ((IInputMethod)msg.obj).unbindInput();
2617 } catch (RemoteException e) {
2618 // There is nothing interesting about the method dying.
2619 }
2620 return true;
2621 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002622 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002623 try {
2624 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2625 } catch (RemoteException e) {
2626 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002627 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002628 return true;
2629 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002630 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002631 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002632 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002633 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002634 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002635 } catch (RemoteException e) {
2636 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002637 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002638 return true;
2639 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002640 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002641 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002642 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002643 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002644 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002645 } catch (RemoteException e) {
2646 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002647 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002648 return true;
2649 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002650 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002651 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002652 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002653 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2654 } catch (RemoteException e) {
2655 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002656 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002657 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002658 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002659 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002660 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002661 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002662 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002663 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002664 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002665 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002666 // Dispose the channel if the input method is not local to this process
2667 // because the remote proxy will get its own copy when unparceled.
2668 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002669 channel.dispose();
2670 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002671 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002672 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002673 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002674 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002675 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002676
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002677 case MSG_START_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002678 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002679 try {
2680 SessionState session = (SessionState)args.arg1;
2681 setEnabledSessionInMainThread(session);
2682 session.method.startInput((IInputContext)args.arg2,
2683 (EditorInfo)args.arg3);
2684 } catch (RemoteException e) {
2685 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002686 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002687 return true;
2688 case MSG_RESTART_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002689 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002690 try {
2691 SessionState session = (SessionState)args.arg1;
2692 setEnabledSessionInMainThread(session);
2693 session.method.restartInput((IInputContext)args.arg2,
2694 (EditorInfo)args.arg3);
2695 } catch (RemoteException e) {
2696 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002697 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002698 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002699
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002700 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002701
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002702 case MSG_UNBIND_METHOD:
2703 try {
2704 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
2705 } catch (RemoteException e) {
2706 // There is nothing interesting about the last client dying.
2707 }
2708 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002709 case MSG_BIND_METHOD: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002710 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002711 IInputMethodClient client = (IInputMethodClient)args.arg1;
2712 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002713 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002714 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002715 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002716 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002717 } finally {
2718 // Dispose the channel if the input method is not local to this process
2719 // because the remote proxy will get its own copy when unparceled.
2720 if (res.channel != null && Binder.isProxy(client)) {
2721 res.channel.dispose();
2722 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002723 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002724 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002725 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002726 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002727 case MSG_SET_ACTIVE:
2728 try {
2729 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2730 } catch (RemoteException e) {
2731 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2732 + ((ClientState)msg.obj).pid + " uid "
2733 + ((ClientState)msg.obj).uid);
2734 }
2735 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002736 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
2737 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07002738 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002739 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07002740 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002741 } catch (RemoteException e) {
2742 Slog.w(TAG, "Got RemoteException sending "
2743 + "setUserActionNotificationSequenceNumber("
2744 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07002745 + clientState.pid + " uid "
2746 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002747 }
2748 return true;
2749 }
satok01038492012-04-09 21:08:27 +09002750
2751 // --------------------------------------------------------------
2752 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07002753 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09002754 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002755 }
2756 return false;
2757 }
2758
satokdc9ddae2011-10-06 12:22:36 +09002759 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002760 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
2761 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09002762 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002763 if (DEBUG) {
2764 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2765 }
satok723a27e2010-11-11 14:58:11 +09002766 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002767 return true;
2768 }
2769
2770 return false;
2771 }
2772
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002773 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002774 HashMap<String, InputMethodInfo> map, boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002775 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002776 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Satoshi Kataoka0766eb02013-07-31 18:30:13 +09002777 + " \n ------ \n" + InputMethodUtils.getStackTrace());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002778 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002779 list.clear();
2780 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002781
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002782 // Use for queryIntentServicesAsUser
2783 final PackageManager pm = mContext.getPackageManager();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002784 String disabledSysImes = mSettings.getDisabledSystemInputMethods();
Amith Yamasanie861ec12010-03-24 21:39:27 -07002785 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002786
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002787 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002788 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002789 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2790 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002791
satoke7c6998e2011-06-03 17:57:59 +09002792 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2793 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002794 for (int i = 0; i < services.size(); ++i) {
2795 ResolveInfo ri = services.get(i);
2796 ServiceInfo si = ri.serviceInfo;
2797 ComponentName compName = new ComponentName(si.packageName, si.name);
2798 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2799 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002800 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002801 + ": it does not require the permission "
2802 + android.Manifest.permission.BIND_INPUT_METHOD);
2803 continue;
2804 }
2805
Joe Onorato8a9b2202010-02-26 18:56:32 -08002806 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002807
2808 try {
satoke7c6998e2011-06-03 17:57:59 +09002809 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002810 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002811 final String id = p.getId();
2812 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002813
2814 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002815 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002816 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002818 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002819 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002820 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002821 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002822 }
2823 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002824
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002825 if (resetDefaultEnabledIme) {
2826 final ArrayList<InputMethodInfo> defaultEnabledIme =
2827 InputMethodUtils.getDefaultEnabledImes(mContext, mSystemReady, list);
2828 for (int i = 0; i < defaultEnabledIme.size(); ++i) {
2829 final InputMethodInfo imi = defaultEnabledIme.get(i);
2830 if (DEBUG) {
2831 Slog.d(TAG, "--- enable ime = " + imi);
2832 }
2833 setInputMethodEnabledLocked(imi.getId(), true);
2834 }
2835 }
2836
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002837 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09002838 if (!TextUtils.isEmpty(defaultImiId)) {
2839 if (!map.containsKey(defaultImiId)) {
2840 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
2841 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07002842 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09002843 }
2844 } else {
2845 // Double check that the default IME is certainly enabled.
2846 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002847 }
2848 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09002849 // Here is not the perfect place to reset the switching controller. Ideally
2850 // mSwitchingController and mSettings should be able to share the same state.
2851 // TODO: Make sure that mSwitchingController and mSettings are sharing the
2852 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09002853 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002854 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002855
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002856 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002857
satokab751aa2010-09-14 19:17:36 +09002858 private void showInputMethodMenu() {
2859 showInputMethodMenuInternal(false);
2860 }
2861
2862 private void showInputMethodSubtypeMenu() {
2863 showInputMethodMenuInternal(true);
2864 }
2865
satok217f5482010-12-15 05:19:19 +09002866 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002867 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002868 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002869 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2870 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002871 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002872 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002873 }
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002874 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok217f5482010-12-15 05:19:19 +09002875 }
2876
2877 private void showConfigureInputMethods() {
2878 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2879 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2880 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2881 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002882 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09002883 }
2884
satok2c93efc2012-04-02 19:33:47 +09002885 private boolean isScreenLocked() {
2886 return mKeyguardManager != null
2887 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
2888 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002889
satokab751aa2010-09-14 19:17:36 +09002890 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002891 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002892
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002893 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09002894 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002895
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002896 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002897 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002898 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002899
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002900 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002901 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09002902 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
2903 mContext);
satok7f35c8c2010-10-07 21:13:11 +09002904 if (immis == null || immis.size() == 0) {
2905 return;
2906 }
2907
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002908 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002909
satok688bd472012-02-09 20:09:17 +09002910 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002911 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
satok688bd472012-02-09 20:09:17 +09002912 showSubtypes, mInputShown, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09002913
satokc3690562012-01-10 20:14:43 +09002914 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002915 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09002916 if (currentSubtype != null) {
2917 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002918 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
2919 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09002920 }
2921 }
2922
Ken Wakasa761eb372011-03-04 19:06:18 +09002923 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09002924 mIms = new InputMethodInfo[N];
2925 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002926 int checkedItem = 0;
2927 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002928 final ImeSubtypeListItem item = imList.get(i);
2929 mIms[i] = item.mImi;
2930 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002931 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002932 int subtypeId = mSubtypeIds[i];
2933 if ((subtypeId == NOT_A_SUBTYPE_ID)
2934 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2935 || (subtypeId == lastInputMethodSubtypeId)) {
2936 checkedItem = i;
2937 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002938 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002939 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08002940
2941 final Context settingsContext = new ContextThemeWrapper(context,
2942 com.android.internal.R.style.Theme_DeviceDefault_Settings);
2943
2944 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002945 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
2946 @Override
2947 public void onCancel(DialogInterface dialog) {
2948 hideInputMethodMenu();
2949 }
2950 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08002951
2952 final Context dialogContext = mDialogBuilder.getContext();
2953 final TypedArray a = dialogContext.obtainStyledAttributes(null,
2954 com.android.internal.R.styleable.DialogPreference,
2955 com.android.internal.R.attr.alertDialogStyle, 0);
2956 final Drawable dialogIcon = a.getDrawable(
2957 com.android.internal.R.styleable.DialogPreference_dialogIcon);
2958 a.recycle();
2959
2960 mDialogBuilder.setIcon(dialogIcon);
2961
2962 final LayoutInflater inflater = (LayoutInflater) dialogContext.getSystemService(
2963 Context.LAYOUT_INFLATER_SERVICE);
satok01038492012-04-09 21:08:27 +09002964 final View tv = inflater.inflate(
2965 com.android.internal.R.layout.input_method_switch_dialog_title, null);
2966 mDialogBuilder.setCustomTitle(tv);
2967
2968 // Setup layout for a toggle switch of the hardware keyboard
2969 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002970 mSwitchingDialogTitleView
2971 .findViewById(com.android.internal.R.id.hard_keyboard_section)
2972 .setVisibility(mWindowManagerService.isHardKeyboardAvailable()
2973 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08002974 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002975 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07002976 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002977 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
2978 @Override
2979 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07002980 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002981 // Ensure that the input method dialog is dismissed when changing
2982 // the hardware keyboard state.
2983 hideInputMethodMenu();
2984 }
2985 });
2986
Alan Viverette505e3ab2014-11-24 15:22:11 -08002987 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002988 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
2989 final OnClickListener choiceListener = new OnClickListener() {
2990 @Override
2991 public void onClick(final DialogInterface dialog, final int which) {
2992 synchronized (mMethodMap) {
2993 if (mIms == null || mIms.length <= which || mSubtypeIds == null
2994 || mSubtypeIds.length <= which) {
2995 return;
satok01038492012-04-09 21:08:27 +09002996 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002997 final InputMethodInfo im = mIms[which];
2998 int subtypeId = mSubtypeIds[which];
2999 adapter.mCheckedItem = which;
3000 adapter.notifyDataSetChanged();
3001 hideInputMethodMenu();
3002 if (im != null) {
3003 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
3004 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003005 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003006 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003007 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003008 }
3009 }
3010 };
3011 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003012
satokbc81b692011-08-26 16:22:22 +09003013 if (showSubtypes && !isScreenLocked) {
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003014 final OnClickListener positiveListener = new OnClickListener() {
3015 @Override
3016 public void onClick(DialogInterface dialog, int whichButton) {
3017 showConfigureInputMethods();
3018 }
3019 };
satok82beadf2010-12-27 19:03:06 +09003020 mDialogBuilder.setPositiveButton(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003021 com.android.internal.R.string.configure_input_methods, positiveListener);
satok7f35c8c2010-10-07 21:13:11 +09003022 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003023 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003024 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003025 mSwitchingDialog.getWindow().setType(
3026 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09003027 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
3028 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003029 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
Jason Monk807ef762014-05-08 15:47:46 -04003030 updateImeWindowStatusLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003031 mSwitchingDialog.show();
3032 }
3033 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003034
Ken Wakasa05dbb652011-08-22 15:22:43 +09003035 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
3036 private final LayoutInflater mInflater;
3037 private final int mTextViewResourceId;
3038 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09003039 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09003040 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
3041 List<ImeSubtypeListItem> itemsList, int checkedItem) {
3042 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003043
Ken Wakasa05dbb652011-08-22 15:22:43 +09003044 mTextViewResourceId = textViewResourceId;
3045 mItemsList = itemsList;
3046 mCheckedItem = checkedItem;
Alan Viverette505e3ab2014-11-24 15:22:11 -08003047 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Ken Wakasa05dbb652011-08-22 15:22:43 +09003048 }
3049
3050 @Override
3051 public View getView(int position, View convertView, ViewGroup parent) {
3052 final View view = convertView != null ? convertView
3053 : mInflater.inflate(mTextViewResourceId, null);
3054 if (position < 0 || position >= mItemsList.size()) return view;
3055 final ImeSubtypeListItem item = mItemsList.get(position);
3056 final CharSequence imeName = item.mImeName;
3057 final CharSequence subtypeName = item.mSubtypeName;
3058 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
3059 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
3060 if (TextUtils.isEmpty(subtypeName)) {
3061 firstTextView.setText(imeName);
3062 secondTextView.setVisibility(View.GONE);
3063 } else {
3064 firstTextView.setText(subtypeName);
3065 secondTextView.setText(imeName);
3066 secondTextView.setVisibility(View.VISIBLE);
3067 }
3068 final RadioButton radioButton =
3069 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
3070 radioButton.setChecked(position == mCheckedItem);
3071 return view;
3072 }
3073 }
3074
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003075 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003076 synchronized (mMethodMap) {
3077 hideInputMethodMenuLocked();
3078 }
3079 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003080
The Android Open Source Project10592532009-03-18 17:39:46 -07003081 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003082 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003083
The Android Open Source Project10592532009-03-18 17:39:46 -07003084 if (mSwitchingDialog != null) {
3085 mSwitchingDialog.dismiss();
3086 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003087 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003088
Jason Monk807ef762014-05-08 15:47:46 -04003089 updateImeWindowStatusLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -07003090 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003091 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003092 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003094 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003095
satok42c5a162011-05-26 16:46:14 +09003096 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003097 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003098 // TODO: Make this work even for non-current users?
3099 if (!calledFromValidUser()) {
3100 return false;
3101 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003102 synchronized (mMethodMap) {
3103 if (mContext.checkCallingOrSelfPermission(
3104 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3105 != PackageManager.PERMISSION_GRANTED) {
3106 throw new SecurityException(
3107 "Requires permission "
3108 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3109 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003111 long ident = Binder.clearCallingIdentity();
3112 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003113 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003114 } finally {
3115 Binder.restoreCallingIdentity(ident);
3116 }
3117 }
3118 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003119
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003120 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3121 // Make sure this is a valid input method.
3122 InputMethodInfo imm = mMethodMap.get(id);
3123 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003124 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003125 }
3126
satokd87c2592010-09-29 11:52:06 +09003127 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3128 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003129
satokd87c2592010-09-29 11:52:06 +09003130 if (enabled) {
3131 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3132 if (pair.first.equals(id)) {
3133 // We are enabling this input method, but it is already enabled.
3134 // Nothing to do. The previous state was enabled.
3135 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003136 }
3137 }
satokd87c2592010-09-29 11:52:06 +09003138 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3139 // Previous state was disabled.
3140 return false;
3141 } else {
3142 StringBuilder builder = new StringBuilder();
3143 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3144 builder, enabledInputMethodsList, id)) {
3145 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003146 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003147 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3148 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3149 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003150 }
3151 // Previous state was enabled.
3152 return true;
3153 } else {
3154 // We are disabling the input method but it is already disabled.
3155 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003156 return false;
3157 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003158 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003159 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003160
satok723a27e2010-11-11 14:58:11 +09003161 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3162 boolean setSubtypeOnly) {
3163 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003164 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003165
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003166 mCurUserActionNotificationSequenceNumber =
3167 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3168 if (DEBUG) {
3169 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3170 + mCurUserActionNotificationSequenceNumber);
3171 }
3172
3173 if (mCurClient != null && mCurClient.client != null) {
3174 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3175 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003176 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003177 }
3178
satok723a27e2010-11-11 14:58:11 +09003179 // Set Subtype here
3180 if (imi == null || subtypeId < 0) {
3181 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003182 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003183 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003184 if (subtypeId < imi.getSubtypeCount()) {
3185 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3186 mSettings.putSelectedSubtype(subtype.hashCode());
3187 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003188 } else {
3189 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003190 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003191 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003192 }
satokab751aa2010-09-14 19:17:36 +09003193 }
satok723a27e2010-11-11 14:58:11 +09003194
satok4c0e7152012-06-20 20:08:44 +09003195 // Workaround.
3196 // ASEC is not ready in the IMMS constructor. Accordingly, forward-locked
3197 // IMEs are not recognized and considered uninstalled.
3198 // Actually, we can't move everything after SystemReady because
3199 // IMMS needs to run in the encryption lock screen. So, we just skip changing
3200 // the default IME here and try cheking the default IME again in systemReady().
3201 // TODO: Do nothing before system ready and implement a separated logic for
3202 // the encryption lock screen.
3203 // TODO: ASEC should be ready before IMMS is instantiated.
3204 if (mSystemReady && !setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003205 // Set InputMethod here
3206 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3207 }
3208 }
3209
3210 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3211 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3212 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3213 // newDefaultIme is empty when there is no candidate for the selected IME.
3214 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3215 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3216 if (subtypeHashCode != null) {
3217 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003218 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok723a27e2010-11-11 14:58:11 +09003219 imi, Integer.valueOf(subtypeHashCode));
3220 } catch (NumberFormatException e) {
3221 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3222 }
3223 }
3224 }
3225 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003226 }
3227
satok4e4569d2010-11-19 18:45:53 +09003228 // If there are no selected shortcuts, tries finding the most applicable ones.
3229 private Pair<InputMethodInfo, InputMethodSubtype>
3230 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3231 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3232 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003233 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003234 boolean foundInSystemIME = false;
3235
3236 // Search applicable subtype for each InputMethodInfo
3237 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003238 final String imiId = imi.getId();
3239 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3240 continue;
3241 }
satokcd7cd292010-11-20 15:46:23 +09003242 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003243 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003244 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003245 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003246 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003247 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003248 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003249 }
satokdf31ae62011-01-15 06:19:44 +09003250 // 2. Search by the system locale from enabledSubtypes.
3251 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003252 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003253 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003254 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003255 }
satoka86f5e42011-09-02 17:12:42 +09003256 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003257 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003258 final ArrayList<InputMethodSubtype> subtypesForSearch =
3259 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003260 ? InputMethodUtils.getSubtypes(imi)
3261 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003262 // 4. Search by the current subtype's locale from all subtypes.
3263 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003264 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003265 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003266 }
3267 // 5. Search by the system locale from all subtypes.
3268 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003269 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003270 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003271 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003272 }
satokcd7cd292010-11-20 15:46:23 +09003273 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003274 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003275 // The current input method is the most applicable IME.
3276 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003277 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003278 break;
satok7599a7f2010-12-22 13:45:23 +09003279 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003280 // The system input method is 2nd applicable IME.
3281 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003282 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003283 if ((imi.getServiceInfo().applicationInfo.flags
3284 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3285 foundInSystemIME = true;
3286 }
satok4e4569d2010-11-19 18:45:53 +09003287 }
3288 }
3289 }
3290 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003291 if (mostApplicableIMI != null) {
3292 Slog.w(TAG, "Most applicable shortcut input method was:"
3293 + mostApplicableIMI.getId());
3294 if (mostApplicableSubtype != null) {
3295 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3296 + "," + mostApplicableSubtype.getMode() + ","
3297 + mostApplicableSubtype.getLocale());
3298 }
3299 }
satok4e4569d2010-11-19 18:45:53 +09003300 }
satokcd7cd292010-11-20 15:46:23 +09003301 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09003302 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09003303 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003304 } else {
3305 return null;
3306 }
3307 }
3308
satokab751aa2010-09-14 19:17:36 +09003309 /**
3310 * @return Return the current subtype of this input method.
3311 */
satok42c5a162011-05-26 16:46:14 +09003312 @Override
satokab751aa2010-09-14 19:17:36 +09003313 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003314 // TODO: Make this work even for non-current users?
3315 if (!calledFromValidUser()) {
3316 return null;
3317 }
3318 synchronized (mMethodMap) {
3319 return getCurrentInputMethodSubtypeLocked();
3320 }
3321 }
3322
3323 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003324 if (mCurMethodId == null) {
3325 return null;
3326 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003327 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003328 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3329 if (imi == null || imi.getSubtypeCount() == 0) {
3330 return null;
satok4e4569d2010-11-19 18:45:53 +09003331 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003332 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003333 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3334 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003335 if (subtypeId == NOT_A_SUBTYPE_ID) {
3336 // If there are no selected subtypes, the framework will try to find
3337 // the most applicable subtype from explicitly or implicitly enabled
3338 // subtypes.
3339 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003340 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003341 // If there is only one explicitly or implicitly enabled subtype,
3342 // just returns it.
3343 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3344 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3345 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003346 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003347 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003348 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003349 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003350 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003351 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3352 true);
satok4e4569d2010-11-19 18:45:53 +09003353 }
satok3ef8b292010-11-23 06:06:29 +09003354 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003355 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003356 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003357 }
3358 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003359 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003360 }
3361
satokf3db1af2010-11-23 13:34:33 +09003362 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
3363 InputMethodSubtype subtype) {
3364 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
3365 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
3366 } else {
3367 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3368 subtypes.add(subtype);
3369 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
3370 }
3371 }
3372
satok4e4569d2010-11-19 18:45:53 +09003373 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003374 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003375 @Override
satok4e4569d2010-11-19 18:45:53 +09003376 public List getShortcutInputMethodsAndSubtypes() {
3377 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09003378 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09003379 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003380 // If there are no selected shortcut subtypes, the framework will try to find
3381 // the most applicable subtype from all subtypes whose mode is
3382 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003383 Pair<InputMethodInfo, InputMethodSubtype> info =
3384 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003385 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003386 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003387 ret.add(info.first);
3388 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003389 }
satok3da92232011-01-11 22:46:30 +09003390 return ret;
satokf3db1af2010-11-23 13:34:33 +09003391 }
satokf3db1af2010-11-23 13:34:33 +09003392 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3393 ret.add(imi);
3394 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3395 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003396 }
3397 }
satokf3db1af2010-11-23 13:34:33 +09003398 return ret;
satok4e4569d2010-11-19 18:45:53 +09003399 }
3400 }
3401
satok42c5a162011-05-26 16:46:14 +09003402 @Override
satokb66d2872010-11-10 01:04:04 +09003403 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003404 // TODO: Make this work even for non-current users?
3405 if (!calledFromValidUser()) {
3406 return false;
3407 }
satokb66d2872010-11-10 01:04:04 +09003408 synchronized (mMethodMap) {
3409 if (subtype != null && mCurMethodId != null) {
3410 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003411 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003412 if (subtypeId != NOT_A_SUBTYPE_ID) {
3413 setInputMethodLocked(mCurMethodId, subtypeId);
3414 return true;
3415 }
3416 }
3417 return false;
3418 }
3419 }
3420
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003421 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003422 private static class InputMethodFileManager {
3423 private static final String SYSTEM_PATH = "system";
3424 private static final String INPUT_METHOD_PATH = "inputmethod";
3425 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3426 private static final String NODE_SUBTYPES = "subtypes";
3427 private static final String NODE_SUBTYPE = "subtype";
3428 private static final String NODE_IMI = "imi";
3429 private static final String ATTR_ID = "id";
3430 private static final String ATTR_LABEL = "label";
3431 private static final String ATTR_ICON = "icon";
3432 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3433 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3434 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3435 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3436 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3437 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003438 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
satoke7c6998e2011-06-03 17:57:59 +09003439 new HashMap<String, List<InputMethodSubtype>>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003440 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003441 if (methodMap == null) {
3442 throw new NullPointerException("methodMap is null");
3443 }
3444 mMethodMap = methodMap;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003445 final File systemDir = userId == UserHandle.USER_OWNER
3446 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3447 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003448 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3449 if (!inputMethodDir.mkdirs()) {
3450 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3451 }
3452 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3453 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3454 if (!subtypeFile.exists()) {
3455 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003456 writeAdditionalInputMethodSubtypes(
3457 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003458 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003459 readAdditionalInputMethodSubtypes(
3460 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003461 }
3462 }
3463
3464 private void deleteAllInputMethodSubtypes(String imiId) {
3465 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003466 mAdditionalSubtypesMap.remove(imiId);
3467 writeAdditionalInputMethodSubtypes(
3468 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003469 }
3470 }
3471
3472 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003473 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003474 synchronized (mMethodMap) {
3475 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3476 final int N = additionalSubtypes.length;
3477 for (int i = 0; i < N; ++i) {
3478 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003479 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003480 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003481 } else {
3482 Slog.w(TAG, "Duplicated subtype definition found: "
3483 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003484 }
3485 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003486 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3487 writeAdditionalInputMethodSubtypes(
3488 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003489 }
3490 }
3491
3492 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3493 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003494 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003495 }
3496 }
3497
3498 private static void writeAdditionalInputMethodSubtypes(
3499 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3500 HashMap<String, InputMethodInfo> methodMap) {
3501 // Safety net for the case that this function is called before methodMap is set.
3502 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3503 FileOutputStream fos = null;
3504 try {
3505 fos = subtypesFile.startWrite();
3506 final XmlSerializer out = new FastXmlSerializer();
3507 out.setOutput(fos, "utf-8");
3508 out.startDocument(null, true);
3509 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3510 out.startTag(null, NODE_SUBTYPES);
3511 for (String imiId : allSubtypes.keySet()) {
3512 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3513 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3514 continue;
3515 }
3516 out.startTag(null, NODE_IMI);
3517 out.attribute(null, ATTR_ID, imiId);
3518 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3519 final int N = subtypesList.size();
3520 for (int i = 0; i < N; ++i) {
3521 final InputMethodSubtype subtype = subtypesList.get(i);
3522 out.startTag(null, NODE_SUBTYPE);
3523 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3524 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3525 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3526 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3527 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3528 out.attribute(null, ATTR_IS_AUXILIARY,
3529 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3530 out.endTag(null, NODE_SUBTYPE);
3531 }
3532 out.endTag(null, NODE_IMI);
3533 }
3534 out.endTag(null, NODE_SUBTYPES);
3535 out.endDocument();
3536 subtypesFile.finishWrite(fos);
3537 } catch (java.io.IOException e) {
3538 Slog.w(TAG, "Error writing subtypes", e);
3539 if (fos != null) {
3540 subtypesFile.failWrite(fos);
3541 }
3542 }
3543 }
3544
3545 private static void readAdditionalInputMethodSubtypes(
3546 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3547 if (allSubtypes == null || subtypesFile == null) return;
3548 allSubtypes.clear();
3549 FileInputStream fis = null;
3550 try {
3551 fis = subtypesFile.openRead();
3552 final XmlPullParser parser = Xml.newPullParser();
3553 parser.setInput(fis, null);
3554 int type = parser.getEventType();
3555 // Skip parsing until START_TAG
3556 while ((type = parser.next()) != XmlPullParser.START_TAG
3557 && type != XmlPullParser.END_DOCUMENT) {}
3558 String firstNodeName = parser.getName();
3559 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3560 throw new XmlPullParserException("Xml doesn't start with subtypes");
3561 }
3562 final int depth =parser.getDepth();
3563 String currentImiId = null;
3564 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3565 while (((type = parser.next()) != XmlPullParser.END_TAG
3566 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3567 if (type != XmlPullParser.START_TAG)
3568 continue;
3569 final String nodeName = parser.getName();
3570 if (NODE_IMI.equals(nodeName)) {
3571 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3572 if (TextUtils.isEmpty(currentImiId)) {
3573 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3574 continue;
3575 }
3576 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
3577 allSubtypes.put(currentImiId, tempSubtypesArray);
3578 } else if (NODE_SUBTYPE.equals(nodeName)) {
3579 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3580 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3581 continue;
3582 }
3583 final int icon = Integer.valueOf(
3584 parser.getAttributeValue(null, ATTR_ICON));
3585 final int label = Integer.valueOf(
3586 parser.getAttributeValue(null, ATTR_LABEL));
3587 final String imeSubtypeLocale =
3588 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3589 final String imeSubtypeMode =
3590 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3591 final String imeSubtypeExtraValue =
3592 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003593 final boolean isAuxiliary = "1".equals(String.valueOf(
3594 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003595 final InputMethodSubtype subtype = new InputMethodSubtypeBuilder()
3596 .setSubtypeNameResId(label)
3597 .setSubtypeIconResId(icon)
3598 .setSubtypeLocale(imeSubtypeLocale)
3599 .setSubtypeMode(imeSubtypeMode)
3600 .setSubtypeExtraValue(imeSubtypeExtraValue)
3601 .setIsAuxiliary(isAuxiliary)
3602 .build();
satoke7c6998e2011-06-03 17:57:59 +09003603 tempSubtypesArray.add(subtype);
3604 }
3605 }
3606 } catch (XmlPullParserException e) {
3607 Slog.w(TAG, "Error reading subtypes: " + e);
3608 return;
3609 } catch (java.io.IOException e) {
3610 Slog.w(TAG, "Error reading subtypes: " + e);
3611 return;
3612 } catch (NumberFormatException e) {
3613 Slog.w(TAG, "Error reading subtypes: " + e);
3614 return;
3615 } finally {
3616 if (fis != null) {
3617 try {
3618 fis.close();
3619 } catch (java.io.IOException e1) {
3620 Slog.w(TAG, "Failed to close.");
3621 }
3622 }
3623 }
3624 }
3625 }
3626
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003627 @Override
3628 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3629 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3630 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003631
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003632 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3633 + Binder.getCallingPid()
3634 + ", uid=" + Binder.getCallingUid());
3635 return;
3636 }
3637
3638 IInputMethod method;
3639 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003640
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003641 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003642
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003643 synchronized (mMethodMap) {
3644 p.println("Current Input Method Manager state:");
3645 int N = mMethodList.size();
3646 p.println(" Input Methods:");
3647 for (int i=0; i<N; i++) {
3648 InputMethodInfo info = mMethodList.get(i);
3649 p.println(" InputMethod #" + i + ":");
3650 info.dump(p, " ");
3651 }
3652 p.println(" Clients:");
3653 for (ClientState ci : mClients.values()) {
3654 p.println(" Client " + ci + ":");
3655 p.println(" client=" + ci.client);
3656 p.println(" inputContext=" + ci.inputContext);
3657 p.println(" sessionRequested=" + ci.sessionRequested);
3658 p.println(" curSession=" + ci.curSession);
3659 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003660 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003661 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003662 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3663 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003664 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3665 + " mBoundToMethod=" + mBoundToMethod);
3666 p.println(" mCurToken=" + mCurToken);
3667 p.println(" mCurIntent=" + mCurIntent);
3668 method = mCurMethod;
3669 p.println(" mCurMethod=" + mCurMethod);
3670 p.println(" mEnabledSession=" + mEnabledSession);
3671 p.println(" mShowRequested=" + mShowRequested
3672 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3673 + " mShowForced=" + mShowForced
3674 + " mInputShown=" + mInputShown);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003675 p.println(" mCurUserActionNotificationSequenceNumber="
3676 + mCurUserActionNotificationSequenceNumber);
Dianne Hackbornd4e6d462014-05-16 16:32:37 -07003677 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003678 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003679
Jeff Brownb88102f2010-09-08 11:49:43 -07003680 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003681 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003682 pw.flush();
3683 try {
3684 client.client.asBinder().dump(fd, args);
3685 } catch (RemoteException e) {
3686 p.println("Input method client dead: " + e);
3687 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003688 } else {
3689 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003690 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003691
Jeff Brownb88102f2010-09-08 11:49:43 -07003692 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003693 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003694 pw.flush();
3695 try {
3696 method.asBinder().dump(fd, args);
3697 } catch (RemoteException e) {
3698 p.println("Input method service dead: " + e);
3699 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003700 } else {
3701 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003702 }
3703 }
3704}