blob: 74ae81286d11c6561cebbaf09e740d133d556bcb [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;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090043import android.app.IUserSwitchObserver;
satokf90a33e2011-07-19 11:55:52 +090044import android.app.KeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +090045import android.app.Notification;
46import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070047import android.app.PendingIntent;
satok5b927c432012-05-01 20:09:34 +090048import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.content.ComponentName;
50import android.content.ContentResolver;
51import android.content.Context;
52import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.content.DialogInterface.OnCancelListener;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +090054import android.content.DialogInterface.OnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090056import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070058import android.content.pm.ApplicationInfo;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090059import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.content.pm.PackageManager;
61import android.content.pm.ResolveInfo;
62import android.content.pm.ServiceInfo;
Amith Yamasani734983f2014-03-04 16:48:05 -080063import android.content.pm.UserInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070064import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.content.res.Resources;
66import android.content.res.TypedArray;
67import android.database.ContentObserver;
Joe Onorato857fd9b2011-01-27 15:08:35 -080068import android.inputmethodservice.InputMethodService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.os.Binder;
satoke7c6998e2011-06-03 17:57:59 +090070import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.os.Handler;
72import android.os.IBinder;
73import android.os.IInterface;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090074import android.os.IRemoteCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.os.Message;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090076import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import android.os.Parcel;
78import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080079import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080import android.os.ServiceManager;
81import android.os.SystemClock;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090082import android.os.UserHandle;
Amith Yamasani734983f2014-03-04 16:48:05 -080083import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084import android.provider.Settings;
85import android.text.TextUtils;
satokf9f01002011-05-19 21:31:50 +090086import android.text.style.SuggestionSpan;
Dianne Hackborn39606a02012-07-31 17:54:35 -070087import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +090089import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +090090import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091import android.util.PrintWriterPrinter;
92import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +090093import android.util.Slog;
94import android.util.Xml;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +090095import android.view.ContextThemeWrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096import android.view.IWindowManager;
Jeff Brownc28867a2013-03-26 15:42:39 -070097import android.view.InputChannel;
Ken Wakasa05dbb652011-08-22 15:22:43 +090098import android.view.LayoutInflater;
99import android.view.View;
100import android.view.ViewGroup;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101import android.view.WindowManager;
satokab751aa2010-09-14 19:17:36 +0900102import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103import android.view.inputmethod.InputBinding;
104import android.view.inputmethod.InputMethod;
105import android.view.inputmethod.InputMethodInfo;
106import android.view.inputmethod.InputMethodManager;
satokab751aa2010-09-14 19:17:36 +0900107import android.view.inputmethod.InputMethodSubtype;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900108import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +0900109import android.widget.CompoundButton;
110import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900111import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +0900112import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900113import android.widget.TextView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114
satoke7c6998e2011-06-03 17:57:59 +0900115import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900117import java.io.FileInputStream;
118import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119import java.io.IOException;
120import java.io.PrintWriter;
121import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900122import java.util.Collections;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123import java.util.HashMap;
124import java.util.List;
satok5b927c432012-05-01 20:09:34 +0900125import java.util.Locale;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126
127/**
128 * This class provides a system service that manages input methods.
129 */
130public class InputMethodManagerService extends IInputMethodManager.Stub
131 implements ServiceConnection, Handler.Callback {
132 static final boolean DEBUG = false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700133 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134
135 static final int MSG_SHOW_IM_PICKER = 1;
satokab751aa2010-09-14 19:17:36 +0900136 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 2;
satok47a44912010-10-06 16:03:58 +0900137 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 3;
satok217f5482010-12-15 05:19:19 +0900138 static final int MSG_SHOW_IM_CONFIG = 4;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 static final int MSG_UNBIND_INPUT = 1000;
141 static final int MSG_BIND_INPUT = 1010;
142 static final int MSG_SHOW_SOFT_INPUT = 1020;
143 static final int MSG_HIDE_SOFT_INPUT = 1030;
144 static final int MSG_ATTACH_TOKEN = 1040;
145 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 static final int MSG_START_INPUT = 2000;
148 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 static final int MSG_UNBIND_METHOD = 3000;
151 static final int MSG_BIND_METHOD = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700152 static final int MSG_SET_ACTIVE = 3020;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900153 static final int MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER = 3040;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800154
satok01038492012-04-09 21:08:27 +0900155 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
156
Satoshi Kataokabcacc322013-10-21 17:57:27 -0700157 static final long TIME_TO_RECONNECT = 3 * 1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800158
satokf9f01002011-05-19 21:31:50 +0900159 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
160
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900161 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
satokb6359412011-06-28 17:47:41 +0900162 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900163
satok4e4569d2010-11-19 18:45:53 +0900164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800166 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900168 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 final IWindowManager mIWindowManager;
171 final HandlerCaller mCaller;
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700172 final boolean mHasFeature;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900173 private InputMethodFileManager mFileManager;
satok01038492012-04-09 21:08:27 +0900174 private final HardKeyboardListener mHardKeyboardListener;
175 private final WindowManagerService mWindowManagerService;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800176
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900177 final InputBindResult mNoBinding = new InputBindResult(null, null, null, -1, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 // All known input methods. mMethodMap also serves as the global
180 // lock for this class.
satokd87c2592010-09-29 11:52:06 +0900181 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<InputMethodInfo>();
182 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<String, InputMethodInfo>();
satokf9f01002011-05-19 21:31:50 +0900183 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
184 new LruCache<SuggestionSpan, InputMethodInfo>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Satoshi Kataokad787f692013-10-26 04:44:21 +0900185 private final InputMethodSubtypeSwitchingController mSwitchingController;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800186
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700187 // Used to bring IME service up to visible adjustment while it is being shown.
188 final ServiceConnection mVisibleConnection = new ServiceConnection() {
189 @Override public void onServiceConnected(ComponentName name, IBinder service) {
190 }
191
192 @Override public void onServiceDisconnected(ComponentName name) {
193 }
194 };
195 boolean mVisibleBound = false;
196
satok7cfc0ed2011-06-20 21:29:36 +0900197 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700198 private NotificationManager mNotificationManager;
199 private KeyguardManager mKeyguardManager;
200 private StatusBarManagerService mStatusBar;
201 private Notification mImeSwitcherNotification;
202 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900203 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900204 private boolean mNotificationShown;
satok0a1bcf42012-05-16 19:26:31 +0900205 private final boolean mImeSelectedOnBoot;
satok7cfc0ed2011-06-20 21:29:36 +0900206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 class SessionState {
208 final ClientState client;
209 final IInputMethod method;
Jeff Brownc28867a2013-03-26 15:42:39 -0700210
211 IInputMethodSession session;
212 InputChannel channel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 @Override
215 public String toString() {
216 return "SessionState{uid " + client.uid + " pid " + client.pid
217 + " method " + Integer.toHexString(
218 System.identityHashCode(method))
219 + " session " + Integer.toHexString(
220 System.identityHashCode(session))
Jeff Brownc28867a2013-03-26 15:42:39 -0700221 + " channel " + channel
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 + "}";
223 }
224
225 SessionState(ClientState _client, IInputMethod _method,
Jeff Brownc28867a2013-03-26 15:42:39 -0700226 IInputMethodSession _session, InputChannel _channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 client = _client;
228 method = _method;
229 session = _session;
Jeff Brownc28867a2013-03-26 15:42:39 -0700230 channel = _channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 }
232 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800233
Jeff Brownc28867a2013-03-26 15:42:39 -0700234 static final class ClientState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 final IInputMethodClient client;
236 final IInputContext inputContext;
237 final int uid;
238 final int pid;
239 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 boolean sessionRequested;
242 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800243
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244 @Override
245 public String toString() {
246 return "ClientState{" + Integer.toHexString(
247 System.identityHashCode(this)) + " uid " + uid
248 + " pid " + pid + "}";
249 }
250
251 ClientState(IInputMethodClient _client, IInputContext _inputContext,
252 int _uid, int _pid) {
253 client = _client;
254 inputContext = _inputContext;
255 uid = _uid;
256 pid = _pid;
257 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
258 }
259 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 final HashMap<IBinder, ClientState> mClients
262 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700265 * Set once the system is ready to run third party code.
266 */
267 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800268
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700269 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 * Id of the currently selected input method.
271 */
272 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 /**
275 * The current binding sequence number, incremented every time there is
276 * a new bind performed.
277 */
278 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 /**
281 * The client that is currently bound to an input method.
282 */
283 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700286 * The last window token that gained focus.
287 */
288 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800289
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700290 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291 * The input context last provided by the current client.
292 */
293 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 /**
296 * The attributes last provided by the current client.
297 */
298 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800299
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800300 /**
301 * The input method ID of the input method service that we are currently
302 * connected to or in the process of connecting to.
303 */
304 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800305
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 /**
satokab751aa2010-09-14 19:17:36 +0900307 * The current subtype of the current input method.
308 */
309 private InputMethodSubtype mCurrentSubtype;
310
satok4e4569d2010-11-19 18:45:53 +0900311 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900312 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
313 mShortcutInputMethodsAndSubtypes =
314 new HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>();
satokab751aa2010-09-14 19:17:36 +0900315
John Spurlocke0980502013-10-25 11:59:29 -0400316 // Was the keyguard locked when this client became current?
317 private boolean mCurClientInKeyguard;
318
satokab751aa2010-09-14 19:17:36 +0900319 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 * Set to true if our ServiceConnection is currently actively bound to
321 * a service (whether or not we have gotten its IBinder back yet).
322 */
323 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800324
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 /**
326 * Set if the client has asked for the input method to be shown.
327 */
328 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800329
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 /**
331 * Set if we were explicitly told to show the input method.
332 */
333 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800334
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 /**
336 * Set if we were forced to be shown.
337 */
338 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 /**
341 * Set if we last told the input method to show itself.
342 */
343 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 /**
346 * The Intent used to connect to the current input method.
347 */
348 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 /**
351 * The token we have made for the currently active input method, to
352 * identify it in the future.
353 */
354 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 /**
357 * If non-null, this is the input method service we are currently connected
358 * to.
359 */
360 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 /**
363 * Time that we last initiated a bind to the input method, to determine
364 * if we should try to disconnect and reconnect to it.
365 */
366 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800367
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 /**
369 * Have we called mCurMethod.bindInput()?
370 */
371 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 /**
374 * Currently enabled session. Only touched by service thread, not
375 * protected by a lock.
376 */
377 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 /**
380 * True if the screen is on. The value is true initially.
381 */
382 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800383
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900384 int mCurUserActionNotificationSequenceNumber = 0;
385
Joe Onorato857fd9b2011-01-27 15:08:35 -0800386 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
387 int mImeWindowVis;
388
Ken Wakasa05dbb652011-08-22 15:22:43 +0900389 private AlertDialog.Builder mDialogBuilder;
390 private AlertDialog mSwitchingDialog;
satok01038492012-04-09 21:08:27 +0900391 private View mSwitchingDialogTitleView;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900392 private InputMethodInfo[] mIms;
393 private int[] mSubtypeIds;
satok5b927c432012-05-01 20:09:34 +0900394 private Locale mLastSystemLocale;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900395 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
396 private final IPackageManager mIPackageManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398 class SettingsObserver extends ContentObserver {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800399 String mLastEnabled = "";
400
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 SettingsObserver(Handler handler) {
402 super(handler);
403 ContentResolver resolver = mContext.getContentResolver();
404 resolver.registerContentObserver(Settings.Secure.getUriFor(
405 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
satokab751aa2010-09-14 19:17:36 +0900406 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokb6109bb2011-02-03 22:24:54 +0900407 Settings.Secure.ENABLED_INPUT_METHODS), false, this);
408 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokab751aa2010-09-14 19:17:36 +0900409 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800411
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 @Override public void onChange(boolean selfChange) {
413 synchronized (mMethodMap) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800414 boolean enabledChanged = false;
415 String newEnabled = mSettings.getEnabledInputMethodsStr();
416 if (!mLastEnabled.equals(newEnabled)) {
417 mLastEnabled = newEnabled;
418 enabledChanged = true;
419 }
420 updateFromSettingsLocked(enabledChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 }
422 }
423 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800424
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900425 class ImmsBroadcastReceiver extends android.content.BroadcastReceiver {
426 private void updateActive() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800427 // Inform the current client of the change in active status
Dianne Hackborna6e41342012-05-22 16:30:34 -0700428 if (mCurClient != null && mCurClient.client != null) {
429 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
430 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 }
432 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900433
434 @Override
435 public void onReceive(Context context, Intent intent) {
436 final String action = intent.getAction();
437 if (Intent.ACTION_SCREEN_ON.equals(action)) {
438 mScreenOn = true;
439 refreshImeWindowVisibilityLocked();
440 updateActive();
441 return;
442 } else if (Intent.ACTION_SCREEN_OFF.equals(action)) {
443 mScreenOn = false;
444 setImeWindowVisibilityStatusHiddenLocked();
445 updateActive();
446 return;
447 } else if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
448 hideInputMethodMenu();
449 // No need to updateActive
450 return;
Amith Yamasani734983f2014-03-04 16:48:05 -0800451 } else if (Intent.ACTION_USER_ADDED.equals(action)
452 || Intent.ACTION_USER_REMOVED.equals(action)) {
Kenny Guy2a764942014-04-02 13:29:20 +0100453 updateCurrentProfileIds();
Amith Yamasani734983f2014-03-04 16:48:05 -0800454 return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900455 } else {
456 Slog.w(TAG, "Unexpected intent " + intent);
457 }
458 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800460
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800461 class MyPackageMonitor extends PackageMonitor {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900462 private boolean isChangingPackagesOfCurrentUser() {
463 final int userId = getChangingUserId();
464 final boolean retval = userId == mSettings.getCurrentUserId();
465 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900466 if (!retval) {
467 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
468 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900469 }
470 return retval;
471 }
472
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800474 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900475 if (!isChangingPackagesOfCurrentUser()) {
476 return false;
477 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900479 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 final int N = mMethodList.size();
481 if (curInputMethodId != null) {
482 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800483 InputMethodInfo imi = mMethodList.get(i);
484 if (imi.getId().equals(curInputMethodId)) {
485 for (String pkg : packages) {
486 if (imi.getPackageName().equals(pkg)) {
487 if (!doit) {
488 return true;
489 }
satok723a27e2010-11-11 14:58:11 +0900490 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800491 chooseNewDefaultIMELocked();
492 return true;
493 }
494 }
495 }
496 }
497 }
498 }
499 return false;
500 }
501
502 @Override
503 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900504 if (!isChangingPackagesOfCurrentUser()) {
505 return;
506 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800507 synchronized (mMethodMap) {
508 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900509 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800510 final int N = mMethodList.size();
511 if (curInputMethodId != null) {
512 for (int i=0; i<N; i++) {
513 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900514 final String imiId = imi.getId();
515 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800516 curIm = imi;
517 }
satoke7c6998e2011-06-03 17:57:59 +0900518
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800519 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900520 if (isPackageModified(imi.getPackageName())) {
521 mFileManager.deleteAllInputMethodSubtypes(imiId);
522 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800523 if (change == PACKAGE_TEMPORARY_CHANGE
524 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800525 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800526 + imi.getComponent());
527 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528 }
529 }
530 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800531
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900532 buildInputMethodListLocked(
533 mMethodList, mMethodMap, false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800534
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800535 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800536
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800537 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800538 int change = isPackageDisappearing(curIm.getPackageName());
539 if (change == PACKAGE_TEMPORARY_CHANGE
540 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800541 ServiceInfo si = null;
542 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900543 si = mIPackageManager.getServiceInfo(
544 curIm.getComponent(), 0, mSettings.getCurrentUserId());
545 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800546 }
547 if (si == null) {
548 // Uh oh, current input method is no longer around!
549 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800550 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
satok15452a42011-10-28 17:58:28 +0900551 setImeWindowVisibilityStatusHiddenLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800552 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800553 changed = true;
554 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800555 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900556 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800557 }
558 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800559 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800560 }
satokab751aa2010-09-14 19:17:36 +0900561
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800562 if (curIm == null) {
563 // We currently don't have a default input method... is
564 // one now available?
565 changed = chooseNewDefaultIMELocked();
566 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800567
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800568 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800569 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570 }
571 }
572 }
573 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800574
Jeff Brownc28867a2013-03-26 15:42:39 -0700575 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900576 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -0700577 private final IInputMethod mMethod;
578 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800579
Jeff Brownc28867a2013-03-26 15:42:39 -0700580 MethodCallback(InputMethodManagerService imms, IInputMethod method,
581 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900582 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -0700583 mMethod = method;
584 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800586
satoke7c6998e2011-06-03 17:57:59 +0900587 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -0700588 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -0700589 long ident = Binder.clearCallingIdentity();
590 try {
591 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
592 } finally {
593 Binder.restoreCallingIdentity(ident);
594 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595 }
596 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800597
satok01038492012-04-09 21:08:27 +0900598 private class HardKeyboardListener
599 implements WindowManagerService.OnHardKeyboardStatusChangeListener {
600 @Override
601 public void onHardKeyboardStatusChange(boolean available, boolean enabled) {
602 mHandler.sendMessage(mHandler.obtainMessage(
603 MSG_HARD_KEYBOARD_SWITCH_CHANGED, available ? 1 : 0, enabled ? 1 : 0));
604 }
605
606 public void handleHardKeyboardStatusChange(boolean available, boolean enabled) {
607 if (DEBUG) {
608 Slog.w(TAG, "HardKeyboardStatusChanged: available = " + available + ", enabled = "
609 + enabled);
610 }
611 synchronized(mMethodMap) {
612 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
613 && mSwitchingDialog.isShowing()) {
614 mSwitchingDialogTitleView.findViewById(
615 com.android.internal.R.id.hard_keyboard_section).setVisibility(
616 available ? View.VISIBLE : View.GONE);
617 }
618 }
619 }
620 }
621
622 public InputMethodManagerService(Context context, WindowManagerService windowManager) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900623 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800625 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626 mHandler = new Handler(this);
627 mIWindowManager = IWindowManager.Stub.asInterface(
628 ServiceManager.getService(Context.WINDOW_SERVICE));
Mita Yuned218c72012-12-06 17:18:25 -0800629 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900630 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631 public void executeMessage(Message msg) {
632 handleMessage(msg);
633 }
Mita Yuned218c72012-12-06 17:18:25 -0800634 }, true /*asyncHandler*/);
satok01038492012-04-09 21:08:27 +0900635 mWindowManagerService = windowManager;
636 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700637 mHasFeature = context.getPackageManager().hasSystemFeature(
638 PackageManager.FEATURE_INPUT_METHODS);
satok7cfc0ed2011-06-20 21:29:36 +0900639
satok7cfc0ed2011-06-20 21:29:36 +0900640 mImeSwitcherNotification = new Notification();
641 mImeSwitcherNotification.icon = com.android.internal.R.drawable.ic_notification_ime_default;
642 mImeSwitcherNotification.when = 0;
643 mImeSwitcherNotification.flags = Notification.FLAG_ONGOING_EVENT;
644 mImeSwitcherNotification.tickerText = null;
645 mImeSwitcherNotification.defaults = 0; // please be quiet
646 mImeSwitcherNotification.sound = null;
647 mImeSwitcherNotification.vibrate = null;
Daniel Sandler590d5152012-06-14 16:10:13 -0400648
649 // Tag this notification specially so SystemUI knows it's important
John Spurlockfd7f1e02014-03-18 16:41:57 -0400650 mImeSwitcherNotification.extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
651 mImeSwitcherNotification.category = Notification.CATEGORY_SYSTEM;
Daniel Sandler590d5152012-06-14 16:10:13 -0400652
satok7cfc0ed2011-06-20 21:29:36 +0900653 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900654 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900655
656 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900657
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900658 final IntentFilter broadcastFilter = new IntentFilter();
659 broadcastFilter.addAction(Intent.ACTION_SCREEN_ON);
660 broadcastFilter.addAction(Intent.ACTION_SCREEN_OFF);
661 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Amith Yamasani734983f2014-03-04 16:48:05 -0800662 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
663 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900664 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800665
satok7cfc0ed2011-06-20 21:29:36 +0900666 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900667 int userId = 0;
668 try {
669 ActivityManagerNative.getDefault().registerUserSwitchObserver(
670 new IUserSwitchObserver.Stub() {
671 @Override
672 public void onUserSwitching(int newUserId, IRemoteCallback reply) {
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900673 synchronized(mMethodMap) {
674 switchUserLocked(newUserId);
675 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900676 if (reply != null) {
677 try {
678 reply.sendResult(null);
679 } catch (RemoteException e) {
680 }
681 }
682 }
683
684 @Override
685 public void onUserSwitchComplete(int newUserId) throws RemoteException {
686 }
687 });
688 userId = ActivityManagerNative.getDefault().getCurrentUser().id;
689 } catch (RemoteException e) {
690 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
691 }
satok81f8b7c2012-12-04 20:42:56 +0900692 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
satok913a8922010-08-26 21:53:41 +0900693
satokd87c2592010-09-29 11:52:06 +0900694 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900695 mSettings = new InputMethodSettings(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900696 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId);
Kenny Guy2a764942014-04-02 13:29:20 +0100697 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900698 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900699 synchronized (mMethodMap) {
700 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
701 mSettings, context);
702 }
satok0a1bcf42012-05-16 19:26:31 +0900703
704 // Just checking if defaultImiId is empty or not
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900705 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900706 if (DEBUG) {
707 Slog.d(TAG, "Initial default ime = " + defaultImiId);
708 }
satok0a1bcf42012-05-16 19:26:31 +0900709 mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
710
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900711 synchronized (mMethodMap) {
712 buildInputMethodListLocked(mMethodList, mMethodMap,
713 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
714 }
satokd87c2592010-09-29 11:52:06 +0900715 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800716
satok0a1bcf42012-05-16 19:26:31 +0900717 if (!mImeSelectedOnBoot) {
718 Slog.w(TAG, "No IME selected. Choose the most applicable IME.");
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900719 synchronized (mMethodMap) {
720 resetDefaultImeLocked(context);
721 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800723
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724 mSettingsObserver = new SettingsObserver(mHandler);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900725 synchronized (mMethodMap) {
726 updateFromSettingsLocked(true);
727 }
satok5b927c432012-05-01 20:09:34 +0900728
729 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME
730 // according to the new system locale.
731 final IntentFilter filter = new IntentFilter();
732 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
733 mContext.registerReceiver(
734 new BroadcastReceiver() {
735 @Override
736 public void onReceive(Context context, Intent intent) {
737 synchronized(mMethodMap) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900738 resetStateIfCurrentLocaleChangedLocked();
satok5b927c432012-05-01 20:09:34 +0900739 }
740 }
741 }, filter);
742 }
743
satok5b927c432012-05-01 20:09:34 +0900744 private void resetDefaultImeLocked(Context context) {
745 // Do not reset the default (current) IME when it is a 3rd-party IME
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900746 if (mCurMethodId != null
747 && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +0900748 return;
749 }
750
751 InputMethodInfo defIm = null;
752 for (InputMethodInfo imi : mMethodList) {
753 if (defIm == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900754 if (InputMethodUtils.isValidSystemDefaultIme(
755 mSystemReady, imi, context)) {
satok5b927c432012-05-01 20:09:34 +0900756 defIm = imi;
757 Slog.i(TAG, "Selected default: " + imi.getId());
758 }
759 }
760 }
761 if (defIm == null && mMethodList.size() > 0) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900762 defIm = InputMethodUtils.getMostApplicableDefaultIME(
763 mSettings.getEnabledInputMethodListLocked());
satok5b927c432012-05-01 20:09:34 +0900764 Slog.i(TAG, "No default found, using " + defIm.getId());
765 }
766 if (defIm != null) {
767 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
768 }
769 }
770
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900771 private void resetAllInternalStateLocked(final boolean updateOnlyWhenLocaleChanged,
772 final boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900773 if (!mSystemReady) {
774 // not system ready
775 return;
776 }
777 final Locale newLocale = mRes.getConfiguration().locale;
778 if (!updateOnlyWhenLocaleChanged
779 || (newLocale != null && !newLocale.equals(mLastSystemLocale))) {
780 if (!updateOnlyWhenLocaleChanged) {
781 hideCurrentInputLocked(0, null);
782 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -0700783 unbindCurrentMethodLocked(true, false);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900784 }
785 if (DEBUG) {
786 Slog.i(TAG, "Locale has been changed to " + newLocale);
787 }
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900788 buildInputMethodListLocked(mMethodList, mMethodMap, resetDefaultEnabledIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900789 if (!updateOnlyWhenLocaleChanged) {
790 final String selectedImiId = mSettings.getSelectedInputMethod();
791 if (TextUtils.isEmpty(selectedImiId)) {
792 // This is the first time of the user switch and
793 // set the current ime to the proper one.
794 resetDefaultImeLocked(mContext);
795 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +0900796 } else {
797 // If the locale is changed, needs to reset the default ime
798 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900799 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800800 updateFromSettingsLocked(true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900801 mLastSystemLocale = newLocale;
802 if (!updateOnlyWhenLocaleChanged) {
803 try {
804 startInputInnerLocked();
805 } catch (RuntimeException e) {
806 Slog.w(TAG, "Unexpected exception", e);
807 }
808 }
809 }
810 }
811
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900812 private void resetStateIfCurrentLocaleChangedLocked() {
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900813 resetAllInternalStateLocked(true /* updateOnlyWhenLocaleChanged */,
814 true /* resetDefaultImeLocked */);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900815 }
816
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900817 private void switchUserLocked(int newUserId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900818 mSettings.setCurrentUserId(newUserId);
Kenny Guy2a764942014-04-02 13:29:20 +0100819 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900820 // InputMethodFileManager should be reset when the user is changed
821 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900822 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +0900823 // For secondary users, the list of enabled IMEs may not have been updated since the
824 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
825 // not be empty even if the IME has been uninstalled by the primary user.
826 // Even in such cases, IMMS works fine because it will find the most applicable
827 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900828 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900829 if (DEBUG) {
830 Slog.d(TAG, "Switch user: " + newUserId + " current ime = " + defaultImiId);
831 }
832 resetAllInternalStateLocked(false /* updateOnlyWhenLocaleChanged */,
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900833 initialUserSwitch /* needsToResetDefaultIme */);
834 if (initialUserSwitch) {
835 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mContext.getPackageManager(),
836 mSettings.getEnabledInputMethodListLocked());
837 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900838 }
839
Kenny Guy2a764942014-04-02 13:29:20 +0100840 void updateCurrentProfileIds() {
841 List<UserInfo> profiles =
842 UserManager.get(mContext).getProfiles(mSettings.getCurrentUserId());
843 int[] currentProfileIds = new int[profiles.size()]; // profiles will not be null
844 for (int i = 0; i < currentProfileIds.length; i++) {
845 currentProfileIds[i] = profiles.get(i).id;
Amith Yamasani734983f2014-03-04 16:48:05 -0800846 }
Kenny Guy2a764942014-04-02 13:29:20 +0100847 mSettings.setCurrentProfileIds(currentProfileIds);
Amith Yamasani734983f2014-03-04 16:48:05 -0800848 }
849
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800850 @Override
851 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
852 throws RemoteException {
853 try {
854 return super.onTransact(code, data, reply, flags);
855 } catch (RuntimeException e) {
856 // The input method manager only throws security exceptions, so let's
857 // log all others.
858 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -0700859 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800860 }
861 throw e;
862 }
863 }
864
Svetoslav Ganova0027152013-06-25 14:59:53 -0700865 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700866 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900867 if (DEBUG) {
868 Slog.d(TAG, "--- systemReady");
869 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700870 if (!mSystemReady) {
871 mSystemReady = true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900872 mKeyguardManager =
873 (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700874 mNotificationManager = (NotificationManager)
875 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
876 mStatusBar = statusBar;
877 statusBar.setIconVisibility("ime", false);
878 updateImeWindowStatusLocked();
satokb858c732011-07-22 19:54:34 +0900879 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
880 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +0900881 if (mShowOngoingImeSwitcherForPhones) {
882 mWindowManagerService.setOnHardKeyboardStatusChangeListener(
883 mHardKeyboardListener);
884 }
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900885 buildInputMethodListLocked(mMethodList, mMethodMap,
886 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satok0a1bcf42012-05-16 19:26:31 +0900887 if (!mImeSelectedOnBoot) {
888 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900889 resetStateIfCurrentLocaleChangedLocked();
890 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(
891 mContext.getPackageManager(),
892 mSettings.getEnabledInputMethodListLocked());
satok0a1bcf42012-05-16 19:26:31 +0900893 }
894 mLastSystemLocale = mRes.getConfiguration().locale;
Dianne Hackborncc278702009-09-02 23:07:23 -0700895 try {
896 startInputInnerLocked();
897 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800898 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700899 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700900 }
901 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800903
satok15452a42011-10-28 17:58:28 +0900904 private void setImeWindowVisibilityStatusHiddenLocked() {
905 mImeWindowVis = 0;
906 updateImeWindowStatusLocked();
907 }
908
satok3afd6c02011-11-18 08:38:19 +0900909 private void refreshImeWindowVisibilityLocked() {
910 final Configuration conf = mRes.getConfiguration();
911 final boolean haveHardKeyboard = conf.keyboard
912 != Configuration.KEYBOARD_NOKEYS;
913 final boolean hardKeyShown = haveHardKeyboard
914 && conf.hardKeyboardHidden
915 != Configuration.HARDKEYBOARDHIDDEN_YES;
John Spurlocke0980502013-10-25 11:59:29 -0400916
917 final boolean isScreenLocked = isKeyguardLocked();
918 final boolean inputActive = !isScreenLocked && (mInputShown || hardKeyShown);
John Spurlock36439b42013-10-23 16:36:47 -0400919 // We assume the softkeyboard is shown when the input is active as long as the
920 // hard keyboard is not shown.
921 final boolean inputVisible = inputActive && !hardKeyShown;
922 mImeWindowVis = (inputActive ? InputMethodService.IME_ACTIVE : 0)
923 | (inputVisible ? InputMethodService.IME_VISIBLE : 0);
satok3afd6c02011-11-18 08:38:19 +0900924 updateImeWindowStatusLocked();
925 }
926
satok15452a42011-10-28 17:58:28 +0900927 private void updateImeWindowStatusLocked() {
satokdbf29502011-08-25 15:28:23 +0900928 setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700929 }
930
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900931 // ---------------------------------------------------------------------------------------
932 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
933 // 1) it comes from the system process
934 // 2) the calling process' user id is identical to the current user id IMMS thinks.
935 private boolean calledFromValidUser() {
936 final int uid = Binder.getCallingUid();
937 final int userId = UserHandle.getUserId(uid);
938 if (DEBUG) {
939 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
940 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
941 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +0900942 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
943 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900944 }
Kenny Guy2a764942014-04-02 13:29:20 +0100945 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900946 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900947 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +0900948
949 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
950 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
951 // must not manage background users' states in any functions.
952 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
953 // by a token.
954 if (mContext.checkCallingOrSelfPermission(
955 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
956 == PackageManager.PERMISSION_GRANTED) {
957 if (DEBUG) {
958 Slog.d(TAG, "--- Access granted because the calling process has "
959 + "the INTERACT_ACROSS_USERS_FULL permission");
960 }
961 return true;
962 }
Satoshi Kataoka0766eb02013-07-31 18:30:13 +0900963 Slog.w(TAG, "--- IPC called from background users. Ignore. \n"
964 + InputMethodUtils.getStackTrace());
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +0900965 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900966 }
967
Yohei Yukawa22c97be2014-06-04 19:43:36 +0900968
969 /**
970 * Returns true iff the caller is identified to be the current input method with the token.
971 * @param token The window token given to the input method when it was started.
972 * @return true if and only if non-null valid token is specified.
973 */
974 private boolean calledWithValidToken(IBinder token) {
975 if (token == null || mCurToken != token) {
976 return false;
977 }
978 return true;
979 }
980
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900981 private boolean bindCurrentInputMethodService(
982 Intent service, ServiceConnection conn, int flags) {
983 if (service == null || conn == null) {
984 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
985 return false;
986 }
Amith Yamasani27b89e62013-01-16 12:30:11 -0800987 return mContext.bindServiceAsUser(service, conn, flags,
988 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900989 }
990
satoke7c6998e2011-06-03 17:57:59 +0900991 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800992 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900993 // TODO: Make this work even for non-current users?
994 if (!calledFromValidUser()) {
995 return Collections.emptyList();
996 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800997 synchronized (mMethodMap) {
998 return new ArrayList<InputMethodInfo>(mMethodList);
999 }
1000 }
1001
satoke7c6998e2011-06-03 17:57:59 +09001002 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001003 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001004 // TODO: Make this work even for non-current users?
1005 if (!calledFromValidUser()) {
1006 return Collections.emptyList();
1007 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001009 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001010 }
1011 }
1012
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001013 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001014 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001015 * @return enabled subtypes of the specified imi
1016 */
satoke7c6998e2011-06-03 17:57:59 +09001017 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001018 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001019 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001020 // TODO: Make this work even for non-current users?
1021 if (!calledFromValidUser()) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001022 return Collections.<InputMethodSubtype>emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001023 }
satok67ddf9c2010-11-17 09:45:54 +09001024 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001025 final InputMethodInfo imi;
1026 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001027 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001028 } else {
1029 imi = mMethodMap.get(imiId);
1030 }
1031 if (imi == null) {
1032 return Collections.<InputMethodSubtype>emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001033 }
1034 return mSettings.getEnabledInputMethodSubtypeListLocked(
1035 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001036 }
1037 }
1038
satoke7c6998e2011-06-03 17:57:59 +09001039 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040 public void addClient(IInputMethodClient client,
1041 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001042 if (!calledFromValidUser()) {
1043 return;
1044 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 synchronized (mMethodMap) {
1046 mClients.put(client.asBinder(), new ClientState(client,
1047 inputContext, uid, pid));
1048 }
1049 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001050
satoke7c6998e2011-06-03 17:57:59 +09001051 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001053 if (!calledFromValidUser()) {
1054 return;
1055 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001056 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001057 ClientState cs = mClients.remove(client.asBinder());
1058 if (cs != null) {
1059 clearClientSessionLocked(cs);
1060 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001061 }
1062 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001063
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064 void executeOrSendMessage(IInterface target, Message msg) {
1065 if (target.asBinder() instanceof Binder) {
1066 mCaller.sendMessage(msg);
1067 } else {
1068 handleMessage(msg);
1069 msg.recycle();
1070 }
1071 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001072
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001073 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001074 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001075 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001076 + mCurClient.client.asBinder());
1077 if (mBoundToMethod) {
1078 mBoundToMethod = false;
1079 if (mCurMethod != null) {
1080 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1081 MSG_UNBIND_INPUT, mCurMethod));
1082 }
1083 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001084
1085 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1086 MSG_SET_ACTIVE, 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001087 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1088 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1089 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001090 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001091
The Android Open Source Project10592532009-03-18 17:39:46 -07001092 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 }
1094 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001096 private int getImeShowFlags() {
1097 int flags = 0;
1098 if (mShowForced) {
1099 flags |= InputMethod.SHOW_FORCED
1100 | InputMethod.SHOW_EXPLICIT;
1101 } else if (mShowExplicitlyRequested) {
1102 flags |= InputMethod.SHOW_EXPLICIT;
1103 }
1104 return flags;
1105 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001107 private int getAppShowFlags() {
1108 int flags = 0;
1109 if (mShowForced) {
1110 flags |= InputMethodManager.SHOW_FORCED;
1111 } else if (!mShowExplicitlyRequested) {
1112 flags |= InputMethodManager.SHOW_IMPLICIT;
1113 }
1114 return flags;
1115 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001116
Dianne Hackborn7663d802012-02-24 13:08:49 -08001117 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001118 if (!mBoundToMethod) {
1119 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1120 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1121 mBoundToMethod = true;
1122 }
1123 final SessionState session = mCurClient.curSession;
1124 if (initial) {
1125 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1126 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
1127 } else {
1128 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1129 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
1130 }
1131 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001132 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001133 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001134 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001135 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001136 (session.channel != null ? session.channel.dup() : null),
1137 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001138 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001140 InputBindResult startInputLocked(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001141 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001142 // If no method is currently selected, do nothing.
1143 if (mCurMethodId == null) {
1144 return mNoBinding;
1145 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001147 ClientState cs = mClients.get(client.asBinder());
1148 if (cs == null) {
1149 throw new IllegalArgumentException("unknown client "
1150 + client.asBinder());
1151 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001153 try {
1154 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1155 // Check with the window manager to make sure this client actually
1156 // has a window with focus. If not, reject. This is thread safe
1157 // because if the focus changes some time before or after, the
1158 // next client receiving focus that has any interest in input will
1159 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001160 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001161 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1162 return null;
1163 }
1164 } catch (RemoteException e) {
1165 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001166
Dianne Hackborn7663d802012-02-24 13:08:49 -08001167 return startInputUncheckedLocked(cs, inputContext, attribute, controlFlags);
1168 }
1169
1170 InputBindResult startInputUncheckedLocked(ClientState cs,
1171 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
1172 // If no method is currently selected, do nothing.
1173 if (mCurMethodId == null) {
1174 return mNoBinding;
1175 }
1176
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001177 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001178 // Was the keyguard locked when switching over to the new client?
1179 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 // If the client is changing, we need to switch over to the new
1181 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001182 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001183 if (DEBUG) Slog.v(TAG, "switching to client: client = "
John Spurlocke0980502013-10-25 11:59:29 -04001184 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185
1186 // If the screen is on, inform the new client it is active
1187 if (mScreenOn) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001188 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
1189 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001190 }
1191 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001192
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001193 // Bump up the sequence for this client and attach it.
1194 mCurSeq++;
1195 if (mCurSeq <= 0) mCurSeq = 1;
1196 mCurClient = cs;
1197 mCurInputContext = inputContext;
1198 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001200 // Check if the input method is changing.
1201 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1202 if (cs.curSession != null) {
1203 // Fast case: if we are already connected to the input method,
1204 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001205 return attachNewInputLocked(
1206 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001207 }
1208 if (mHaveConnection) {
1209 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 // Return to client, and we will get back with it when
1211 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001212 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001213 return new InputBindResult(null, null, mCurId, mCurSeq,
1214 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 } else if (SystemClock.uptimeMillis()
1216 < (mLastBindTime+TIME_TO_RECONNECT)) {
1217 // In this case we have connected to the service, but
1218 // don't yet have its interface. If it hasn't been too
1219 // long since we did the connection, we'll return to
1220 // the client and wait to get the service interface so
1221 // we can report back. If it has been too long, we want
1222 // to fall through so we can try a disconnect/reconnect
1223 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001224 return new InputBindResult(null, null, mCurId, mCurSeq,
1225 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001227 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1228 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 }
1230 }
1231 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001232
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001233 return startInputInnerLocked();
1234 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001235
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001236 InputBindResult startInputInnerLocked() {
1237 if (mCurMethodId == null) {
1238 return mNoBinding;
1239 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001240
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001241 if (!mSystemReady) {
1242 // If the system is not yet ready, we shouldn't be running third
1243 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001244 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1245 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001246 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001248 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1249 if (info == null) {
1250 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1251 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001252
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001253 unbindCurrentMethodLocked(false, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1256 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001257 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1258 com.android.internal.R.string.input_method_binding_label);
1259 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1260 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001261 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001262 | Context.BIND_NOT_VISIBLE | Context.BIND_NOT_FOREGROUND
1263 | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 mLastBindTime = SystemClock.uptimeMillis();
1265 mHaveConnection = true;
1266 mCurId = info.getId();
1267 mCurToken = new Binder();
1268 try {
Craig Mautnerca0ac712013-03-14 09:43:02 -07001269 if (true || DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001270 mIWindowManager.addWindowToken(mCurToken,
1271 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1272 } catch (RemoteException e) {
1273 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001274 return new InputBindResult(null, null, mCurId, mCurSeq,
1275 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001276 } else {
1277 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001278 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001279 + mCurIntent);
1280 }
1281 return null;
1282 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001283
satoke7c6998e2011-06-03 17:57:59 +09001284 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001285 public InputBindResult startInput(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001286 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001287 if (!calledFromValidUser()) {
1288 return null;
1289 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001290 synchronized (mMethodMap) {
1291 final long ident = Binder.clearCallingIdentity();
1292 try {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001293 return startInputLocked(client, inputContext, attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294 } finally {
1295 Binder.restoreCallingIdentity(ident);
1296 }
1297 }
1298 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001299
satoke7c6998e2011-06-03 17:57:59 +09001300 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001301 public void finishInput(IInputMethodClient client) {
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 void onServiceConnected(ComponentName name, IBinder service) {
1306 synchronized (mMethodMap) {
1307 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1308 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001309 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001310 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001311 unbindCurrentMethodLocked(false, false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001312 return;
1313 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001314 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001315 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1316 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001317 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001318 clearClientSessionLocked(mCurClient);
1319 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320 }
1321 }
1322 }
1323 }
1324
Jeff Brownc28867a2013-03-26 15:42:39 -07001325 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1326 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001327 synchronized (mMethodMap) {
1328 if (mCurMethod != null && method != null
1329 && mCurMethod.asBinder() == method.asBinder()) {
1330 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001331 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001332 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001333 method, session, channel);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001334 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001335 if (res.method != null) {
1336 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
1337 MSG_BIND_METHOD, mCurClient.client, res));
1338 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001339 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001340 }
1341 }
1342 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001343
1344 // Session abandoned. Close its associated input channel.
1345 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001346 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001347
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001348 void unbindCurrentMethodLocked(boolean reportToClient, boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001349 if (mVisibleBound) {
1350 mContext.unbindService(mVisibleConnection);
1351 mVisibleBound = false;
1352 }
1353
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001354 if (mHaveConnection) {
1355 mContext.unbindService(this);
1356 mHaveConnection = false;
1357 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001358
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001359 if (mCurToken != null) {
1360 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001361 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001362 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001363 // The current IME is shown. Hence an IME switch (transition) is happening.
1364 mWindowManagerService.saveLastInputMethodWindowForTransition();
1365 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001366 mIWindowManager.removeWindowToken(mCurToken);
1367 } catch (RemoteException e) {
1368 }
1369 mCurToken = null;
1370 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001371
The Android Open Source Project10592532009-03-18 17:39:46 -07001372 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001373 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001374
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001375 if (reportToClient && mCurClient != null) {
1376 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1377 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1378 }
1379 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001380
1381 void requestClientSessionLocked(ClientState cs) {
1382 if (!cs.sessionRequested) {
1383 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1384 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1385 cs.sessionRequested = true;
1386 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1387 MSG_CREATE_SESSION, mCurMethod, channels[1],
1388 new MethodCallback(this, mCurMethod, channels[0])));
1389 }
1390 }
1391
1392 void clearClientSessionLocked(ClientState cs) {
1393 finishSessionLocked(cs.curSession);
1394 cs.curSession = null;
1395 cs.sessionRequested = false;
1396 }
1397
1398 private void finishSessionLocked(SessionState sessionState) {
1399 if (sessionState != null) {
1400 if (sessionState.session != null) {
1401 try {
1402 sessionState.session.finishSession();
1403 } catch (RemoteException e) {
1404 Slog.w(TAG, "Session failed to close due to remote exception", e);
1405 setImeWindowVisibilityStatusHiddenLocked();
1406 }
1407 sessionState.session = null;
1408 }
1409 if (sessionState.channel != null) {
1410 sessionState.channel.dispose();
1411 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001412 }
1413 }
1414 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001415
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001416 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001417 if (mCurMethod != null) {
1418 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001419 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001420 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001421
Jeff Brownc28867a2013-03-26 15:42:39 -07001422 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001423 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424 mCurMethod = null;
1425 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001426 if (mStatusBar != null) {
1427 mStatusBar.setIconVisibility("ime", false);
1428 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001429 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001430
satoke7c6998e2011-06-03 17:57:59 +09001431 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001432 public void onServiceDisconnected(ComponentName name) {
1433 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001434 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001435 + " mCurIntent=" + mCurIntent);
1436 if (mCurMethod != null && mCurIntent != null
1437 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001438 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001439 // We consider this to be a new bind attempt, since the system
1440 // should now try to restart the service for us.
1441 mLastBindTime = SystemClock.uptimeMillis();
1442 mShowRequested = mInputShown;
1443 mInputShown = false;
1444 if (mCurClient != null) {
1445 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1446 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1447 }
1448 }
1449 }
1450 }
1451
satokf9f01002011-05-19 21:31:50 +09001452 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001453 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
1454 long ident = Binder.clearCallingIdentity();
1455 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001456 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09001457 if (!calledWithValidToken(token)) {
1458 final int uid = Binder.getCallingUid();
1459 Slog.e(TAG, "Ignoring updateStatusIcon due to an invalid token. uid:" + uid
1460 + " token:" + token);
1461 return;
1462 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001463 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001464 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001465 if (mStatusBar != null) {
1466 mStatusBar.setIconVisibility("ime", false);
1467 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001468 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001469 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001470 CharSequence contentDescription = null;
1471 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001472 // Use PackageManager to load label
1473 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001474 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001475 mIPackageManager.getApplicationInfo(packageName, 0,
1476 mSettings.getCurrentUserId()));
1477 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001478 /* ignore */
1479 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001480 if (mStatusBar != null) {
1481 mStatusBar.setIcon("ime", packageName, iconId, 0,
1482 contentDescription != null
1483 ? contentDescription.toString() : null);
1484 mStatusBar.setIconVisibility("ime", true);
1485 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001486 }
1487 }
1488 } finally {
1489 Binder.restoreCallingIdentity(ident);
1490 }
1491 }
1492
satok7cfc0ed2011-06-20 21:29:36 +09001493 private boolean needsToShowImeSwitchOngoingNotification() {
1494 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04001495 if (mSwitchingDialog != null) return false;
satok2c93efc2012-04-02 19:33:47 +09001496 if (isScreenLocked()) return false;
satok7cfc0ed2011-06-20 21:29:36 +09001497 synchronized (mMethodMap) {
1498 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1499 final int N = imis.size();
satokb6359412011-06-28 17:47:41 +09001500 if (N > 2) return true;
1501 if (N < 1) return false;
1502 int nonAuxCount = 0;
1503 int auxCount = 0;
1504 InputMethodSubtype nonAuxSubtype = null;
1505 InputMethodSubtype auxSubtype = null;
satok7cfc0ed2011-06-20 21:29:36 +09001506 for(int i = 0; i < N; ++i) {
1507 final InputMethodInfo imi = imis.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001508 final List<InputMethodSubtype> subtypes =
1509 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satok7cfc0ed2011-06-20 21:29:36 +09001510 final int subtypeCount = subtypes.size();
1511 if (subtypeCount == 0) {
satokb6359412011-06-28 17:47:41 +09001512 ++nonAuxCount;
satok7cfc0ed2011-06-20 21:29:36 +09001513 } else {
1514 for (int j = 0; j < subtypeCount; ++j) {
satokb6359412011-06-28 17:47:41 +09001515 final InputMethodSubtype subtype = subtypes.get(j);
1516 if (!subtype.isAuxiliary()) {
1517 ++nonAuxCount;
1518 nonAuxSubtype = subtype;
1519 } else {
1520 ++auxCount;
1521 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001522 }
1523 }
1524 }
satok7cfc0ed2011-06-20 21:29:36 +09001525 }
satokb6359412011-06-28 17:47:41 +09001526 if (nonAuxCount > 1 || auxCount > 1) {
1527 return true;
1528 } else if (nonAuxCount == 1 && auxCount == 1) {
1529 if (nonAuxSubtype != null && auxSubtype != null
satok9747f892011-09-12 15:56:40 +09001530 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1531 || auxSubtype.overridesImplicitlyEnabledSubtype()
1532 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
satokb6359412011-06-28 17:47:41 +09001533 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1534 return false;
1535 }
1536 return true;
1537 }
1538 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001539 }
satok7cfc0ed2011-06-20 21:29:36 +09001540 }
1541
John Spurlocke0980502013-10-25 11:59:29 -04001542 private boolean isKeyguardLocked() {
1543 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1544 }
1545
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001546 // Caution! This method is called in this class. Handle multi-user carefully
satokdbf29502011-08-25 15:28:23 +09001547 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001548 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001549 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001550 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001551 try {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001552 if (!calledWithValidToken(token)) {
1553 final int uid = Binder.getCallingUid();
1554 Slog.e(TAG, "Ignoring setImeWindowStatus due to an invalid token. uid:" + uid
1555 + " token:" + token);
satok06487a52010-10-29 11:37:18 +09001556 return;
1557 }
satok06487a52010-10-29 11:37:18 +09001558 synchronized (mMethodMap) {
John Spurlocke0980502013-10-25 11:59:29 -04001559 // apply policy for binder calls
1560 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
1561 vis = 0;
1562 }
Joe Onorato857fd9b2011-01-27 15:08:35 -08001563 mImeWindowVis = vis;
1564 mBackDisposition = backDisposition;
jungheang.lee217fd292013-02-26 16:53:22 +09001565 final boolean iconVisibility = ((vis & (InputMethodService.IME_ACTIVE)) != 0)
1566 && (mWindowManagerService.isHardKeyboardAvailable()
1567 || (vis & (InputMethodService.IME_VISIBLE)) != 0);
Jason Monkb605fec2014-05-02 17:04:10 -04001568 final boolean needsToShowImeSwitcher = iconVisibility
1569 && needsToShowImeSwitchOngoingNotification();
1570 if (mStatusBar != null) {
1571 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
1572 needsToShowImeSwitcher);
1573 }
satok5bc8e732011-07-22 21:07:23 +09001574 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Jason Monkb605fec2014-05-02 17:04:10 -04001575 if (imi != null && needsToShowImeSwitcher) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001576 // Used to load label
satok7cfc0ed2011-06-20 21:29:36 +09001577 final CharSequence title = mRes.getText(
1578 com.android.internal.R.string.select_input_method);
Satoshi Kataokab2827262013-07-04 19:43:14 +09001579 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
1580 mContext, imi, mCurrentSubtype);
satok5bc8e732011-07-22 21:07:23 +09001581
satok7cfc0ed2011-06-20 21:29:36 +09001582 mImeSwitcherNotification.setLatestEventInfo(
satok5bc8e732011-07-22 21:07:23 +09001583 mContext, title, summary, mImeSwitchPendingIntent);
Jason Monke463c742014-05-02 12:31:45 -04001584 if ((mNotificationManager != null)
1585 && !mWindowManagerService.hasNavigationBar()) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001586 if (DEBUG) {
Satoshi Kataokab2827262013-07-04 19:43:14 +09001587 Slog.d(TAG, "--- show notification: label = " + summary);
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001588 }
1589 mNotificationManager.notifyAsUser(null,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001590 com.android.internal.R.string.select_input_method,
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001591 mImeSwitcherNotification, UserHandle.ALL);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001592 mNotificationShown = true;
1593 }
satok7cfc0ed2011-06-20 21:29:36 +09001594 } else {
Dianne Hackborn661cd522011-08-22 00:26:20 -07001595 if (mNotificationShown && mNotificationManager != null) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001596 if (DEBUG) {
1597 Slog.d(TAG, "--- hide notification");
1598 }
1599 mNotificationManager.cancelAsUser(null,
1600 com.android.internal.R.string.select_input_method, UserHandle.ALL);
satok7cfc0ed2011-06-20 21:29:36 +09001601 mNotificationShown = false;
1602 }
1603 }
satok06487a52010-10-29 11:37:18 +09001604 }
1605 } finally {
1606 Binder.restoreCallingIdentity(ident);
1607 }
1608 }
1609
satoke7c6998e2011-06-03 17:57:59 +09001610 @Override
satokf9f01002011-05-19 21:31:50 +09001611 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001612 if (!calledFromValidUser()) {
1613 return;
1614 }
satokf9f01002011-05-19 21:31:50 +09001615 synchronized (mMethodMap) {
1616 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1617 for (int i = 0; i < spans.length; ++i) {
1618 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001619 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001620 mSecureSuggestionSpans.put(ss, currentImi);
1621 }
1622 }
1623 }
1624 }
1625
satoke7c6998e2011-06-03 17:57:59 +09001626 @Override
satokf9f01002011-05-19 21:31:50 +09001627 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001628 if (!calledFromValidUser()) {
1629 return false;
1630 }
satokf9f01002011-05-19 21:31:50 +09001631 synchronized (mMethodMap) {
1632 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1633 // TODO: Do not send the intent if the process of the targetImi is already dead.
1634 if (targetImi != null) {
1635 final String[] suggestions = span.getSuggestions();
1636 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001637 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001638 final Intent intent = new Intent();
1639 // Ensures that only a class in the original IME package will receive the
1640 // notification.
satok42c5a162011-05-26 16:46:14 +09001641 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001642 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1643 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1644 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1645 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001646 final long ident = Binder.clearCallingIdentity();
1647 try {
1648 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1649 } finally {
1650 Binder.restoreCallingIdentity(ident);
1651 }
satokf9f01002011-05-19 21:31:50 +09001652 return true;
1653 }
1654 }
1655 return false;
1656 }
1657
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001658 void updateFromSettingsLocked(boolean enabledMayChange) {
1659 if (enabledMayChange) {
1660 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1661 for (int i=0; i<enabled.size(); i++) {
1662 // We allow the user to select "disabled until used" apps, so if they
1663 // are enabling one of those here we now need to make it enabled.
1664 InputMethodInfo imm = enabled.get(i);
1665 try {
1666 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1667 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1668 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09001669 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001670 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001671 if (DEBUG) {
1672 Slog.d(TAG, "Update state(" + imm.getId()
1673 + "): DISABLED_UNTIL_USED -> DEFAULT");
1674 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001675 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1676 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001677 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
1678 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001679 }
1680 } catch (RemoteException e) {
1681 }
1682 }
1683 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001684 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1685 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1686 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1687 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001688 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001689 // There is no input method selected, try to choose new applicable input method.
1690 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001691 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001692 }
1693 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001694 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001695 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001696 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001697 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001698 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001699 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001700 }
satokf3db1af2010-11-23 13:34:33 +09001701 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001702 } else {
1703 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001704 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001705 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001706 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09001707 // Here is not the perfect place to reset the switching controller. Ideally
1708 // mSwitchingController and mSettings should be able to share the same state.
1709 // TODO: Make sure that mSwitchingController and mSettings are sharing the
1710 // the same enabled IMEs list.
1711 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001712 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001713
satokab751aa2010-09-14 19:17:36 +09001714 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001715 InputMethodInfo info = mMethodMap.get(id);
1716 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001717 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001718 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001719
satokd81e9502012-05-21 12:58:45 +09001720 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001721 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09001722 final int subtypeCount = info.getSubtypeCount();
1723 if (subtypeCount <= 0) {
1724 return;
satokcd7cd292010-11-20 15:46:23 +09001725 }
satokd81e9502012-05-21 12:58:45 +09001726 final InputMethodSubtype oldSubtype = mCurrentSubtype;
1727 final InputMethodSubtype newSubtype;
1728 if (subtypeId >= 0 && subtypeId < subtypeCount) {
1729 newSubtype = info.getSubtypeAt(subtypeId);
1730 } else {
1731 // If subtype is null, try to find the most applicable one from
1732 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001733 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09001734 }
1735 if (newSubtype == null || oldSubtype == null) {
1736 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
1737 + ", new subtype = " + newSubtype);
1738 return;
1739 }
1740 if (newSubtype != oldSubtype) {
1741 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1742 if (mCurMethod != null) {
1743 try {
1744 refreshImeWindowVisibilityLocked();
1745 mCurMethod.changeInputMethodSubtype(newSubtype);
1746 } catch (RemoteException e) {
1747 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
satokab751aa2010-09-14 19:17:36 +09001748 }
1749 }
1750 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001751 return;
1752 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001753
satokd81e9502012-05-21 12:58:45 +09001754 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001755 final long ident = Binder.clearCallingIdentity();
1756 try {
satokab751aa2010-09-14 19:17:36 +09001757 // Set a subtype to this input method.
1758 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001759 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1760 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1761 // because mCurMethodId is stored as a history in
1762 // setSelectedInputMethodAndSubtypeLocked().
1763 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001764
1765 if (ActivityManagerNative.isSystemReady()) {
1766 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001767 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001768 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07001769 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001770 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001771 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001772 } finally {
1773 Binder.restoreCallingIdentity(ident);
1774 }
1775 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001776
satok42c5a162011-05-26 16:46:14 +09001777 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001778 public boolean showSoftInput(IInputMethodClient client, int flags,
1779 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001780 if (!calledFromValidUser()) {
1781 return false;
1782 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001783 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001784 long ident = Binder.clearCallingIdentity();
1785 try {
1786 synchronized (mMethodMap) {
1787 if (mCurClient == null || client == null
1788 || mCurClient.client.asBinder() != client.asBinder()) {
1789 try {
1790 // We need to check if this is the current client with
1791 // focus in the window manager, to allow this call to
1792 // be made before input is started in it.
1793 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001794 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001795 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001796 }
1797 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001798 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001799 }
1800 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001801
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001802 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001803 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001804 }
1805 } finally {
1806 Binder.restoreCallingIdentity(ident);
1807 }
1808 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001809
The Android Open Source Project4df24232009-03-05 14:34:35 -08001810 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001811 mShowRequested = true;
1812 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1813 mShowExplicitlyRequested = true;
1814 }
1815 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1816 mShowExplicitlyRequested = true;
1817 mShowForced = true;
1818 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001819
Dianne Hackborncc278702009-09-02 23:07:23 -07001820 if (!mSystemReady) {
1821 return false;
1822 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001823
The Android Open Source Project4df24232009-03-05 14:34:35 -08001824 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001825 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001826 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001827 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1828 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1829 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001830 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001831 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001832 bindCurrentInputMethodService(
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001833 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE
1834 | Context.BIND_TREAT_LIKE_ACTIVITY);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001835 mVisibleBound = true;
1836 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001837 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001838 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09001839 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001840 // The client has asked to have the input method shown, but
1841 // we have been sitting here too long with a connection to the
1842 // service and no interface received, so let's disconnect/connect
1843 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001844 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001845 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09001846 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001847 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001848 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001849 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001850 } else {
1851 if (DEBUG) {
1852 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
1853 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
1854 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001855 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001856
The Android Open Source Project4df24232009-03-05 14:34:35 -08001857 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001858 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001859
satok42c5a162011-05-26 16:46:14 +09001860 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001861 public boolean hideSoftInput(IInputMethodClient client, int flags,
1862 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001863 if (!calledFromValidUser()) {
1864 return false;
1865 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001866 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001867 long ident = Binder.clearCallingIdentity();
1868 try {
1869 synchronized (mMethodMap) {
1870 if (mCurClient == null || client == null
1871 || mCurClient.client.asBinder() != client.asBinder()) {
1872 try {
1873 // We need to check if this is the current client with
1874 // focus in the window manager, to allow this call to
1875 // be made before input is started in it.
1876 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001877 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1878 + uid + ": " + client);
satok15452a42011-10-28 17:58:28 +09001879 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001880 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001881 }
1882 } catch (RemoteException e) {
satok15452a42011-10-28 17:58:28 +09001883 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001884 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001885 }
1886 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001887
Joe Onorato8a9b2202010-02-26 18:56:32 -08001888 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001889 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001890 }
1891 } finally {
1892 Binder.restoreCallingIdentity(ident);
1893 }
1894 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001895
The Android Open Source Project4df24232009-03-05 14:34:35 -08001896 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001897 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1898 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001899 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 -08001900 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001901 }
1902 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001903 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 -08001904 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001905 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001906 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001907 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001908 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1909 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1910 res = true;
1911 } else {
1912 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001913 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001914 if (mHaveConnection && mVisibleBound) {
1915 mContext.unbindService(mVisibleConnection);
1916 mVisibleBound = false;
1917 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001918 mInputShown = false;
1919 mShowRequested = false;
1920 mShowExplicitlyRequested = false;
1921 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001922 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001923 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001924
satok42c5a162011-05-26 16:46:14 +09001925 @Override
Dianne Hackborn7663d802012-02-24 13:08:49 -08001926 public InputBindResult windowGainedFocus(IInputMethodClient client, IBinder windowToken,
1927 int controlFlags, int softInputMode, int windowFlags,
1928 EditorInfo attribute, IInputContext inputContext) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09001929 // Needs to check the validity before clearing calling identity
1930 final boolean calledFromValidUser = calledFromValidUser();
1931
Dianne Hackborn7663d802012-02-24 13:08:49 -08001932 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001933 long ident = Binder.clearCallingIdentity();
1934 try {
1935 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001936 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
Dianne Hackborn7663d802012-02-24 13:08:49 -08001937 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001938 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08001939 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001940
Dianne Hackborn7663d802012-02-24 13:08:49 -08001941 ClientState cs = mClients.get(client.asBinder());
1942 if (cs == null) {
1943 throw new IllegalArgumentException("unknown client "
1944 + client.asBinder());
1945 }
1946
1947 try {
1948 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1949 // Check with the window manager to make sure this client actually
1950 // has a window with focus. If not, reject. This is thread safe
1951 // because if the focus changes some time before or after, the
1952 // next client receiving focus that has any interest in input will
1953 // be calling through here after that change happens.
1954 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
1955 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1956 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001957 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001958 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001959 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001960
Satoshi Kataoka8d033052012-11-19 17:30:40 +09001961 if (!calledFromValidUser) {
1962 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
1963 Slog.w(TAG, "If you want to interect with IME, you need "
1964 + "android.permission.INTERACT_ACROSS_USERS_FULL");
1965 hideCurrentInputLocked(0, null);
1966 return null;
1967 }
1968
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001969 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07001970 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09001971 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001972 if (attribute != null) {
1973 return startInputUncheckedLocked(cs, inputContext, attribute,
1974 controlFlags);
1975 }
1976 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001977 }
1978 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001979
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001980 // Should we auto-show the IME even if the caller has not
1981 // specified what should be done with it?
1982 // We only do this automatically if the window can resize
1983 // to accommodate the IME (so what the user sees will give
1984 // them good context without input information being obscured
1985 // by the IME) or if running on a large screen where there
1986 // is more room for the target window + IME.
1987 final boolean doAutoShow =
1988 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1989 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1990 || mRes.getConfiguration().isLayoutSizeAtLeast(
1991 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001992 final boolean isTextEditor =
1993 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
1994
1995 // We want to start input before showing the IME, but after closing
1996 // it. We want to do this after closing it to help the IME disappear
1997 // more quickly (not get stuck behind it initializing itself for the
1998 // new focused input, even if its window wants to hide the IME).
1999 boolean didStart = false;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002000
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002001 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2002 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002003 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002004 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2005 // There is no focus view, and this window will
2006 // be behind any soft input window, so hide the
2007 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002008 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002009 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002010 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002011 } else if (isTextEditor && doAutoShow && (softInputMode &
2012 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002013 // There is a focus view, and we are navigating forward
2014 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002015 // We only do this automatically if the window can resize
2016 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002017 // them good context without input information being obscured
2018 // by the IME) or if running on a large screen where there
2019 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002020 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002021 if (attribute != null) {
2022 res = startInputUncheckedLocked(cs, inputContext, attribute,
2023 controlFlags);
2024 didStart = true;
2025 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002026 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002027 }
2028 break;
2029 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2030 // Do nothing.
2031 break;
2032 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2033 if ((softInputMode &
2034 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002035 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002036 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002037 }
2038 break;
2039 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002040 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002041 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002042 break;
2043 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2044 if ((softInputMode &
2045 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002046 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002047 if (attribute != null) {
2048 res = startInputUncheckedLocked(cs, inputContext, attribute,
2049 controlFlags);
2050 didStart = true;
2051 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002052 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002053 }
2054 break;
2055 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002056 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002057 if (attribute != null) {
2058 res = startInputUncheckedLocked(cs, inputContext, attribute,
2059 controlFlags);
2060 didStart = true;
2061 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002062 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002063 break;
2064 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002065
2066 if (!didStart && attribute != null) {
2067 res = startInputUncheckedLocked(cs, inputContext, attribute,
2068 controlFlags);
2069 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002070 }
2071 } finally {
2072 Binder.restoreCallingIdentity(ident);
2073 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002074
2075 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002076 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002077
satok42c5a162011-05-26 16:46:14 +09002078 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002079 public void showInputMethodPickerFromClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002080 if (!calledFromValidUser()) {
2081 return;
2082 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002083 synchronized (mMethodMap) {
2084 if (mCurClient == null || client == null
2085 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002086 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002087 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002088 }
2089
satok440aab52010-11-25 09:43:11 +09002090 // Always call subtype picker, because subtype picker is a superset of input method
2091 // picker.
satokab751aa2010-09-14 19:17:36 +09002092 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
2093 }
2094 }
2095
satok42c5a162011-05-26 16:46:14 +09002096 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002097 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002098 if (!calledFromValidUser()) {
2099 return;
2100 }
satok28203512010-11-24 11:06:49 +09002101 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2102 }
2103
satok42c5a162011-05-26 16:46:14 +09002104 @Override
satok28203512010-11-24 11:06:49 +09002105 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002106 if (!calledFromValidUser()) {
2107 return;
2108 }
satok28203512010-11-24 11:06:49 +09002109 synchronized (mMethodMap) {
2110 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002111 setInputMethodWithSubtypeIdLocked(token, id,
2112 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2113 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002114 } else {
2115 setInputMethod(token, id);
2116 }
2117 }
satokab751aa2010-09-14 19:17:36 +09002118 }
2119
satok42c5a162011-05-26 16:46:14 +09002120 @Override
satokb416a712010-11-25 20:42:14 +09002121 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002122 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002123 if (!calledFromValidUser()) {
2124 return;
2125 }
satokb416a712010-11-25 20:42:14 +09002126 synchronized (mMethodMap) {
2127 if (mCurClient == null || client == null
2128 || mCurClient.client.asBinder() != client.asBinder()) {
2129 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
2130 }
satok7fee71f2010-12-17 18:54:26 +09002131 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2132 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002133 }
2134 }
2135
satok4fc87d62011-05-20 16:13:43 +09002136 @Override
satok735cf382010-11-11 20:40:09 +09002137 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002138 if (!calledFromValidUser()) {
2139 return false;
2140 }
satok735cf382010-11-11 20:40:09 +09002141 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002142 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002143 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002144 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002145 lastImi = mMethodMap.get(lastIme.first);
2146 } else {
2147 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002148 }
satok4fc87d62011-05-20 16:13:43 +09002149 String targetLastImiId = null;
2150 int subtypeId = NOT_A_SUBTYPE_ID;
2151 if (lastIme != null && lastImi != null) {
2152 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
2153 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
2154 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2155 : mCurrentSubtype.hashCode();
2156 // If the last IME is the same as the current IME and the last subtype is not
2157 // defined, there is no need to switch to the last IME.
2158 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2159 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002160 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002161 }
2162 }
2163
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002164 if (TextUtils.isEmpty(targetLastImiId)
2165 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002166 // This is a safety net. If the currentSubtype can't be added to the history
2167 // and the framework couldn't find the last ime, we will make the last ime be
2168 // the most applicable enabled keyboard subtype of the system imes.
2169 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2170 if (enabled != null) {
2171 final int N = enabled.size();
2172 final String locale = mCurrentSubtype == null
2173 ? mRes.getConfiguration().locale.toString()
2174 : mCurrentSubtype.getLocale();
2175 for (int i = 0; i < N; ++i) {
2176 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002177 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002178 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002179 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2180 InputMethodUtils.getSubtypes(imi),
2181 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002182 if (keyboardSubtype != null) {
2183 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002184 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002185 imi, keyboardSubtype.hashCode());
2186 if(keyboardSubtype.getLocale().equals(locale)) {
2187 break;
2188 }
2189 }
2190 }
2191 }
2192 }
2193 }
2194
2195 if (!TextUtils.isEmpty(targetLastImiId)) {
2196 if (DEBUG) {
2197 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2198 + ", from: " + mCurMethodId + ", " + subtypeId);
2199 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002200 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002201 return true;
2202 } else {
2203 return false;
2204 }
satok735cf382010-11-11 20:40:09 +09002205 }
2206 }
2207
satoke7c6998e2011-06-03 17:57:59 +09002208 @Override
satok688bd472012-02-09 20:09:17 +09002209 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002210 if (!calledFromValidUser()) {
2211 return false;
2212 }
satok688bd472012-02-09 20:09:17 +09002213 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002214 if (!calledWithValidToken(token)) {
2215 final int uid = Binder.getCallingUid();
2216 Slog.e(TAG, "Ignoring switchToNextInputMethod due to an invalid token. uid:" + uid
2217 + " token:" + token);
2218 return false;
2219 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002220 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
satok688bd472012-02-09 20:09:17 +09002221 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2222 if (nextSubtype == null) {
2223 return false;
2224 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002225 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2226 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002227 return true;
2228 }
2229 }
2230
2231 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002232 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2233 if (!calledFromValidUser()) {
2234 return false;
2235 }
2236 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002237 if (!calledWithValidToken(token)) {
2238 final int uid = Binder.getCallingUid();
2239 Slog.e(TAG, "Ignoring shouldOfferSwitchingToNextInputMethod due to an invalid "
2240 + "token. uid:" + uid + " token:" + token);
2241 return false;
2242 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002243 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002244 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2245 if (nextSubtype == null) {
2246 return false;
2247 }
2248 return true;
2249 }
2250 }
2251
2252 @Override
satok68f1b782011-04-11 14:26:04 +09002253 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002254 if (!calledFromValidUser()) {
2255 return null;
2256 }
satok68f1b782011-04-11 14:26:04 +09002257 synchronized (mMethodMap) {
2258 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2259 // TODO: Handle the case of the last IME with no subtypes
2260 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2261 || TextUtils.isEmpty(lastIme.second)) return null;
2262 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2263 if (lastImi == null) return null;
2264 try {
2265 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002266 final int lastSubtypeId =
2267 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002268 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2269 return null;
2270 }
2271 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002272 } catch (NumberFormatException e) {
2273 return null;
2274 }
2275 }
2276 }
2277
satoke7c6998e2011-06-03 17:57:59 +09002278 @Override
satokee5e77c2011-09-02 18:50:15 +09002279 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002280 if (!calledFromValidUser()) {
2281 return;
2282 }
satok91e88122011-07-18 11:11:42 +09002283 // By this IPC call, only a process which shares the same uid with the IME can add
2284 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09002285 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09002286 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002287 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002288 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002289 final String[] packageInfos;
2290 try {
2291 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2292 } catch (RemoteException e) {
2293 Slog.e(TAG, "Failed to get package infos");
2294 return;
2295 }
satok91e88122011-07-18 11:11:42 +09002296 if (packageInfos != null) {
2297 final int packageNum = packageInfos.length;
2298 for (int i = 0; i < packageNum; ++i) {
2299 if (packageInfos[i].equals(imi.getPackageName())) {
2300 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002301 final long ident = Binder.clearCallingIdentity();
2302 try {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002303 buildInputMethodListLocked(mMethodList, mMethodMap,
2304 false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002305 } finally {
2306 Binder.restoreCallingIdentity(ident);
2307 }
satokee5e77c2011-09-02 18:50:15 +09002308 return;
satok91e88122011-07-18 11:11:42 +09002309 }
2310 }
2311 }
satoke7c6998e2011-06-03 17:57:59 +09002312 }
satokee5e77c2011-09-02 18:50:15 +09002313 return;
satoke7c6998e2011-06-03 17:57:59 +09002314 }
2315
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002316 @Override
2317 public int getInputMethodWindowVisibleHeight() {
2318 return mWindowManagerService.getInputMethodWindowVisibleHeight();
2319 }
2320
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002321 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002322 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002323 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002324 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002325 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002326 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002327 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
2328 if (DEBUG) {
2329 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
2330 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
2331 + " actual: " + sequenceNumber);
2332 }
2333 return;
2334 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002335 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2336 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09002337 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002338 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002339 }
2340 }
2341
satok28203512010-11-24 11:06:49 +09002342 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002343 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002344 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
2345 }
2346 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002347
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002348 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
2349 if (token == null) {
2350 if (mContext.checkCallingOrSelfPermission(
2351 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2352 != PackageManager.PERMISSION_GRANTED) {
2353 throw new SecurityException(
2354 "Using null token requires permission "
2355 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002356 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002357 } else if (mCurToken != token) {
2358 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2359 + " token: " + token);
2360 return;
2361 }
2362
2363 final long ident = Binder.clearCallingIdentity();
2364 try {
2365 setInputMethodLocked(id, subtypeId);
2366 } finally {
2367 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002368 }
2369 }
2370
satok42c5a162011-05-26 16:46:14 +09002371 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002372 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002373 if (!calledFromValidUser()) {
2374 return;
2375 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002376 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002377 if (!calledWithValidToken(token)) {
2378 final int uid = Binder.getCallingUid();
2379 Slog.e(TAG, "Ignoring hideInputMethod due to an invalid token. uid:"
2380 + uid + " token:" + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002381 return;
2382 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002383 long ident = Binder.clearCallingIdentity();
2384 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002385 hideCurrentInputLocked(flags, null);
2386 } finally {
2387 Binder.restoreCallingIdentity(ident);
2388 }
2389 }
2390 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002391
satok42c5a162011-05-26 16:46:14 +09002392 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002393 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002394 if (!calledFromValidUser()) {
2395 return;
2396 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002397 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002398 if (!calledWithValidToken(token)) {
2399 final int uid = Binder.getCallingUid();
2400 Slog.e(TAG, "Ignoring showMySoftInput due to an invalid token. uid:"
2401 + uid + " token:" + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002402 return;
2403 }
2404 long ident = Binder.clearCallingIdentity();
2405 try {
2406 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002407 } finally {
2408 Binder.restoreCallingIdentity(ident);
2409 }
2410 }
2411 }
2412
2413 void setEnabledSessionInMainThread(SessionState session) {
2414 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002415 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002416 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002417 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002418 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002419 } catch (RemoteException e) {
2420 }
2421 }
2422 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002423 if (mEnabledSession != null && mEnabledSession.session != null) {
2424 try {
2425 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
2426 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
2427 } catch (RemoteException e) {
2428 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002429 }
2430 }
2431 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002432
satok42c5a162011-05-26 16:46:14 +09002433 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002434 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002435 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002436 switch (msg.what) {
2437 case MSG_SHOW_IM_PICKER:
2438 showInputMethodMenu();
2439 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002440
satokab751aa2010-09-14 19:17:36 +09002441 case MSG_SHOW_IM_SUBTYPE_PICKER:
2442 showInputMethodSubtypeMenu();
2443 return true;
2444
satok47a44912010-10-06 16:03:58 +09002445 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002446 args = (SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09002447 showInputMethodAndSubtypeEnabler((String)args.arg1);
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002448 args.recycle();
satok217f5482010-12-15 05:19:19 +09002449 return true;
2450
2451 case MSG_SHOW_IM_CONFIG:
2452 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002453 return true;
2454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002455 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002456
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002457 case MSG_UNBIND_INPUT:
2458 try {
2459 ((IInputMethod)msg.obj).unbindInput();
2460 } catch (RemoteException e) {
2461 // There is nothing interesting about the method dying.
2462 }
2463 return true;
2464 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002465 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002466 try {
2467 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2468 } catch (RemoteException e) {
2469 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002470 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002471 return true;
2472 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002473 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002474 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002475 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002476 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002477 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002478 } catch (RemoteException e) {
2479 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002480 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002481 return true;
2482 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002483 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002484 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002485 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002486 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002487 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002488 } catch (RemoteException e) {
2489 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002490 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002491 return true;
2492 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002493 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002494 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002495 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002496 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2497 } catch (RemoteException e) {
2498 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002499 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002500 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002501 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002502 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002503 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002504 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002505 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002506 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002507 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002508 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002509 // Dispose the channel if the input method is not local to this process
2510 // because the remote proxy will get its own copy when unparceled.
2511 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002512 channel.dispose();
2513 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002514 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002515 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002516 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002517 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002518 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002520 case MSG_START_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002521 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002522 try {
2523 SessionState session = (SessionState)args.arg1;
2524 setEnabledSessionInMainThread(session);
2525 session.method.startInput((IInputContext)args.arg2,
2526 (EditorInfo)args.arg3);
2527 } catch (RemoteException e) {
2528 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002529 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002530 return true;
2531 case MSG_RESTART_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002532 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002533 try {
2534 SessionState session = (SessionState)args.arg1;
2535 setEnabledSessionInMainThread(session);
2536 session.method.restartInput((IInputContext)args.arg2,
2537 (EditorInfo)args.arg3);
2538 } catch (RemoteException e) {
2539 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002540 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002541 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002542
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002543 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002544
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002545 case MSG_UNBIND_METHOD:
2546 try {
2547 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
2548 } catch (RemoteException e) {
2549 // There is nothing interesting about the last client dying.
2550 }
2551 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002552 case MSG_BIND_METHOD: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002553 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002554 IInputMethodClient client = (IInputMethodClient)args.arg1;
2555 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002556 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002557 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002558 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002559 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002560 } finally {
2561 // Dispose the channel if the input method is not local to this process
2562 // because the remote proxy will get its own copy when unparceled.
2563 if (res.channel != null && Binder.isProxy(client)) {
2564 res.channel.dispose();
2565 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002566 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002567 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002568 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002569 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002570 case MSG_SET_ACTIVE:
2571 try {
2572 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2573 } catch (RemoteException e) {
2574 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2575 + ((ClientState)msg.obj).pid + " uid "
2576 + ((ClientState)msg.obj).uid);
2577 }
2578 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002579 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
2580 final int sequenceNumber = msg.arg1;
2581 final IInputMethodClient client = (IInputMethodClient)msg.obj;
2582 try {
2583 client.setUserActionNotificationSequenceNumber(sequenceNumber);
2584 } catch (RemoteException e) {
2585 Slog.w(TAG, "Got RemoteException sending "
2586 + "setUserActionNotificationSequenceNumber("
2587 + sequenceNumber + ") notification to pid "
2588 + ((ClientState)msg.obj).pid + " uid "
2589 + ((ClientState)msg.obj).uid);
2590 }
2591 return true;
2592 }
satok01038492012-04-09 21:08:27 +09002593
2594 // --------------------------------------------------------------
2595 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
2596 mHardKeyboardListener.handleHardKeyboardStatusChange(
2597 msg.arg1 == 1, msg.arg2 == 1);
2598 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002599 }
2600 return false;
2601 }
2602
satokdc9ddae2011-10-06 12:22:36 +09002603 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002604 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
2605 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09002606 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002607 if (DEBUG) {
2608 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2609 }
satok723a27e2010-11-11 14:58:11 +09002610 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002611 return true;
2612 }
2613
2614 return false;
2615 }
2616
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002617 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002618 HashMap<String, InputMethodInfo> map, boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002619 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002620 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Satoshi Kataoka0766eb02013-07-31 18:30:13 +09002621 + " \n ------ \n" + InputMethodUtils.getStackTrace());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002622 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002623 list.clear();
2624 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002625
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002626 // Use for queryIntentServicesAsUser
2627 final PackageManager pm = mContext.getPackageManager();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002628 String disabledSysImes = mSettings.getDisabledSystemInputMethods();
Amith Yamasanie861ec12010-03-24 21:39:27 -07002629 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002630
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002631 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002632 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002633 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2634 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002635
satoke7c6998e2011-06-03 17:57:59 +09002636 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2637 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002638 for (int i = 0; i < services.size(); ++i) {
2639 ResolveInfo ri = services.get(i);
2640 ServiceInfo si = ri.serviceInfo;
2641 ComponentName compName = new ComponentName(si.packageName, si.name);
2642 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2643 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002644 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002645 + ": it does not require the permission "
2646 + android.Manifest.permission.BIND_INPUT_METHOD);
2647 continue;
2648 }
2649
Joe Onorato8a9b2202010-02-26 18:56:32 -08002650 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002651
2652 try {
satoke7c6998e2011-06-03 17:57:59 +09002653 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002654 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002655 final String id = p.getId();
2656 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002657
2658 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002659 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002660 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002661
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002662 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002663 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002664 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002665 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002666 }
2667 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002668
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002669 if (resetDefaultEnabledIme) {
2670 final ArrayList<InputMethodInfo> defaultEnabledIme =
2671 InputMethodUtils.getDefaultEnabledImes(mContext, mSystemReady, list);
2672 for (int i = 0; i < defaultEnabledIme.size(); ++i) {
2673 final InputMethodInfo imi = defaultEnabledIme.get(i);
2674 if (DEBUG) {
2675 Slog.d(TAG, "--- enable ime = " + imi);
2676 }
2677 setInputMethodEnabledLocked(imi.getId(), true);
2678 }
2679 }
2680
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002681 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09002682 if (!TextUtils.isEmpty(defaultImiId)) {
2683 if (!map.containsKey(defaultImiId)) {
2684 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
2685 if (chooseNewDefaultIMELocked()) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002686 updateFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09002687 }
2688 } else {
2689 // Double check that the default IME is certainly enabled.
2690 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002691 }
2692 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09002693 // Here is not the perfect place to reset the switching controller. Ideally
2694 // mSwitchingController and mSettings should be able to share the same state.
2695 // TODO: Make sure that mSwitchingController and mSettings are sharing the
2696 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09002697 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002698 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002699
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002700 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002701
satokab751aa2010-09-14 19:17:36 +09002702 private void showInputMethodMenu() {
2703 showInputMethodMenuInternal(false);
2704 }
2705
2706 private void showInputMethodSubtypeMenu() {
2707 showInputMethodMenuInternal(true);
2708 }
2709
satok217f5482010-12-15 05:19:19 +09002710 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002711 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002712 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002713 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2714 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002715 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002716 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002717 }
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002718 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok217f5482010-12-15 05:19:19 +09002719 }
2720
2721 private void showConfigureInputMethods() {
2722 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2723 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2724 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2725 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002726 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09002727 }
2728
satok2c93efc2012-04-02 19:33:47 +09002729 private boolean isScreenLocked() {
2730 return mKeyguardManager != null
2731 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
2732 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002733
satokab751aa2010-09-14 19:17:36 +09002734 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002735 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002736
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002737 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09002738 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002739
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002740 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002741 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002742 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002743
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002744 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002745 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09002746 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
2747 mContext);
satok7f35c8c2010-10-07 21:13:11 +09002748 if (immis == null || immis.size() == 0) {
2749 return;
2750 }
2751
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002752 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002753
satok688bd472012-02-09 20:09:17 +09002754 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002755 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
satok688bd472012-02-09 20:09:17 +09002756 showSubtypes, mInputShown, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09002757
satokc3690562012-01-10 20:14:43 +09002758 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002759 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09002760 if (currentSubtype != null) {
2761 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002762 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
2763 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09002764 }
2765 }
2766
Ken Wakasa761eb372011-03-04 19:06:18 +09002767 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09002768 mIms = new InputMethodInfo[N];
2769 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002770 int checkedItem = 0;
2771 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002772 final ImeSubtypeListItem item = imList.get(i);
2773 mIms[i] = item.mImi;
2774 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002775 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002776 int subtypeId = mSubtypeIds[i];
2777 if ((subtypeId == NOT_A_SUBTYPE_ID)
2778 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2779 || (subtypeId == lastInputMethodSubtypeId)) {
2780 checkedItem = i;
2781 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002782 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002783 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002784 final Context themedContext = new ContextThemeWrapper(context,
2785 android.R.style.Theme_DeviceDefault_Settings);
2786 mDialogBuilder = new AlertDialog.Builder(themedContext);
2787 final TypedArray a = themedContext.obtainStyledAttributes(null,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002788 com.android.internal.R.styleable.DialogPreference,
2789 com.android.internal.R.attr.alertDialogStyle, 0);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002790 mDialogBuilder.setIcon(a.getDrawable(
2791 com.android.internal.R.styleable.DialogPreference_dialogIcon));
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002792 a.recycle();
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002793 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
2794 @Override
2795 public void onCancel(DialogInterface dialog) {
2796 hideInputMethodMenu();
2797 }
2798 });
satok01038492012-04-09 21:08:27 +09002799 final LayoutInflater inflater =
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002800 (LayoutInflater)themedContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
satok01038492012-04-09 21:08:27 +09002801 final View tv = inflater.inflate(
2802 com.android.internal.R.layout.input_method_switch_dialog_title, null);
2803 mDialogBuilder.setCustomTitle(tv);
2804
2805 // Setup layout for a toggle switch of the hardware keyboard
2806 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002807 mSwitchingDialogTitleView
2808 .findViewById(com.android.internal.R.id.hard_keyboard_section)
2809 .setVisibility(mWindowManagerService.isHardKeyboardAvailable()
2810 ? View.VISIBLE : View.GONE);
2811 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
2812 com.android.internal.R.id.hard_keyboard_switch);
satok01038492012-04-09 21:08:27 +09002813 hardKeySwitch.setChecked(mWindowManagerService.isHardKeyboardEnabled());
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002814 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
2815 @Override
2816 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
2817 mWindowManagerService.setHardKeyboardEnabled(isChecked);
2818 // Ensure that the input method dialog is dismissed when changing
2819 // the hardware keyboard state.
2820 hideInputMethodMenu();
2821 }
2822 });
2823
2824 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(themedContext,
2825 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
2826 final OnClickListener choiceListener = new OnClickListener() {
2827 @Override
2828 public void onClick(final DialogInterface dialog, final int which) {
2829 synchronized (mMethodMap) {
2830 if (mIms == null || mIms.length <= which || mSubtypeIds == null
2831 || mSubtypeIds.length <= which) {
2832 return;
satok01038492012-04-09 21:08:27 +09002833 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002834 final InputMethodInfo im = mIms[which];
2835 int subtypeId = mSubtypeIds[which];
2836 adapter.mCheckedItem = which;
2837 adapter.notifyDataSetChanged();
2838 hideInputMethodMenu();
2839 if (im != null) {
2840 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
2841 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08002842 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002843 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002844 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002845 }
2846 }
2847 };
2848 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002849
satokbc81b692011-08-26 16:22:22 +09002850 if (showSubtypes && !isScreenLocked) {
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002851 final OnClickListener positiveListener = new OnClickListener() {
2852 @Override
2853 public void onClick(DialogInterface dialog, int whichButton) {
2854 showConfigureInputMethods();
2855 }
2856 };
satok82beadf2010-12-27 19:03:06 +09002857 mDialogBuilder.setPositiveButton(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002858 com.android.internal.R.string.configure_input_methods, positiveListener);
satok7f35c8c2010-10-07 21:13:11 +09002859 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002860 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002861 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002862 mSwitchingDialog.getWindow().setType(
2863 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09002864 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
2865 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002866 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
Jason Monk807ef762014-05-08 15:47:46 -04002867 updateImeWindowStatusLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002868 mSwitchingDialog.show();
2869 }
2870 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002871
Ken Wakasa05dbb652011-08-22 15:22:43 +09002872 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
2873 private final LayoutInflater mInflater;
2874 private final int mTextViewResourceId;
2875 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09002876 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09002877 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
2878 List<ImeSubtypeListItem> itemsList, int checkedItem) {
2879 super(context, textViewResourceId, itemsList);
2880 mTextViewResourceId = textViewResourceId;
2881 mItemsList = itemsList;
2882 mCheckedItem = checkedItem;
2883 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2884 }
2885
2886 @Override
2887 public View getView(int position, View convertView, ViewGroup parent) {
2888 final View view = convertView != null ? convertView
2889 : mInflater.inflate(mTextViewResourceId, null);
2890 if (position < 0 || position >= mItemsList.size()) return view;
2891 final ImeSubtypeListItem item = mItemsList.get(position);
2892 final CharSequence imeName = item.mImeName;
2893 final CharSequence subtypeName = item.mSubtypeName;
2894 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
2895 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
2896 if (TextUtils.isEmpty(subtypeName)) {
2897 firstTextView.setText(imeName);
2898 secondTextView.setVisibility(View.GONE);
2899 } else {
2900 firstTextView.setText(subtypeName);
2901 secondTextView.setText(imeName);
2902 secondTextView.setVisibility(View.VISIBLE);
2903 }
2904 final RadioButton radioButton =
2905 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
2906 radioButton.setChecked(position == mCheckedItem);
2907 return view;
2908 }
2909 }
2910
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002911 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07002912 synchronized (mMethodMap) {
2913 hideInputMethodMenuLocked();
2914 }
2915 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002916
The Android Open Source Project10592532009-03-18 17:39:46 -07002917 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002918 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002919
The Android Open Source Project10592532009-03-18 17:39:46 -07002920 if (mSwitchingDialog != null) {
2921 mSwitchingDialog.dismiss();
2922 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002923 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002924
Jason Monk807ef762014-05-08 15:47:46 -04002925 updateImeWindowStatusLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -07002926 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07002927 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002928 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002929
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002930 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002931
satok42c5a162011-05-26 16:46:14 +09002932 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002933 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002934 // TODO: Make this work even for non-current users?
2935 if (!calledFromValidUser()) {
2936 return false;
2937 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002938 synchronized (mMethodMap) {
2939 if (mContext.checkCallingOrSelfPermission(
2940 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2941 != PackageManager.PERMISSION_GRANTED) {
2942 throw new SecurityException(
2943 "Requires permission "
2944 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2945 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002946
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002947 long ident = Binder.clearCallingIdentity();
2948 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002949 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002950 } finally {
2951 Binder.restoreCallingIdentity(ident);
2952 }
2953 }
2954 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002955
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002956 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
2957 // Make sure this is a valid input method.
2958 InputMethodInfo imm = mMethodMap.get(id);
2959 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09002960 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002961 }
2962
satokd87c2592010-09-29 11:52:06 +09002963 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
2964 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002965
satokd87c2592010-09-29 11:52:06 +09002966 if (enabled) {
2967 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
2968 if (pair.first.equals(id)) {
2969 // We are enabling this input method, but it is already enabled.
2970 // Nothing to do. The previous state was enabled.
2971 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002972 }
2973 }
satokd87c2592010-09-29 11:52:06 +09002974 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
2975 // Previous state was disabled.
2976 return false;
2977 } else {
2978 StringBuilder builder = new StringBuilder();
2979 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2980 builder, enabledInputMethodsList, id)) {
2981 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002982 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002983 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
2984 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
2985 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09002986 }
2987 // Previous state was enabled.
2988 return true;
2989 } else {
2990 // We are disabling the input method but it is already disabled.
2991 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002992 return false;
2993 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002994 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002995 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002996
satok723a27e2010-11-11 14:58:11 +09002997 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
2998 boolean setSubtypeOnly) {
2999 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003000 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003001
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003002 mCurUserActionNotificationSequenceNumber =
3003 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3004 if (DEBUG) {
3005 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3006 + mCurUserActionNotificationSequenceNumber);
3007 }
3008
3009 if (mCurClient != null && mCurClient.client != null) {
3010 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3011 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
3012 mCurUserActionNotificationSequenceNumber, mCurClient.client));
3013 }
3014
satok723a27e2010-11-11 14:58:11 +09003015 // Set Subtype here
3016 if (imi == null || subtypeId < 0) {
3017 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003018 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003019 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003020 if (subtypeId < imi.getSubtypeCount()) {
3021 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3022 mSettings.putSelectedSubtype(subtype.hashCode());
3023 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003024 } else {
3025 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003026 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003027 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003028 }
satokab751aa2010-09-14 19:17:36 +09003029 }
satok723a27e2010-11-11 14:58:11 +09003030
satok4c0e7152012-06-20 20:08:44 +09003031 // Workaround.
3032 // ASEC is not ready in the IMMS constructor. Accordingly, forward-locked
3033 // IMEs are not recognized and considered uninstalled.
3034 // Actually, we can't move everything after SystemReady because
3035 // IMMS needs to run in the encryption lock screen. So, we just skip changing
3036 // the default IME here and try cheking the default IME again in systemReady().
3037 // TODO: Do nothing before system ready and implement a separated logic for
3038 // the encryption lock screen.
3039 // TODO: ASEC should be ready before IMMS is instantiated.
3040 if (mSystemReady && !setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003041 // Set InputMethod here
3042 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3043 }
3044 }
3045
3046 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3047 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3048 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3049 // newDefaultIme is empty when there is no candidate for the selected IME.
3050 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3051 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3052 if (subtypeHashCode != null) {
3053 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003054 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok723a27e2010-11-11 14:58:11 +09003055 imi, Integer.valueOf(subtypeHashCode));
3056 } catch (NumberFormatException e) {
3057 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3058 }
3059 }
3060 }
3061 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003062 }
3063
satok4e4569d2010-11-19 18:45:53 +09003064 // If there are no selected shortcuts, tries finding the most applicable ones.
3065 private Pair<InputMethodInfo, InputMethodSubtype>
3066 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3067 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3068 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003069 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003070 boolean foundInSystemIME = false;
3071
3072 // Search applicable subtype for each InputMethodInfo
3073 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003074 final String imiId = imi.getId();
3075 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3076 continue;
3077 }
satokcd7cd292010-11-20 15:46:23 +09003078 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003079 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003080 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003081 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003082 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003083 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003084 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003085 }
satokdf31ae62011-01-15 06:19:44 +09003086 // 2. Search by the system locale from enabledSubtypes.
3087 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003088 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003089 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003090 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003091 }
satoka86f5e42011-09-02 17:12:42 +09003092 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003093 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003094 final ArrayList<InputMethodSubtype> subtypesForSearch =
3095 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003096 ? InputMethodUtils.getSubtypes(imi)
3097 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003098 // 4. Search by the current subtype's locale from all subtypes.
3099 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003100 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003101 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003102 }
3103 // 5. Search by the system locale from all subtypes.
3104 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003105 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003106 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003107 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003108 }
satokcd7cd292010-11-20 15:46:23 +09003109 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003110 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003111 // The current input method is the most applicable IME.
3112 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003113 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003114 break;
satok7599a7f2010-12-22 13:45:23 +09003115 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003116 // The system input method is 2nd applicable IME.
3117 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003118 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003119 if ((imi.getServiceInfo().applicationInfo.flags
3120 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3121 foundInSystemIME = true;
3122 }
satok4e4569d2010-11-19 18:45:53 +09003123 }
3124 }
3125 }
3126 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003127 if (mostApplicableIMI != null) {
3128 Slog.w(TAG, "Most applicable shortcut input method was:"
3129 + mostApplicableIMI.getId());
3130 if (mostApplicableSubtype != null) {
3131 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3132 + "," + mostApplicableSubtype.getMode() + ","
3133 + mostApplicableSubtype.getLocale());
3134 }
3135 }
satok4e4569d2010-11-19 18:45:53 +09003136 }
satokcd7cd292010-11-20 15:46:23 +09003137 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09003138 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09003139 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003140 } else {
3141 return null;
3142 }
3143 }
3144
satokab751aa2010-09-14 19:17:36 +09003145 /**
3146 * @return Return the current subtype of this input method.
3147 */
satok42c5a162011-05-26 16:46:14 +09003148 @Override
satokab751aa2010-09-14 19:17:36 +09003149 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003150 // TODO: Make this work even for non-current users?
3151 if (!calledFromValidUser()) {
3152 return null;
3153 }
3154 synchronized (mMethodMap) {
3155 return getCurrentInputMethodSubtypeLocked();
3156 }
3157 }
3158
3159 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003160 if (mCurMethodId == null) {
3161 return null;
3162 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003163 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003164 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3165 if (imi == null || imi.getSubtypeCount() == 0) {
3166 return null;
satok4e4569d2010-11-19 18:45:53 +09003167 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003168 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003169 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3170 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003171 if (subtypeId == NOT_A_SUBTYPE_ID) {
3172 // If there are no selected subtypes, the framework will try to find
3173 // the most applicable subtype from explicitly or implicitly enabled
3174 // subtypes.
3175 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003176 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003177 // If there is only one explicitly or implicitly enabled subtype,
3178 // just returns it.
3179 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3180 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3181 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003182 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003183 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003184 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003185 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003186 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003187 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3188 true);
satok4e4569d2010-11-19 18:45:53 +09003189 }
satok3ef8b292010-11-23 06:06:29 +09003190 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003191 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003192 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003193 }
3194 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003195 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003196 }
3197
satokf3db1af2010-11-23 13:34:33 +09003198 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
3199 InputMethodSubtype subtype) {
3200 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
3201 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
3202 } else {
3203 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3204 subtypes.add(subtype);
3205 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
3206 }
3207 }
3208
satok4e4569d2010-11-19 18:45:53 +09003209 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003210 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003211 @Override
satok4e4569d2010-11-19 18:45:53 +09003212 public List getShortcutInputMethodsAndSubtypes() {
3213 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09003214 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09003215 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003216 // If there are no selected shortcut subtypes, the framework will try to find
3217 // the most applicable subtype from all subtypes whose mode is
3218 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003219 Pair<InputMethodInfo, InputMethodSubtype> info =
3220 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003221 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003222 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003223 ret.add(info.first);
3224 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003225 }
satok3da92232011-01-11 22:46:30 +09003226 return ret;
satokf3db1af2010-11-23 13:34:33 +09003227 }
satokf3db1af2010-11-23 13:34:33 +09003228 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3229 ret.add(imi);
3230 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3231 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003232 }
3233 }
satokf3db1af2010-11-23 13:34:33 +09003234 return ret;
satok4e4569d2010-11-19 18:45:53 +09003235 }
3236 }
3237
satok42c5a162011-05-26 16:46:14 +09003238 @Override
satokb66d2872010-11-10 01:04:04 +09003239 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003240 // TODO: Make this work even for non-current users?
3241 if (!calledFromValidUser()) {
3242 return false;
3243 }
satokb66d2872010-11-10 01:04:04 +09003244 synchronized (mMethodMap) {
3245 if (subtype != null && mCurMethodId != null) {
3246 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003247 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003248 if (subtypeId != NOT_A_SUBTYPE_ID) {
3249 setInputMethodLocked(mCurMethodId, subtypeId);
3250 return true;
3251 }
3252 }
3253 return false;
3254 }
3255 }
3256
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003257 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003258 private static class InputMethodFileManager {
3259 private static final String SYSTEM_PATH = "system";
3260 private static final String INPUT_METHOD_PATH = "inputmethod";
3261 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3262 private static final String NODE_SUBTYPES = "subtypes";
3263 private static final String NODE_SUBTYPE = "subtype";
3264 private static final String NODE_IMI = "imi";
3265 private static final String ATTR_ID = "id";
3266 private static final String ATTR_LABEL = "label";
3267 private static final String ATTR_ICON = "icon";
3268 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3269 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3270 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3271 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3272 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3273 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003274 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
satoke7c6998e2011-06-03 17:57:59 +09003275 new HashMap<String, List<InputMethodSubtype>>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003276 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003277 if (methodMap == null) {
3278 throw new NullPointerException("methodMap is null");
3279 }
3280 mMethodMap = methodMap;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003281 final File systemDir = userId == UserHandle.USER_OWNER
3282 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3283 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003284 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3285 if (!inputMethodDir.mkdirs()) {
3286 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3287 }
3288 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3289 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3290 if (!subtypeFile.exists()) {
3291 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003292 writeAdditionalInputMethodSubtypes(
3293 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003294 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003295 readAdditionalInputMethodSubtypes(
3296 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003297 }
3298 }
3299
3300 private void deleteAllInputMethodSubtypes(String imiId) {
3301 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003302 mAdditionalSubtypesMap.remove(imiId);
3303 writeAdditionalInputMethodSubtypes(
3304 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003305 }
3306 }
3307
3308 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003309 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003310 synchronized (mMethodMap) {
3311 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3312 final int N = additionalSubtypes.length;
3313 for (int i = 0; i < N; ++i) {
3314 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003315 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003316 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003317 } else {
3318 Slog.w(TAG, "Duplicated subtype definition found: "
3319 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003320 }
3321 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003322 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3323 writeAdditionalInputMethodSubtypes(
3324 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003325 }
3326 }
3327
3328 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3329 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003330 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003331 }
3332 }
3333
3334 private static void writeAdditionalInputMethodSubtypes(
3335 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3336 HashMap<String, InputMethodInfo> methodMap) {
3337 // Safety net for the case that this function is called before methodMap is set.
3338 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3339 FileOutputStream fos = null;
3340 try {
3341 fos = subtypesFile.startWrite();
3342 final XmlSerializer out = new FastXmlSerializer();
3343 out.setOutput(fos, "utf-8");
3344 out.startDocument(null, true);
3345 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3346 out.startTag(null, NODE_SUBTYPES);
3347 for (String imiId : allSubtypes.keySet()) {
3348 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3349 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3350 continue;
3351 }
3352 out.startTag(null, NODE_IMI);
3353 out.attribute(null, ATTR_ID, imiId);
3354 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3355 final int N = subtypesList.size();
3356 for (int i = 0; i < N; ++i) {
3357 final InputMethodSubtype subtype = subtypesList.get(i);
3358 out.startTag(null, NODE_SUBTYPE);
3359 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3360 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3361 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3362 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3363 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3364 out.attribute(null, ATTR_IS_AUXILIARY,
3365 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3366 out.endTag(null, NODE_SUBTYPE);
3367 }
3368 out.endTag(null, NODE_IMI);
3369 }
3370 out.endTag(null, NODE_SUBTYPES);
3371 out.endDocument();
3372 subtypesFile.finishWrite(fos);
3373 } catch (java.io.IOException e) {
3374 Slog.w(TAG, "Error writing subtypes", e);
3375 if (fos != null) {
3376 subtypesFile.failWrite(fos);
3377 }
3378 }
3379 }
3380
3381 private static void readAdditionalInputMethodSubtypes(
3382 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3383 if (allSubtypes == null || subtypesFile == null) return;
3384 allSubtypes.clear();
3385 FileInputStream fis = null;
3386 try {
3387 fis = subtypesFile.openRead();
3388 final XmlPullParser parser = Xml.newPullParser();
3389 parser.setInput(fis, null);
3390 int type = parser.getEventType();
3391 // Skip parsing until START_TAG
3392 while ((type = parser.next()) != XmlPullParser.START_TAG
3393 && type != XmlPullParser.END_DOCUMENT) {}
3394 String firstNodeName = parser.getName();
3395 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3396 throw new XmlPullParserException("Xml doesn't start with subtypes");
3397 }
3398 final int depth =parser.getDepth();
3399 String currentImiId = null;
3400 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3401 while (((type = parser.next()) != XmlPullParser.END_TAG
3402 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3403 if (type != XmlPullParser.START_TAG)
3404 continue;
3405 final String nodeName = parser.getName();
3406 if (NODE_IMI.equals(nodeName)) {
3407 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3408 if (TextUtils.isEmpty(currentImiId)) {
3409 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3410 continue;
3411 }
3412 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
3413 allSubtypes.put(currentImiId, tempSubtypesArray);
3414 } else if (NODE_SUBTYPE.equals(nodeName)) {
3415 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3416 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3417 continue;
3418 }
3419 final int icon = Integer.valueOf(
3420 parser.getAttributeValue(null, ATTR_ICON));
3421 final int label = Integer.valueOf(
3422 parser.getAttributeValue(null, ATTR_LABEL));
3423 final String imeSubtypeLocale =
3424 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3425 final String imeSubtypeMode =
3426 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3427 final String imeSubtypeExtraValue =
3428 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003429 final boolean isAuxiliary = "1".equals(String.valueOf(
3430 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
satoke7c6998e2011-06-03 17:57:59 +09003431 final InputMethodSubtype subtype =
3432 new InputMethodSubtype(label, icon, imeSubtypeLocale,
3433 imeSubtypeMode, imeSubtypeExtraValue, isAuxiliary);
3434 tempSubtypesArray.add(subtype);
3435 }
3436 }
3437 } catch (XmlPullParserException e) {
3438 Slog.w(TAG, "Error reading subtypes: " + e);
3439 return;
3440 } catch (java.io.IOException e) {
3441 Slog.w(TAG, "Error reading subtypes: " + e);
3442 return;
3443 } catch (NumberFormatException e) {
3444 Slog.w(TAG, "Error reading subtypes: " + e);
3445 return;
3446 } finally {
3447 if (fis != null) {
3448 try {
3449 fis.close();
3450 } catch (java.io.IOException e1) {
3451 Slog.w(TAG, "Failed to close.");
3452 }
3453 }
3454 }
3455 }
3456 }
3457
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003458 @Override
3459 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3460 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3461 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003463 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3464 + Binder.getCallingPid()
3465 + ", uid=" + Binder.getCallingUid());
3466 return;
3467 }
3468
3469 IInputMethod method;
3470 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003472 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003474 synchronized (mMethodMap) {
3475 p.println("Current Input Method Manager state:");
3476 int N = mMethodList.size();
3477 p.println(" Input Methods:");
3478 for (int i=0; i<N; i++) {
3479 InputMethodInfo info = mMethodList.get(i);
3480 p.println(" InputMethod #" + i + ":");
3481 info.dump(p, " ");
3482 }
3483 p.println(" Clients:");
3484 for (ClientState ci : mClients.values()) {
3485 p.println(" Client " + ci + ":");
3486 p.println(" client=" + ci.client);
3487 p.println(" inputContext=" + ci.inputContext);
3488 p.println(" sessionRequested=" + ci.sessionRequested);
3489 p.println(" curSession=" + ci.curSession);
3490 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003491 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003492 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003493 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3494 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003495 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3496 + " mBoundToMethod=" + mBoundToMethod);
3497 p.println(" mCurToken=" + mCurToken);
3498 p.println(" mCurIntent=" + mCurIntent);
3499 method = mCurMethod;
3500 p.println(" mCurMethod=" + mCurMethod);
3501 p.println(" mEnabledSession=" + mEnabledSession);
3502 p.println(" mShowRequested=" + mShowRequested
3503 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3504 + " mShowForced=" + mShowForced
3505 + " mInputShown=" + mInputShown);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003506 p.println(" mCurUserActionNotificationSequenceNumber="
3507 + mCurUserActionNotificationSequenceNumber);
Dianne Hackbornd4e6d462014-05-16 16:32:37 -07003508 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003509 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003510
Jeff Brownb88102f2010-09-08 11:49:43 -07003511 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003512 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003513 pw.flush();
3514 try {
3515 client.client.asBinder().dump(fd, args);
3516 } catch (RemoteException e) {
3517 p.println("Input method client dead: " + e);
3518 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003519 } else {
3520 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003521 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003522
Jeff Brownb88102f2010-09-08 11:49:43 -07003523 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003524 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003525 pw.flush();
3526 try {
3527 method.asBinder().dump(fd, args);
3528 } catch (RemoteException e) {
3529 p.println("Input method service dead: " + e);
3530 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003531 } else {
3532 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003533 }
3534 }
3535}