blob: 4d0868a4ed019e7d40319d0304547d1fa05c879f [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
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080018import com.android.internal.content.PackageMonitor;
Satoshi Kataokad7443c82013-10-15 17:45:43 +090019import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController;
Satoshi Kataokad787f692013-10-26 04:44:21 +090020import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController.ImeSubtypeListItem;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +090021import com.android.internal.inputmethod.InputMethodUtils;
22import com.android.internal.inputmethod.InputMethodUtils.InputMethodSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import com.android.internal.os.HandlerCaller;
Svetoslav Ganov758143e2012-08-06 16:40:27 -070024import com.android.internal.os.SomeArgs;
satoke7c6998e2011-06-03 17:57:59 +090025import com.android.internal.util.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import com.android.internal.view.IInputContext;
27import com.android.internal.view.IInputMethod;
Michael Wright52a53522013-03-14 10:59:38 -070028import com.android.internal.view.IInputSessionCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import com.android.internal.view.IInputMethodClient;
30import com.android.internal.view.IInputMethodManager;
31import com.android.internal.view.IInputMethodSession;
32import com.android.internal.view.InputBindResult;
Adam Lesinskief2ea1f2013-12-05 16:48:06 -080033import com.android.server.statusbar.StatusBarManagerService;
satok01038492012-04-09 21:08:27 +090034import com.android.server.wm.WindowManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035
satoke7c6998e2011-06-03 17:57:59 +090036import org.xmlpull.v1.XmlPullParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import org.xmlpull.v1.XmlPullParserException;
satoke7c6998e2011-06-03 17:57:59 +090038import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039
40import android.app.ActivityManagerNative;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090041import android.app.AppGlobals;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.app.AlertDialog;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +090043import android.app.AppOpsManager;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090044import android.app.IUserSwitchObserver;
satokf90a33e2011-07-19 11:55:52 +090045import android.app.KeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +090046import android.app.Notification;
47import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070048import android.app.PendingIntent;
satok5b927c432012-05-01 20:09:34 +090049import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.content.ComponentName;
51import android.content.ContentResolver;
52import android.content.Context;
53import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.content.DialogInterface.OnCancelListener;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +090055import android.content.DialogInterface.OnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090057import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070059import android.content.pm.ApplicationInfo;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090060import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.content.pm.PackageManager;
62import android.content.pm.ResolveInfo;
63import android.content.pm.ServiceInfo;
Amith Yamasani734983f2014-03-04 16:48:05 -080064import android.content.pm.UserInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070065import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066import android.content.res.Resources;
67import android.content.res.TypedArray;
68import android.database.ContentObserver;
Alan Viverette505e3ab2014-11-24 15:22:11 -080069import android.graphics.drawable.Drawable;
Joe Onorato857fd9b2011-01-27 15:08:35 -080070import android.inputmethodservice.InputMethodService;
Michael Wright7b5a96b2014-08-09 19:28:42 -070071import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072import android.os.Binder;
satoke7c6998e2011-06-03 17:57:59 +090073import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074import android.os.Handler;
75import android.os.IBinder;
76import android.os.IInterface;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090077import android.os.IRemoteCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078import android.os.Message;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090079import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080import android.os.Parcel;
81import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080082import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083import android.os.ServiceManager;
84import android.os.SystemClock;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090085import android.os.UserHandle;
Amith Yamasani734983f2014-03-04 16:48:05 -080086import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087import android.provider.Settings;
88import android.text.TextUtils;
satokf9f01002011-05-19 21:31:50 +090089import android.text.style.SuggestionSpan;
Dianne Hackborn39606a02012-07-31 17:54:35 -070090import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +090092import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +090093import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094import android.util.PrintWriterPrinter;
95import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +090096import android.util.Slog;
97import android.util.Xml;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +090098import android.view.ContextThemeWrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099import android.view.IWindowManager;
Jeff Brownc28867a2013-03-26 15:42:39 -0700100import android.view.InputChannel;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900101import android.view.LayoutInflater;
102import android.view.View;
103import android.view.ViewGroup;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104import android.view.WindowManager;
satokab751aa2010-09-14 19:17:36 +0900105import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106import android.view.inputmethod.InputBinding;
107import android.view.inputmethod.InputMethod;
108import android.view.inputmethod.InputMethodInfo;
109import android.view.inputmethod.InputMethodManager;
satokab751aa2010-09-14 19:17:36 +0900110import android.view.inputmethod.InputMethodSubtype;
Yohei Yukawa443c2ba2014-09-10 14:10:30 +0900111import android.view.inputmethod.InputMethodSubtype.InputMethodSubtypeBuilder;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900112import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +0900113import android.widget.CompoundButton;
114import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900115import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +0900116import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900117import android.widget.TextView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118
satoke7c6998e2011-06-03 17:57:59 +0900119import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900121import java.io.FileInputStream;
122import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123import java.io.IOException;
124import java.io.PrintWriter;
125import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900126import java.util.Collections;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127import java.util.HashMap;
128import java.util.List;
satok5b927c432012-05-01 20:09:34 +0900129import java.util.Locale;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130
131/**
132 * This class provides a system service that manages input methods.
133 */
134public class InputMethodManagerService extends IInputMethodManager.Stub
135 implements ServiceConnection, Handler.Callback {
136 static final boolean DEBUG = false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700137 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138
139 static final int MSG_SHOW_IM_PICKER = 1;
satokab751aa2010-09-14 19:17:36 +0900140 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 2;
satok47a44912010-10-06 16:03:58 +0900141 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 3;
satok217f5482010-12-15 05:19:19 +0900142 static final int MSG_SHOW_IM_CONFIG = 4;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 static final int MSG_UNBIND_INPUT = 1000;
145 static final int MSG_BIND_INPUT = 1010;
146 static final int MSG_SHOW_SOFT_INPUT = 1020;
147 static final int MSG_HIDE_SOFT_INPUT = 1030;
148 static final int MSG_ATTACH_TOKEN = 1040;
149 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 static final int MSG_START_INPUT = 2000;
152 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 static final int MSG_UNBIND_METHOD = 3000;
155 static final int MSG_BIND_METHOD = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700156 static final int MSG_SET_ACTIVE = 3020;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900157 static final int MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER = 3040;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800158
satok01038492012-04-09 21:08:27 +0900159 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
160
Satoshi Kataokabcacc322013-10-21 17:57:27 -0700161 static final long TIME_TO_RECONNECT = 3 * 1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800162
satokf9f01002011-05-19 21:31:50 +0900163 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
164
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900165 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
satokb6359412011-06-28 17:47:41 +0900166 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900167
satok4e4569d2010-11-19 18:45:53 +0900168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800170 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900172 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 final IWindowManager mIWindowManager;
175 final HandlerCaller mCaller;
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700176 final boolean mHasFeature;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900177 private InputMethodFileManager mFileManager;
satok01038492012-04-09 21:08:27 +0900178 private final HardKeyboardListener mHardKeyboardListener;
179 private final WindowManagerService mWindowManagerService;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900180 private final AppOpsManager mAppOpsManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800181
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900182 final InputBindResult mNoBinding = new InputBindResult(null, null, null, -1, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 // All known input methods. mMethodMap also serves as the global
185 // lock for this class.
satokd87c2592010-09-29 11:52:06 +0900186 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<InputMethodInfo>();
187 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<String, InputMethodInfo>();
satokf9f01002011-05-19 21:31:50 +0900188 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
189 new LruCache<SuggestionSpan, InputMethodInfo>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Satoshi Kataokad787f692013-10-26 04:44:21 +0900190 private final InputMethodSubtypeSwitchingController mSwitchingController;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800191
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700192 // Used to bring IME service up to visible adjustment while it is being shown.
193 final ServiceConnection mVisibleConnection = new ServiceConnection() {
194 @Override public void onServiceConnected(ComponentName name, IBinder service) {
195 }
196
197 @Override public void onServiceDisconnected(ComponentName name) {
198 }
199 };
200 boolean mVisibleBound = false;
201
satok7cfc0ed2011-06-20 21:29:36 +0900202 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700203 private NotificationManager mNotificationManager;
204 private KeyguardManager mKeyguardManager;
205 private StatusBarManagerService mStatusBar;
206 private Notification mImeSwitcherNotification;
207 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900208 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900209 private boolean mNotificationShown;
satok0a1bcf42012-05-16 19:26:31 +0900210 private final boolean mImeSelectedOnBoot;
satok7cfc0ed2011-06-20 21:29:36 +0900211
Tadashi G. Takaoka8c6d4772014-08-05 15:29:17 +0900212 static class SessionState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 final ClientState client;
214 final IInputMethod method;
Jeff Brownc28867a2013-03-26 15:42:39 -0700215
216 IInputMethodSession session;
217 InputChannel channel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 @Override
220 public String toString() {
221 return "SessionState{uid " + client.uid + " pid " + client.pid
222 + " method " + Integer.toHexString(
223 System.identityHashCode(method))
224 + " session " + Integer.toHexString(
225 System.identityHashCode(session))
Jeff Brownc28867a2013-03-26 15:42:39 -0700226 + " channel " + channel
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 + "}";
228 }
229
230 SessionState(ClientState _client, IInputMethod _method,
Jeff Brownc28867a2013-03-26 15:42:39 -0700231 IInputMethodSession _session, InputChannel _channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 client = _client;
233 method = _method;
234 session = _session;
Jeff Brownc28867a2013-03-26 15:42:39 -0700235 channel = _channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 }
237 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800238
Jeff Brownc28867a2013-03-26 15:42:39 -0700239 static final class ClientState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 final IInputMethodClient client;
241 final IInputContext inputContext;
242 final int uid;
243 final int pid;
244 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800245
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 boolean sessionRequested;
247 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800248
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 @Override
250 public String toString() {
251 return "ClientState{" + Integer.toHexString(
252 System.identityHashCode(this)) + " uid " + uid
253 + " pid " + pid + "}";
254 }
255
256 ClientState(IInputMethodClient _client, IInputContext _inputContext,
257 int _uid, int _pid) {
258 client = _client;
259 inputContext = _inputContext;
260 uid = _uid;
261 pid = _pid;
262 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
263 }
264 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800265
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 final HashMap<IBinder, ClientState> mClients
267 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800268
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700270 * Set once the system is ready to run third party code.
271 */
272 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800273
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700274 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 * Id of the currently selected input method.
276 */
277 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 /**
280 * The current binding sequence number, incremented every time there is
281 * a new bind performed.
282 */
283 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 /**
286 * The client that is currently bound to an input method.
287 */
288 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700291 * The last window token that gained focus.
292 */
293 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800294
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700295 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296 * The input context last provided by the current client.
297 */
298 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800299
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800300 /**
301 * The attributes last provided by the current client.
302 */
303 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800304
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 /**
306 * The input method ID of the input method service that we are currently
307 * connected to or in the process of connecting to.
308 */
309 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 /**
satokab751aa2010-09-14 19:17:36 +0900312 * The current subtype of the current input method.
313 */
314 private InputMethodSubtype mCurrentSubtype;
315
satok4e4569d2010-11-19 18:45:53 +0900316 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900317 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
318 mShortcutInputMethodsAndSubtypes =
319 new HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>();
satokab751aa2010-09-14 19:17:36 +0900320
John Spurlocke0980502013-10-25 11:59:29 -0400321 // Was the keyguard locked when this client became current?
322 private boolean mCurClientInKeyguard;
323
satokab751aa2010-09-14 19:17:36 +0900324 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 * Set to true if our ServiceConnection is currently actively bound to
326 * a service (whether or not we have gotten its IBinder back yet).
327 */
328 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800329
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 /**
331 * Set if the client has asked for the input method to be shown.
332 */
333 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800334
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 /**
336 * Set if we were explicitly told to show the input method.
337 */
338 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 /**
341 * Set if we were forced to be shown.
342 */
343 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 /**
346 * Set if we last told the input method to show itself.
347 */
348 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 /**
351 * The Intent used to connect to the current input method.
352 */
353 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 /**
356 * The token we have made for the currently active input method, to
357 * identify it in the future.
358 */
359 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 /**
362 * If non-null, this is the input method service we are currently connected
363 * to.
364 */
365 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800366
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367 /**
368 * Time that we last initiated a bind to the input method, to determine
369 * if we should try to disconnect and reconnect to it.
370 */
371 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 /**
374 * Have we called mCurMethod.bindInput()?
375 */
376 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800377
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378 /**
379 * Currently enabled session. Only touched by service thread, not
380 * protected by a lock.
381 */
382 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800383
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 /**
385 * True if the screen is on. The value is true initially.
386 */
387 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800388
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900389 int mCurUserActionNotificationSequenceNumber = 0;
390
Joe Onorato857fd9b2011-01-27 15:08:35 -0800391 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
392 int mImeWindowVis;
393
Ken Wakasa05dbb652011-08-22 15:22:43 +0900394 private AlertDialog.Builder mDialogBuilder;
395 private AlertDialog mSwitchingDialog;
satok01038492012-04-09 21:08:27 +0900396 private View mSwitchingDialogTitleView;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900397 private InputMethodInfo[] mIms;
398 private int[] mSubtypeIds;
satok5b927c432012-05-01 20:09:34 +0900399 private Locale mLastSystemLocale;
Michael Wright7b5a96b2014-08-09 19:28:42 -0700400 private boolean mShowImeWithHardKeyboard;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900401 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
402 private final IPackageManager mIPackageManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800403
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 class SettingsObserver extends ContentObserver {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800405 String mLastEnabled = "";
406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407 SettingsObserver(Handler handler) {
408 super(handler);
409 ContentResolver resolver = mContext.getContentResolver();
410 resolver.registerContentObserver(Settings.Secure.getUriFor(
411 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
satokab751aa2010-09-14 19:17:36 +0900412 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokb6109bb2011-02-03 22:24:54 +0900413 Settings.Secure.ENABLED_INPUT_METHODS), false, this);
414 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokab751aa2010-09-14 19:17:36 +0900415 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
Michael Wright7b5a96b2014-08-09 19:28:42 -0700416 resolver.registerContentObserver(Settings.Secure.getUriFor(
417 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800419
Michael Wright7b5a96b2014-08-09 19:28:42 -0700420 @Override public void onChange(boolean selfChange, Uri uri) {
421 final Uri showImeUri =
422 Settings.Secure.getUriFor(Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423 synchronized (mMethodMap) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700424 if (showImeUri.equals(uri)) {
425 updateKeyboardFromSettingsLocked();
426 } else {
427 boolean enabledChanged = false;
428 String newEnabled = mSettings.getEnabledInputMethodsStr();
429 if (!mLastEnabled.equals(newEnabled)) {
430 mLastEnabled = newEnabled;
431 enabledChanged = true;
432 }
433 updateInputMethodsFromSettingsLocked(enabledChanged);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800434 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 }
436 }
437 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800438
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900439 class ImmsBroadcastReceiver extends android.content.BroadcastReceiver {
440 private void updateActive() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441 // Inform the current client of the change in active status
Dianne Hackborna6e41342012-05-22 16:30:34 -0700442 if (mCurClient != null && mCurClient.client != null) {
443 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
444 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 }
446 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900447
448 @Override
449 public void onReceive(Context context, Intent intent) {
450 final String action = intent.getAction();
451 if (Intent.ACTION_SCREEN_ON.equals(action)) {
452 mScreenOn = true;
453 refreshImeWindowVisibilityLocked();
454 updateActive();
455 return;
456 } else if (Intent.ACTION_SCREEN_OFF.equals(action)) {
457 mScreenOn = false;
458 setImeWindowVisibilityStatusHiddenLocked();
459 updateActive();
460 return;
461 } else if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
462 hideInputMethodMenu();
463 // No need to updateActive
464 return;
Amith Yamasani734983f2014-03-04 16:48:05 -0800465 } else if (Intent.ACTION_USER_ADDED.equals(action)
466 || Intent.ACTION_USER_REMOVED.equals(action)) {
Kenny Guy2a764942014-04-02 13:29:20 +0100467 updateCurrentProfileIds();
Amith Yamasani734983f2014-03-04 16:48:05 -0800468 return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900469 } else {
470 Slog.w(TAG, "Unexpected intent " + intent);
471 }
472 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800474
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800475 class MyPackageMonitor extends PackageMonitor {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900476 private boolean isChangingPackagesOfCurrentUser() {
477 final int userId = getChangingUserId();
478 final boolean retval = userId == mSettings.getCurrentUserId();
479 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900480 if (!retval) {
481 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
482 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900483 }
484 return retval;
485 }
486
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800488 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900489 if (!isChangingPackagesOfCurrentUser()) {
490 return false;
491 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900493 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 final int N = mMethodList.size();
495 if (curInputMethodId != null) {
496 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800497 InputMethodInfo imi = mMethodList.get(i);
498 if (imi.getId().equals(curInputMethodId)) {
499 for (String pkg : packages) {
500 if (imi.getPackageName().equals(pkg)) {
501 if (!doit) {
502 return true;
503 }
satok723a27e2010-11-11 14:58:11 +0900504 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800505 chooseNewDefaultIMELocked();
506 return true;
507 }
508 }
509 }
510 }
511 }
512 }
513 return false;
514 }
515
516 @Override
517 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900518 if (!isChangingPackagesOfCurrentUser()) {
519 return;
520 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800521 synchronized (mMethodMap) {
522 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900523 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800524 final int N = mMethodList.size();
525 if (curInputMethodId != null) {
526 for (int i=0; i<N; i++) {
527 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900528 final String imiId = imi.getId();
529 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800530 curIm = imi;
531 }
satoke7c6998e2011-06-03 17:57:59 +0900532
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800533 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900534 if (isPackageModified(imi.getPackageName())) {
535 mFileManager.deleteAllInputMethodSubtypes(imiId);
536 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800537 if (change == PACKAGE_TEMPORARY_CHANGE
538 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800539 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800540 + imi.getComponent());
541 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800542 }
543 }
544 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800545
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900546 buildInputMethodListLocked(
547 mMethodList, mMethodMap, false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800548
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800550
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800551 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800552 int change = isPackageDisappearing(curIm.getPackageName());
553 if (change == PACKAGE_TEMPORARY_CHANGE
554 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800555 ServiceInfo si = null;
556 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900557 si = mIPackageManager.getServiceInfo(
558 curIm.getComponent(), 0, mSettings.getCurrentUserId());
559 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800560 }
561 if (si == null) {
562 // Uh oh, current input method is no longer around!
563 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800564 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
satok15452a42011-10-28 17:58:28 +0900565 setImeWindowVisibilityStatusHiddenLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800566 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800567 changed = true;
568 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800569 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900570 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800571 }
572 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800573 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800574 }
satokab751aa2010-09-14 19:17:36 +0900575
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800576 if (curIm == null) {
577 // We currently don't have a default input method... is
578 // one now available?
579 changed = chooseNewDefaultIMELocked();
580 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800581
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800582 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800583 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 }
585 }
586 }
587 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800588
Jeff Brownc28867a2013-03-26 15:42:39 -0700589 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900590 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -0700591 private final IInputMethod mMethod;
592 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800593
Jeff Brownc28867a2013-03-26 15:42:39 -0700594 MethodCallback(InputMethodManagerService imms, IInputMethod method,
595 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900596 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -0700597 mMethod = method;
598 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800600
satoke7c6998e2011-06-03 17:57:59 +0900601 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -0700602 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -0700603 long ident = Binder.clearCallingIdentity();
604 try {
605 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
606 } finally {
607 Binder.restoreCallingIdentity(ident);
608 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609 }
610 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800611
satok01038492012-04-09 21:08:27 +0900612 private class HardKeyboardListener
613 implements WindowManagerService.OnHardKeyboardStatusChangeListener {
614 @Override
Michael Wright7b5a96b2014-08-09 19:28:42 -0700615 public void onHardKeyboardStatusChange(boolean available) {
616 mHandler.sendMessage(mHandler.obtainMessage(MSG_HARD_KEYBOARD_SWITCH_CHANGED,
617 available ? 1 : 0));
satok01038492012-04-09 21:08:27 +0900618 }
619
Michael Wright7b5a96b2014-08-09 19:28:42 -0700620 public void handleHardKeyboardStatusChange(boolean available) {
satok01038492012-04-09 21:08:27 +0900621 if (DEBUG) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700622 Slog.w(TAG, "HardKeyboardStatusChanged: available=" + available);
satok01038492012-04-09 21:08:27 +0900623 }
624 synchronized(mMethodMap) {
625 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
626 && mSwitchingDialog.isShowing()) {
627 mSwitchingDialogTitleView.findViewById(
628 com.android.internal.R.id.hard_keyboard_section).setVisibility(
629 available ? View.VISIBLE : View.GONE);
630 }
631 }
632 }
633 }
634
635 public InputMethodManagerService(Context context, WindowManagerService windowManager) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900636 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800637 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800638 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639 mHandler = new Handler(this);
640 mIWindowManager = IWindowManager.Stub.asInterface(
641 ServiceManager.getService(Context.WINDOW_SERVICE));
Mita Yuned218c72012-12-06 17:18:25 -0800642 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900643 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644 public void executeMessage(Message msg) {
645 handleMessage(msg);
646 }
Mita Yuned218c72012-12-06 17:18:25 -0800647 }, true /*asyncHandler*/);
satok01038492012-04-09 21:08:27 +0900648 mWindowManagerService = windowManager;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900649 mAppOpsManager = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE);
satok01038492012-04-09 21:08:27 +0900650 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700651 mHasFeature = context.getPackageManager().hasSystemFeature(
652 PackageManager.FEATURE_INPUT_METHODS);
satok7cfc0ed2011-06-20 21:29:36 +0900653
satok7cfc0ed2011-06-20 21:29:36 +0900654 mImeSwitcherNotification = new Notification();
655 mImeSwitcherNotification.icon = com.android.internal.R.drawable.ic_notification_ime_default;
656 mImeSwitcherNotification.when = 0;
657 mImeSwitcherNotification.flags = Notification.FLAG_ONGOING_EVENT;
658 mImeSwitcherNotification.tickerText = null;
659 mImeSwitcherNotification.defaults = 0; // please be quiet
660 mImeSwitcherNotification.sound = null;
661 mImeSwitcherNotification.vibrate = null;
Daniel Sandler590d5152012-06-14 16:10:13 -0400662
663 // Tag this notification specially so SystemUI knows it's important
John Spurlockfd7f1e02014-03-18 16:41:57 -0400664 mImeSwitcherNotification.extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
665 mImeSwitcherNotification.category = Notification.CATEGORY_SYSTEM;
Daniel Sandler590d5152012-06-14 16:10:13 -0400666
satok7cfc0ed2011-06-20 21:29:36 +0900667 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900668 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900669
670 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900671
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900672 final IntentFilter broadcastFilter = new IntentFilter();
673 broadcastFilter.addAction(Intent.ACTION_SCREEN_ON);
674 broadcastFilter.addAction(Intent.ACTION_SCREEN_OFF);
675 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Amith Yamasani734983f2014-03-04 16:48:05 -0800676 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
677 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900678 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800679
satok7cfc0ed2011-06-20 21:29:36 +0900680 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900681 int userId = 0;
682 try {
683 ActivityManagerNative.getDefault().registerUserSwitchObserver(
684 new IUserSwitchObserver.Stub() {
685 @Override
686 public void onUserSwitching(int newUserId, IRemoteCallback reply) {
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900687 synchronized(mMethodMap) {
688 switchUserLocked(newUserId);
689 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900690 if (reply != null) {
691 try {
692 reply.sendResult(null);
693 } catch (RemoteException e) {
694 }
695 }
696 }
697
698 @Override
699 public void onUserSwitchComplete(int newUserId) throws RemoteException {
700 }
701 });
702 userId = ActivityManagerNative.getDefault().getCurrentUser().id;
703 } catch (RemoteException e) {
704 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
705 }
satok81f8b7c2012-12-04 20:42:56 +0900706 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
satok913a8922010-08-26 21:53:41 +0900707
satokd87c2592010-09-29 11:52:06 +0900708 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900709 mSettings = new InputMethodSettings(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900710 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId);
Kenny Guy2a764942014-04-02 13:29:20 +0100711 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900712 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900713 synchronized (mMethodMap) {
714 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
715 mSettings, context);
716 }
satok0a1bcf42012-05-16 19:26:31 +0900717
718 // Just checking if defaultImiId is empty or not
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900719 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900720 if (DEBUG) {
721 Slog.d(TAG, "Initial default ime = " + defaultImiId);
722 }
satok0a1bcf42012-05-16 19:26:31 +0900723 mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
724
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900725 synchronized (mMethodMap) {
726 buildInputMethodListLocked(mMethodList, mMethodMap,
727 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
728 }
satokd87c2592010-09-29 11:52:06 +0900729 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800730
satok0a1bcf42012-05-16 19:26:31 +0900731 if (!mImeSelectedOnBoot) {
732 Slog.w(TAG, "No IME selected. Choose the most applicable IME.");
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900733 synchronized (mMethodMap) {
734 resetDefaultImeLocked(context);
735 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800736 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800737
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800738 mSettingsObserver = new SettingsObserver(mHandler);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900739 synchronized (mMethodMap) {
740 updateFromSettingsLocked(true);
741 }
satok5b927c432012-05-01 20:09:34 +0900742
743 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME
744 // according to the new system locale.
745 final IntentFilter filter = new IntentFilter();
746 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
747 mContext.registerReceiver(
748 new BroadcastReceiver() {
749 @Override
750 public void onReceive(Context context, Intent intent) {
751 synchronized(mMethodMap) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900752 resetStateIfCurrentLocaleChangedLocked();
satok5b927c432012-05-01 20:09:34 +0900753 }
754 }
755 }, filter);
756 }
757
satok5b927c432012-05-01 20:09:34 +0900758 private void resetDefaultImeLocked(Context context) {
759 // Do not reset the default (current) IME when it is a 3rd-party IME
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900760 if (mCurMethodId != null
761 && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +0900762 return;
763 }
764
765 InputMethodInfo defIm = null;
766 for (InputMethodInfo imi : mMethodList) {
767 if (defIm == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900768 if (InputMethodUtils.isValidSystemDefaultIme(
769 mSystemReady, imi, context)) {
satok5b927c432012-05-01 20:09:34 +0900770 defIm = imi;
771 Slog.i(TAG, "Selected default: " + imi.getId());
772 }
773 }
774 }
775 if (defIm == null && mMethodList.size() > 0) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900776 defIm = InputMethodUtils.getMostApplicableDefaultIME(
777 mSettings.getEnabledInputMethodListLocked());
Hyejin Kim5baaaac2014-10-27 17:17:06 +0900778 if (defIm != null) {
779 Slog.i(TAG, "Default found, using " + defIm.getId());
780 } else {
781 Slog.i(TAG, "No default found");
782 }
satok5b927c432012-05-01 20:09:34 +0900783 }
784 if (defIm != null) {
785 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
786 }
787 }
788
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900789 private void resetAllInternalStateLocked(final boolean updateOnlyWhenLocaleChanged,
790 final boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900791 if (!mSystemReady) {
792 // not system ready
793 return;
794 }
795 final Locale newLocale = mRes.getConfiguration().locale;
796 if (!updateOnlyWhenLocaleChanged
797 || (newLocale != null && !newLocale.equals(mLastSystemLocale))) {
798 if (!updateOnlyWhenLocaleChanged) {
799 hideCurrentInputLocked(0, null);
800 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -0700801 unbindCurrentMethodLocked(true, false);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900802 }
803 if (DEBUG) {
804 Slog.i(TAG, "Locale has been changed to " + newLocale);
805 }
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900806 buildInputMethodListLocked(mMethodList, mMethodMap, resetDefaultEnabledIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900807 if (!updateOnlyWhenLocaleChanged) {
808 final String selectedImiId = mSettings.getSelectedInputMethod();
809 if (TextUtils.isEmpty(selectedImiId)) {
810 // This is the first time of the user switch and
811 // set the current ime to the proper one.
812 resetDefaultImeLocked(mContext);
813 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +0900814 } else {
815 // If the locale is changed, needs to reset the default ime
816 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900817 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800818 updateFromSettingsLocked(true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900819 mLastSystemLocale = newLocale;
820 if (!updateOnlyWhenLocaleChanged) {
821 try {
822 startInputInnerLocked();
823 } catch (RuntimeException e) {
824 Slog.w(TAG, "Unexpected exception", e);
825 }
826 }
827 }
828 }
829
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900830 private void resetStateIfCurrentLocaleChangedLocked() {
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900831 resetAllInternalStateLocked(true /* updateOnlyWhenLocaleChanged */,
832 true /* resetDefaultImeLocked */);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900833 }
834
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900835 private void switchUserLocked(int newUserId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900836 mSettings.setCurrentUserId(newUserId);
Kenny Guy2a764942014-04-02 13:29:20 +0100837 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900838 // InputMethodFileManager should be reset when the user is changed
839 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900840 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +0900841 // For secondary users, the list of enabled IMEs may not have been updated since the
842 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
843 // not be empty even if the IME has been uninstalled by the primary user.
844 // Even in such cases, IMMS works fine because it will find the most applicable
845 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900846 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900847 if (DEBUG) {
848 Slog.d(TAG, "Switch user: " + newUserId + " current ime = " + defaultImiId);
849 }
850 resetAllInternalStateLocked(false /* updateOnlyWhenLocaleChanged */,
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900851 initialUserSwitch /* needsToResetDefaultIme */);
852 if (initialUserSwitch) {
853 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mContext.getPackageManager(),
854 mSettings.getEnabledInputMethodListLocked());
855 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900856 }
857
Kenny Guy2a764942014-04-02 13:29:20 +0100858 void updateCurrentProfileIds() {
859 List<UserInfo> profiles =
860 UserManager.get(mContext).getProfiles(mSettings.getCurrentUserId());
861 int[] currentProfileIds = new int[profiles.size()]; // profiles will not be null
862 for (int i = 0; i < currentProfileIds.length; i++) {
863 currentProfileIds[i] = profiles.get(i).id;
Amith Yamasani734983f2014-03-04 16:48:05 -0800864 }
Kenny Guy2a764942014-04-02 13:29:20 +0100865 mSettings.setCurrentProfileIds(currentProfileIds);
Amith Yamasani734983f2014-03-04 16:48:05 -0800866 }
867
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800868 @Override
869 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
870 throws RemoteException {
871 try {
872 return super.onTransact(code, data, reply, flags);
873 } catch (RuntimeException e) {
874 // The input method manager only throws security exceptions, so let's
875 // log all others.
876 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -0700877 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800878 }
879 throw e;
880 }
881 }
882
Svetoslav Ganova0027152013-06-25 14:59:53 -0700883 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700884 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900885 if (DEBUG) {
886 Slog.d(TAG, "--- systemReady");
887 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700888 if (!mSystemReady) {
889 mSystemReady = true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900890 mKeyguardManager =
891 (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700892 mNotificationManager = (NotificationManager)
893 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
894 mStatusBar = statusBar;
895 statusBar.setIconVisibility("ime", false);
896 updateImeWindowStatusLocked();
satokb858c732011-07-22 19:54:34 +0900897 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
898 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +0900899 if (mShowOngoingImeSwitcherForPhones) {
900 mWindowManagerService.setOnHardKeyboardStatusChangeListener(
901 mHardKeyboardListener);
902 }
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900903 buildInputMethodListLocked(mMethodList, mMethodMap,
904 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satok0a1bcf42012-05-16 19:26:31 +0900905 if (!mImeSelectedOnBoot) {
906 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900907 resetStateIfCurrentLocaleChangedLocked();
908 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(
909 mContext.getPackageManager(),
910 mSettings.getEnabledInputMethodListLocked());
satok0a1bcf42012-05-16 19:26:31 +0900911 }
912 mLastSystemLocale = mRes.getConfiguration().locale;
Dianne Hackborncc278702009-09-02 23:07:23 -0700913 try {
914 startInputInnerLocked();
915 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800916 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700917 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700918 }
919 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800920 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800921
satok15452a42011-10-28 17:58:28 +0900922 private void setImeWindowVisibilityStatusHiddenLocked() {
923 mImeWindowVis = 0;
924 updateImeWindowStatusLocked();
925 }
926
satok3afd6c02011-11-18 08:38:19 +0900927 private void refreshImeWindowVisibilityLocked() {
928 final Configuration conf = mRes.getConfiguration();
929 final boolean haveHardKeyboard = conf.keyboard
930 != Configuration.KEYBOARD_NOKEYS;
931 final boolean hardKeyShown = haveHardKeyboard
932 && conf.hardKeyboardHidden
933 != Configuration.HARDKEYBOARDHIDDEN_YES;
John Spurlocke0980502013-10-25 11:59:29 -0400934
935 final boolean isScreenLocked = isKeyguardLocked();
936 final boolean inputActive = !isScreenLocked && (mInputShown || hardKeyShown);
John Spurlock36439b42013-10-23 16:36:47 -0400937 // We assume the softkeyboard is shown when the input is active as long as the
938 // hard keyboard is not shown.
939 final boolean inputVisible = inputActive && !hardKeyShown;
940 mImeWindowVis = (inputActive ? InputMethodService.IME_ACTIVE : 0)
941 | (inputVisible ? InputMethodService.IME_VISIBLE : 0);
satok3afd6c02011-11-18 08:38:19 +0900942 updateImeWindowStatusLocked();
943 }
944
satok15452a42011-10-28 17:58:28 +0900945 private void updateImeWindowStatusLocked() {
satokdbf29502011-08-25 15:28:23 +0900946 setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700947 }
948
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900949 // ---------------------------------------------------------------------------------------
950 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
951 // 1) it comes from the system process
952 // 2) the calling process' user id is identical to the current user id IMMS thinks.
953 private boolean calledFromValidUser() {
954 final int uid = Binder.getCallingUid();
955 final int userId = UserHandle.getUserId(uid);
956 if (DEBUG) {
957 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
958 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
959 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +0900960 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
961 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900962 }
Kenny Guy2a764942014-04-02 13:29:20 +0100963 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900964 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900965 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +0900966
967 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
968 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
969 // must not manage background users' states in any functions.
970 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
971 // by a token.
972 if (mContext.checkCallingOrSelfPermission(
973 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
974 == PackageManager.PERMISSION_GRANTED) {
975 if (DEBUG) {
976 Slog.d(TAG, "--- Access granted because the calling process has "
977 + "the INTERACT_ACROSS_USERS_FULL permission");
978 }
979 return true;
980 }
Satoshi Kataoka0766eb02013-07-31 18:30:13 +0900981 Slog.w(TAG, "--- IPC called from background users. Ignore. \n"
982 + InputMethodUtils.getStackTrace());
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +0900983 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900984 }
985
Yohei Yukawa22c97be2014-06-04 19:43:36 +0900986
987 /**
988 * Returns true iff the caller is identified to be the current input method with the token.
989 * @param token The window token given to the input method when it was started.
990 * @return true if and only if non-null valid token is specified.
991 */
992 private boolean calledWithValidToken(IBinder token) {
993 if (token == null || mCurToken != token) {
994 return false;
995 }
996 return true;
997 }
998
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900999 private boolean bindCurrentInputMethodService(
1000 Intent service, ServiceConnection conn, int flags) {
1001 if (service == null || conn == null) {
1002 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
1003 return false;
1004 }
Amith Yamasani27b89e62013-01-16 12:30:11 -08001005 return mContext.bindServiceAsUser(service, conn, flags,
1006 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001007 }
1008
satoke7c6998e2011-06-03 17:57:59 +09001009 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001010 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001011 // TODO: Make this work even for non-current users?
1012 if (!calledFromValidUser()) {
1013 return Collections.emptyList();
1014 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015 synchronized (mMethodMap) {
1016 return new ArrayList<InputMethodInfo>(mMethodList);
1017 }
1018 }
1019
satoke7c6998e2011-06-03 17:57:59 +09001020 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001022 // TODO: Make this work even for non-current users?
1023 if (!calledFromValidUser()) {
1024 return Collections.emptyList();
1025 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001027 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001028 }
1029 }
1030
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001031 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001032 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001033 * @return enabled subtypes of the specified imi
1034 */
satoke7c6998e2011-06-03 17:57:59 +09001035 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001036 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001037 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001038 // TODO: Make this work even for non-current users?
1039 if (!calledFromValidUser()) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001040 return Collections.<InputMethodSubtype>emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001041 }
satok67ddf9c2010-11-17 09:45:54 +09001042 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001043 final InputMethodInfo imi;
1044 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001045 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001046 } else {
1047 imi = mMethodMap.get(imiId);
1048 }
1049 if (imi == null) {
1050 return Collections.<InputMethodSubtype>emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001051 }
1052 return mSettings.getEnabledInputMethodSubtypeListLocked(
1053 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001054 }
1055 }
1056
satoke7c6998e2011-06-03 17:57:59 +09001057 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001058 public void addClient(IInputMethodClient client,
1059 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001060 if (!calledFromValidUser()) {
1061 return;
1062 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001063 synchronized (mMethodMap) {
1064 mClients.put(client.asBinder(), new ClientState(client,
1065 inputContext, uid, pid));
1066 }
1067 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001068
satoke7c6998e2011-06-03 17:57:59 +09001069 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001070 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001071 if (!calledFromValidUser()) {
1072 return;
1073 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001074 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001075 ClientState cs = mClients.remove(client.asBinder());
1076 if (cs != null) {
1077 clearClientSessionLocked(cs);
1078 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001079 }
1080 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001081
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001082 void executeOrSendMessage(IInterface target, Message msg) {
1083 if (target.asBinder() instanceof Binder) {
1084 mCaller.sendMessage(msg);
1085 } else {
1086 handleMessage(msg);
1087 msg.recycle();
1088 }
1089 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001090
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001091 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001093 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094 + mCurClient.client.asBinder());
1095 if (mBoundToMethod) {
1096 mBoundToMethod = false;
1097 if (mCurMethod != null) {
1098 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1099 MSG_UNBIND_INPUT, mCurMethod));
1100 }
1101 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001102
1103 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1104 MSG_SET_ACTIVE, 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1106 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1107 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001109
The Android Open Source Project10592532009-03-18 17:39:46 -07001110 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 }
1112 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001114 private int getImeShowFlags() {
1115 int flags = 0;
1116 if (mShowForced) {
1117 flags |= InputMethod.SHOW_FORCED
1118 | InputMethod.SHOW_EXPLICIT;
1119 } else if (mShowExplicitlyRequested) {
1120 flags |= InputMethod.SHOW_EXPLICIT;
1121 }
1122 return flags;
1123 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001124
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001125 private int getAppShowFlags() {
1126 int flags = 0;
1127 if (mShowForced) {
1128 flags |= InputMethodManager.SHOW_FORCED;
1129 } else if (!mShowExplicitlyRequested) {
1130 flags |= InputMethodManager.SHOW_IMPLICIT;
1131 }
1132 return flags;
1133 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001134
Dianne Hackborn7663d802012-02-24 13:08:49 -08001135 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001136 if (!mBoundToMethod) {
1137 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1138 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1139 mBoundToMethod = true;
1140 }
1141 final SessionState session = mCurClient.curSession;
1142 if (initial) {
1143 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1144 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
1145 } else {
1146 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1147 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
1148 }
1149 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001150 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001151 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001152 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001153 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001154 (session.channel != null ? session.channel.dup() : null),
1155 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001156 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001158 InputBindResult startInputLocked(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001159 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160 // If no method is currently selected, do nothing.
1161 if (mCurMethodId == null) {
1162 return mNoBinding;
1163 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001165 ClientState cs = mClients.get(client.asBinder());
1166 if (cs == null) {
1167 throw new IllegalArgumentException("unknown client "
1168 + client.asBinder());
1169 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001170
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 try {
1172 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1173 // Check with the window manager to make sure this client actually
1174 // has a window with focus. If not, reject. This is thread safe
1175 // because if the focus changes some time before or after, the
1176 // next client receiving focus that has any interest in input will
1177 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001178 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001179 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1180 return null;
1181 }
1182 } catch (RemoteException e) {
1183 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001184
Dianne Hackborn7663d802012-02-24 13:08:49 -08001185 return startInputUncheckedLocked(cs, inputContext, attribute, controlFlags);
1186 }
1187
1188 InputBindResult startInputUncheckedLocked(ClientState cs,
1189 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
1190 // If no method is currently selected, do nothing.
1191 if (mCurMethodId == null) {
1192 return mNoBinding;
1193 }
1194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001195 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001196 // Was the keyguard locked when switching over to the new client?
1197 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 // If the client is changing, we need to switch over to the new
1199 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001200 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001201 if (DEBUG) Slog.v(TAG, "switching to client: client = "
John Spurlocke0980502013-10-25 11:59:29 -04001202 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203
1204 // If the screen is on, inform the new client it is active
1205 if (mScreenOn) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001206 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
1207 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001208 }
1209 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 // Bump up the sequence for this client and attach it.
1212 mCurSeq++;
1213 if (mCurSeq <= 0) mCurSeq = 1;
1214 mCurClient = cs;
1215 mCurInputContext = inputContext;
1216 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001217
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001218 // Check if the input method is changing.
1219 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1220 if (cs.curSession != null) {
1221 // Fast case: if we are already connected to the input method,
1222 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001223 return attachNewInputLocked(
1224 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001225 }
1226 if (mHaveConnection) {
1227 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 // Return to client, and we will get back with it when
1229 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001230 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001231 return new InputBindResult(null, null, mCurId, mCurSeq,
1232 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001233 } else if (SystemClock.uptimeMillis()
1234 < (mLastBindTime+TIME_TO_RECONNECT)) {
1235 // In this case we have connected to the service, but
1236 // don't yet have its interface. If it hasn't been too
1237 // long since we did the connection, we'll return to
1238 // the client and wait to get the service interface so
1239 // we can report back. If it has been too long, we want
1240 // to fall through so we can try a disconnect/reconnect
1241 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001242 return new InputBindResult(null, null, mCurId, mCurSeq,
1243 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001244 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001245 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1246 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001247 }
1248 }
1249 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001250
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001251 return startInputInnerLocked();
1252 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001253
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001254 InputBindResult startInputInnerLocked() {
1255 if (mCurMethodId == null) {
1256 return mNoBinding;
1257 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001258
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001259 if (!mSystemReady) {
1260 // If the system is not yet ready, we shouldn't be running third
1261 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001262 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1263 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001264 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001265
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001266 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1267 if (info == null) {
1268 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1269 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001270
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001271 unbindCurrentMethodLocked(false, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1274 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001275 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1276 com.android.internal.R.string.input_method_binding_label);
1277 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1278 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001279 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001280 | Context.BIND_NOT_VISIBLE | Context.BIND_NOT_FOREGROUND
1281 | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001282 mLastBindTime = SystemClock.uptimeMillis();
1283 mHaveConnection = true;
1284 mCurId = info.getId();
1285 mCurToken = new Binder();
1286 try {
Craig Mautnerca0ac712013-03-14 09:43:02 -07001287 if (true || DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288 mIWindowManager.addWindowToken(mCurToken,
1289 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1290 } catch (RemoteException e) {
1291 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001292 return new InputBindResult(null, null, mCurId, mCurSeq,
1293 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294 } else {
1295 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001296 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001297 + mCurIntent);
1298 }
1299 return null;
1300 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001301
satoke7c6998e2011-06-03 17:57:59 +09001302 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001303 public InputBindResult startInput(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001304 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001305 if (!calledFromValidUser()) {
1306 return null;
1307 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001308 synchronized (mMethodMap) {
1309 final long ident = Binder.clearCallingIdentity();
1310 try {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001311 return startInputLocked(client, inputContext, attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001312 } finally {
1313 Binder.restoreCallingIdentity(ident);
1314 }
1315 }
1316 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001317
satoke7c6998e2011-06-03 17:57:59 +09001318 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001319 public void finishInput(IInputMethodClient client) {
1320 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001321
satoke7c6998e2011-06-03 17:57:59 +09001322 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001323 public void onServiceConnected(ComponentName name, IBinder service) {
1324 synchronized (mMethodMap) {
1325 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1326 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001327 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001328 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001329 unbindCurrentMethodLocked(false, false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001330 return;
1331 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001332 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001333 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1334 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001335 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001336 clearClientSessionLocked(mCurClient);
1337 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001338 }
1339 }
1340 }
1341 }
1342
Jeff Brownc28867a2013-03-26 15:42:39 -07001343 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1344 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001345 synchronized (mMethodMap) {
1346 if (mCurMethod != null && method != null
1347 && mCurMethod.asBinder() == method.asBinder()) {
1348 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001349 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001350 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001351 method, session, channel);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001352 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001353 if (res.method != null) {
1354 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
1355 MSG_BIND_METHOD, mCurClient.client, res));
1356 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001357 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001358 }
1359 }
1360 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001361
1362 // Session abandoned. Close its associated input channel.
1363 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001364 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001365
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001366 void unbindCurrentMethodLocked(boolean reportToClient, boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001367 if (mVisibleBound) {
1368 mContext.unbindService(mVisibleConnection);
1369 mVisibleBound = false;
1370 }
1371
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001372 if (mHaveConnection) {
1373 mContext.unbindService(this);
1374 mHaveConnection = false;
1375 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001376
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001377 if (mCurToken != null) {
1378 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001379 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001380 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001381 // The current IME is shown. Hence an IME switch (transition) is happening.
1382 mWindowManagerService.saveLastInputMethodWindowForTransition();
1383 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001384 mIWindowManager.removeWindowToken(mCurToken);
1385 } catch (RemoteException e) {
1386 }
1387 mCurToken = null;
1388 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001389
The Android Open Source Project10592532009-03-18 17:39:46 -07001390 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001391 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001392
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001393 if (reportToClient && mCurClient != null) {
1394 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1395 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1396 }
1397 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001398
1399 void requestClientSessionLocked(ClientState cs) {
1400 if (!cs.sessionRequested) {
1401 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1402 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1403 cs.sessionRequested = true;
1404 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1405 MSG_CREATE_SESSION, mCurMethod, channels[1],
1406 new MethodCallback(this, mCurMethod, channels[0])));
1407 }
1408 }
1409
1410 void clearClientSessionLocked(ClientState cs) {
1411 finishSessionLocked(cs.curSession);
1412 cs.curSession = null;
1413 cs.sessionRequested = false;
1414 }
1415
1416 private void finishSessionLocked(SessionState sessionState) {
1417 if (sessionState != null) {
1418 if (sessionState.session != null) {
1419 try {
1420 sessionState.session.finishSession();
1421 } catch (RemoteException e) {
1422 Slog.w(TAG, "Session failed to close due to remote exception", e);
1423 setImeWindowVisibilityStatusHiddenLocked();
1424 }
1425 sessionState.session = null;
1426 }
1427 if (sessionState.channel != null) {
1428 sessionState.channel.dispose();
1429 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001430 }
1431 }
1432 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001433
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001434 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001435 if (mCurMethod != null) {
1436 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001437 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001438 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001439
Jeff Brownc28867a2013-03-26 15:42:39 -07001440 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001441 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001442 mCurMethod = null;
1443 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001444 if (mStatusBar != null) {
1445 mStatusBar.setIconVisibility("ime", false);
1446 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001447 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001448
satoke7c6998e2011-06-03 17:57:59 +09001449 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001450 public void onServiceDisconnected(ComponentName name) {
1451 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001452 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001453 + " mCurIntent=" + mCurIntent);
1454 if (mCurMethod != null && mCurIntent != null
1455 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001456 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001457 // We consider this to be a new bind attempt, since the system
1458 // should now try to restart the service for us.
1459 mLastBindTime = SystemClock.uptimeMillis();
1460 mShowRequested = mInputShown;
1461 mInputShown = false;
1462 if (mCurClient != null) {
1463 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1464 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1465 }
1466 }
1467 }
1468 }
1469
satokf9f01002011-05-19 21:31:50 +09001470 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001471 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
1472 long ident = Binder.clearCallingIdentity();
1473 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001474 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09001475 if (!calledWithValidToken(token)) {
1476 final int uid = Binder.getCallingUid();
1477 Slog.e(TAG, "Ignoring updateStatusIcon due to an invalid token. uid:" + uid
1478 + " token:" + token);
1479 return;
1480 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001481 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001482 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001483 if (mStatusBar != null) {
1484 mStatusBar.setIconVisibility("ime", false);
1485 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001486 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001487 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001488 CharSequence contentDescription = null;
1489 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001490 // Use PackageManager to load label
1491 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001492 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001493 mIPackageManager.getApplicationInfo(packageName, 0,
1494 mSettings.getCurrentUserId()));
1495 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001496 /* ignore */
1497 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001498 if (mStatusBar != null) {
1499 mStatusBar.setIcon("ime", packageName, iconId, 0,
1500 contentDescription != null
1501 ? contentDescription.toString() : null);
1502 mStatusBar.setIconVisibility("ime", true);
1503 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001504 }
1505 }
1506 } finally {
1507 Binder.restoreCallingIdentity(ident);
1508 }
1509 }
1510
satok7cfc0ed2011-06-20 21:29:36 +09001511 private boolean needsToShowImeSwitchOngoingNotification() {
1512 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04001513 if (mSwitchingDialog != null) return false;
satok2c93efc2012-04-02 19:33:47 +09001514 if (isScreenLocked()) return false;
satok7cfc0ed2011-06-20 21:29:36 +09001515 synchronized (mMethodMap) {
1516 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1517 final int N = imis.size();
satokb6359412011-06-28 17:47:41 +09001518 if (N > 2) return true;
1519 if (N < 1) return false;
1520 int nonAuxCount = 0;
1521 int auxCount = 0;
1522 InputMethodSubtype nonAuxSubtype = null;
1523 InputMethodSubtype auxSubtype = null;
satok7cfc0ed2011-06-20 21:29:36 +09001524 for(int i = 0; i < N; ++i) {
1525 final InputMethodInfo imi = imis.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001526 final List<InputMethodSubtype> subtypes =
1527 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satok7cfc0ed2011-06-20 21:29:36 +09001528 final int subtypeCount = subtypes.size();
1529 if (subtypeCount == 0) {
satokb6359412011-06-28 17:47:41 +09001530 ++nonAuxCount;
satok7cfc0ed2011-06-20 21:29:36 +09001531 } else {
1532 for (int j = 0; j < subtypeCount; ++j) {
satokb6359412011-06-28 17:47:41 +09001533 final InputMethodSubtype subtype = subtypes.get(j);
1534 if (!subtype.isAuxiliary()) {
1535 ++nonAuxCount;
1536 nonAuxSubtype = subtype;
1537 } else {
1538 ++auxCount;
1539 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001540 }
1541 }
1542 }
satok7cfc0ed2011-06-20 21:29:36 +09001543 }
satokb6359412011-06-28 17:47:41 +09001544 if (nonAuxCount > 1 || auxCount > 1) {
1545 return true;
1546 } else if (nonAuxCount == 1 && auxCount == 1) {
1547 if (nonAuxSubtype != null && auxSubtype != null
satok9747f892011-09-12 15:56:40 +09001548 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1549 || auxSubtype.overridesImplicitlyEnabledSubtype()
1550 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
satokb6359412011-06-28 17:47:41 +09001551 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1552 return false;
1553 }
1554 return true;
1555 }
1556 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001557 }
satok7cfc0ed2011-06-20 21:29:36 +09001558 }
1559
John Spurlocke0980502013-10-25 11:59:29 -04001560 private boolean isKeyguardLocked() {
1561 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1562 }
1563
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001564 // Caution! This method is called in this class. Handle multi-user carefully
satokdbf29502011-08-25 15:28:23 +09001565 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001566 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001567 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001568 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001569 try {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001570 if (!calledWithValidToken(token)) {
1571 final int uid = Binder.getCallingUid();
1572 Slog.e(TAG, "Ignoring setImeWindowStatus due to an invalid token. uid:" + uid
1573 + " token:" + token);
satok06487a52010-10-29 11:37:18 +09001574 return;
1575 }
satok06487a52010-10-29 11:37:18 +09001576 synchronized (mMethodMap) {
John Spurlocke0980502013-10-25 11:59:29 -04001577 // apply policy for binder calls
1578 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
1579 vis = 0;
1580 }
Joe Onorato857fd9b2011-01-27 15:08:35 -08001581 mImeWindowVis = vis;
1582 mBackDisposition = backDisposition;
jungheang.lee217fd292013-02-26 16:53:22 +09001583 final boolean iconVisibility = ((vis & (InputMethodService.IME_ACTIVE)) != 0)
1584 && (mWindowManagerService.isHardKeyboardAvailable()
1585 || (vis & (InputMethodService.IME_VISIBLE)) != 0);
Jason Monkb605fec2014-05-02 17:04:10 -04001586 final boolean needsToShowImeSwitcher = iconVisibility
1587 && needsToShowImeSwitchOngoingNotification();
1588 if (mStatusBar != null) {
1589 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
1590 needsToShowImeSwitcher);
1591 }
satok5bc8e732011-07-22 21:07:23 +09001592 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Jason Monkb605fec2014-05-02 17:04:10 -04001593 if (imi != null && needsToShowImeSwitcher) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001594 // Used to load label
satok7cfc0ed2011-06-20 21:29:36 +09001595 final CharSequence title = mRes.getText(
1596 com.android.internal.R.string.select_input_method);
Satoshi Kataokab2827262013-07-04 19:43:14 +09001597 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
1598 mContext, imi, mCurrentSubtype);
satok5bc8e732011-07-22 21:07:23 +09001599
Selim Cinek255dd042014-08-19 22:29:02 +02001600 mImeSwitcherNotification.color = mContext.getResources().getColor(
1601 com.android.internal.R.color.system_notification_accent_color);
satok7cfc0ed2011-06-20 21:29:36 +09001602 mImeSwitcherNotification.setLatestEventInfo(
satok5bc8e732011-07-22 21:07:23 +09001603 mContext, title, summary, mImeSwitchPendingIntent);
Jason Monke463c742014-05-02 12:31:45 -04001604 if ((mNotificationManager != null)
1605 && !mWindowManagerService.hasNavigationBar()) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001606 if (DEBUG) {
Satoshi Kataokab2827262013-07-04 19:43:14 +09001607 Slog.d(TAG, "--- show notification: label = " + summary);
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001608 }
1609 mNotificationManager.notifyAsUser(null,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001610 com.android.internal.R.string.select_input_method,
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001611 mImeSwitcherNotification, UserHandle.ALL);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001612 mNotificationShown = true;
1613 }
satok7cfc0ed2011-06-20 21:29:36 +09001614 } else {
Dianne Hackborn661cd522011-08-22 00:26:20 -07001615 if (mNotificationShown && mNotificationManager != null) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001616 if (DEBUG) {
1617 Slog.d(TAG, "--- hide notification");
1618 }
1619 mNotificationManager.cancelAsUser(null,
1620 com.android.internal.R.string.select_input_method, UserHandle.ALL);
satok7cfc0ed2011-06-20 21:29:36 +09001621 mNotificationShown = false;
1622 }
1623 }
satok06487a52010-10-29 11:37:18 +09001624 }
1625 } finally {
1626 Binder.restoreCallingIdentity(ident);
1627 }
1628 }
1629
satoke7c6998e2011-06-03 17:57:59 +09001630 @Override
satokf9f01002011-05-19 21:31:50 +09001631 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001632 if (!calledFromValidUser()) {
1633 return;
1634 }
satokf9f01002011-05-19 21:31:50 +09001635 synchronized (mMethodMap) {
1636 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1637 for (int i = 0; i < spans.length; ++i) {
1638 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001639 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001640 mSecureSuggestionSpans.put(ss, currentImi);
1641 }
1642 }
1643 }
1644 }
1645
satoke7c6998e2011-06-03 17:57:59 +09001646 @Override
satokf9f01002011-05-19 21:31:50 +09001647 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001648 if (!calledFromValidUser()) {
1649 return false;
1650 }
satokf9f01002011-05-19 21:31:50 +09001651 synchronized (mMethodMap) {
1652 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1653 // TODO: Do not send the intent if the process of the targetImi is already dead.
1654 if (targetImi != null) {
1655 final String[] suggestions = span.getSuggestions();
1656 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001657 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001658 final Intent intent = new Intent();
1659 // Ensures that only a class in the original IME package will receive the
1660 // notification.
satok42c5a162011-05-26 16:46:14 +09001661 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001662 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1663 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1664 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1665 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001666 final long ident = Binder.clearCallingIdentity();
1667 try {
1668 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1669 } finally {
1670 Binder.restoreCallingIdentity(ident);
1671 }
satokf9f01002011-05-19 21:31:50 +09001672 return true;
1673 }
1674 }
1675 return false;
1676 }
1677
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001678 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001679 updateInputMethodsFromSettingsLocked(enabledMayChange);
1680 updateKeyboardFromSettingsLocked();
1681 }
1682
1683 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001684 if (enabledMayChange) {
1685 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1686 for (int i=0; i<enabled.size(); i++) {
1687 // We allow the user to select "disabled until used" apps, so if they
1688 // are enabling one of those here we now need to make it enabled.
1689 InputMethodInfo imm = enabled.get(i);
1690 try {
1691 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1692 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1693 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09001694 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001695 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001696 if (DEBUG) {
1697 Slog.d(TAG, "Update state(" + imm.getId()
1698 + "): DISABLED_UNTIL_USED -> DEFAULT");
1699 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001700 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1701 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001702 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
1703 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001704 }
1705 } catch (RemoteException e) {
1706 }
1707 }
1708 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001709 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1710 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1711 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1712 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001713 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001714 // There is no input method selected, try to choose new applicable input method.
1715 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001716 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001717 }
1718 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001719 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001720 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001721 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001722 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001723 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001724 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001725 }
satokf3db1af2010-11-23 13:34:33 +09001726 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001727 } else {
1728 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001729 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001730 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001731 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09001732 // Here is not the perfect place to reset the switching controller. Ideally
1733 // mSwitchingController and mSettings should be able to share the same state.
1734 // TODO: Make sure that mSwitchingController and mSettings are sharing the
1735 // the same enabled IMEs list.
1736 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07001737
1738 }
1739
1740 public void updateKeyboardFromSettingsLocked() {
1741 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
1742 if (mSwitchingDialog != null
1743 && mSwitchingDialogTitleView != null
1744 && mSwitchingDialog.isShowing()) {
1745 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
1746 com.android.internal.R.id.hard_keyboard_switch);
1747 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
1748 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001749 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001750
satokab751aa2010-09-14 19:17:36 +09001751 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001752 InputMethodInfo info = mMethodMap.get(id);
1753 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001754 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001755 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001756
Yohei Yukawae63b5fa2014-09-19 13:14:55 +09001757 if (mCurClient != null && mCurAttribute != null) {
1758 final int uid = mCurClient.uid;
1759 final String packageName = mCurAttribute.packageName;
1760 if (SystemConfig.getInstance().getFixedImeApps().contains(packageName)) {
1761 if (InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, uid, packageName)) {
1762 return;
1763 }
1764 // TODO: Do we need to lock the input method when the application reported an
1765 // incorrect package name?
1766 Slog.e(TAG, "Ignoring FixedImeApps due to the validation failure. uid=" + uid
1767 + " package=" + packageName);
1768 }
1769 }
1770
satokd81e9502012-05-21 12:58:45 +09001771 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001772 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09001773 final int subtypeCount = info.getSubtypeCount();
1774 if (subtypeCount <= 0) {
1775 return;
satokcd7cd292010-11-20 15:46:23 +09001776 }
satokd81e9502012-05-21 12:58:45 +09001777 final InputMethodSubtype oldSubtype = mCurrentSubtype;
1778 final InputMethodSubtype newSubtype;
1779 if (subtypeId >= 0 && subtypeId < subtypeCount) {
1780 newSubtype = info.getSubtypeAt(subtypeId);
1781 } else {
1782 // If subtype is null, try to find the most applicable one from
1783 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001784 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09001785 }
1786 if (newSubtype == null || oldSubtype == null) {
1787 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
1788 + ", new subtype = " + newSubtype);
1789 return;
1790 }
1791 if (newSubtype != oldSubtype) {
1792 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1793 if (mCurMethod != null) {
1794 try {
1795 refreshImeWindowVisibilityLocked();
1796 mCurMethod.changeInputMethodSubtype(newSubtype);
1797 } catch (RemoteException e) {
1798 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
satokab751aa2010-09-14 19:17:36 +09001799 }
1800 }
1801 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001802 return;
1803 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001804
satokd81e9502012-05-21 12:58:45 +09001805 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001806 final long ident = Binder.clearCallingIdentity();
1807 try {
satokab751aa2010-09-14 19:17:36 +09001808 // Set a subtype to this input method.
1809 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001810 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1811 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1812 // because mCurMethodId is stored as a history in
1813 // setSelectedInputMethodAndSubtypeLocked().
1814 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001815
1816 if (ActivityManagerNative.isSystemReady()) {
1817 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001818 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001819 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07001820 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001821 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001822 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001823 } finally {
1824 Binder.restoreCallingIdentity(ident);
1825 }
1826 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001827
satok42c5a162011-05-26 16:46:14 +09001828 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001829 public boolean showSoftInput(IInputMethodClient client, int flags,
1830 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001831 if (!calledFromValidUser()) {
1832 return false;
1833 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001834 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001835 long ident = Binder.clearCallingIdentity();
1836 try {
1837 synchronized (mMethodMap) {
1838 if (mCurClient == null || client == null
1839 || mCurClient.client.asBinder() != client.asBinder()) {
1840 try {
1841 // We need to check if this is the current client with
1842 // focus in the window manager, to allow this call to
1843 // be made before input is started in it.
1844 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001845 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001846 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001847 }
1848 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001849 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001850 }
1851 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001852
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001853 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001854 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001855 }
1856 } finally {
1857 Binder.restoreCallingIdentity(ident);
1858 }
1859 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001860
The Android Open Source Project4df24232009-03-05 14:34:35 -08001861 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001862 mShowRequested = true;
1863 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1864 mShowExplicitlyRequested = true;
1865 }
1866 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1867 mShowExplicitlyRequested = true;
1868 mShowForced = true;
1869 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001870
Dianne Hackborncc278702009-09-02 23:07:23 -07001871 if (!mSystemReady) {
1872 return false;
1873 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001874
The Android Open Source Project4df24232009-03-05 14:34:35 -08001875 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001876 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001877 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001878 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1879 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1880 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001881 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001882 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001883 bindCurrentInputMethodService(
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001884 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE
1885 | Context.BIND_TREAT_LIKE_ACTIVITY);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001886 mVisibleBound = true;
1887 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001888 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001889 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09001890 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001891 // The client has asked to have the input method shown, but
1892 // we have been sitting here too long with a connection to the
1893 // service and no interface received, so let's disconnect/connect
1894 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001895 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001896 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09001897 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001898 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001899 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001900 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001901 } else {
1902 if (DEBUG) {
1903 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
1904 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
1905 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001906 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001907
The Android Open Source Project4df24232009-03-05 14:34:35 -08001908 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001909 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001910
satok42c5a162011-05-26 16:46:14 +09001911 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001912 public boolean hideSoftInput(IInputMethodClient client, int flags,
1913 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001914 if (!calledFromValidUser()) {
1915 return false;
1916 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001917 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001918 long ident = Binder.clearCallingIdentity();
1919 try {
1920 synchronized (mMethodMap) {
1921 if (mCurClient == null || client == null
1922 || mCurClient.client.asBinder() != client.asBinder()) {
1923 try {
1924 // We need to check if this is the current client with
1925 // focus in the window manager, to allow this call to
1926 // be made before input is started in it.
1927 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001928 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1929 + uid + ": " + client);
satok15452a42011-10-28 17:58:28 +09001930 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001931 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001932 }
1933 } catch (RemoteException e) {
satok15452a42011-10-28 17:58:28 +09001934 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001935 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001936 }
1937 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001938
Joe Onorato8a9b2202010-02-26 18:56:32 -08001939 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001940 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001941 }
1942 } finally {
1943 Binder.restoreCallingIdentity(ident);
1944 }
1945 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001946
The Android Open Source Project4df24232009-03-05 14:34:35 -08001947 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001948 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1949 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001950 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 -08001951 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001952 }
1953 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001954 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 -08001955 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001956 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001957 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001958 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001959 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1960 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1961 res = true;
1962 } else {
1963 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001964 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001965 if (mHaveConnection && mVisibleBound) {
1966 mContext.unbindService(mVisibleConnection);
1967 mVisibleBound = false;
1968 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001969 mInputShown = false;
1970 mShowRequested = false;
1971 mShowExplicitlyRequested = false;
1972 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001973 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001974 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001975
satok42c5a162011-05-26 16:46:14 +09001976 @Override
Dianne Hackborn7663d802012-02-24 13:08:49 -08001977 public InputBindResult windowGainedFocus(IInputMethodClient client, IBinder windowToken,
1978 int controlFlags, int softInputMode, int windowFlags,
1979 EditorInfo attribute, IInputContext inputContext) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09001980 // Needs to check the validity before clearing calling identity
1981 final boolean calledFromValidUser = calledFromValidUser();
1982
Dianne Hackborn7663d802012-02-24 13:08:49 -08001983 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001984 long ident = Binder.clearCallingIdentity();
1985 try {
1986 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001987 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
Dianne Hackborn7663d802012-02-24 13:08:49 -08001988 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001989 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08001990 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001991
Dianne Hackborn7663d802012-02-24 13:08:49 -08001992 ClientState cs = mClients.get(client.asBinder());
1993 if (cs == null) {
1994 throw new IllegalArgumentException("unknown client "
1995 + client.asBinder());
1996 }
1997
1998 try {
1999 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2000 // Check with the window manager to make sure this client actually
2001 // has a window with focus. If not, reject. This is thread safe
2002 // because if the focus changes some time before or after, the
2003 // next client receiving focus that has any interest in input will
2004 // be calling through here after that change happens.
2005 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2006 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2007 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002008 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002009 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002010 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002011
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002012 if (!calledFromValidUser) {
2013 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2014 Slog.w(TAG, "If you want to interect with IME, you need "
2015 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2016 hideCurrentInputLocked(0, null);
2017 return null;
2018 }
2019
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002020 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07002021 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09002022 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002023 if (attribute != null) {
2024 return startInputUncheckedLocked(cs, inputContext, attribute,
2025 controlFlags);
2026 }
2027 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002028 }
2029 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002030
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002031 // Should we auto-show the IME even if the caller has not
2032 // specified what should be done with it?
2033 // We only do this automatically if the window can resize
2034 // to accommodate the IME (so what the user sees will give
2035 // them good context without input information being obscured
2036 // by the IME) or if running on a large screen where there
2037 // is more room for the target window + IME.
2038 final boolean doAutoShow =
2039 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2040 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2041 || mRes.getConfiguration().isLayoutSizeAtLeast(
2042 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002043 final boolean isTextEditor =
2044 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2045
2046 // We want to start input before showing the IME, but after closing
2047 // it. We want to do this after closing it to help the IME disappear
2048 // more quickly (not get stuck behind it initializing itself for the
2049 // new focused input, even if its window wants to hide the IME).
2050 boolean didStart = false;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002051
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002052 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2053 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002054 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002055 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2056 // There is no focus view, and this window will
2057 // be behind any soft input window, so hide the
2058 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002059 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002060 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002061 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002062 } else if (isTextEditor && doAutoShow && (softInputMode &
2063 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002064 // There is a focus view, and we are navigating forward
2065 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002066 // We only do this automatically if the window can resize
2067 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002068 // them good context without input information being obscured
2069 // by the IME) or if running on a large screen where there
2070 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002071 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002072 if (attribute != null) {
2073 res = startInputUncheckedLocked(cs, inputContext, attribute,
2074 controlFlags);
2075 didStart = true;
2076 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002077 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002078 }
2079 break;
2080 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2081 // Do nothing.
2082 break;
2083 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2084 if ((softInputMode &
2085 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002086 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002087 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002088 }
2089 break;
2090 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002091 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002092 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002093 break;
2094 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2095 if ((softInputMode &
2096 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002097 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002098 if (attribute != null) {
2099 res = startInputUncheckedLocked(cs, inputContext, attribute,
2100 controlFlags);
2101 didStart = true;
2102 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002103 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002104 }
2105 break;
2106 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002107 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002108 if (attribute != null) {
2109 res = startInputUncheckedLocked(cs, inputContext, attribute,
2110 controlFlags);
2111 didStart = true;
2112 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002113 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002114 break;
2115 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002116
2117 if (!didStart && attribute != null) {
2118 res = startInputUncheckedLocked(cs, inputContext, attribute,
2119 controlFlags);
2120 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002121 }
2122 } finally {
2123 Binder.restoreCallingIdentity(ident);
2124 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002125
2126 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002127 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002128
satok42c5a162011-05-26 16:46:14 +09002129 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002130 public void showInputMethodPickerFromClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002131 if (!calledFromValidUser()) {
2132 return;
2133 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002134 synchronized (mMethodMap) {
2135 if (mCurClient == null || client == null
2136 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002137 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002138 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002139 }
2140
satok440aab52010-11-25 09:43:11 +09002141 // Always call subtype picker, because subtype picker is a superset of input method
2142 // picker.
satokab751aa2010-09-14 19:17:36 +09002143 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
2144 }
2145 }
2146
satok42c5a162011-05-26 16:46:14 +09002147 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002148 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002149 if (!calledFromValidUser()) {
2150 return;
2151 }
satok28203512010-11-24 11:06:49 +09002152 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2153 }
2154
satok42c5a162011-05-26 16:46:14 +09002155 @Override
satok28203512010-11-24 11:06:49 +09002156 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002157 if (!calledFromValidUser()) {
2158 return;
2159 }
satok28203512010-11-24 11:06:49 +09002160 synchronized (mMethodMap) {
2161 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002162 setInputMethodWithSubtypeIdLocked(token, id,
2163 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2164 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002165 } else {
2166 setInputMethod(token, id);
2167 }
2168 }
satokab751aa2010-09-14 19:17:36 +09002169 }
2170
satok42c5a162011-05-26 16:46:14 +09002171 @Override
satokb416a712010-11-25 20:42:14 +09002172 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002173 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002174 if (!calledFromValidUser()) {
2175 return;
2176 }
satokb416a712010-11-25 20:42:14 +09002177 synchronized (mMethodMap) {
2178 if (mCurClient == null || client == null
2179 || mCurClient.client.asBinder() != client.asBinder()) {
2180 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
2181 }
satok7fee71f2010-12-17 18:54:26 +09002182 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2183 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002184 }
2185 }
2186
satok4fc87d62011-05-20 16:13:43 +09002187 @Override
satok735cf382010-11-11 20:40:09 +09002188 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002189 if (!calledFromValidUser()) {
2190 return false;
2191 }
satok735cf382010-11-11 20:40:09 +09002192 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002193 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002194 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002195 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002196 lastImi = mMethodMap.get(lastIme.first);
2197 } else {
2198 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002199 }
satok4fc87d62011-05-20 16:13:43 +09002200 String targetLastImiId = null;
2201 int subtypeId = NOT_A_SUBTYPE_ID;
2202 if (lastIme != null && lastImi != null) {
2203 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
2204 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
2205 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2206 : mCurrentSubtype.hashCode();
2207 // If the last IME is the same as the current IME and the last subtype is not
2208 // defined, there is no need to switch to the last IME.
2209 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2210 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002211 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002212 }
2213 }
2214
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002215 if (TextUtils.isEmpty(targetLastImiId)
2216 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002217 // This is a safety net. If the currentSubtype can't be added to the history
2218 // and the framework couldn't find the last ime, we will make the last ime be
2219 // the most applicable enabled keyboard subtype of the system imes.
2220 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2221 if (enabled != null) {
2222 final int N = enabled.size();
2223 final String locale = mCurrentSubtype == null
2224 ? mRes.getConfiguration().locale.toString()
2225 : mCurrentSubtype.getLocale();
2226 for (int i = 0; i < N; ++i) {
2227 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002228 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002229 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002230 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2231 InputMethodUtils.getSubtypes(imi),
2232 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002233 if (keyboardSubtype != null) {
2234 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002235 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002236 imi, keyboardSubtype.hashCode());
2237 if(keyboardSubtype.getLocale().equals(locale)) {
2238 break;
2239 }
2240 }
2241 }
2242 }
2243 }
2244 }
2245
2246 if (!TextUtils.isEmpty(targetLastImiId)) {
2247 if (DEBUG) {
2248 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2249 + ", from: " + mCurMethodId + ", " + subtypeId);
2250 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002251 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002252 return true;
2253 } else {
2254 return false;
2255 }
satok735cf382010-11-11 20:40:09 +09002256 }
2257 }
2258
satoke7c6998e2011-06-03 17:57:59 +09002259 @Override
satok688bd472012-02-09 20:09:17 +09002260 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002261 if (!calledFromValidUser()) {
2262 return false;
2263 }
satok688bd472012-02-09 20:09:17 +09002264 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002265 if (!calledWithValidToken(token)) {
2266 final int uid = Binder.getCallingUid();
2267 Slog.e(TAG, "Ignoring switchToNextInputMethod due to an invalid token. uid:" + uid
2268 + " token:" + token);
2269 return false;
2270 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002271 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
satok688bd472012-02-09 20:09:17 +09002272 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2273 if (nextSubtype == null) {
2274 return false;
2275 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002276 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2277 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002278 return true;
2279 }
2280 }
2281
2282 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002283 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2284 if (!calledFromValidUser()) {
2285 return false;
2286 }
2287 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002288 if (!calledWithValidToken(token)) {
2289 final int uid = Binder.getCallingUid();
2290 Slog.e(TAG, "Ignoring shouldOfferSwitchingToNextInputMethod due to an invalid "
2291 + "token. uid:" + uid + " token:" + token);
2292 return false;
2293 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002294 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002295 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2296 if (nextSubtype == null) {
2297 return false;
2298 }
2299 return true;
2300 }
2301 }
2302
2303 @Override
satok68f1b782011-04-11 14:26:04 +09002304 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002305 if (!calledFromValidUser()) {
2306 return null;
2307 }
satok68f1b782011-04-11 14:26:04 +09002308 synchronized (mMethodMap) {
2309 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2310 // TODO: Handle the case of the last IME with no subtypes
2311 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2312 || TextUtils.isEmpty(lastIme.second)) return null;
2313 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2314 if (lastImi == null) return null;
2315 try {
2316 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002317 final int lastSubtypeId =
2318 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002319 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2320 return null;
2321 }
2322 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002323 } catch (NumberFormatException e) {
2324 return null;
2325 }
2326 }
2327 }
2328
satoke7c6998e2011-06-03 17:57:59 +09002329 @Override
satokee5e77c2011-09-02 18:50:15 +09002330 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002331 if (!calledFromValidUser()) {
2332 return;
2333 }
satok91e88122011-07-18 11:11:42 +09002334 // By this IPC call, only a process which shares the same uid with the IME can add
2335 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09002336 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09002337 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002338 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002339 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002340 final String[] packageInfos;
2341 try {
2342 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2343 } catch (RemoteException e) {
2344 Slog.e(TAG, "Failed to get package infos");
2345 return;
2346 }
satok91e88122011-07-18 11:11:42 +09002347 if (packageInfos != null) {
2348 final int packageNum = packageInfos.length;
2349 for (int i = 0; i < packageNum; ++i) {
2350 if (packageInfos[i].equals(imi.getPackageName())) {
2351 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002352 final long ident = Binder.clearCallingIdentity();
2353 try {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002354 buildInputMethodListLocked(mMethodList, mMethodMap,
2355 false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002356 } finally {
2357 Binder.restoreCallingIdentity(ident);
2358 }
satokee5e77c2011-09-02 18:50:15 +09002359 return;
satok91e88122011-07-18 11:11:42 +09002360 }
2361 }
2362 }
satoke7c6998e2011-06-03 17:57:59 +09002363 }
satokee5e77c2011-09-02 18:50:15 +09002364 return;
satoke7c6998e2011-06-03 17:57:59 +09002365 }
2366
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002367 @Override
2368 public int getInputMethodWindowVisibleHeight() {
2369 return mWindowManagerService.getInputMethodWindowVisibleHeight();
2370 }
2371
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002372 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002373 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002374 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002375 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002376 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002377 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002378 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
2379 if (DEBUG) {
2380 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
2381 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
2382 + " actual: " + sequenceNumber);
2383 }
2384 return;
2385 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002386 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2387 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09002388 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002389 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002390 }
2391 }
2392
satok28203512010-11-24 11:06:49 +09002393 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002394 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002395 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
2396 }
2397 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002398
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002399 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
2400 if (token == null) {
2401 if (mContext.checkCallingOrSelfPermission(
2402 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2403 != PackageManager.PERMISSION_GRANTED) {
2404 throw new SecurityException(
2405 "Using null token requires permission "
2406 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002407 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002408 } else if (mCurToken != token) {
2409 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2410 + " token: " + token);
2411 return;
2412 }
2413
2414 final long ident = Binder.clearCallingIdentity();
2415 try {
2416 setInputMethodLocked(id, subtypeId);
2417 } finally {
2418 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002419 }
2420 }
2421
satok42c5a162011-05-26 16:46:14 +09002422 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002423 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002424 if (!calledFromValidUser()) {
2425 return;
2426 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002427 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002428 if (!calledWithValidToken(token)) {
2429 final int uid = Binder.getCallingUid();
2430 Slog.e(TAG, "Ignoring hideInputMethod due to an invalid token. uid:"
2431 + uid + " token:" + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002432 return;
2433 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002434 long ident = Binder.clearCallingIdentity();
2435 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002436 hideCurrentInputLocked(flags, null);
2437 } finally {
2438 Binder.restoreCallingIdentity(ident);
2439 }
2440 }
2441 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002442
satok42c5a162011-05-26 16:46:14 +09002443 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002444 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002445 if (!calledFromValidUser()) {
2446 return;
2447 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002448 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002449 if (!calledWithValidToken(token)) {
2450 final int uid = Binder.getCallingUid();
2451 Slog.e(TAG, "Ignoring showMySoftInput due to an invalid token. uid:"
2452 + uid + " token:" + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002453 return;
2454 }
2455 long ident = Binder.clearCallingIdentity();
2456 try {
2457 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002458 } finally {
2459 Binder.restoreCallingIdentity(ident);
2460 }
2461 }
2462 }
2463
2464 void setEnabledSessionInMainThread(SessionState session) {
2465 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002466 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002467 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002468 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002469 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002470 } catch (RemoteException e) {
2471 }
2472 }
2473 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002474 if (mEnabledSession != null && mEnabledSession.session != null) {
2475 try {
2476 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
2477 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
2478 } catch (RemoteException e) {
2479 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002480 }
2481 }
2482 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002483
satok42c5a162011-05-26 16:46:14 +09002484 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002485 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002486 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002487 switch (msg.what) {
2488 case MSG_SHOW_IM_PICKER:
2489 showInputMethodMenu();
2490 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002491
satokab751aa2010-09-14 19:17:36 +09002492 case MSG_SHOW_IM_SUBTYPE_PICKER:
2493 showInputMethodSubtypeMenu();
2494 return true;
2495
satok47a44912010-10-06 16:03:58 +09002496 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002497 args = (SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09002498 showInputMethodAndSubtypeEnabler((String)args.arg1);
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002499 args.recycle();
satok217f5482010-12-15 05:19:19 +09002500 return true;
2501
2502 case MSG_SHOW_IM_CONFIG:
2503 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002504 return true;
2505
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002506 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002507
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002508 case MSG_UNBIND_INPUT:
2509 try {
2510 ((IInputMethod)msg.obj).unbindInput();
2511 } catch (RemoteException e) {
2512 // There is nothing interesting about the method dying.
2513 }
2514 return true;
2515 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002516 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002517 try {
2518 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2519 } catch (RemoteException e) {
2520 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002521 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002522 return true;
2523 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002524 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002525 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002526 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002527 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002528 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002529 } catch (RemoteException e) {
2530 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002531 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002532 return true;
2533 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002534 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002535 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002536 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002537 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002538 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002539 } catch (RemoteException e) {
2540 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002541 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002542 return true;
2543 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002544 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002545 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002546 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002547 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2548 } catch (RemoteException e) {
2549 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002550 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002551 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002552 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002553 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002554 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002555 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002556 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002557 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002558 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002559 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002560 // Dispose the channel if the input method is not local to this process
2561 // because the remote proxy will get its own copy when unparceled.
2562 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002563 channel.dispose();
2564 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002565 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002566 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002567 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002568 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002569 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002571 case MSG_START_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002572 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002573 try {
2574 SessionState session = (SessionState)args.arg1;
2575 setEnabledSessionInMainThread(session);
2576 session.method.startInput((IInputContext)args.arg2,
2577 (EditorInfo)args.arg3);
2578 } catch (RemoteException e) {
2579 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002580 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002581 return true;
2582 case MSG_RESTART_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002583 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002584 try {
2585 SessionState session = (SessionState)args.arg1;
2586 setEnabledSessionInMainThread(session);
2587 session.method.restartInput((IInputContext)args.arg2,
2588 (EditorInfo)args.arg3);
2589 } catch (RemoteException e) {
2590 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002591 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002592 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002593
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002594 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002595
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002596 case MSG_UNBIND_METHOD:
2597 try {
2598 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
2599 } catch (RemoteException e) {
2600 // There is nothing interesting about the last client dying.
2601 }
2602 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002603 case MSG_BIND_METHOD: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002604 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002605 IInputMethodClient client = (IInputMethodClient)args.arg1;
2606 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002607 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002608 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002609 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002610 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002611 } finally {
2612 // Dispose the channel if the input method is not local to this process
2613 // because the remote proxy will get its own copy when unparceled.
2614 if (res.channel != null && Binder.isProxy(client)) {
2615 res.channel.dispose();
2616 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002617 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002618 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002619 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002620 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002621 case MSG_SET_ACTIVE:
2622 try {
2623 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2624 } catch (RemoteException e) {
2625 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2626 + ((ClientState)msg.obj).pid + " uid "
2627 + ((ClientState)msg.obj).uid);
2628 }
2629 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002630 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
2631 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07002632 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002633 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07002634 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002635 } catch (RemoteException e) {
2636 Slog.w(TAG, "Got RemoteException sending "
2637 + "setUserActionNotificationSequenceNumber("
2638 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07002639 + clientState.pid + " uid "
2640 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002641 }
2642 return true;
2643 }
satok01038492012-04-09 21:08:27 +09002644
2645 // --------------------------------------------------------------
2646 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07002647 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09002648 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002649 }
2650 return false;
2651 }
2652
satokdc9ddae2011-10-06 12:22:36 +09002653 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002654 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
2655 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09002656 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002657 if (DEBUG) {
2658 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2659 }
satok723a27e2010-11-11 14:58:11 +09002660 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002661 return true;
2662 }
2663
2664 return false;
2665 }
2666
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002667 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002668 HashMap<String, InputMethodInfo> map, boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002669 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002670 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Satoshi Kataoka0766eb02013-07-31 18:30:13 +09002671 + " \n ------ \n" + InputMethodUtils.getStackTrace());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002672 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002673 list.clear();
2674 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002675
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002676 // Use for queryIntentServicesAsUser
2677 final PackageManager pm = mContext.getPackageManager();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002678 String disabledSysImes = mSettings.getDisabledSystemInputMethods();
Amith Yamasanie861ec12010-03-24 21:39:27 -07002679 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002680
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002681 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002682 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002683 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2684 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002685
satoke7c6998e2011-06-03 17:57:59 +09002686 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2687 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002688 for (int i = 0; i < services.size(); ++i) {
2689 ResolveInfo ri = services.get(i);
2690 ServiceInfo si = ri.serviceInfo;
2691 ComponentName compName = new ComponentName(si.packageName, si.name);
2692 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2693 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002694 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002695 + ": it does not require the permission "
2696 + android.Manifest.permission.BIND_INPUT_METHOD);
2697 continue;
2698 }
2699
Joe Onorato8a9b2202010-02-26 18:56:32 -08002700 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002701
2702 try {
satoke7c6998e2011-06-03 17:57:59 +09002703 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002704 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002705 final String id = p.getId();
2706 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002707
2708 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002709 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002710 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002711
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002712 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002713 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002714 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002715 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002716 }
2717 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002718
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002719 if (resetDefaultEnabledIme) {
2720 final ArrayList<InputMethodInfo> defaultEnabledIme =
2721 InputMethodUtils.getDefaultEnabledImes(mContext, mSystemReady, list);
2722 for (int i = 0; i < defaultEnabledIme.size(); ++i) {
2723 final InputMethodInfo imi = defaultEnabledIme.get(i);
2724 if (DEBUG) {
2725 Slog.d(TAG, "--- enable ime = " + imi);
2726 }
2727 setInputMethodEnabledLocked(imi.getId(), true);
2728 }
2729 }
2730
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002731 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09002732 if (!TextUtils.isEmpty(defaultImiId)) {
2733 if (!map.containsKey(defaultImiId)) {
2734 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
2735 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07002736 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09002737 }
2738 } else {
2739 // Double check that the default IME is certainly enabled.
2740 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002741 }
2742 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09002743 // Here is not the perfect place to reset the switching controller. Ideally
2744 // mSwitchingController and mSettings should be able to share the same state.
2745 // TODO: Make sure that mSwitchingController and mSettings are sharing the
2746 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09002747 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002748 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002749
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002750 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002751
satokab751aa2010-09-14 19:17:36 +09002752 private void showInputMethodMenu() {
2753 showInputMethodMenuInternal(false);
2754 }
2755
2756 private void showInputMethodSubtypeMenu() {
2757 showInputMethodMenuInternal(true);
2758 }
2759
satok217f5482010-12-15 05:19:19 +09002760 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002761 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002762 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002763 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2764 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002765 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002766 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002767 }
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002768 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok217f5482010-12-15 05:19:19 +09002769 }
2770
2771 private void showConfigureInputMethods() {
2772 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2773 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2774 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2775 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002776 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09002777 }
2778
satok2c93efc2012-04-02 19:33:47 +09002779 private boolean isScreenLocked() {
2780 return mKeyguardManager != null
2781 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
2782 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002783
satokab751aa2010-09-14 19:17:36 +09002784 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002785 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002786
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002787 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09002788 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002789
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002790 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002791 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002792 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002793
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002794 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002795 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09002796 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
2797 mContext);
satok7f35c8c2010-10-07 21:13:11 +09002798 if (immis == null || immis.size() == 0) {
2799 return;
2800 }
2801
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002802 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002803
satok688bd472012-02-09 20:09:17 +09002804 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002805 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
satok688bd472012-02-09 20:09:17 +09002806 showSubtypes, mInputShown, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09002807
satokc3690562012-01-10 20:14:43 +09002808 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002809 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09002810 if (currentSubtype != null) {
2811 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002812 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
2813 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09002814 }
2815 }
2816
Ken Wakasa761eb372011-03-04 19:06:18 +09002817 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09002818 mIms = new InputMethodInfo[N];
2819 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002820 int checkedItem = 0;
2821 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002822 final ImeSubtypeListItem item = imList.get(i);
2823 mIms[i] = item.mImi;
2824 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002825 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002826 int subtypeId = mSubtypeIds[i];
2827 if ((subtypeId == NOT_A_SUBTYPE_ID)
2828 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2829 || (subtypeId == lastInputMethodSubtypeId)) {
2830 checkedItem = i;
2831 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002832 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002833 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08002834
2835 final Context settingsContext = new ContextThemeWrapper(context,
2836 com.android.internal.R.style.Theme_DeviceDefault_Settings);
2837
2838 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002839 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
2840 @Override
2841 public void onCancel(DialogInterface dialog) {
2842 hideInputMethodMenu();
2843 }
2844 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08002845
2846 final Context dialogContext = mDialogBuilder.getContext();
2847 final TypedArray a = dialogContext.obtainStyledAttributes(null,
2848 com.android.internal.R.styleable.DialogPreference,
2849 com.android.internal.R.attr.alertDialogStyle, 0);
2850 final Drawable dialogIcon = a.getDrawable(
2851 com.android.internal.R.styleable.DialogPreference_dialogIcon);
2852 a.recycle();
2853
2854 mDialogBuilder.setIcon(dialogIcon);
2855
2856 final LayoutInflater inflater = (LayoutInflater) dialogContext.getSystemService(
2857 Context.LAYOUT_INFLATER_SERVICE);
satok01038492012-04-09 21:08:27 +09002858 final View tv = inflater.inflate(
2859 com.android.internal.R.layout.input_method_switch_dialog_title, null);
2860 mDialogBuilder.setCustomTitle(tv);
2861
2862 // Setup layout for a toggle switch of the hardware keyboard
2863 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002864 mSwitchingDialogTitleView
2865 .findViewById(com.android.internal.R.id.hard_keyboard_section)
2866 .setVisibility(mWindowManagerService.isHardKeyboardAvailable()
2867 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08002868 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002869 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07002870 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002871 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
2872 @Override
2873 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07002874 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002875 // Ensure that the input method dialog is dismissed when changing
2876 // the hardware keyboard state.
2877 hideInputMethodMenu();
2878 }
2879 });
2880
Alan Viverette505e3ab2014-11-24 15:22:11 -08002881 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002882 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
2883 final OnClickListener choiceListener = new OnClickListener() {
2884 @Override
2885 public void onClick(final DialogInterface dialog, final int which) {
2886 synchronized (mMethodMap) {
2887 if (mIms == null || mIms.length <= which || mSubtypeIds == null
2888 || mSubtypeIds.length <= which) {
2889 return;
satok01038492012-04-09 21:08:27 +09002890 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002891 final InputMethodInfo im = mIms[which];
2892 int subtypeId = mSubtypeIds[which];
2893 adapter.mCheckedItem = which;
2894 adapter.notifyDataSetChanged();
2895 hideInputMethodMenu();
2896 if (im != null) {
2897 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
2898 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08002899 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002900 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002901 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002902 }
2903 }
2904 };
2905 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002906
satokbc81b692011-08-26 16:22:22 +09002907 if (showSubtypes && !isScreenLocked) {
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002908 final OnClickListener positiveListener = new OnClickListener() {
2909 @Override
2910 public void onClick(DialogInterface dialog, int whichButton) {
2911 showConfigureInputMethods();
2912 }
2913 };
satok82beadf2010-12-27 19:03:06 +09002914 mDialogBuilder.setPositiveButton(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002915 com.android.internal.R.string.configure_input_methods, positiveListener);
satok7f35c8c2010-10-07 21:13:11 +09002916 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002917 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002918 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002919 mSwitchingDialog.getWindow().setType(
2920 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09002921 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
2922 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002923 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
Jason Monk807ef762014-05-08 15:47:46 -04002924 updateImeWindowStatusLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002925 mSwitchingDialog.show();
2926 }
2927 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002928
Ken Wakasa05dbb652011-08-22 15:22:43 +09002929 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
2930 private final LayoutInflater mInflater;
2931 private final int mTextViewResourceId;
2932 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09002933 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09002934 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
2935 List<ImeSubtypeListItem> itemsList, int checkedItem) {
2936 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08002937
Ken Wakasa05dbb652011-08-22 15:22:43 +09002938 mTextViewResourceId = textViewResourceId;
2939 mItemsList = itemsList;
2940 mCheckedItem = checkedItem;
Alan Viverette505e3ab2014-11-24 15:22:11 -08002941 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Ken Wakasa05dbb652011-08-22 15:22:43 +09002942 }
2943
2944 @Override
2945 public View getView(int position, View convertView, ViewGroup parent) {
2946 final View view = convertView != null ? convertView
2947 : mInflater.inflate(mTextViewResourceId, null);
2948 if (position < 0 || position >= mItemsList.size()) return view;
2949 final ImeSubtypeListItem item = mItemsList.get(position);
2950 final CharSequence imeName = item.mImeName;
2951 final CharSequence subtypeName = item.mSubtypeName;
2952 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
2953 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
2954 if (TextUtils.isEmpty(subtypeName)) {
2955 firstTextView.setText(imeName);
2956 secondTextView.setVisibility(View.GONE);
2957 } else {
2958 firstTextView.setText(subtypeName);
2959 secondTextView.setText(imeName);
2960 secondTextView.setVisibility(View.VISIBLE);
2961 }
2962 final RadioButton radioButton =
2963 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
2964 radioButton.setChecked(position == mCheckedItem);
2965 return view;
2966 }
2967 }
2968
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002969 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07002970 synchronized (mMethodMap) {
2971 hideInputMethodMenuLocked();
2972 }
2973 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002974
The Android Open Source Project10592532009-03-18 17:39:46 -07002975 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002976 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002977
The Android Open Source Project10592532009-03-18 17:39:46 -07002978 if (mSwitchingDialog != null) {
2979 mSwitchingDialog.dismiss();
2980 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002981 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002982
Jason Monk807ef762014-05-08 15:47:46 -04002983 updateImeWindowStatusLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -07002984 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07002985 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002986 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002987
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002988 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002989
satok42c5a162011-05-26 16:46:14 +09002990 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002991 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002992 // TODO: Make this work even for non-current users?
2993 if (!calledFromValidUser()) {
2994 return false;
2995 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002996 synchronized (mMethodMap) {
2997 if (mContext.checkCallingOrSelfPermission(
2998 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2999 != PackageManager.PERMISSION_GRANTED) {
3000 throw new SecurityException(
3001 "Requires permission "
3002 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3003 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003004
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003005 long ident = Binder.clearCallingIdentity();
3006 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003007 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003008 } finally {
3009 Binder.restoreCallingIdentity(ident);
3010 }
3011 }
3012 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003013
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003014 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3015 // Make sure this is a valid input method.
3016 InputMethodInfo imm = mMethodMap.get(id);
3017 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003018 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003019 }
3020
satokd87c2592010-09-29 11:52:06 +09003021 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3022 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003023
satokd87c2592010-09-29 11:52:06 +09003024 if (enabled) {
3025 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3026 if (pair.first.equals(id)) {
3027 // We are enabling this input method, but it is already enabled.
3028 // Nothing to do. The previous state was enabled.
3029 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003030 }
3031 }
satokd87c2592010-09-29 11:52:06 +09003032 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3033 // Previous state was disabled.
3034 return false;
3035 } else {
3036 StringBuilder builder = new StringBuilder();
3037 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3038 builder, enabledInputMethodsList, id)) {
3039 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003040 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003041 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3042 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3043 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003044 }
3045 // Previous state was enabled.
3046 return true;
3047 } else {
3048 // We are disabling the input method but it is already disabled.
3049 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003050 return false;
3051 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003052 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003053 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003054
satok723a27e2010-11-11 14:58:11 +09003055 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3056 boolean setSubtypeOnly) {
3057 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003058 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003059
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003060 mCurUserActionNotificationSequenceNumber =
3061 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3062 if (DEBUG) {
3063 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3064 + mCurUserActionNotificationSequenceNumber);
3065 }
3066
3067 if (mCurClient != null && mCurClient.client != null) {
3068 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3069 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003070 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003071 }
3072
satok723a27e2010-11-11 14:58:11 +09003073 // Set Subtype here
3074 if (imi == null || subtypeId < 0) {
3075 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003076 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003077 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003078 if (subtypeId < imi.getSubtypeCount()) {
3079 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3080 mSettings.putSelectedSubtype(subtype.hashCode());
3081 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003082 } else {
3083 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003084 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003085 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003086 }
satokab751aa2010-09-14 19:17:36 +09003087 }
satok723a27e2010-11-11 14:58:11 +09003088
satok4c0e7152012-06-20 20:08:44 +09003089 // Workaround.
3090 // ASEC is not ready in the IMMS constructor. Accordingly, forward-locked
3091 // IMEs are not recognized and considered uninstalled.
3092 // Actually, we can't move everything after SystemReady because
3093 // IMMS needs to run in the encryption lock screen. So, we just skip changing
3094 // the default IME here and try cheking the default IME again in systemReady().
3095 // TODO: Do nothing before system ready and implement a separated logic for
3096 // the encryption lock screen.
3097 // TODO: ASEC should be ready before IMMS is instantiated.
3098 if (mSystemReady && !setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003099 // Set InputMethod here
3100 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3101 }
3102 }
3103
3104 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3105 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3106 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3107 // newDefaultIme is empty when there is no candidate for the selected IME.
3108 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3109 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3110 if (subtypeHashCode != null) {
3111 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003112 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok723a27e2010-11-11 14:58:11 +09003113 imi, Integer.valueOf(subtypeHashCode));
3114 } catch (NumberFormatException e) {
3115 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3116 }
3117 }
3118 }
3119 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003120 }
3121
satok4e4569d2010-11-19 18:45:53 +09003122 // If there are no selected shortcuts, tries finding the most applicable ones.
3123 private Pair<InputMethodInfo, InputMethodSubtype>
3124 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3125 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3126 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003127 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003128 boolean foundInSystemIME = false;
3129
3130 // Search applicable subtype for each InputMethodInfo
3131 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003132 final String imiId = imi.getId();
3133 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3134 continue;
3135 }
satokcd7cd292010-11-20 15:46:23 +09003136 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003137 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003138 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003139 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003140 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003141 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003142 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003143 }
satokdf31ae62011-01-15 06:19:44 +09003144 // 2. Search by the system locale from enabledSubtypes.
3145 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003146 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003147 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003148 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003149 }
satoka86f5e42011-09-02 17:12:42 +09003150 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003151 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003152 final ArrayList<InputMethodSubtype> subtypesForSearch =
3153 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003154 ? InputMethodUtils.getSubtypes(imi)
3155 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003156 // 4. Search by the current subtype's locale from all subtypes.
3157 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003158 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003159 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003160 }
3161 // 5. Search by the system locale from all subtypes.
3162 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003163 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003164 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003165 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003166 }
satokcd7cd292010-11-20 15:46:23 +09003167 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003168 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003169 // The current input method is the most applicable IME.
3170 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003171 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003172 break;
satok7599a7f2010-12-22 13:45:23 +09003173 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003174 // The system input method is 2nd applicable IME.
3175 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003176 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003177 if ((imi.getServiceInfo().applicationInfo.flags
3178 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3179 foundInSystemIME = true;
3180 }
satok4e4569d2010-11-19 18:45:53 +09003181 }
3182 }
3183 }
3184 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003185 if (mostApplicableIMI != null) {
3186 Slog.w(TAG, "Most applicable shortcut input method was:"
3187 + mostApplicableIMI.getId());
3188 if (mostApplicableSubtype != null) {
3189 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3190 + "," + mostApplicableSubtype.getMode() + ","
3191 + mostApplicableSubtype.getLocale());
3192 }
3193 }
satok4e4569d2010-11-19 18:45:53 +09003194 }
satokcd7cd292010-11-20 15:46:23 +09003195 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09003196 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09003197 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003198 } else {
3199 return null;
3200 }
3201 }
3202
satokab751aa2010-09-14 19:17:36 +09003203 /**
3204 * @return Return the current subtype of this input method.
3205 */
satok42c5a162011-05-26 16:46:14 +09003206 @Override
satokab751aa2010-09-14 19:17:36 +09003207 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003208 // TODO: Make this work even for non-current users?
3209 if (!calledFromValidUser()) {
3210 return null;
3211 }
3212 synchronized (mMethodMap) {
3213 return getCurrentInputMethodSubtypeLocked();
3214 }
3215 }
3216
3217 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003218 if (mCurMethodId == null) {
3219 return null;
3220 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003221 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003222 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3223 if (imi == null || imi.getSubtypeCount() == 0) {
3224 return null;
satok4e4569d2010-11-19 18:45:53 +09003225 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003226 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003227 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3228 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003229 if (subtypeId == NOT_A_SUBTYPE_ID) {
3230 // If there are no selected subtypes, the framework will try to find
3231 // the most applicable subtype from explicitly or implicitly enabled
3232 // subtypes.
3233 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003234 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003235 // If there is only one explicitly or implicitly enabled subtype,
3236 // just returns it.
3237 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3238 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3239 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003240 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003241 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003242 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003243 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003244 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003245 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3246 true);
satok4e4569d2010-11-19 18:45:53 +09003247 }
satok3ef8b292010-11-23 06:06:29 +09003248 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003249 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003250 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003251 }
3252 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003253 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003254 }
3255
satokf3db1af2010-11-23 13:34:33 +09003256 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
3257 InputMethodSubtype subtype) {
3258 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
3259 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
3260 } else {
3261 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3262 subtypes.add(subtype);
3263 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
3264 }
3265 }
3266
satok4e4569d2010-11-19 18:45:53 +09003267 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003268 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003269 @Override
satok4e4569d2010-11-19 18:45:53 +09003270 public List getShortcutInputMethodsAndSubtypes() {
3271 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09003272 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09003273 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003274 // If there are no selected shortcut subtypes, the framework will try to find
3275 // the most applicable subtype from all subtypes whose mode is
3276 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003277 Pair<InputMethodInfo, InputMethodSubtype> info =
3278 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003279 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003280 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003281 ret.add(info.first);
3282 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003283 }
satok3da92232011-01-11 22:46:30 +09003284 return ret;
satokf3db1af2010-11-23 13:34:33 +09003285 }
satokf3db1af2010-11-23 13:34:33 +09003286 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3287 ret.add(imi);
3288 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3289 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003290 }
3291 }
satokf3db1af2010-11-23 13:34:33 +09003292 return ret;
satok4e4569d2010-11-19 18:45:53 +09003293 }
3294 }
3295
satok42c5a162011-05-26 16:46:14 +09003296 @Override
satokb66d2872010-11-10 01:04:04 +09003297 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003298 // TODO: Make this work even for non-current users?
3299 if (!calledFromValidUser()) {
3300 return false;
3301 }
satokb66d2872010-11-10 01:04:04 +09003302 synchronized (mMethodMap) {
3303 if (subtype != null && mCurMethodId != null) {
3304 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003305 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003306 if (subtypeId != NOT_A_SUBTYPE_ID) {
3307 setInputMethodLocked(mCurMethodId, subtypeId);
3308 return true;
3309 }
3310 }
3311 return false;
3312 }
3313 }
3314
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003315 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003316 private static class InputMethodFileManager {
3317 private static final String SYSTEM_PATH = "system";
3318 private static final String INPUT_METHOD_PATH = "inputmethod";
3319 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3320 private static final String NODE_SUBTYPES = "subtypes";
3321 private static final String NODE_SUBTYPE = "subtype";
3322 private static final String NODE_IMI = "imi";
3323 private static final String ATTR_ID = "id";
3324 private static final String ATTR_LABEL = "label";
3325 private static final String ATTR_ICON = "icon";
3326 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3327 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3328 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3329 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3330 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3331 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003332 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
satoke7c6998e2011-06-03 17:57:59 +09003333 new HashMap<String, List<InputMethodSubtype>>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003334 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003335 if (methodMap == null) {
3336 throw new NullPointerException("methodMap is null");
3337 }
3338 mMethodMap = methodMap;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003339 final File systemDir = userId == UserHandle.USER_OWNER
3340 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3341 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003342 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3343 if (!inputMethodDir.mkdirs()) {
3344 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3345 }
3346 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3347 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3348 if (!subtypeFile.exists()) {
3349 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003350 writeAdditionalInputMethodSubtypes(
3351 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003352 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003353 readAdditionalInputMethodSubtypes(
3354 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003355 }
3356 }
3357
3358 private void deleteAllInputMethodSubtypes(String imiId) {
3359 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003360 mAdditionalSubtypesMap.remove(imiId);
3361 writeAdditionalInputMethodSubtypes(
3362 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003363 }
3364 }
3365
3366 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003367 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003368 synchronized (mMethodMap) {
3369 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3370 final int N = additionalSubtypes.length;
3371 for (int i = 0; i < N; ++i) {
3372 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003373 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003374 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003375 } else {
3376 Slog.w(TAG, "Duplicated subtype definition found: "
3377 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003378 }
3379 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003380 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3381 writeAdditionalInputMethodSubtypes(
3382 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003383 }
3384 }
3385
3386 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3387 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003388 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003389 }
3390 }
3391
3392 private static void writeAdditionalInputMethodSubtypes(
3393 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3394 HashMap<String, InputMethodInfo> methodMap) {
3395 // Safety net for the case that this function is called before methodMap is set.
3396 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3397 FileOutputStream fos = null;
3398 try {
3399 fos = subtypesFile.startWrite();
3400 final XmlSerializer out = new FastXmlSerializer();
3401 out.setOutput(fos, "utf-8");
3402 out.startDocument(null, true);
3403 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3404 out.startTag(null, NODE_SUBTYPES);
3405 for (String imiId : allSubtypes.keySet()) {
3406 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3407 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3408 continue;
3409 }
3410 out.startTag(null, NODE_IMI);
3411 out.attribute(null, ATTR_ID, imiId);
3412 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3413 final int N = subtypesList.size();
3414 for (int i = 0; i < N; ++i) {
3415 final InputMethodSubtype subtype = subtypesList.get(i);
3416 out.startTag(null, NODE_SUBTYPE);
3417 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3418 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3419 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3420 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3421 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3422 out.attribute(null, ATTR_IS_AUXILIARY,
3423 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3424 out.endTag(null, NODE_SUBTYPE);
3425 }
3426 out.endTag(null, NODE_IMI);
3427 }
3428 out.endTag(null, NODE_SUBTYPES);
3429 out.endDocument();
3430 subtypesFile.finishWrite(fos);
3431 } catch (java.io.IOException e) {
3432 Slog.w(TAG, "Error writing subtypes", e);
3433 if (fos != null) {
3434 subtypesFile.failWrite(fos);
3435 }
3436 }
3437 }
3438
3439 private static void readAdditionalInputMethodSubtypes(
3440 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3441 if (allSubtypes == null || subtypesFile == null) return;
3442 allSubtypes.clear();
3443 FileInputStream fis = null;
3444 try {
3445 fis = subtypesFile.openRead();
3446 final XmlPullParser parser = Xml.newPullParser();
3447 parser.setInput(fis, null);
3448 int type = parser.getEventType();
3449 // Skip parsing until START_TAG
3450 while ((type = parser.next()) != XmlPullParser.START_TAG
3451 && type != XmlPullParser.END_DOCUMENT) {}
3452 String firstNodeName = parser.getName();
3453 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3454 throw new XmlPullParserException("Xml doesn't start with subtypes");
3455 }
3456 final int depth =parser.getDepth();
3457 String currentImiId = null;
3458 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3459 while (((type = parser.next()) != XmlPullParser.END_TAG
3460 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3461 if (type != XmlPullParser.START_TAG)
3462 continue;
3463 final String nodeName = parser.getName();
3464 if (NODE_IMI.equals(nodeName)) {
3465 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3466 if (TextUtils.isEmpty(currentImiId)) {
3467 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3468 continue;
3469 }
3470 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
3471 allSubtypes.put(currentImiId, tempSubtypesArray);
3472 } else if (NODE_SUBTYPE.equals(nodeName)) {
3473 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3474 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3475 continue;
3476 }
3477 final int icon = Integer.valueOf(
3478 parser.getAttributeValue(null, ATTR_ICON));
3479 final int label = Integer.valueOf(
3480 parser.getAttributeValue(null, ATTR_LABEL));
3481 final String imeSubtypeLocale =
3482 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3483 final String imeSubtypeMode =
3484 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3485 final String imeSubtypeExtraValue =
3486 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003487 final boolean isAuxiliary = "1".equals(String.valueOf(
3488 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003489 final InputMethodSubtype subtype = new InputMethodSubtypeBuilder()
3490 .setSubtypeNameResId(label)
3491 .setSubtypeIconResId(icon)
3492 .setSubtypeLocale(imeSubtypeLocale)
3493 .setSubtypeMode(imeSubtypeMode)
3494 .setSubtypeExtraValue(imeSubtypeExtraValue)
3495 .setIsAuxiliary(isAuxiliary)
3496 .build();
satoke7c6998e2011-06-03 17:57:59 +09003497 tempSubtypesArray.add(subtype);
3498 }
3499 }
3500 } catch (XmlPullParserException e) {
3501 Slog.w(TAG, "Error reading subtypes: " + e);
3502 return;
3503 } catch (java.io.IOException e) {
3504 Slog.w(TAG, "Error reading subtypes: " + e);
3505 return;
3506 } catch (NumberFormatException e) {
3507 Slog.w(TAG, "Error reading subtypes: " + e);
3508 return;
3509 } finally {
3510 if (fis != null) {
3511 try {
3512 fis.close();
3513 } catch (java.io.IOException e1) {
3514 Slog.w(TAG, "Failed to close.");
3515 }
3516 }
3517 }
3518 }
3519 }
3520
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003521 @Override
3522 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3523 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3524 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003525
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003526 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3527 + Binder.getCallingPid()
3528 + ", uid=" + Binder.getCallingUid());
3529 return;
3530 }
3531
3532 IInputMethod method;
3533 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003534
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003535 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003536
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003537 synchronized (mMethodMap) {
3538 p.println("Current Input Method Manager state:");
3539 int N = mMethodList.size();
3540 p.println(" Input Methods:");
3541 for (int i=0; i<N; i++) {
3542 InputMethodInfo info = mMethodList.get(i);
3543 p.println(" InputMethod #" + i + ":");
3544 info.dump(p, " ");
3545 }
3546 p.println(" Clients:");
3547 for (ClientState ci : mClients.values()) {
3548 p.println(" Client " + ci + ":");
3549 p.println(" client=" + ci.client);
3550 p.println(" inputContext=" + ci.inputContext);
3551 p.println(" sessionRequested=" + ci.sessionRequested);
3552 p.println(" curSession=" + ci.curSession);
3553 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003554 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003555 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003556 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3557 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003558 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3559 + " mBoundToMethod=" + mBoundToMethod);
3560 p.println(" mCurToken=" + mCurToken);
3561 p.println(" mCurIntent=" + mCurIntent);
3562 method = mCurMethod;
3563 p.println(" mCurMethod=" + mCurMethod);
3564 p.println(" mEnabledSession=" + mEnabledSession);
3565 p.println(" mShowRequested=" + mShowRequested
3566 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3567 + " mShowForced=" + mShowForced
3568 + " mInputShown=" + mInputShown);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003569 p.println(" mCurUserActionNotificationSequenceNumber="
3570 + mCurUserActionNotificationSequenceNumber);
Dianne Hackbornd4e6d462014-05-16 16:32:37 -07003571 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003572 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003573
Jeff Brownb88102f2010-09-08 11:49:43 -07003574 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003575 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003576 pw.flush();
3577 try {
3578 client.client.asBinder().dump(fd, args);
3579 } catch (RemoteException e) {
3580 p.println("Input method client dead: " + e);
3581 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003582 } else {
3583 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003584 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003585
Jeff Brownb88102f2010-09-08 11:49:43 -07003586 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003587 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003588 pw.flush();
3589 try {
3590 method.asBinder().dump(fd, args);
3591 } catch (RemoteException e) {
3592 p.println("Input method service dead: " + e);
3593 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003594 } else {
3595 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003596 }
3597 }
3598}