blob: 9ec886cbc3f9aff090362eca83fff08c0e090d07 [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) {
Yohei Yukawa6aa03782015-02-21 03:00:22 +0900767 if (defIm == null && mSystemReady) {
768 final Locale systemLocale = context.getResources().getConfiguration().locale;
769 if (InputMethodUtils.isSystemImeThatHasSubtypeOf(imi, context,
770 true /* checkDefaultAttribute */, systemLocale, false /* checkCountry */,
771 InputMethodUtils.SUBTYPE_MODE_ANY)) {
satok5b927c432012-05-01 20:09:34 +0900772 defIm = imi;
773 Slog.i(TAG, "Selected default: " + imi.getId());
774 }
775 }
776 }
777 if (defIm == null && mMethodList.size() > 0) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900778 defIm = InputMethodUtils.getMostApplicableDefaultIME(
779 mSettings.getEnabledInputMethodListLocked());
Hyejin Kim5baaaac2014-10-27 17:17:06 +0900780 if (defIm != null) {
781 Slog.i(TAG, "Default found, using " + defIm.getId());
782 } else {
783 Slog.i(TAG, "No default found");
784 }
satok5b927c432012-05-01 20:09:34 +0900785 }
786 if (defIm != null) {
787 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
788 }
789 }
790
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900791 private void resetAllInternalStateLocked(final boolean updateOnlyWhenLocaleChanged,
792 final boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900793 if (!mSystemReady) {
794 // not system ready
795 return;
796 }
797 final Locale newLocale = mRes.getConfiguration().locale;
798 if (!updateOnlyWhenLocaleChanged
799 || (newLocale != null && !newLocale.equals(mLastSystemLocale))) {
800 if (!updateOnlyWhenLocaleChanged) {
801 hideCurrentInputLocked(0, null);
802 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -0700803 unbindCurrentMethodLocked(true, false);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900804 }
805 if (DEBUG) {
806 Slog.i(TAG, "Locale has been changed to " + newLocale);
807 }
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900808 buildInputMethodListLocked(mMethodList, mMethodMap, resetDefaultEnabledIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900809 if (!updateOnlyWhenLocaleChanged) {
810 final String selectedImiId = mSettings.getSelectedInputMethod();
811 if (TextUtils.isEmpty(selectedImiId)) {
812 // This is the first time of the user switch and
813 // set the current ime to the proper one.
814 resetDefaultImeLocked(mContext);
815 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +0900816 } else {
817 // If the locale is changed, needs to reset the default ime
818 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900819 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800820 updateFromSettingsLocked(true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900821 mLastSystemLocale = newLocale;
822 if (!updateOnlyWhenLocaleChanged) {
823 try {
824 startInputInnerLocked();
825 } catch (RuntimeException e) {
826 Slog.w(TAG, "Unexpected exception", e);
827 }
828 }
829 }
830 }
831
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900832 private void resetStateIfCurrentLocaleChangedLocked() {
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900833 resetAllInternalStateLocked(true /* updateOnlyWhenLocaleChanged */,
834 true /* resetDefaultImeLocked */);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900835 }
836
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900837 private void switchUserLocked(int newUserId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900838 mSettings.setCurrentUserId(newUserId);
Kenny Guy2a764942014-04-02 13:29:20 +0100839 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900840 // InputMethodFileManager should be reset when the user is changed
841 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900842 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +0900843 // For secondary users, the list of enabled IMEs may not have been updated since the
844 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
845 // not be empty even if the IME has been uninstalled by the primary user.
846 // Even in such cases, IMMS works fine because it will find the most applicable
847 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900848 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900849 if (DEBUG) {
850 Slog.d(TAG, "Switch user: " + newUserId + " current ime = " + defaultImiId);
851 }
852 resetAllInternalStateLocked(false /* updateOnlyWhenLocaleChanged */,
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900853 initialUserSwitch /* needsToResetDefaultIme */);
854 if (initialUserSwitch) {
855 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mContext.getPackageManager(),
856 mSettings.getEnabledInputMethodListLocked());
857 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900858 }
859
Kenny Guy2a764942014-04-02 13:29:20 +0100860 void updateCurrentProfileIds() {
861 List<UserInfo> profiles =
862 UserManager.get(mContext).getProfiles(mSettings.getCurrentUserId());
863 int[] currentProfileIds = new int[profiles.size()]; // profiles will not be null
864 for (int i = 0; i < currentProfileIds.length; i++) {
865 currentProfileIds[i] = profiles.get(i).id;
Amith Yamasani734983f2014-03-04 16:48:05 -0800866 }
Kenny Guy2a764942014-04-02 13:29:20 +0100867 mSettings.setCurrentProfileIds(currentProfileIds);
Amith Yamasani734983f2014-03-04 16:48:05 -0800868 }
869
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870 @Override
871 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
872 throws RemoteException {
873 try {
874 return super.onTransact(code, data, reply, flags);
875 } catch (RuntimeException e) {
876 // The input method manager only throws security exceptions, so let's
877 // log all others.
878 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -0700879 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800880 }
881 throw e;
882 }
883 }
884
Svetoslav Ganova0027152013-06-25 14:59:53 -0700885 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700886 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900887 if (DEBUG) {
888 Slog.d(TAG, "--- systemReady");
889 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700890 if (!mSystemReady) {
891 mSystemReady = true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900892 mKeyguardManager =
893 (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700894 mNotificationManager = (NotificationManager)
895 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
896 mStatusBar = statusBar;
897 statusBar.setIconVisibility("ime", false);
898 updateImeWindowStatusLocked();
satokb858c732011-07-22 19:54:34 +0900899 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
900 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +0900901 if (mShowOngoingImeSwitcherForPhones) {
902 mWindowManagerService.setOnHardKeyboardStatusChangeListener(
903 mHardKeyboardListener);
904 }
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900905 buildInputMethodListLocked(mMethodList, mMethodMap,
906 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satok0a1bcf42012-05-16 19:26:31 +0900907 if (!mImeSelectedOnBoot) {
908 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900909 resetStateIfCurrentLocaleChangedLocked();
910 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(
911 mContext.getPackageManager(),
912 mSettings.getEnabledInputMethodListLocked());
satok0a1bcf42012-05-16 19:26:31 +0900913 }
914 mLastSystemLocale = mRes.getConfiguration().locale;
Dianne Hackborncc278702009-09-02 23:07:23 -0700915 try {
916 startInputInnerLocked();
917 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800918 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700919 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700920 }
921 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800922 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800923
satok15452a42011-10-28 17:58:28 +0900924 private void setImeWindowVisibilityStatusHiddenLocked() {
925 mImeWindowVis = 0;
926 updateImeWindowStatusLocked();
927 }
928
satok3afd6c02011-11-18 08:38:19 +0900929 private void refreshImeWindowVisibilityLocked() {
930 final Configuration conf = mRes.getConfiguration();
931 final boolean haveHardKeyboard = conf.keyboard
932 != Configuration.KEYBOARD_NOKEYS;
933 final boolean hardKeyShown = haveHardKeyboard
934 && conf.hardKeyboardHidden
935 != Configuration.HARDKEYBOARDHIDDEN_YES;
John Spurlocke0980502013-10-25 11:59:29 -0400936
937 final boolean isScreenLocked = isKeyguardLocked();
938 final boolean inputActive = !isScreenLocked && (mInputShown || hardKeyShown);
John Spurlock36439b42013-10-23 16:36:47 -0400939 // We assume the softkeyboard is shown when the input is active as long as the
940 // hard keyboard is not shown.
941 final boolean inputVisible = inputActive && !hardKeyShown;
942 mImeWindowVis = (inputActive ? InputMethodService.IME_ACTIVE : 0)
943 | (inputVisible ? InputMethodService.IME_VISIBLE : 0);
satok3afd6c02011-11-18 08:38:19 +0900944 updateImeWindowStatusLocked();
945 }
946
satok15452a42011-10-28 17:58:28 +0900947 private void updateImeWindowStatusLocked() {
satokdbf29502011-08-25 15:28:23 +0900948 setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700949 }
950
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900951 // ---------------------------------------------------------------------------------------
952 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
953 // 1) it comes from the system process
954 // 2) the calling process' user id is identical to the current user id IMMS thinks.
955 private boolean calledFromValidUser() {
956 final int uid = Binder.getCallingUid();
957 final int userId = UserHandle.getUserId(uid);
958 if (DEBUG) {
959 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
960 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
961 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +0900962 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
963 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900964 }
Kenny Guy2a764942014-04-02 13:29:20 +0100965 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900966 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900967 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +0900968
969 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
970 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
971 // must not manage background users' states in any functions.
972 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
973 // by a token.
974 if (mContext.checkCallingOrSelfPermission(
975 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
976 == PackageManager.PERMISSION_GRANTED) {
977 if (DEBUG) {
978 Slog.d(TAG, "--- Access granted because the calling process has "
979 + "the INTERACT_ACROSS_USERS_FULL permission");
980 }
981 return true;
982 }
Satoshi Kataoka0766eb02013-07-31 18:30:13 +0900983 Slog.w(TAG, "--- IPC called from background users. Ignore. \n"
984 + InputMethodUtils.getStackTrace());
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +0900985 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900986 }
987
Yohei Yukawa22c97be2014-06-04 19:43:36 +0900988
989 /**
990 * Returns true iff the caller is identified to be the current input method with the token.
991 * @param token The window token given to the input method when it was started.
992 * @return true if and only if non-null valid token is specified.
993 */
994 private boolean calledWithValidToken(IBinder token) {
995 if (token == null || mCurToken != token) {
996 return false;
997 }
998 return true;
999 }
1000
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001001 private boolean bindCurrentInputMethodService(
1002 Intent service, ServiceConnection conn, int flags) {
1003 if (service == null || conn == null) {
1004 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
1005 return false;
1006 }
Amith Yamasani27b89e62013-01-16 12:30:11 -08001007 return mContext.bindServiceAsUser(service, conn, flags,
1008 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001009 }
1010
satoke7c6998e2011-06-03 17:57:59 +09001011 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001013 // TODO: Make this work even for non-current users?
1014 if (!calledFromValidUser()) {
1015 return Collections.emptyList();
1016 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001017 synchronized (mMethodMap) {
1018 return new ArrayList<InputMethodInfo>(mMethodList);
1019 }
1020 }
1021
satoke7c6998e2011-06-03 17:57:59 +09001022 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001023 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001024 // TODO: Make this work even for non-current users?
1025 if (!calledFromValidUser()) {
1026 return Collections.emptyList();
1027 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001028 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001029 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001030 }
1031 }
1032
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001033 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001034 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001035 * @return enabled subtypes of the specified imi
1036 */
satoke7c6998e2011-06-03 17:57:59 +09001037 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001038 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001039 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001040 // TODO: Make this work even for non-current users?
1041 if (!calledFromValidUser()) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001042 return Collections.<InputMethodSubtype>emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001043 }
satok67ddf9c2010-11-17 09:45:54 +09001044 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001045 final InputMethodInfo imi;
1046 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001047 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001048 } else {
1049 imi = mMethodMap.get(imiId);
1050 }
1051 if (imi == null) {
1052 return Collections.<InputMethodSubtype>emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001053 }
1054 return mSettings.getEnabledInputMethodSubtypeListLocked(
1055 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001056 }
1057 }
1058
satoke7c6998e2011-06-03 17:57:59 +09001059 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 public void addClient(IInputMethodClient client,
1061 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001062 if (!calledFromValidUser()) {
1063 return;
1064 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001065 synchronized (mMethodMap) {
1066 mClients.put(client.asBinder(), new ClientState(client,
1067 inputContext, uid, pid));
1068 }
1069 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001070
satoke7c6998e2011-06-03 17:57:59 +09001071 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001072 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001073 if (!calledFromValidUser()) {
1074 return;
1075 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001076 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001077 ClientState cs = mClients.remove(client.asBinder());
1078 if (cs != null) {
1079 clearClientSessionLocked(cs);
1080 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001081 }
1082 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001083
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001084 void executeOrSendMessage(IInterface target, Message msg) {
1085 if (target.asBinder() instanceof Binder) {
1086 mCaller.sendMessage(msg);
1087 } else {
1088 handleMessage(msg);
1089 msg.recycle();
1090 }
1091 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001092
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001093 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001095 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001096 + mCurClient.client.asBinder());
1097 if (mBoundToMethod) {
1098 mBoundToMethod = false;
1099 if (mCurMethod != null) {
1100 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1101 MSG_UNBIND_INPUT, mCurMethod));
1102 }
1103 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001104
1105 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1106 MSG_SET_ACTIVE, 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001107 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1108 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1109 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001110 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001111
The Android Open Source Project10592532009-03-18 17:39:46 -07001112 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001113 }
1114 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001116 private int getImeShowFlags() {
1117 int flags = 0;
1118 if (mShowForced) {
1119 flags |= InputMethod.SHOW_FORCED
1120 | InputMethod.SHOW_EXPLICIT;
1121 } else if (mShowExplicitlyRequested) {
1122 flags |= InputMethod.SHOW_EXPLICIT;
1123 }
1124 return flags;
1125 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001126
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001127 private int getAppShowFlags() {
1128 int flags = 0;
1129 if (mShowForced) {
1130 flags |= InputMethodManager.SHOW_FORCED;
1131 } else if (!mShowExplicitlyRequested) {
1132 flags |= InputMethodManager.SHOW_IMPLICIT;
1133 }
1134 return flags;
1135 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001136
Dianne Hackborn7663d802012-02-24 13:08:49 -08001137 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001138 if (!mBoundToMethod) {
1139 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1140 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1141 mBoundToMethod = true;
1142 }
1143 final SessionState session = mCurClient.curSession;
1144 if (initial) {
1145 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1146 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
1147 } else {
1148 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1149 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
1150 }
1151 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001152 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001153 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001154 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001155 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001156 (session.channel != null ? session.channel.dup() : null),
1157 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001158 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160 InputBindResult startInputLocked(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001161 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001162 // If no method is currently selected, do nothing.
1163 if (mCurMethodId == null) {
1164 return mNoBinding;
1165 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001167 ClientState cs = mClients.get(client.asBinder());
1168 if (cs == null) {
1169 throw new IllegalArgumentException("unknown client "
1170 + client.asBinder());
1171 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 try {
1174 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1175 // Check with the window manager to make sure this client actually
1176 // has a window with focus. If not, reject. This is thread safe
1177 // because if the focus changes some time before or after, the
1178 // next client receiving focus that has any interest in input will
1179 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001180 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1182 return null;
1183 }
1184 } catch (RemoteException e) {
1185 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001186
Dianne Hackborn7663d802012-02-24 13:08:49 -08001187 return startInputUncheckedLocked(cs, inputContext, attribute, controlFlags);
1188 }
1189
1190 InputBindResult startInputUncheckedLocked(ClientState cs,
1191 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
1192 // If no method is currently selected, do nothing.
1193 if (mCurMethodId == null) {
1194 return mNoBinding;
1195 }
1196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001198 // Was the keyguard locked when switching over to the new client?
1199 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001200 // If the client is changing, we need to switch over to the new
1201 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001202 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001203 if (DEBUG) Slog.v(TAG, "switching to client: client = "
John Spurlocke0980502013-10-25 11:59:29 -04001204 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205
1206 // If the screen is on, inform the new client it is active
1207 if (mScreenOn) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001208 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
1209 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 }
1211 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 // Bump up the sequence for this client and attach it.
1214 mCurSeq++;
1215 if (mCurSeq <= 0) mCurSeq = 1;
1216 mCurClient = cs;
1217 mCurInputContext = inputContext;
1218 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001220 // Check if the input method is changing.
1221 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1222 if (cs.curSession != null) {
1223 // Fast case: if we are already connected to the input method,
1224 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001225 return attachNewInputLocked(
1226 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 }
1228 if (mHaveConnection) {
1229 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230 // Return to client, and we will get back with it when
1231 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001232 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001233 return new InputBindResult(null, null, mCurId, mCurSeq,
1234 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 } else if (SystemClock.uptimeMillis()
1236 < (mLastBindTime+TIME_TO_RECONNECT)) {
1237 // In this case we have connected to the service, but
1238 // don't yet have its interface. If it hasn't been too
1239 // long since we did the connection, we'll return to
1240 // the client and wait to get the service interface so
1241 // we can report back. If it has been too long, we want
1242 // to fall through so we can try a disconnect/reconnect
1243 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001244 return new InputBindResult(null, null, mCurId, mCurSeq,
1245 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001247 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1248 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001249 }
1250 }
1251 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001252
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001253 return startInputInnerLocked();
1254 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001255
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001256 InputBindResult startInputInnerLocked() {
1257 if (mCurMethodId == null) {
1258 return mNoBinding;
1259 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001260
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001261 if (!mSystemReady) {
1262 // If the system is not yet ready, we shouldn't be running third
1263 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001264 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1265 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001266 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001267
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001268 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1269 if (info == null) {
1270 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1271 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001272
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001273 unbindCurrentMethodLocked(false, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001274
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001275 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1276 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001277 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1278 com.android.internal.R.string.input_method_binding_label);
1279 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1280 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001281 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001282 | Context.BIND_NOT_VISIBLE | Context.BIND_NOT_FOREGROUND
1283 | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 mLastBindTime = SystemClock.uptimeMillis();
1285 mHaveConnection = true;
1286 mCurId = info.getId();
1287 mCurToken = new Binder();
1288 try {
Craig Mautnerca0ac712013-03-14 09:43:02 -07001289 if (true || DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001290 mIWindowManager.addWindowToken(mCurToken,
1291 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1292 } catch (RemoteException e) {
1293 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001294 return new InputBindResult(null, null, mCurId, mCurSeq,
1295 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001296 } else {
1297 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001298 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001299 + mCurIntent);
1300 }
1301 return null;
1302 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001303
satoke7c6998e2011-06-03 17:57:59 +09001304 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001305 public InputBindResult startInput(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001306 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001307 if (!calledFromValidUser()) {
1308 return null;
1309 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310 synchronized (mMethodMap) {
1311 final long ident = Binder.clearCallingIdentity();
1312 try {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001313 return startInputLocked(client, inputContext, attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001314 } finally {
1315 Binder.restoreCallingIdentity(ident);
1316 }
1317 }
1318 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001319
satoke7c6998e2011-06-03 17:57:59 +09001320 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001321 public void finishInput(IInputMethodClient client) {
1322 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001323
satoke7c6998e2011-06-03 17:57:59 +09001324 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001325 public void onServiceConnected(ComponentName name, IBinder service) {
1326 synchronized (mMethodMap) {
1327 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1328 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001329 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001330 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001331 unbindCurrentMethodLocked(false, false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001332 return;
1333 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001334 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001335 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1336 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001337 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001338 clearClientSessionLocked(mCurClient);
1339 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001340 }
1341 }
1342 }
1343 }
1344
Jeff Brownc28867a2013-03-26 15:42:39 -07001345 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1346 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001347 synchronized (mMethodMap) {
1348 if (mCurMethod != null && method != null
1349 && mCurMethod.asBinder() == method.asBinder()) {
1350 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001351 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001352 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001353 method, session, channel);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001354 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001355 if (res.method != null) {
1356 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
1357 MSG_BIND_METHOD, mCurClient.client, res));
1358 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001359 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001360 }
1361 }
1362 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001363
1364 // Session abandoned. Close its associated input channel.
1365 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001366 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001367
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001368 void unbindCurrentMethodLocked(boolean reportToClient, boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001369 if (mVisibleBound) {
1370 mContext.unbindService(mVisibleConnection);
1371 mVisibleBound = false;
1372 }
1373
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001374 if (mHaveConnection) {
1375 mContext.unbindService(this);
1376 mHaveConnection = false;
1377 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001378
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001379 if (mCurToken != null) {
1380 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001381 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001382 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001383 // The current IME is shown. Hence an IME switch (transition) is happening.
1384 mWindowManagerService.saveLastInputMethodWindowForTransition();
1385 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001386 mIWindowManager.removeWindowToken(mCurToken);
1387 } catch (RemoteException e) {
1388 }
1389 mCurToken = null;
1390 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001391
The Android Open Source Project10592532009-03-18 17:39:46 -07001392 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001393 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001394
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001395 if (reportToClient && mCurClient != null) {
1396 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1397 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1398 }
1399 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001400
1401 void requestClientSessionLocked(ClientState cs) {
1402 if (!cs.sessionRequested) {
1403 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1404 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1405 cs.sessionRequested = true;
1406 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1407 MSG_CREATE_SESSION, mCurMethod, channels[1],
1408 new MethodCallback(this, mCurMethod, channels[0])));
1409 }
1410 }
1411
1412 void clearClientSessionLocked(ClientState cs) {
1413 finishSessionLocked(cs.curSession);
1414 cs.curSession = null;
1415 cs.sessionRequested = false;
1416 }
1417
1418 private void finishSessionLocked(SessionState sessionState) {
1419 if (sessionState != null) {
1420 if (sessionState.session != null) {
1421 try {
1422 sessionState.session.finishSession();
1423 } catch (RemoteException e) {
1424 Slog.w(TAG, "Session failed to close due to remote exception", e);
1425 setImeWindowVisibilityStatusHiddenLocked();
1426 }
1427 sessionState.session = null;
1428 }
1429 if (sessionState.channel != null) {
1430 sessionState.channel.dispose();
1431 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001432 }
1433 }
1434 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001435
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001436 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001437 if (mCurMethod != null) {
1438 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001439 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001440 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001441
Jeff Brownc28867a2013-03-26 15:42:39 -07001442 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001443 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001444 mCurMethod = null;
1445 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001446 if (mStatusBar != null) {
1447 mStatusBar.setIconVisibility("ime", false);
1448 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001449 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001450
satoke7c6998e2011-06-03 17:57:59 +09001451 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001452 public void onServiceDisconnected(ComponentName name) {
1453 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001454 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001455 + " mCurIntent=" + mCurIntent);
1456 if (mCurMethod != null && mCurIntent != null
1457 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001458 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001459 // We consider this to be a new bind attempt, since the system
1460 // should now try to restart the service for us.
1461 mLastBindTime = SystemClock.uptimeMillis();
1462 mShowRequested = mInputShown;
1463 mInputShown = false;
1464 if (mCurClient != null) {
1465 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1466 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1467 }
1468 }
1469 }
1470 }
1471
satokf9f01002011-05-19 21:31:50 +09001472 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001473 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
1474 long ident = Binder.clearCallingIdentity();
1475 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001476 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09001477 if (!calledWithValidToken(token)) {
1478 final int uid = Binder.getCallingUid();
1479 Slog.e(TAG, "Ignoring updateStatusIcon due to an invalid token. uid:" + uid
1480 + " token:" + token);
1481 return;
1482 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001483 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001484 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001485 if (mStatusBar != null) {
1486 mStatusBar.setIconVisibility("ime", false);
1487 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001488 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001489 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001490 CharSequence contentDescription = null;
1491 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001492 // Use PackageManager to load label
1493 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001494 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001495 mIPackageManager.getApplicationInfo(packageName, 0,
1496 mSettings.getCurrentUserId()));
1497 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001498 /* ignore */
1499 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001500 if (mStatusBar != null) {
1501 mStatusBar.setIcon("ime", packageName, iconId, 0,
1502 contentDescription != null
1503 ? contentDescription.toString() : null);
1504 mStatusBar.setIconVisibility("ime", true);
1505 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001506 }
1507 }
1508 } finally {
1509 Binder.restoreCallingIdentity(ident);
1510 }
1511 }
1512
satok7cfc0ed2011-06-20 21:29:36 +09001513 private boolean needsToShowImeSwitchOngoingNotification() {
1514 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04001515 if (mSwitchingDialog != null) return false;
satok2c93efc2012-04-02 19:33:47 +09001516 if (isScreenLocked()) return false;
satok7cfc0ed2011-06-20 21:29:36 +09001517 synchronized (mMethodMap) {
1518 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1519 final int N = imis.size();
satokb6359412011-06-28 17:47:41 +09001520 if (N > 2) return true;
1521 if (N < 1) return false;
1522 int nonAuxCount = 0;
1523 int auxCount = 0;
1524 InputMethodSubtype nonAuxSubtype = null;
1525 InputMethodSubtype auxSubtype = null;
satok7cfc0ed2011-06-20 21:29:36 +09001526 for(int i = 0; i < N; ++i) {
1527 final InputMethodInfo imi = imis.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001528 final List<InputMethodSubtype> subtypes =
1529 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satok7cfc0ed2011-06-20 21:29:36 +09001530 final int subtypeCount = subtypes.size();
1531 if (subtypeCount == 0) {
satokb6359412011-06-28 17:47:41 +09001532 ++nonAuxCount;
satok7cfc0ed2011-06-20 21:29:36 +09001533 } else {
1534 for (int j = 0; j < subtypeCount; ++j) {
satokb6359412011-06-28 17:47:41 +09001535 final InputMethodSubtype subtype = subtypes.get(j);
1536 if (!subtype.isAuxiliary()) {
1537 ++nonAuxCount;
1538 nonAuxSubtype = subtype;
1539 } else {
1540 ++auxCount;
1541 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001542 }
1543 }
1544 }
satok7cfc0ed2011-06-20 21:29:36 +09001545 }
satokb6359412011-06-28 17:47:41 +09001546 if (nonAuxCount > 1 || auxCount > 1) {
1547 return true;
1548 } else if (nonAuxCount == 1 && auxCount == 1) {
1549 if (nonAuxSubtype != null && auxSubtype != null
satok9747f892011-09-12 15:56:40 +09001550 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1551 || auxSubtype.overridesImplicitlyEnabledSubtype()
1552 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
satokb6359412011-06-28 17:47:41 +09001553 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1554 return false;
1555 }
1556 return true;
1557 }
1558 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001559 }
satok7cfc0ed2011-06-20 21:29:36 +09001560 }
1561
John Spurlocke0980502013-10-25 11:59:29 -04001562 private boolean isKeyguardLocked() {
1563 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1564 }
1565
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001566 // Caution! This method is called in this class. Handle multi-user carefully
satokdbf29502011-08-25 15:28:23 +09001567 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001568 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001569 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001570 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001571 try {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001572 if (!calledWithValidToken(token)) {
1573 final int uid = Binder.getCallingUid();
1574 Slog.e(TAG, "Ignoring setImeWindowStatus due to an invalid token. uid:" + uid
1575 + " token:" + token);
satok06487a52010-10-29 11:37:18 +09001576 return;
1577 }
satok06487a52010-10-29 11:37:18 +09001578 synchronized (mMethodMap) {
John Spurlocke0980502013-10-25 11:59:29 -04001579 // apply policy for binder calls
1580 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
1581 vis = 0;
1582 }
Joe Onorato857fd9b2011-01-27 15:08:35 -08001583 mImeWindowVis = vis;
1584 mBackDisposition = backDisposition;
jungheang.lee217fd292013-02-26 16:53:22 +09001585 final boolean iconVisibility = ((vis & (InputMethodService.IME_ACTIVE)) != 0)
1586 && (mWindowManagerService.isHardKeyboardAvailable()
1587 || (vis & (InputMethodService.IME_VISIBLE)) != 0);
Jason Monkb605fec2014-05-02 17:04:10 -04001588 final boolean needsToShowImeSwitcher = iconVisibility
1589 && needsToShowImeSwitchOngoingNotification();
1590 if (mStatusBar != null) {
1591 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
1592 needsToShowImeSwitcher);
1593 }
satok5bc8e732011-07-22 21:07:23 +09001594 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Jason Monkb605fec2014-05-02 17:04:10 -04001595 if (imi != null && needsToShowImeSwitcher) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001596 // Used to load label
satok7cfc0ed2011-06-20 21:29:36 +09001597 final CharSequence title = mRes.getText(
1598 com.android.internal.R.string.select_input_method);
Satoshi Kataokab2827262013-07-04 19:43:14 +09001599 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
1600 mContext, imi, mCurrentSubtype);
satok5bc8e732011-07-22 21:07:23 +09001601
Selim Cinek255dd042014-08-19 22:29:02 +02001602 mImeSwitcherNotification.color = mContext.getResources().getColor(
1603 com.android.internal.R.color.system_notification_accent_color);
satok7cfc0ed2011-06-20 21:29:36 +09001604 mImeSwitcherNotification.setLatestEventInfo(
satok5bc8e732011-07-22 21:07:23 +09001605 mContext, title, summary, mImeSwitchPendingIntent);
Jason Monke463c742014-05-02 12:31:45 -04001606 if ((mNotificationManager != null)
1607 && !mWindowManagerService.hasNavigationBar()) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001608 if (DEBUG) {
Satoshi Kataokab2827262013-07-04 19:43:14 +09001609 Slog.d(TAG, "--- show notification: label = " + summary);
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001610 }
1611 mNotificationManager.notifyAsUser(null,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001612 com.android.internal.R.string.select_input_method,
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001613 mImeSwitcherNotification, UserHandle.ALL);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001614 mNotificationShown = true;
1615 }
satok7cfc0ed2011-06-20 21:29:36 +09001616 } else {
Dianne Hackborn661cd522011-08-22 00:26:20 -07001617 if (mNotificationShown && mNotificationManager != null) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001618 if (DEBUG) {
1619 Slog.d(TAG, "--- hide notification");
1620 }
1621 mNotificationManager.cancelAsUser(null,
1622 com.android.internal.R.string.select_input_method, UserHandle.ALL);
satok7cfc0ed2011-06-20 21:29:36 +09001623 mNotificationShown = false;
1624 }
1625 }
satok06487a52010-10-29 11:37:18 +09001626 }
1627 } finally {
1628 Binder.restoreCallingIdentity(ident);
1629 }
1630 }
1631
satoke7c6998e2011-06-03 17:57:59 +09001632 @Override
satokf9f01002011-05-19 21:31:50 +09001633 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001634 if (!calledFromValidUser()) {
1635 return;
1636 }
satokf9f01002011-05-19 21:31:50 +09001637 synchronized (mMethodMap) {
1638 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1639 for (int i = 0; i < spans.length; ++i) {
1640 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001641 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001642 mSecureSuggestionSpans.put(ss, currentImi);
1643 }
1644 }
1645 }
1646 }
1647
satoke7c6998e2011-06-03 17:57:59 +09001648 @Override
satokf9f01002011-05-19 21:31:50 +09001649 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001650 if (!calledFromValidUser()) {
1651 return false;
1652 }
satokf9f01002011-05-19 21:31:50 +09001653 synchronized (mMethodMap) {
1654 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1655 // TODO: Do not send the intent if the process of the targetImi is already dead.
1656 if (targetImi != null) {
1657 final String[] suggestions = span.getSuggestions();
1658 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001659 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001660 final Intent intent = new Intent();
1661 // Ensures that only a class in the original IME package will receive the
1662 // notification.
satok42c5a162011-05-26 16:46:14 +09001663 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001664 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1665 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1666 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1667 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001668 final long ident = Binder.clearCallingIdentity();
1669 try {
1670 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1671 } finally {
1672 Binder.restoreCallingIdentity(ident);
1673 }
satokf9f01002011-05-19 21:31:50 +09001674 return true;
1675 }
1676 }
1677 return false;
1678 }
1679
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001680 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001681 updateInputMethodsFromSettingsLocked(enabledMayChange);
1682 updateKeyboardFromSettingsLocked();
1683 }
1684
1685 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001686 if (enabledMayChange) {
1687 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1688 for (int i=0; i<enabled.size(); i++) {
1689 // We allow the user to select "disabled until used" apps, so if they
1690 // are enabling one of those here we now need to make it enabled.
1691 InputMethodInfo imm = enabled.get(i);
1692 try {
1693 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1694 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1695 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09001696 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001697 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001698 if (DEBUG) {
1699 Slog.d(TAG, "Update state(" + imm.getId()
1700 + "): DISABLED_UNTIL_USED -> DEFAULT");
1701 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001702 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1703 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001704 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
1705 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001706 }
1707 } catch (RemoteException e) {
1708 }
1709 }
1710 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001711 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1712 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1713 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1714 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001715 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001716 // There is no input method selected, try to choose new applicable input method.
1717 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001718 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001719 }
1720 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001721 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001722 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001723 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001724 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001725 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001726 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001727 }
satokf3db1af2010-11-23 13:34:33 +09001728 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001729 } else {
1730 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001731 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001732 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001733 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09001734 // Here is not the perfect place to reset the switching controller. Ideally
1735 // mSwitchingController and mSettings should be able to share the same state.
1736 // TODO: Make sure that mSwitchingController and mSettings are sharing the
1737 // the same enabled IMEs list.
1738 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07001739
1740 }
1741
1742 public void updateKeyboardFromSettingsLocked() {
1743 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
1744 if (mSwitchingDialog != null
1745 && mSwitchingDialogTitleView != null
1746 && mSwitchingDialog.isShowing()) {
1747 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
1748 com.android.internal.R.id.hard_keyboard_switch);
1749 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
1750 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001751 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001752
satokab751aa2010-09-14 19:17:36 +09001753 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001754 InputMethodInfo info = mMethodMap.get(id);
1755 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001756 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001757 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001758
Yohei Yukawae63b5fa2014-09-19 13:14:55 +09001759 if (mCurClient != null && mCurAttribute != null) {
1760 final int uid = mCurClient.uid;
1761 final String packageName = mCurAttribute.packageName;
1762 if (SystemConfig.getInstance().getFixedImeApps().contains(packageName)) {
1763 if (InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, uid, packageName)) {
1764 return;
1765 }
1766 // TODO: Do we need to lock the input method when the application reported an
1767 // incorrect package name?
1768 Slog.e(TAG, "Ignoring FixedImeApps due to the validation failure. uid=" + uid
1769 + " package=" + packageName);
1770 }
1771 }
1772
satokd81e9502012-05-21 12:58:45 +09001773 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001774 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09001775 final int subtypeCount = info.getSubtypeCount();
1776 if (subtypeCount <= 0) {
1777 return;
satokcd7cd292010-11-20 15:46:23 +09001778 }
satokd81e9502012-05-21 12:58:45 +09001779 final InputMethodSubtype oldSubtype = mCurrentSubtype;
1780 final InputMethodSubtype newSubtype;
1781 if (subtypeId >= 0 && subtypeId < subtypeCount) {
1782 newSubtype = info.getSubtypeAt(subtypeId);
1783 } else {
1784 // If subtype is null, try to find the most applicable one from
1785 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001786 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09001787 }
1788 if (newSubtype == null || oldSubtype == null) {
1789 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
1790 + ", new subtype = " + newSubtype);
1791 return;
1792 }
1793 if (newSubtype != oldSubtype) {
1794 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1795 if (mCurMethod != null) {
1796 try {
1797 refreshImeWindowVisibilityLocked();
1798 mCurMethod.changeInputMethodSubtype(newSubtype);
1799 } catch (RemoteException e) {
1800 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
satokab751aa2010-09-14 19:17:36 +09001801 }
1802 }
1803 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001804 return;
1805 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001806
satokd81e9502012-05-21 12:58:45 +09001807 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001808 final long ident = Binder.clearCallingIdentity();
1809 try {
satokab751aa2010-09-14 19:17:36 +09001810 // Set a subtype to this input method.
1811 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001812 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1813 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1814 // because mCurMethodId is stored as a history in
1815 // setSelectedInputMethodAndSubtypeLocked().
1816 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001817
1818 if (ActivityManagerNative.isSystemReady()) {
1819 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001820 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001821 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07001822 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001823 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001824 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001825 } finally {
1826 Binder.restoreCallingIdentity(ident);
1827 }
1828 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001829
satok42c5a162011-05-26 16:46:14 +09001830 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001831 public boolean showSoftInput(IInputMethodClient client, int flags,
1832 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001833 if (!calledFromValidUser()) {
1834 return false;
1835 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001836 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001837 long ident = Binder.clearCallingIdentity();
1838 try {
1839 synchronized (mMethodMap) {
1840 if (mCurClient == null || client == null
1841 || mCurClient.client.asBinder() != client.asBinder()) {
1842 try {
1843 // We need to check if this is the current client with
1844 // focus in the window manager, to allow this call to
1845 // be made before input is started in it.
1846 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001847 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001848 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001849 }
1850 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001851 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001852 }
1853 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001854
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001855 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001856 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001857 }
1858 } finally {
1859 Binder.restoreCallingIdentity(ident);
1860 }
1861 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001862
The Android Open Source Project4df24232009-03-05 14:34:35 -08001863 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001864 mShowRequested = true;
1865 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1866 mShowExplicitlyRequested = true;
1867 }
1868 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1869 mShowExplicitlyRequested = true;
1870 mShowForced = true;
1871 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001872
Dianne Hackborncc278702009-09-02 23:07:23 -07001873 if (!mSystemReady) {
1874 return false;
1875 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001876
The Android Open Source Project4df24232009-03-05 14:34:35 -08001877 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001878 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001879 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001880 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1881 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1882 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001883 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001884 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001885 bindCurrentInputMethodService(
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001886 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE
1887 | Context.BIND_TREAT_LIKE_ACTIVITY);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001888 mVisibleBound = true;
1889 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001890 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001891 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09001892 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001893 // The client has asked to have the input method shown, but
1894 // we have been sitting here too long with a connection to the
1895 // service and no interface received, so let's disconnect/connect
1896 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001897 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001898 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09001899 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001900 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001901 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001902 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001903 } else {
1904 if (DEBUG) {
1905 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
1906 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
1907 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001908 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001909
The Android Open Source Project4df24232009-03-05 14:34:35 -08001910 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001911 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001912
satok42c5a162011-05-26 16:46:14 +09001913 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001914 public boolean hideSoftInput(IInputMethodClient client, int flags,
1915 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001916 if (!calledFromValidUser()) {
1917 return false;
1918 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001919 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001920 long ident = Binder.clearCallingIdentity();
1921 try {
1922 synchronized (mMethodMap) {
1923 if (mCurClient == null || client == null
1924 || mCurClient.client.asBinder() != client.asBinder()) {
1925 try {
1926 // We need to check if this is the current client with
1927 // focus in the window manager, to allow this call to
1928 // be made before input is started in it.
1929 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001930 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1931 + uid + ": " + client);
satok15452a42011-10-28 17:58:28 +09001932 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001933 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001934 }
1935 } catch (RemoteException e) {
satok15452a42011-10-28 17:58:28 +09001936 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001937 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001938 }
1939 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001940
Joe Onorato8a9b2202010-02-26 18:56:32 -08001941 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001942 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001943 }
1944 } finally {
1945 Binder.restoreCallingIdentity(ident);
1946 }
1947 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001948
The Android Open Source Project4df24232009-03-05 14:34:35 -08001949 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001950 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1951 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001952 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 -08001953 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001954 }
1955 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001956 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 -08001957 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001958 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001959 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001960 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001961 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1962 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1963 res = true;
1964 } else {
1965 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001966 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001967 if (mHaveConnection && mVisibleBound) {
1968 mContext.unbindService(mVisibleConnection);
1969 mVisibleBound = false;
1970 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001971 mInputShown = false;
1972 mShowRequested = false;
1973 mShowExplicitlyRequested = false;
1974 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001975 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001976 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001977
satok42c5a162011-05-26 16:46:14 +09001978 @Override
Dianne Hackborn7663d802012-02-24 13:08:49 -08001979 public InputBindResult windowGainedFocus(IInputMethodClient client, IBinder windowToken,
1980 int controlFlags, int softInputMode, int windowFlags,
1981 EditorInfo attribute, IInputContext inputContext) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09001982 // Needs to check the validity before clearing calling identity
1983 final boolean calledFromValidUser = calledFromValidUser();
1984
Dianne Hackborn7663d802012-02-24 13:08:49 -08001985 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001986 long ident = Binder.clearCallingIdentity();
1987 try {
1988 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001989 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
Dianne Hackborn7663d802012-02-24 13:08:49 -08001990 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001991 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08001992 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001993
Dianne Hackborn7663d802012-02-24 13:08:49 -08001994 ClientState cs = mClients.get(client.asBinder());
1995 if (cs == null) {
1996 throw new IllegalArgumentException("unknown client "
1997 + client.asBinder());
1998 }
1999
2000 try {
2001 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2002 // Check with the window manager to make sure this client actually
2003 // has a window with focus. If not, reject. This is thread safe
2004 // because if the focus changes some time before or after, the
2005 // next client receiving focus that has any interest in input will
2006 // be calling through here after that change happens.
2007 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2008 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2009 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002010 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002011 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002012 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002013
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002014 if (!calledFromValidUser) {
2015 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2016 Slog.w(TAG, "If you want to interect with IME, you need "
2017 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2018 hideCurrentInputLocked(0, null);
2019 return null;
2020 }
2021
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002022 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07002023 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09002024 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002025 if (attribute != null) {
2026 return startInputUncheckedLocked(cs, inputContext, attribute,
2027 controlFlags);
2028 }
2029 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002030 }
2031 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002032
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002033 // Should we auto-show the IME even if the caller has not
2034 // specified what should be done with it?
2035 // We only do this automatically if the window can resize
2036 // to accommodate the IME (so what the user sees will give
2037 // them good context without input information being obscured
2038 // by the IME) or if running on a large screen where there
2039 // is more room for the target window + IME.
2040 final boolean doAutoShow =
2041 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2042 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2043 || mRes.getConfiguration().isLayoutSizeAtLeast(
2044 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002045 final boolean isTextEditor =
2046 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2047
2048 // We want to start input before showing the IME, but after closing
2049 // it. We want to do this after closing it to help the IME disappear
2050 // more quickly (not get stuck behind it initializing itself for the
2051 // new focused input, even if its window wants to hide the IME).
2052 boolean didStart = false;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002053
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002054 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2055 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002056 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002057 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2058 // There is no focus view, and this window will
2059 // be behind any soft input window, so hide the
2060 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002061 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002062 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002063 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002064 } else if (isTextEditor && doAutoShow && (softInputMode &
2065 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002066 // There is a focus view, and we are navigating forward
2067 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002068 // We only do this automatically if the window can resize
2069 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002070 // them good context without input information being obscured
2071 // by the IME) or if running on a large screen where there
2072 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002073 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002074 if (attribute != null) {
2075 res = startInputUncheckedLocked(cs, inputContext, attribute,
2076 controlFlags);
2077 didStart = true;
2078 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002079 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002080 }
2081 break;
2082 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2083 // Do nothing.
2084 break;
2085 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2086 if ((softInputMode &
2087 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002088 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002089 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002090 }
2091 break;
2092 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002093 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002094 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002095 break;
2096 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2097 if ((softInputMode &
2098 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002099 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002100 if (attribute != null) {
2101 res = startInputUncheckedLocked(cs, inputContext, attribute,
2102 controlFlags);
2103 didStart = true;
2104 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002105 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002106 }
2107 break;
2108 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002109 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002110 if (attribute != null) {
2111 res = startInputUncheckedLocked(cs, inputContext, attribute,
2112 controlFlags);
2113 didStart = true;
2114 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002115 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002116 break;
2117 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002118
2119 if (!didStart && attribute != null) {
2120 res = startInputUncheckedLocked(cs, inputContext, attribute,
2121 controlFlags);
2122 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002123 }
2124 } finally {
2125 Binder.restoreCallingIdentity(ident);
2126 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002127
2128 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002129 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002130
satok42c5a162011-05-26 16:46:14 +09002131 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002132 public void showInputMethodPickerFromClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002133 if (!calledFromValidUser()) {
2134 return;
2135 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002136 synchronized (mMethodMap) {
2137 if (mCurClient == null || client == null
2138 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002139 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002140 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002141 }
2142
satok440aab52010-11-25 09:43:11 +09002143 // Always call subtype picker, because subtype picker is a superset of input method
2144 // picker.
satokab751aa2010-09-14 19:17:36 +09002145 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
2146 }
2147 }
2148
satok42c5a162011-05-26 16:46:14 +09002149 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002150 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002151 if (!calledFromValidUser()) {
2152 return;
2153 }
satok28203512010-11-24 11:06:49 +09002154 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2155 }
2156
satok42c5a162011-05-26 16:46:14 +09002157 @Override
satok28203512010-11-24 11:06:49 +09002158 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002159 if (!calledFromValidUser()) {
2160 return;
2161 }
satok28203512010-11-24 11:06:49 +09002162 synchronized (mMethodMap) {
2163 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002164 setInputMethodWithSubtypeIdLocked(token, id,
2165 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2166 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002167 } else {
2168 setInputMethod(token, id);
2169 }
2170 }
satokab751aa2010-09-14 19:17:36 +09002171 }
2172
satok42c5a162011-05-26 16:46:14 +09002173 @Override
satokb416a712010-11-25 20:42:14 +09002174 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002175 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002176 if (!calledFromValidUser()) {
2177 return;
2178 }
satokb416a712010-11-25 20:42:14 +09002179 synchronized (mMethodMap) {
2180 if (mCurClient == null || client == null
2181 || mCurClient.client.asBinder() != client.asBinder()) {
2182 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
2183 }
satok7fee71f2010-12-17 18:54:26 +09002184 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2185 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002186 }
2187 }
2188
satok4fc87d62011-05-20 16:13:43 +09002189 @Override
satok735cf382010-11-11 20:40:09 +09002190 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002191 if (!calledFromValidUser()) {
2192 return false;
2193 }
satok735cf382010-11-11 20:40:09 +09002194 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002195 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002196 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002197 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002198 lastImi = mMethodMap.get(lastIme.first);
2199 } else {
2200 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002201 }
satok4fc87d62011-05-20 16:13:43 +09002202 String targetLastImiId = null;
2203 int subtypeId = NOT_A_SUBTYPE_ID;
2204 if (lastIme != null && lastImi != null) {
2205 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
2206 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
2207 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2208 : mCurrentSubtype.hashCode();
2209 // If the last IME is the same as the current IME and the last subtype is not
2210 // defined, there is no need to switch to the last IME.
2211 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2212 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002213 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002214 }
2215 }
2216
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002217 if (TextUtils.isEmpty(targetLastImiId)
2218 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002219 // This is a safety net. If the currentSubtype can't be added to the history
2220 // and the framework couldn't find the last ime, we will make the last ime be
2221 // the most applicable enabled keyboard subtype of the system imes.
2222 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2223 if (enabled != null) {
2224 final int N = enabled.size();
2225 final String locale = mCurrentSubtype == null
2226 ? mRes.getConfiguration().locale.toString()
2227 : mCurrentSubtype.getLocale();
2228 for (int i = 0; i < N; ++i) {
2229 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002230 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002231 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002232 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2233 InputMethodUtils.getSubtypes(imi),
2234 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002235 if (keyboardSubtype != null) {
2236 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002237 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002238 imi, keyboardSubtype.hashCode());
2239 if(keyboardSubtype.getLocale().equals(locale)) {
2240 break;
2241 }
2242 }
2243 }
2244 }
2245 }
2246 }
2247
2248 if (!TextUtils.isEmpty(targetLastImiId)) {
2249 if (DEBUG) {
2250 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2251 + ", from: " + mCurMethodId + ", " + subtypeId);
2252 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002253 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002254 return true;
2255 } else {
2256 return false;
2257 }
satok735cf382010-11-11 20:40:09 +09002258 }
2259 }
2260
satoke7c6998e2011-06-03 17:57:59 +09002261 @Override
satok688bd472012-02-09 20:09:17 +09002262 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002263 if (!calledFromValidUser()) {
2264 return false;
2265 }
satok688bd472012-02-09 20:09:17 +09002266 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002267 if (!calledWithValidToken(token)) {
2268 final int uid = Binder.getCallingUid();
2269 Slog.e(TAG, "Ignoring switchToNextInputMethod due to an invalid token. uid:" + uid
2270 + " token:" + token);
2271 return false;
2272 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002273 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
satok688bd472012-02-09 20:09:17 +09002274 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2275 if (nextSubtype == null) {
2276 return false;
2277 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002278 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2279 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002280 return true;
2281 }
2282 }
2283
2284 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002285 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2286 if (!calledFromValidUser()) {
2287 return false;
2288 }
2289 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002290 if (!calledWithValidToken(token)) {
2291 final int uid = Binder.getCallingUid();
2292 Slog.e(TAG, "Ignoring shouldOfferSwitchingToNextInputMethod due to an invalid "
2293 + "token. uid:" + uid + " token:" + token);
2294 return false;
2295 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002296 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002297 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2298 if (nextSubtype == null) {
2299 return false;
2300 }
2301 return true;
2302 }
2303 }
2304
2305 @Override
satok68f1b782011-04-11 14:26:04 +09002306 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002307 if (!calledFromValidUser()) {
2308 return null;
2309 }
satok68f1b782011-04-11 14:26:04 +09002310 synchronized (mMethodMap) {
2311 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2312 // TODO: Handle the case of the last IME with no subtypes
2313 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2314 || TextUtils.isEmpty(lastIme.second)) return null;
2315 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2316 if (lastImi == null) return null;
2317 try {
2318 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002319 final int lastSubtypeId =
2320 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002321 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2322 return null;
2323 }
2324 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002325 } catch (NumberFormatException e) {
2326 return null;
2327 }
2328 }
2329 }
2330
satoke7c6998e2011-06-03 17:57:59 +09002331 @Override
satokee5e77c2011-09-02 18:50:15 +09002332 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002333 if (!calledFromValidUser()) {
2334 return;
2335 }
satok91e88122011-07-18 11:11:42 +09002336 // By this IPC call, only a process which shares the same uid with the IME can add
2337 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09002338 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09002339 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002340 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002341 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002342 final String[] packageInfos;
2343 try {
2344 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2345 } catch (RemoteException e) {
2346 Slog.e(TAG, "Failed to get package infos");
2347 return;
2348 }
satok91e88122011-07-18 11:11:42 +09002349 if (packageInfos != null) {
2350 final int packageNum = packageInfos.length;
2351 for (int i = 0; i < packageNum; ++i) {
2352 if (packageInfos[i].equals(imi.getPackageName())) {
2353 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002354 final long ident = Binder.clearCallingIdentity();
2355 try {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002356 buildInputMethodListLocked(mMethodList, mMethodMap,
2357 false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002358 } finally {
2359 Binder.restoreCallingIdentity(ident);
2360 }
satokee5e77c2011-09-02 18:50:15 +09002361 return;
satok91e88122011-07-18 11:11:42 +09002362 }
2363 }
2364 }
satoke7c6998e2011-06-03 17:57:59 +09002365 }
satokee5e77c2011-09-02 18:50:15 +09002366 return;
satoke7c6998e2011-06-03 17:57:59 +09002367 }
2368
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002369 @Override
2370 public int getInputMethodWindowVisibleHeight() {
2371 return mWindowManagerService.getInputMethodWindowVisibleHeight();
2372 }
2373
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002374 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002375 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002376 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002377 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002378 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002379 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002380 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
2381 if (DEBUG) {
2382 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
2383 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
2384 + " actual: " + sequenceNumber);
2385 }
2386 return;
2387 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002388 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2389 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09002390 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002391 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002392 }
2393 }
2394
satok28203512010-11-24 11:06:49 +09002395 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002396 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002397 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
2398 }
2399 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002400
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002401 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
2402 if (token == null) {
2403 if (mContext.checkCallingOrSelfPermission(
2404 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2405 != PackageManager.PERMISSION_GRANTED) {
2406 throw new SecurityException(
2407 "Using null token requires permission "
2408 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002409 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002410 } else if (mCurToken != token) {
2411 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2412 + " token: " + token);
2413 return;
2414 }
2415
2416 final long ident = Binder.clearCallingIdentity();
2417 try {
2418 setInputMethodLocked(id, subtypeId);
2419 } finally {
2420 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002421 }
2422 }
2423
satok42c5a162011-05-26 16:46:14 +09002424 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002425 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002426 if (!calledFromValidUser()) {
2427 return;
2428 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002429 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002430 if (!calledWithValidToken(token)) {
2431 final int uid = Binder.getCallingUid();
2432 Slog.e(TAG, "Ignoring hideInputMethod due to an invalid token. uid:"
2433 + uid + " token:" + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002434 return;
2435 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002436 long ident = Binder.clearCallingIdentity();
2437 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002438 hideCurrentInputLocked(flags, null);
2439 } finally {
2440 Binder.restoreCallingIdentity(ident);
2441 }
2442 }
2443 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002444
satok42c5a162011-05-26 16:46:14 +09002445 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002446 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002447 if (!calledFromValidUser()) {
2448 return;
2449 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002450 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002451 if (!calledWithValidToken(token)) {
2452 final int uid = Binder.getCallingUid();
2453 Slog.e(TAG, "Ignoring showMySoftInput due to an invalid token. uid:"
2454 + uid + " token:" + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002455 return;
2456 }
2457 long ident = Binder.clearCallingIdentity();
2458 try {
2459 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002460 } finally {
2461 Binder.restoreCallingIdentity(ident);
2462 }
2463 }
2464 }
2465
2466 void setEnabledSessionInMainThread(SessionState session) {
2467 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002468 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002469 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002470 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002471 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002472 } catch (RemoteException e) {
2473 }
2474 }
2475 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002476 if (mEnabledSession != null && mEnabledSession.session != null) {
2477 try {
2478 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
2479 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
2480 } catch (RemoteException e) {
2481 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002482 }
2483 }
2484 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002485
satok42c5a162011-05-26 16:46:14 +09002486 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002487 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002488 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002489 switch (msg.what) {
2490 case MSG_SHOW_IM_PICKER:
2491 showInputMethodMenu();
2492 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002493
satokab751aa2010-09-14 19:17:36 +09002494 case MSG_SHOW_IM_SUBTYPE_PICKER:
2495 showInputMethodSubtypeMenu();
2496 return true;
2497
satok47a44912010-10-06 16:03:58 +09002498 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002499 args = (SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09002500 showInputMethodAndSubtypeEnabler((String)args.arg1);
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002501 args.recycle();
satok217f5482010-12-15 05:19:19 +09002502 return true;
2503
2504 case MSG_SHOW_IM_CONFIG:
2505 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002506 return true;
2507
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002508 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002509
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002510 case MSG_UNBIND_INPUT:
2511 try {
2512 ((IInputMethod)msg.obj).unbindInput();
2513 } catch (RemoteException e) {
2514 // There is nothing interesting about the method dying.
2515 }
2516 return true;
2517 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002518 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002519 try {
2520 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2521 } catch (RemoteException e) {
2522 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002523 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002524 return true;
2525 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002526 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002527 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002528 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002529 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002530 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002531 } catch (RemoteException e) {
2532 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002533 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002534 return true;
2535 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002536 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002537 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002538 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002539 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002540 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002541 } catch (RemoteException e) {
2542 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002543 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002544 return true;
2545 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002546 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002547 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002548 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002549 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2550 } catch (RemoteException e) {
2551 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002552 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002553 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002554 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002555 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002556 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002557 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002558 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002559 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002560 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002561 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002562 // Dispose the channel if the input method is not local to this process
2563 // because the remote proxy will get its own copy when unparceled.
2564 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002565 channel.dispose();
2566 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002567 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002568 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002569 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002570 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002571 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002572
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002573 case MSG_START_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002574 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002575 try {
2576 SessionState session = (SessionState)args.arg1;
2577 setEnabledSessionInMainThread(session);
2578 session.method.startInput((IInputContext)args.arg2,
2579 (EditorInfo)args.arg3);
2580 } catch (RemoteException e) {
2581 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002582 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002583 return true;
2584 case MSG_RESTART_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002585 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002586 try {
2587 SessionState session = (SessionState)args.arg1;
2588 setEnabledSessionInMainThread(session);
2589 session.method.restartInput((IInputContext)args.arg2,
2590 (EditorInfo)args.arg3);
2591 } catch (RemoteException e) {
2592 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002593 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002594 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002595
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002596 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002597
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002598 case MSG_UNBIND_METHOD:
2599 try {
2600 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
2601 } catch (RemoteException e) {
2602 // There is nothing interesting about the last client dying.
2603 }
2604 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002605 case MSG_BIND_METHOD: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002606 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002607 IInputMethodClient client = (IInputMethodClient)args.arg1;
2608 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002609 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002610 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002611 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002612 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002613 } finally {
2614 // Dispose the channel if the input method is not local to this process
2615 // because the remote proxy will get its own copy when unparceled.
2616 if (res.channel != null && Binder.isProxy(client)) {
2617 res.channel.dispose();
2618 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002619 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002620 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002621 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002622 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002623 case MSG_SET_ACTIVE:
2624 try {
2625 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2626 } catch (RemoteException e) {
2627 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2628 + ((ClientState)msg.obj).pid + " uid "
2629 + ((ClientState)msg.obj).uid);
2630 }
2631 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002632 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
2633 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07002634 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002635 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07002636 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002637 } catch (RemoteException e) {
2638 Slog.w(TAG, "Got RemoteException sending "
2639 + "setUserActionNotificationSequenceNumber("
2640 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07002641 + clientState.pid + " uid "
2642 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002643 }
2644 return true;
2645 }
satok01038492012-04-09 21:08:27 +09002646
2647 // --------------------------------------------------------------
2648 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07002649 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09002650 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002651 }
2652 return false;
2653 }
2654
satokdc9ddae2011-10-06 12:22:36 +09002655 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002656 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
2657 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09002658 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002659 if (DEBUG) {
2660 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2661 }
satok723a27e2010-11-11 14:58:11 +09002662 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002663 return true;
2664 }
2665
2666 return false;
2667 }
2668
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002669 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002670 HashMap<String, InputMethodInfo> map, boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002671 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002672 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Satoshi Kataoka0766eb02013-07-31 18:30:13 +09002673 + " \n ------ \n" + InputMethodUtils.getStackTrace());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002674 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002675 list.clear();
2676 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002677
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002678 // Use for queryIntentServicesAsUser
2679 final PackageManager pm = mContext.getPackageManager();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002680 String disabledSysImes = mSettings.getDisabledSystemInputMethods();
Amith Yamasanie861ec12010-03-24 21:39:27 -07002681 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002682
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002683 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002684 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002685 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2686 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002687
satoke7c6998e2011-06-03 17:57:59 +09002688 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2689 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002690 for (int i = 0; i < services.size(); ++i) {
2691 ResolveInfo ri = services.get(i);
2692 ServiceInfo si = ri.serviceInfo;
2693 ComponentName compName = new ComponentName(si.packageName, si.name);
2694 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2695 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002696 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002697 + ": it does not require the permission "
2698 + android.Manifest.permission.BIND_INPUT_METHOD);
2699 continue;
2700 }
2701
Joe Onorato8a9b2202010-02-26 18:56:32 -08002702 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002703
2704 try {
satoke7c6998e2011-06-03 17:57:59 +09002705 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002706 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002707 final String id = p.getId();
2708 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002709
2710 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002711 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002712 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002713
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002714 } catch (XmlPullParserException 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 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002717 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002718 }
2719 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002720
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002721 if (resetDefaultEnabledIme) {
2722 final ArrayList<InputMethodInfo> defaultEnabledIme =
2723 InputMethodUtils.getDefaultEnabledImes(mContext, mSystemReady, list);
2724 for (int i = 0; i < defaultEnabledIme.size(); ++i) {
2725 final InputMethodInfo imi = defaultEnabledIme.get(i);
2726 if (DEBUG) {
2727 Slog.d(TAG, "--- enable ime = " + imi);
2728 }
2729 setInputMethodEnabledLocked(imi.getId(), true);
2730 }
2731 }
2732
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002733 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09002734 if (!TextUtils.isEmpty(defaultImiId)) {
2735 if (!map.containsKey(defaultImiId)) {
2736 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
2737 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07002738 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09002739 }
2740 } else {
2741 // Double check that the default IME is certainly enabled.
2742 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002743 }
2744 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09002745 // Here is not the perfect place to reset the switching controller. Ideally
2746 // mSwitchingController and mSettings should be able to share the same state.
2747 // TODO: Make sure that mSwitchingController and mSettings are sharing the
2748 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09002749 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002750 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002751
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002752 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002753
satokab751aa2010-09-14 19:17:36 +09002754 private void showInputMethodMenu() {
2755 showInputMethodMenuInternal(false);
2756 }
2757
2758 private void showInputMethodSubtypeMenu() {
2759 showInputMethodMenuInternal(true);
2760 }
2761
satok217f5482010-12-15 05:19:19 +09002762 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002763 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002764 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002765 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2766 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002767 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002768 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002769 }
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002770 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok217f5482010-12-15 05:19:19 +09002771 }
2772
2773 private void showConfigureInputMethods() {
2774 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2775 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2776 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2777 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002778 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09002779 }
2780
satok2c93efc2012-04-02 19:33:47 +09002781 private boolean isScreenLocked() {
2782 return mKeyguardManager != null
2783 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
2784 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002785
satokab751aa2010-09-14 19:17:36 +09002786 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002787 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002788
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002789 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09002790 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002791
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002792 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002793 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002794 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002795
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002796 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002797 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09002798 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
2799 mContext);
satok7f35c8c2010-10-07 21:13:11 +09002800 if (immis == null || immis.size() == 0) {
2801 return;
2802 }
2803
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002804 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002805
satok688bd472012-02-09 20:09:17 +09002806 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002807 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
satok688bd472012-02-09 20:09:17 +09002808 showSubtypes, mInputShown, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09002809
satokc3690562012-01-10 20:14:43 +09002810 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002811 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09002812 if (currentSubtype != null) {
2813 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002814 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
2815 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09002816 }
2817 }
2818
Ken Wakasa761eb372011-03-04 19:06:18 +09002819 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09002820 mIms = new InputMethodInfo[N];
2821 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002822 int checkedItem = 0;
2823 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002824 final ImeSubtypeListItem item = imList.get(i);
2825 mIms[i] = item.mImi;
2826 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002827 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002828 int subtypeId = mSubtypeIds[i];
2829 if ((subtypeId == NOT_A_SUBTYPE_ID)
2830 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2831 || (subtypeId == lastInputMethodSubtypeId)) {
2832 checkedItem = i;
2833 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002834 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002835 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08002836
2837 final Context settingsContext = new ContextThemeWrapper(context,
2838 com.android.internal.R.style.Theme_DeviceDefault_Settings);
2839
2840 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002841 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
2842 @Override
2843 public void onCancel(DialogInterface dialog) {
2844 hideInputMethodMenu();
2845 }
2846 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08002847
2848 final Context dialogContext = mDialogBuilder.getContext();
2849 final TypedArray a = dialogContext.obtainStyledAttributes(null,
2850 com.android.internal.R.styleable.DialogPreference,
2851 com.android.internal.R.attr.alertDialogStyle, 0);
2852 final Drawable dialogIcon = a.getDrawable(
2853 com.android.internal.R.styleable.DialogPreference_dialogIcon);
2854 a.recycle();
2855
2856 mDialogBuilder.setIcon(dialogIcon);
2857
2858 final LayoutInflater inflater = (LayoutInflater) dialogContext.getSystemService(
2859 Context.LAYOUT_INFLATER_SERVICE);
satok01038492012-04-09 21:08:27 +09002860 final View tv = inflater.inflate(
2861 com.android.internal.R.layout.input_method_switch_dialog_title, null);
2862 mDialogBuilder.setCustomTitle(tv);
2863
2864 // Setup layout for a toggle switch of the hardware keyboard
2865 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002866 mSwitchingDialogTitleView
2867 .findViewById(com.android.internal.R.id.hard_keyboard_section)
2868 .setVisibility(mWindowManagerService.isHardKeyboardAvailable()
2869 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08002870 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002871 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07002872 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002873 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
2874 @Override
2875 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07002876 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002877 // Ensure that the input method dialog is dismissed when changing
2878 // the hardware keyboard state.
2879 hideInputMethodMenu();
2880 }
2881 });
2882
Alan Viverette505e3ab2014-11-24 15:22:11 -08002883 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002884 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
2885 final OnClickListener choiceListener = new OnClickListener() {
2886 @Override
2887 public void onClick(final DialogInterface dialog, final int which) {
2888 synchronized (mMethodMap) {
2889 if (mIms == null || mIms.length <= which || mSubtypeIds == null
2890 || mSubtypeIds.length <= which) {
2891 return;
satok01038492012-04-09 21:08:27 +09002892 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002893 final InputMethodInfo im = mIms[which];
2894 int subtypeId = mSubtypeIds[which];
2895 adapter.mCheckedItem = which;
2896 adapter.notifyDataSetChanged();
2897 hideInputMethodMenu();
2898 if (im != null) {
2899 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
2900 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08002901 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002902 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002903 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002904 }
2905 }
2906 };
2907 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002908
satokbc81b692011-08-26 16:22:22 +09002909 if (showSubtypes && !isScreenLocked) {
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002910 final OnClickListener positiveListener = new OnClickListener() {
2911 @Override
2912 public void onClick(DialogInterface dialog, int whichButton) {
2913 showConfigureInputMethods();
2914 }
2915 };
satok82beadf2010-12-27 19:03:06 +09002916 mDialogBuilder.setPositiveButton(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002917 com.android.internal.R.string.configure_input_methods, positiveListener);
satok7f35c8c2010-10-07 21:13:11 +09002918 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002919 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002920 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002921 mSwitchingDialog.getWindow().setType(
2922 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09002923 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
2924 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002925 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
Jason Monk807ef762014-05-08 15:47:46 -04002926 updateImeWindowStatusLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002927 mSwitchingDialog.show();
2928 }
2929 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002930
Ken Wakasa05dbb652011-08-22 15:22:43 +09002931 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
2932 private final LayoutInflater mInflater;
2933 private final int mTextViewResourceId;
2934 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09002935 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09002936 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
2937 List<ImeSubtypeListItem> itemsList, int checkedItem) {
2938 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08002939
Ken Wakasa05dbb652011-08-22 15:22:43 +09002940 mTextViewResourceId = textViewResourceId;
2941 mItemsList = itemsList;
2942 mCheckedItem = checkedItem;
Alan Viverette505e3ab2014-11-24 15:22:11 -08002943 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Ken Wakasa05dbb652011-08-22 15:22:43 +09002944 }
2945
2946 @Override
2947 public View getView(int position, View convertView, ViewGroup parent) {
2948 final View view = convertView != null ? convertView
2949 : mInflater.inflate(mTextViewResourceId, null);
2950 if (position < 0 || position >= mItemsList.size()) return view;
2951 final ImeSubtypeListItem item = mItemsList.get(position);
2952 final CharSequence imeName = item.mImeName;
2953 final CharSequence subtypeName = item.mSubtypeName;
2954 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
2955 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
2956 if (TextUtils.isEmpty(subtypeName)) {
2957 firstTextView.setText(imeName);
2958 secondTextView.setVisibility(View.GONE);
2959 } else {
2960 firstTextView.setText(subtypeName);
2961 secondTextView.setText(imeName);
2962 secondTextView.setVisibility(View.VISIBLE);
2963 }
2964 final RadioButton radioButton =
2965 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
2966 radioButton.setChecked(position == mCheckedItem);
2967 return view;
2968 }
2969 }
2970
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002971 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07002972 synchronized (mMethodMap) {
2973 hideInputMethodMenuLocked();
2974 }
2975 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002976
The Android Open Source Project10592532009-03-18 17:39:46 -07002977 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002978 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002979
The Android Open Source Project10592532009-03-18 17:39:46 -07002980 if (mSwitchingDialog != null) {
2981 mSwitchingDialog.dismiss();
2982 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002983 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002984
Jason Monk807ef762014-05-08 15:47:46 -04002985 updateImeWindowStatusLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -07002986 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07002987 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002988 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002989
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002990 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002991
satok42c5a162011-05-26 16:46:14 +09002992 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002993 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002994 // TODO: Make this work even for non-current users?
2995 if (!calledFromValidUser()) {
2996 return false;
2997 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002998 synchronized (mMethodMap) {
2999 if (mContext.checkCallingOrSelfPermission(
3000 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3001 != PackageManager.PERMISSION_GRANTED) {
3002 throw new SecurityException(
3003 "Requires permission "
3004 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3005 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003006
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003007 long ident = Binder.clearCallingIdentity();
3008 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003009 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003010 } finally {
3011 Binder.restoreCallingIdentity(ident);
3012 }
3013 }
3014 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003015
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003016 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3017 // Make sure this is a valid input method.
3018 InputMethodInfo imm = mMethodMap.get(id);
3019 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003020 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003021 }
3022
satokd87c2592010-09-29 11:52:06 +09003023 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3024 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003025
satokd87c2592010-09-29 11:52:06 +09003026 if (enabled) {
3027 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3028 if (pair.first.equals(id)) {
3029 // We are enabling this input method, but it is already enabled.
3030 // Nothing to do. The previous state was enabled.
3031 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003032 }
3033 }
satokd87c2592010-09-29 11:52:06 +09003034 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3035 // Previous state was disabled.
3036 return false;
3037 } else {
3038 StringBuilder builder = new StringBuilder();
3039 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3040 builder, enabledInputMethodsList, id)) {
3041 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003042 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003043 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3044 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3045 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003046 }
3047 // Previous state was enabled.
3048 return true;
3049 } else {
3050 // We are disabling the input method but it is already disabled.
3051 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003052 return false;
3053 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003054 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003055 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003056
satok723a27e2010-11-11 14:58:11 +09003057 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3058 boolean setSubtypeOnly) {
3059 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003060 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003061
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003062 mCurUserActionNotificationSequenceNumber =
3063 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3064 if (DEBUG) {
3065 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3066 + mCurUserActionNotificationSequenceNumber);
3067 }
3068
3069 if (mCurClient != null && mCurClient.client != null) {
3070 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3071 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003072 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003073 }
3074
satok723a27e2010-11-11 14:58:11 +09003075 // Set Subtype here
3076 if (imi == null || subtypeId < 0) {
3077 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003078 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003079 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003080 if (subtypeId < imi.getSubtypeCount()) {
3081 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3082 mSettings.putSelectedSubtype(subtype.hashCode());
3083 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003084 } else {
3085 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003086 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003087 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003088 }
satokab751aa2010-09-14 19:17:36 +09003089 }
satok723a27e2010-11-11 14:58:11 +09003090
satok4c0e7152012-06-20 20:08:44 +09003091 // Workaround.
3092 // ASEC is not ready in the IMMS constructor. Accordingly, forward-locked
3093 // IMEs are not recognized and considered uninstalled.
3094 // Actually, we can't move everything after SystemReady because
3095 // IMMS needs to run in the encryption lock screen. So, we just skip changing
3096 // the default IME here and try cheking the default IME again in systemReady().
3097 // TODO: Do nothing before system ready and implement a separated logic for
3098 // the encryption lock screen.
3099 // TODO: ASEC should be ready before IMMS is instantiated.
3100 if (mSystemReady && !setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003101 // Set InputMethod here
3102 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3103 }
3104 }
3105
3106 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3107 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3108 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3109 // newDefaultIme is empty when there is no candidate for the selected IME.
3110 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3111 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3112 if (subtypeHashCode != null) {
3113 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003114 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok723a27e2010-11-11 14:58:11 +09003115 imi, Integer.valueOf(subtypeHashCode));
3116 } catch (NumberFormatException e) {
3117 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3118 }
3119 }
3120 }
3121 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003122 }
3123
satok4e4569d2010-11-19 18:45:53 +09003124 // If there are no selected shortcuts, tries finding the most applicable ones.
3125 private Pair<InputMethodInfo, InputMethodSubtype>
3126 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3127 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3128 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003129 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003130 boolean foundInSystemIME = false;
3131
3132 // Search applicable subtype for each InputMethodInfo
3133 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003134 final String imiId = imi.getId();
3135 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3136 continue;
3137 }
satokcd7cd292010-11-20 15:46:23 +09003138 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003139 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003140 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003141 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003142 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003143 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003144 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003145 }
satokdf31ae62011-01-15 06:19:44 +09003146 // 2. Search by the system locale from enabledSubtypes.
3147 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003148 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003149 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003150 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003151 }
satoka86f5e42011-09-02 17:12:42 +09003152 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003153 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003154 final ArrayList<InputMethodSubtype> subtypesForSearch =
3155 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003156 ? InputMethodUtils.getSubtypes(imi)
3157 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003158 // 4. Search by the current subtype's locale from all subtypes.
3159 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003160 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003161 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003162 }
3163 // 5. Search by the system locale from all subtypes.
3164 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003165 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003166 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003167 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003168 }
satokcd7cd292010-11-20 15:46:23 +09003169 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003170 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003171 // The current input method is the most applicable IME.
3172 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003173 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003174 break;
satok7599a7f2010-12-22 13:45:23 +09003175 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003176 // The system input method is 2nd applicable IME.
3177 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003178 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003179 if ((imi.getServiceInfo().applicationInfo.flags
3180 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3181 foundInSystemIME = true;
3182 }
satok4e4569d2010-11-19 18:45:53 +09003183 }
3184 }
3185 }
3186 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003187 if (mostApplicableIMI != null) {
3188 Slog.w(TAG, "Most applicable shortcut input method was:"
3189 + mostApplicableIMI.getId());
3190 if (mostApplicableSubtype != null) {
3191 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3192 + "," + mostApplicableSubtype.getMode() + ","
3193 + mostApplicableSubtype.getLocale());
3194 }
3195 }
satok4e4569d2010-11-19 18:45:53 +09003196 }
satokcd7cd292010-11-20 15:46:23 +09003197 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09003198 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09003199 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003200 } else {
3201 return null;
3202 }
3203 }
3204
satokab751aa2010-09-14 19:17:36 +09003205 /**
3206 * @return Return the current subtype of this input method.
3207 */
satok42c5a162011-05-26 16:46:14 +09003208 @Override
satokab751aa2010-09-14 19:17:36 +09003209 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003210 // TODO: Make this work even for non-current users?
3211 if (!calledFromValidUser()) {
3212 return null;
3213 }
3214 synchronized (mMethodMap) {
3215 return getCurrentInputMethodSubtypeLocked();
3216 }
3217 }
3218
3219 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003220 if (mCurMethodId == null) {
3221 return null;
3222 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003223 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003224 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3225 if (imi == null || imi.getSubtypeCount() == 0) {
3226 return null;
satok4e4569d2010-11-19 18:45:53 +09003227 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003228 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003229 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3230 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003231 if (subtypeId == NOT_A_SUBTYPE_ID) {
3232 // If there are no selected subtypes, the framework will try to find
3233 // the most applicable subtype from explicitly or implicitly enabled
3234 // subtypes.
3235 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003236 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003237 // If there is only one explicitly or implicitly enabled subtype,
3238 // just returns it.
3239 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3240 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3241 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003242 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003243 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003244 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003245 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003246 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003247 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3248 true);
satok4e4569d2010-11-19 18:45:53 +09003249 }
satok3ef8b292010-11-23 06:06:29 +09003250 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003251 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003252 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003253 }
3254 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003255 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003256 }
3257
satokf3db1af2010-11-23 13:34:33 +09003258 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
3259 InputMethodSubtype subtype) {
3260 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
3261 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
3262 } else {
3263 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3264 subtypes.add(subtype);
3265 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
3266 }
3267 }
3268
satok4e4569d2010-11-19 18:45:53 +09003269 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003270 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003271 @Override
satok4e4569d2010-11-19 18:45:53 +09003272 public List getShortcutInputMethodsAndSubtypes() {
3273 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09003274 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09003275 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003276 // If there are no selected shortcut subtypes, the framework will try to find
3277 // the most applicable subtype from all subtypes whose mode is
3278 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003279 Pair<InputMethodInfo, InputMethodSubtype> info =
3280 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003281 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003282 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003283 ret.add(info.first);
3284 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003285 }
satok3da92232011-01-11 22:46:30 +09003286 return ret;
satokf3db1af2010-11-23 13:34:33 +09003287 }
satokf3db1af2010-11-23 13:34:33 +09003288 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3289 ret.add(imi);
3290 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3291 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003292 }
3293 }
satokf3db1af2010-11-23 13:34:33 +09003294 return ret;
satok4e4569d2010-11-19 18:45:53 +09003295 }
3296 }
3297
satok42c5a162011-05-26 16:46:14 +09003298 @Override
satokb66d2872010-11-10 01:04:04 +09003299 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003300 // TODO: Make this work even for non-current users?
3301 if (!calledFromValidUser()) {
3302 return false;
3303 }
satokb66d2872010-11-10 01:04:04 +09003304 synchronized (mMethodMap) {
3305 if (subtype != null && mCurMethodId != null) {
3306 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003307 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003308 if (subtypeId != NOT_A_SUBTYPE_ID) {
3309 setInputMethodLocked(mCurMethodId, subtypeId);
3310 return true;
3311 }
3312 }
3313 return false;
3314 }
3315 }
3316
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003317 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003318 private static class InputMethodFileManager {
3319 private static final String SYSTEM_PATH = "system";
3320 private static final String INPUT_METHOD_PATH = "inputmethod";
3321 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3322 private static final String NODE_SUBTYPES = "subtypes";
3323 private static final String NODE_SUBTYPE = "subtype";
3324 private static final String NODE_IMI = "imi";
3325 private static final String ATTR_ID = "id";
3326 private static final String ATTR_LABEL = "label";
3327 private static final String ATTR_ICON = "icon";
3328 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3329 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3330 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3331 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3332 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3333 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003334 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
satoke7c6998e2011-06-03 17:57:59 +09003335 new HashMap<String, List<InputMethodSubtype>>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003336 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003337 if (methodMap == null) {
3338 throw new NullPointerException("methodMap is null");
3339 }
3340 mMethodMap = methodMap;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003341 final File systemDir = userId == UserHandle.USER_OWNER
3342 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3343 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003344 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3345 if (!inputMethodDir.mkdirs()) {
3346 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3347 }
3348 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3349 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3350 if (!subtypeFile.exists()) {
3351 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003352 writeAdditionalInputMethodSubtypes(
3353 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003354 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003355 readAdditionalInputMethodSubtypes(
3356 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003357 }
3358 }
3359
3360 private void deleteAllInputMethodSubtypes(String imiId) {
3361 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003362 mAdditionalSubtypesMap.remove(imiId);
3363 writeAdditionalInputMethodSubtypes(
3364 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003365 }
3366 }
3367
3368 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003369 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003370 synchronized (mMethodMap) {
3371 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3372 final int N = additionalSubtypes.length;
3373 for (int i = 0; i < N; ++i) {
3374 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003375 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003376 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003377 } else {
3378 Slog.w(TAG, "Duplicated subtype definition found: "
3379 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003380 }
3381 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003382 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3383 writeAdditionalInputMethodSubtypes(
3384 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003385 }
3386 }
3387
3388 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3389 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003390 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003391 }
3392 }
3393
3394 private static void writeAdditionalInputMethodSubtypes(
3395 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3396 HashMap<String, InputMethodInfo> methodMap) {
3397 // Safety net for the case that this function is called before methodMap is set.
3398 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3399 FileOutputStream fos = null;
3400 try {
3401 fos = subtypesFile.startWrite();
3402 final XmlSerializer out = new FastXmlSerializer();
3403 out.setOutput(fos, "utf-8");
3404 out.startDocument(null, true);
3405 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3406 out.startTag(null, NODE_SUBTYPES);
3407 for (String imiId : allSubtypes.keySet()) {
3408 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3409 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3410 continue;
3411 }
3412 out.startTag(null, NODE_IMI);
3413 out.attribute(null, ATTR_ID, imiId);
3414 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3415 final int N = subtypesList.size();
3416 for (int i = 0; i < N; ++i) {
3417 final InputMethodSubtype subtype = subtypesList.get(i);
3418 out.startTag(null, NODE_SUBTYPE);
3419 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3420 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3421 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3422 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3423 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3424 out.attribute(null, ATTR_IS_AUXILIARY,
3425 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3426 out.endTag(null, NODE_SUBTYPE);
3427 }
3428 out.endTag(null, NODE_IMI);
3429 }
3430 out.endTag(null, NODE_SUBTYPES);
3431 out.endDocument();
3432 subtypesFile.finishWrite(fos);
3433 } catch (java.io.IOException e) {
3434 Slog.w(TAG, "Error writing subtypes", e);
3435 if (fos != null) {
3436 subtypesFile.failWrite(fos);
3437 }
3438 }
3439 }
3440
3441 private static void readAdditionalInputMethodSubtypes(
3442 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3443 if (allSubtypes == null || subtypesFile == null) return;
3444 allSubtypes.clear();
3445 FileInputStream fis = null;
3446 try {
3447 fis = subtypesFile.openRead();
3448 final XmlPullParser parser = Xml.newPullParser();
3449 parser.setInput(fis, null);
3450 int type = parser.getEventType();
3451 // Skip parsing until START_TAG
3452 while ((type = parser.next()) != XmlPullParser.START_TAG
3453 && type != XmlPullParser.END_DOCUMENT) {}
3454 String firstNodeName = parser.getName();
3455 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3456 throw new XmlPullParserException("Xml doesn't start with subtypes");
3457 }
3458 final int depth =parser.getDepth();
3459 String currentImiId = null;
3460 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3461 while (((type = parser.next()) != XmlPullParser.END_TAG
3462 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3463 if (type != XmlPullParser.START_TAG)
3464 continue;
3465 final String nodeName = parser.getName();
3466 if (NODE_IMI.equals(nodeName)) {
3467 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3468 if (TextUtils.isEmpty(currentImiId)) {
3469 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3470 continue;
3471 }
3472 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
3473 allSubtypes.put(currentImiId, tempSubtypesArray);
3474 } else if (NODE_SUBTYPE.equals(nodeName)) {
3475 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3476 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3477 continue;
3478 }
3479 final int icon = Integer.valueOf(
3480 parser.getAttributeValue(null, ATTR_ICON));
3481 final int label = Integer.valueOf(
3482 parser.getAttributeValue(null, ATTR_LABEL));
3483 final String imeSubtypeLocale =
3484 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3485 final String imeSubtypeMode =
3486 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3487 final String imeSubtypeExtraValue =
3488 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003489 final boolean isAuxiliary = "1".equals(String.valueOf(
3490 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003491 final InputMethodSubtype subtype = new InputMethodSubtypeBuilder()
3492 .setSubtypeNameResId(label)
3493 .setSubtypeIconResId(icon)
3494 .setSubtypeLocale(imeSubtypeLocale)
3495 .setSubtypeMode(imeSubtypeMode)
3496 .setSubtypeExtraValue(imeSubtypeExtraValue)
3497 .setIsAuxiliary(isAuxiliary)
3498 .build();
satoke7c6998e2011-06-03 17:57:59 +09003499 tempSubtypesArray.add(subtype);
3500 }
3501 }
3502 } catch (XmlPullParserException e) {
3503 Slog.w(TAG, "Error reading subtypes: " + e);
3504 return;
3505 } catch (java.io.IOException e) {
3506 Slog.w(TAG, "Error reading subtypes: " + e);
3507 return;
3508 } catch (NumberFormatException e) {
3509 Slog.w(TAG, "Error reading subtypes: " + e);
3510 return;
3511 } finally {
3512 if (fis != null) {
3513 try {
3514 fis.close();
3515 } catch (java.io.IOException e1) {
3516 Slog.w(TAG, "Failed to close.");
3517 }
3518 }
3519 }
3520 }
3521 }
3522
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003523 @Override
3524 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3525 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3526 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003527
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003528 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3529 + Binder.getCallingPid()
3530 + ", uid=" + Binder.getCallingUid());
3531 return;
3532 }
3533
3534 IInputMethod method;
3535 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003536
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003537 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003538
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003539 synchronized (mMethodMap) {
3540 p.println("Current Input Method Manager state:");
3541 int N = mMethodList.size();
3542 p.println(" Input Methods:");
3543 for (int i=0; i<N; i++) {
3544 InputMethodInfo info = mMethodList.get(i);
3545 p.println(" InputMethod #" + i + ":");
3546 info.dump(p, " ");
3547 }
3548 p.println(" Clients:");
3549 for (ClientState ci : mClients.values()) {
3550 p.println(" Client " + ci + ":");
3551 p.println(" client=" + ci.client);
3552 p.println(" inputContext=" + ci.inputContext);
3553 p.println(" sessionRequested=" + ci.sessionRequested);
3554 p.println(" curSession=" + ci.curSession);
3555 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003556 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003557 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003558 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3559 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003560 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3561 + " mBoundToMethod=" + mBoundToMethod);
3562 p.println(" mCurToken=" + mCurToken);
3563 p.println(" mCurIntent=" + mCurIntent);
3564 method = mCurMethod;
3565 p.println(" mCurMethod=" + mCurMethod);
3566 p.println(" mEnabledSession=" + mEnabledSession);
3567 p.println(" mShowRequested=" + mShowRequested
3568 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3569 + " mShowForced=" + mShowForced
3570 + " mInputShown=" + mInputShown);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003571 p.println(" mCurUserActionNotificationSequenceNumber="
3572 + mCurUserActionNotificationSequenceNumber);
Dianne Hackbornd4e6d462014-05-16 16:32:37 -07003573 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003574 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003575
Jeff Brownb88102f2010-09-08 11:49:43 -07003576 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003577 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003578 pw.flush();
3579 try {
3580 client.client.asBinder().dump(fd, args);
3581 } catch (RemoteException e) {
3582 p.println("Input method client dead: " + e);
3583 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003584 } else {
3585 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003586 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003587
Jeff Brownb88102f2010-09-08 11:49:43 -07003588 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003589 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003590 pw.flush();
3591 try {
3592 method.asBinder().dump(fd, args);
3593 } catch (RemoteException e) {
3594 p.println("Input method service dead: " + e);
3595 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003596 } else {
3597 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003598 }
3599 }
3600}