blob: 2d270e71bf14f94a02f52eb0dfbfa80c79f4946f [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;
54import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090055import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070057import android.content.pm.ApplicationInfo;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090058import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.content.pm.PackageManager;
60import android.content.pm.ResolveInfo;
61import android.content.pm.ServiceInfo;
Amith Yamasani734983f2014-03-04 16:48:05 -080062import android.content.pm.UserInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070063import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.content.res.Resources;
65import android.content.res.TypedArray;
66import android.database.ContentObserver;
Joe Onorato857fd9b2011-01-27 15:08:35 -080067import android.inputmethodservice.InputMethodService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.os.Binder;
satoke7c6998e2011-06-03 17:57:59 +090069import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.os.Handler;
71import android.os.IBinder;
72import android.os.IInterface;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090073import android.os.IRemoteCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074import android.os.Message;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090075import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076import android.os.Parcel;
77import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080078import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079import android.os.ServiceManager;
80import android.os.SystemClock;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090081import android.os.UserHandle;
Amith Yamasani734983f2014-03-04 16:48:05 -080082import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083import android.provider.Settings;
84import android.text.TextUtils;
satokf9f01002011-05-19 21:31:50 +090085import android.text.style.SuggestionSpan;
Dianne Hackborn39606a02012-07-31 17:54:35 -070086import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +090088import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +090089import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090import android.util.PrintWriterPrinter;
91import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +090092import android.util.Slog;
93import android.util.Xml;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094import android.view.IWindowManager;
Jeff Brownc28867a2013-03-26 15:42:39 -070095import android.view.InputChannel;
Ken Wakasa05dbb652011-08-22 15:22:43 +090096import android.view.LayoutInflater;
97import android.view.View;
98import android.view.ViewGroup;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099import android.view.WindowManager;
satokab751aa2010-09-14 19:17:36 +0900100import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101import android.view.inputmethod.InputBinding;
102import android.view.inputmethod.InputMethod;
103import android.view.inputmethod.InputMethodInfo;
104import android.view.inputmethod.InputMethodManager;
satokab751aa2010-09-14 19:17:36 +0900105import android.view.inputmethod.InputMethodSubtype;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900106import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +0900107import android.widget.CompoundButton;
108import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900109import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +0900110import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900111import android.widget.TextView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112
satoke7c6998e2011-06-03 17:57:59 +0900113import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900115import java.io.FileInputStream;
116import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117import java.io.IOException;
118import java.io.PrintWriter;
119import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900120import java.util.Collections;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121import java.util.HashMap;
122import java.util.List;
satok5b927c432012-05-01 20:09:34 +0900123import java.util.Locale;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124
125/**
126 * This class provides a system service that manages input methods.
127 */
128public class InputMethodManagerService extends IInputMethodManager.Stub
129 implements ServiceConnection, Handler.Callback {
130 static final boolean DEBUG = false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700131 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132
133 static final int MSG_SHOW_IM_PICKER = 1;
satokab751aa2010-09-14 19:17:36 +0900134 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 2;
satok47a44912010-10-06 16:03:58 +0900135 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 3;
satok217f5482010-12-15 05:19:19 +0900136 static final int MSG_SHOW_IM_CONFIG = 4;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 static final int MSG_UNBIND_INPUT = 1000;
139 static final int MSG_BIND_INPUT = 1010;
140 static final int MSG_SHOW_SOFT_INPUT = 1020;
141 static final int MSG_HIDE_SOFT_INPUT = 1030;
142 static final int MSG_ATTACH_TOKEN = 1040;
143 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 static final int MSG_START_INPUT = 2000;
146 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 static final int MSG_UNBIND_METHOD = 3000;
149 static final int MSG_BIND_METHOD = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700150 static final int MSG_SET_ACTIVE = 3020;
Yohei Yukawab7b79072014-03-25 11:02:00 +0900151 static final int MSG_SET_CURSOR_ANCHOR_MONITOR_MODE = 3030;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800152
satok01038492012-04-09 21:08:27 +0900153 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
154
Satoshi Kataokabcacc322013-10-21 17:57:27 -0700155 static final long TIME_TO_RECONNECT = 3 * 1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800156
satokf9f01002011-05-19 21:31:50 +0900157 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
158
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900159 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
satokb6359412011-06-28 17:47:41 +0900160 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900161
satok4e4569d2010-11-19 18:45:53 +0900162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800164 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900166 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 final IWindowManager mIWindowManager;
169 final HandlerCaller mCaller;
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700170 final boolean mHasFeature;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900171 private InputMethodFileManager mFileManager;
satok01038492012-04-09 21:08:27 +0900172 private final HardKeyboardListener mHardKeyboardListener;
173 private final WindowManagerService mWindowManagerService;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800174
Jeff Brownc28867a2013-03-26 15:42:39 -0700175 final InputBindResult mNoBinding = new InputBindResult(null, null, null, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800176
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 // All known input methods. mMethodMap also serves as the global
178 // lock for this class.
satokd87c2592010-09-29 11:52:06 +0900179 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<InputMethodInfo>();
180 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<String, InputMethodInfo>();
satokf9f01002011-05-19 21:31:50 +0900181 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
182 new LruCache<SuggestionSpan, InputMethodInfo>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Satoshi Kataokad787f692013-10-26 04:44:21 +0900183 private final InputMethodSubtypeSwitchingController mSwitchingController;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800184
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700185 // Used to bring IME service up to visible adjustment while it is being shown.
186 final ServiceConnection mVisibleConnection = new ServiceConnection() {
187 @Override public void onServiceConnected(ComponentName name, IBinder service) {
188 }
189
190 @Override public void onServiceDisconnected(ComponentName name) {
191 }
192 };
193 boolean mVisibleBound = false;
194
satok7cfc0ed2011-06-20 21:29:36 +0900195 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700196 private NotificationManager mNotificationManager;
197 private KeyguardManager mKeyguardManager;
198 private StatusBarManagerService mStatusBar;
199 private Notification mImeSwitcherNotification;
200 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900201 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900202 private boolean mNotificationShown;
satok0a1bcf42012-05-16 19:26:31 +0900203 private final boolean mImeSelectedOnBoot;
satok7cfc0ed2011-06-20 21:29:36 +0900204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 class SessionState {
206 final ClientState client;
207 final IInputMethod method;
Jeff Brownc28867a2013-03-26 15:42:39 -0700208
209 IInputMethodSession session;
210 InputChannel channel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 @Override
213 public String toString() {
214 return "SessionState{uid " + client.uid + " pid " + client.pid
215 + " method " + Integer.toHexString(
216 System.identityHashCode(method))
217 + " session " + Integer.toHexString(
218 System.identityHashCode(session))
Jeff Brownc28867a2013-03-26 15:42:39 -0700219 + " channel " + channel
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 + "}";
221 }
222
223 SessionState(ClientState _client, IInputMethod _method,
Jeff Brownc28867a2013-03-26 15:42:39 -0700224 IInputMethodSession _session, InputChannel _channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 client = _client;
226 method = _method;
227 session = _session;
Jeff Brownc28867a2013-03-26 15:42:39 -0700228 channel = _channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 }
230 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800231
Jeff Brownc28867a2013-03-26 15:42:39 -0700232 static final class ClientState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 final IInputMethodClient client;
234 final IInputContext inputContext;
235 final int uid;
236 final int pid;
237 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800238
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 boolean sessionRequested;
240 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 @Override
243 public String toString() {
244 return "ClientState{" + Integer.toHexString(
245 System.identityHashCode(this)) + " uid " + uid
246 + " pid " + pid + "}";
247 }
248
249 ClientState(IInputMethodClient _client, IInputContext _inputContext,
250 int _uid, int _pid) {
251 client = _client;
252 inputContext = _inputContext;
253 uid = _uid;
254 pid = _pid;
255 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
256 }
257 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 final HashMap<IBinder, ClientState> mClients
260 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700263 * Set once the system is ready to run third party code.
264 */
265 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800266
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700267 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 * Id of the currently selected input method.
269 */
270 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 /**
273 * The current binding sequence number, incremented every time there is
274 * a new bind performed.
275 */
276 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 /**
279 * The client that is currently bound to an input method.
280 */
281 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700284 * The last window token that gained focus.
285 */
286 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800287
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700288 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 * The input context last provided by the current client.
290 */
291 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 /**
294 * The attributes last provided by the current client.
295 */
296 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800297
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298 /**
299 * The input method ID of the input method service that we are currently
300 * connected to or in the process of connecting to.
301 */
302 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800303
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 /**
satokab751aa2010-09-14 19:17:36 +0900305 * The current subtype of the current input method.
306 */
307 private InputMethodSubtype mCurrentSubtype;
308
satok4e4569d2010-11-19 18:45:53 +0900309 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900310 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
311 mShortcutInputMethodsAndSubtypes =
312 new HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>();
satokab751aa2010-09-14 19:17:36 +0900313
John Spurlocke0980502013-10-25 11:59:29 -0400314 // Was the keyguard locked when this client became current?
315 private boolean mCurClientInKeyguard;
316
satokab751aa2010-09-14 19:17:36 +0900317 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 * Set to true if our ServiceConnection is currently actively bound to
319 * a service (whether or not we have gotten its IBinder back yet).
320 */
321 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 /**
324 * Set if the client has asked for the input method to be shown.
325 */
326 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328 /**
329 * Set if we were explicitly told to show the input method.
330 */
331 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 /**
334 * Set if we were forced to be shown.
335 */
336 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 /**
339 * Set if we last told the input method to show itself.
340 */
341 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 /**
344 * The Intent used to connect to the current input method.
345 */
346 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800347
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 /**
349 * The token we have made for the currently active input method, to
350 * identify it in the future.
351 */
352 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 /**
355 * If non-null, this is the input method service we are currently connected
356 * to.
357 */
358 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 /**
361 * Time that we last initiated a bind to the input method, to determine
362 * if we should try to disconnect and reconnect to it.
363 */
364 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 /**
367 * Have we called mCurMethod.bindInput()?
368 */
369 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 /**
372 * Currently enabled session. Only touched by service thread, not
373 * protected by a lock.
374 */
375 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800376
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377 /**
378 * True if the screen is on. The value is true initially.
379 */
380 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800381
Joe Onorato857fd9b2011-01-27 15:08:35 -0800382 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
383 int mImeWindowVis;
384
Ken Wakasa05dbb652011-08-22 15:22:43 +0900385 private AlertDialog.Builder mDialogBuilder;
386 private AlertDialog mSwitchingDialog;
satok01038492012-04-09 21:08:27 +0900387 private View mSwitchingDialogTitleView;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900388 private InputMethodInfo[] mIms;
389 private int[] mSubtypeIds;
satok5b927c432012-05-01 20:09:34 +0900390 private Locale mLastSystemLocale;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900391 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
392 private final IPackageManager mIPackageManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 class SettingsObserver extends ContentObserver {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800395 String mLastEnabled = "";
396
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 SettingsObserver(Handler handler) {
398 super(handler);
399 ContentResolver resolver = mContext.getContentResolver();
400 resolver.registerContentObserver(Settings.Secure.getUriFor(
401 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
satokab751aa2010-09-14 19:17:36 +0900402 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokb6109bb2011-02-03 22:24:54 +0900403 Settings.Secure.ENABLED_INPUT_METHODS), false, this);
404 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokab751aa2010-09-14 19:17:36 +0900405 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 @Override public void onChange(boolean selfChange) {
409 synchronized (mMethodMap) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800410 boolean enabledChanged = false;
411 String newEnabled = mSettings.getEnabledInputMethodsStr();
412 if (!mLastEnabled.equals(newEnabled)) {
413 mLastEnabled = newEnabled;
414 enabledChanged = true;
415 }
416 updateFromSettingsLocked(enabledChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 }
418 }
419 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800420
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900421 class ImmsBroadcastReceiver extends android.content.BroadcastReceiver {
422 private void updateActive() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423 // Inform the current client of the change in active status
Dianne Hackborna6e41342012-05-22 16:30:34 -0700424 if (mCurClient != null && mCurClient.client != null) {
425 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
426 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800427 }
428 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900429
430 @Override
431 public void onReceive(Context context, Intent intent) {
432 final String action = intent.getAction();
433 if (Intent.ACTION_SCREEN_ON.equals(action)) {
434 mScreenOn = true;
435 refreshImeWindowVisibilityLocked();
436 updateActive();
437 return;
438 } else if (Intent.ACTION_SCREEN_OFF.equals(action)) {
439 mScreenOn = false;
440 setImeWindowVisibilityStatusHiddenLocked();
441 updateActive();
442 return;
443 } else if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
444 hideInputMethodMenu();
445 // No need to updateActive
446 return;
Amith Yamasani734983f2014-03-04 16:48:05 -0800447 } else if (Intent.ACTION_USER_ADDED.equals(action)
448 || Intent.ACTION_USER_REMOVED.equals(action)) {
Kenny Guy2a764942014-04-02 13:29:20 +0100449 updateCurrentProfileIds();
Amith Yamasani734983f2014-03-04 16:48:05 -0800450 return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900451 } else {
452 Slog.w(TAG, "Unexpected intent " + intent);
453 }
454 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800456
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800457 class MyPackageMonitor extends PackageMonitor {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900458 private boolean isChangingPackagesOfCurrentUser() {
459 final int userId = getChangingUserId();
460 final boolean retval = userId == mSettings.getCurrentUserId();
461 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900462 if (!retval) {
463 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
464 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900465 }
466 return retval;
467 }
468
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800470 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900471 if (!isChangingPackagesOfCurrentUser()) {
472 return false;
473 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900475 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 final int N = mMethodList.size();
477 if (curInputMethodId != null) {
478 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800479 InputMethodInfo imi = mMethodList.get(i);
480 if (imi.getId().equals(curInputMethodId)) {
481 for (String pkg : packages) {
482 if (imi.getPackageName().equals(pkg)) {
483 if (!doit) {
484 return true;
485 }
satok723a27e2010-11-11 14:58:11 +0900486 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800487 chooseNewDefaultIMELocked();
488 return true;
489 }
490 }
491 }
492 }
493 }
494 }
495 return false;
496 }
497
498 @Override
499 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900500 if (!isChangingPackagesOfCurrentUser()) {
501 return;
502 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800503 synchronized (mMethodMap) {
504 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900505 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800506 final int N = mMethodList.size();
507 if (curInputMethodId != null) {
508 for (int i=0; i<N; i++) {
509 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900510 final String imiId = imi.getId();
511 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800512 curIm = imi;
513 }
satoke7c6998e2011-06-03 17:57:59 +0900514
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800515 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900516 if (isPackageModified(imi.getPackageName())) {
517 mFileManager.deleteAllInputMethodSubtypes(imiId);
518 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800519 if (change == PACKAGE_TEMPORARY_CHANGE
520 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800521 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800522 + imi.getComponent());
523 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524 }
525 }
526 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800527
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900528 buildInputMethodListLocked(
529 mMethodList, mMethodMap, false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800530
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800532
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800533 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800534 int change = isPackageDisappearing(curIm.getPackageName());
535 if (change == PACKAGE_TEMPORARY_CHANGE
536 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800537 ServiceInfo si = null;
538 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900539 si = mIPackageManager.getServiceInfo(
540 curIm.getComponent(), 0, mSettings.getCurrentUserId());
541 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800542 }
543 if (si == null) {
544 // Uh oh, current input method is no longer around!
545 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800546 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
satok15452a42011-10-28 17:58:28 +0900547 setImeWindowVisibilityStatusHiddenLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800548 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800549 changed = true;
550 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800551 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900552 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800553 }
554 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800555 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800556 }
satokab751aa2010-09-14 19:17:36 +0900557
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800558 if (curIm == null) {
559 // We currently don't have a default input method... is
560 // one now available?
561 changed = chooseNewDefaultIMELocked();
562 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800563
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800564 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800565 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566 }
567 }
568 }
569 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800570
Jeff Brownc28867a2013-03-26 15:42:39 -0700571 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900572 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -0700573 private final IInputMethod mMethod;
574 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800575
Jeff Brownc28867a2013-03-26 15:42:39 -0700576 MethodCallback(InputMethodManagerService imms, IInputMethod method,
577 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900578 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -0700579 mMethod = method;
580 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800581 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800582
satoke7c6998e2011-06-03 17:57:59 +0900583 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -0700584 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -0700585 long ident = Binder.clearCallingIdentity();
586 try {
587 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
588 } finally {
589 Binder.restoreCallingIdentity(ident);
590 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591 }
592 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800593
satok01038492012-04-09 21:08:27 +0900594 private class HardKeyboardListener
595 implements WindowManagerService.OnHardKeyboardStatusChangeListener {
596 @Override
597 public void onHardKeyboardStatusChange(boolean available, boolean enabled) {
598 mHandler.sendMessage(mHandler.obtainMessage(
599 MSG_HARD_KEYBOARD_SWITCH_CHANGED, available ? 1 : 0, enabled ? 1 : 0));
600 }
601
602 public void handleHardKeyboardStatusChange(boolean available, boolean enabled) {
603 if (DEBUG) {
604 Slog.w(TAG, "HardKeyboardStatusChanged: available = " + available + ", enabled = "
605 + enabled);
606 }
607 synchronized(mMethodMap) {
608 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
609 && mSwitchingDialog.isShowing()) {
610 mSwitchingDialogTitleView.findViewById(
611 com.android.internal.R.id.hard_keyboard_section).setVisibility(
612 available ? View.VISIBLE : View.GONE);
613 }
614 }
615 }
616 }
617
618 public InputMethodManagerService(Context context, WindowManagerService windowManager) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900619 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800621 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 mHandler = new Handler(this);
623 mIWindowManager = IWindowManager.Stub.asInterface(
624 ServiceManager.getService(Context.WINDOW_SERVICE));
Mita Yuned218c72012-12-06 17:18:25 -0800625 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900626 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627 public void executeMessage(Message msg) {
628 handleMessage(msg);
629 }
Mita Yuned218c72012-12-06 17:18:25 -0800630 }, true /*asyncHandler*/);
satok01038492012-04-09 21:08:27 +0900631 mWindowManagerService = windowManager;
632 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700633 mHasFeature = context.getPackageManager().hasSystemFeature(
634 PackageManager.FEATURE_INPUT_METHODS);
satok7cfc0ed2011-06-20 21:29:36 +0900635
satok7cfc0ed2011-06-20 21:29:36 +0900636 mImeSwitcherNotification = new Notification();
637 mImeSwitcherNotification.icon = com.android.internal.R.drawable.ic_notification_ime_default;
638 mImeSwitcherNotification.when = 0;
639 mImeSwitcherNotification.flags = Notification.FLAG_ONGOING_EVENT;
640 mImeSwitcherNotification.tickerText = null;
641 mImeSwitcherNotification.defaults = 0; // please be quiet
642 mImeSwitcherNotification.sound = null;
643 mImeSwitcherNotification.vibrate = null;
Daniel Sandler590d5152012-06-14 16:10:13 -0400644
645 // Tag this notification specially so SystemUI knows it's important
John Spurlockfd7f1e02014-03-18 16:41:57 -0400646 mImeSwitcherNotification.extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
647 mImeSwitcherNotification.category = Notification.CATEGORY_SYSTEM;
Daniel Sandler590d5152012-06-14 16:10:13 -0400648
satok7cfc0ed2011-06-20 21:29:36 +0900649 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900650 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900651
652 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900653
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900654 final IntentFilter broadcastFilter = new IntentFilter();
655 broadcastFilter.addAction(Intent.ACTION_SCREEN_ON);
656 broadcastFilter.addAction(Intent.ACTION_SCREEN_OFF);
657 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Amith Yamasani734983f2014-03-04 16:48:05 -0800658 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
659 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900660 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800661
satok7cfc0ed2011-06-20 21:29:36 +0900662 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900663 int userId = 0;
664 try {
665 ActivityManagerNative.getDefault().registerUserSwitchObserver(
666 new IUserSwitchObserver.Stub() {
667 @Override
668 public void onUserSwitching(int newUserId, IRemoteCallback reply) {
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900669 synchronized(mMethodMap) {
670 switchUserLocked(newUserId);
671 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900672 if (reply != null) {
673 try {
674 reply.sendResult(null);
675 } catch (RemoteException e) {
676 }
677 }
678 }
679
680 @Override
681 public void onUserSwitchComplete(int newUserId) throws RemoteException {
682 }
683 });
684 userId = ActivityManagerNative.getDefault().getCurrentUser().id;
685 } catch (RemoteException e) {
686 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
687 }
satok81f8b7c2012-12-04 20:42:56 +0900688 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
satok913a8922010-08-26 21:53:41 +0900689
satokd87c2592010-09-29 11:52:06 +0900690 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900691 mSettings = new InputMethodSettings(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900692 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId);
Kenny Guy2a764942014-04-02 13:29:20 +0100693 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900694 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Satoshi Kataokad787f692013-10-26 04:44:21 +0900695 mSwitchingController = new InputMethodSubtypeSwitchingController(mSettings);
696 mSwitchingController.resetCircularListLocked(context);
satok0a1bcf42012-05-16 19:26:31 +0900697
698 // Just checking if defaultImiId is empty or not
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900699 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900700 if (DEBUG) {
701 Slog.d(TAG, "Initial default ime = " + defaultImiId);
702 }
satok0a1bcf42012-05-16 19:26:31 +0900703 mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
704
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900705 buildInputMethodListLocked(mMethodList, mMethodMap,
706 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satokd87c2592010-09-29 11:52:06 +0900707 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800708
satok0a1bcf42012-05-16 19:26:31 +0900709 if (!mImeSelectedOnBoot) {
710 Slog.w(TAG, "No IME selected. Choose the most applicable IME.");
satok5b927c432012-05-01 20:09:34 +0900711 resetDefaultImeLocked(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800713
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800714 mSettingsObserver = new SettingsObserver(mHandler);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800715 updateFromSettingsLocked(true);
satok5b927c432012-05-01 20:09:34 +0900716
717 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME
718 // according to the new system locale.
719 final IntentFilter filter = new IntentFilter();
720 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
721 mContext.registerReceiver(
722 new BroadcastReceiver() {
723 @Override
724 public void onReceive(Context context, Intent intent) {
725 synchronized(mMethodMap) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900726 resetStateIfCurrentLocaleChangedLocked();
satok5b927c432012-05-01 20:09:34 +0900727 }
728 }
729 }, filter);
730 }
731
satok5b927c432012-05-01 20:09:34 +0900732 private void resetDefaultImeLocked(Context context) {
733 // Do not reset the default (current) IME when it is a 3rd-party IME
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900734 if (mCurMethodId != null
735 && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +0900736 return;
737 }
738
739 InputMethodInfo defIm = null;
740 for (InputMethodInfo imi : mMethodList) {
741 if (defIm == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900742 if (InputMethodUtils.isValidSystemDefaultIme(
743 mSystemReady, imi, context)) {
satok5b927c432012-05-01 20:09:34 +0900744 defIm = imi;
745 Slog.i(TAG, "Selected default: " + imi.getId());
746 }
747 }
748 }
749 if (defIm == null && mMethodList.size() > 0) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900750 defIm = InputMethodUtils.getMostApplicableDefaultIME(
751 mSettings.getEnabledInputMethodListLocked());
satok5b927c432012-05-01 20:09:34 +0900752 Slog.i(TAG, "No default found, using " + defIm.getId());
753 }
754 if (defIm != null) {
755 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
756 }
757 }
758
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900759 private void resetAllInternalStateLocked(final boolean updateOnlyWhenLocaleChanged,
760 final boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900761 if (!mSystemReady) {
762 // not system ready
763 return;
764 }
765 final Locale newLocale = mRes.getConfiguration().locale;
766 if (!updateOnlyWhenLocaleChanged
767 || (newLocale != null && !newLocale.equals(mLastSystemLocale))) {
768 if (!updateOnlyWhenLocaleChanged) {
769 hideCurrentInputLocked(0, null);
770 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -0700771 unbindCurrentMethodLocked(true, false);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900772 }
773 if (DEBUG) {
774 Slog.i(TAG, "Locale has been changed to " + newLocale);
775 }
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900776 buildInputMethodListLocked(mMethodList, mMethodMap, resetDefaultEnabledIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900777 if (!updateOnlyWhenLocaleChanged) {
778 final String selectedImiId = mSettings.getSelectedInputMethod();
779 if (TextUtils.isEmpty(selectedImiId)) {
780 // This is the first time of the user switch and
781 // set the current ime to the proper one.
782 resetDefaultImeLocked(mContext);
783 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +0900784 } else {
785 // If the locale is changed, needs to reset the default ime
786 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900787 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800788 updateFromSettingsLocked(true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900789 mLastSystemLocale = newLocale;
790 if (!updateOnlyWhenLocaleChanged) {
791 try {
792 startInputInnerLocked();
793 } catch (RuntimeException e) {
794 Slog.w(TAG, "Unexpected exception", e);
795 }
796 }
797 }
798 }
799
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900800 private void resetStateIfCurrentLocaleChangedLocked() {
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900801 resetAllInternalStateLocked(true /* updateOnlyWhenLocaleChanged */,
802 true /* resetDefaultImeLocked */);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900803 }
804
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900805 private void switchUserLocked(int newUserId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900806 mSettings.setCurrentUserId(newUserId);
Kenny Guy2a764942014-04-02 13:29:20 +0100807 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900808 // InputMethodFileManager should be reset when the user is changed
809 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900810 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +0900811 // For secondary users, the list of enabled IMEs may not have been updated since the
812 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
813 // not be empty even if the IME has been uninstalled by the primary user.
814 // Even in such cases, IMMS works fine because it will find the most applicable
815 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900816 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900817 if (DEBUG) {
818 Slog.d(TAG, "Switch user: " + newUserId + " current ime = " + defaultImiId);
819 }
820 resetAllInternalStateLocked(false /* updateOnlyWhenLocaleChanged */,
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900821 initialUserSwitch /* needsToResetDefaultIme */);
822 if (initialUserSwitch) {
823 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mContext.getPackageManager(),
824 mSettings.getEnabledInputMethodListLocked());
825 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900826 }
827
Kenny Guy2a764942014-04-02 13:29:20 +0100828 void updateCurrentProfileIds() {
829 List<UserInfo> profiles =
830 UserManager.get(mContext).getProfiles(mSettings.getCurrentUserId());
831 int[] currentProfileIds = new int[profiles.size()]; // profiles will not be null
832 for (int i = 0; i < currentProfileIds.length; i++) {
833 currentProfileIds[i] = profiles.get(i).id;
Amith Yamasani734983f2014-03-04 16:48:05 -0800834 }
Kenny Guy2a764942014-04-02 13:29:20 +0100835 mSettings.setCurrentProfileIds(currentProfileIds);
Amith Yamasani734983f2014-03-04 16:48:05 -0800836 }
837
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800838 @Override
839 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
840 throws RemoteException {
841 try {
842 return super.onTransact(code, data, reply, flags);
843 } catch (RuntimeException e) {
844 // The input method manager only throws security exceptions, so let's
845 // log all others.
846 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -0700847 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800848 }
849 throw e;
850 }
851 }
852
Svetoslav Ganova0027152013-06-25 14:59:53 -0700853 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700854 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900855 if (DEBUG) {
856 Slog.d(TAG, "--- systemReady");
857 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700858 if (!mSystemReady) {
859 mSystemReady = true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900860 mKeyguardManager =
861 (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700862 mNotificationManager = (NotificationManager)
863 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
864 mStatusBar = statusBar;
865 statusBar.setIconVisibility("ime", false);
866 updateImeWindowStatusLocked();
satokb858c732011-07-22 19:54:34 +0900867 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
868 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +0900869 if (mShowOngoingImeSwitcherForPhones) {
870 mWindowManagerService.setOnHardKeyboardStatusChangeListener(
871 mHardKeyboardListener);
872 }
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900873 buildInputMethodListLocked(mMethodList, mMethodMap,
874 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satok0a1bcf42012-05-16 19:26:31 +0900875 if (!mImeSelectedOnBoot) {
876 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900877 resetStateIfCurrentLocaleChangedLocked();
878 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(
879 mContext.getPackageManager(),
880 mSettings.getEnabledInputMethodListLocked());
satok0a1bcf42012-05-16 19:26:31 +0900881 }
882 mLastSystemLocale = mRes.getConfiguration().locale;
Dianne Hackborncc278702009-09-02 23:07:23 -0700883 try {
884 startInputInnerLocked();
885 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800886 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700887 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700888 }
889 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800890 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800891
satok15452a42011-10-28 17:58:28 +0900892 private void setImeWindowVisibilityStatusHiddenLocked() {
893 mImeWindowVis = 0;
894 updateImeWindowStatusLocked();
895 }
896
satok3afd6c02011-11-18 08:38:19 +0900897 private void refreshImeWindowVisibilityLocked() {
898 final Configuration conf = mRes.getConfiguration();
899 final boolean haveHardKeyboard = conf.keyboard
900 != Configuration.KEYBOARD_NOKEYS;
901 final boolean hardKeyShown = haveHardKeyboard
902 && conf.hardKeyboardHidden
903 != Configuration.HARDKEYBOARDHIDDEN_YES;
John Spurlocke0980502013-10-25 11:59:29 -0400904
905 final boolean isScreenLocked = isKeyguardLocked();
906 final boolean inputActive = !isScreenLocked && (mInputShown || hardKeyShown);
John Spurlock36439b42013-10-23 16:36:47 -0400907 // We assume the softkeyboard is shown when the input is active as long as the
908 // hard keyboard is not shown.
909 final boolean inputVisible = inputActive && !hardKeyShown;
910 mImeWindowVis = (inputActive ? InputMethodService.IME_ACTIVE : 0)
911 | (inputVisible ? InputMethodService.IME_VISIBLE : 0);
satok3afd6c02011-11-18 08:38:19 +0900912 updateImeWindowStatusLocked();
913 }
914
satok15452a42011-10-28 17:58:28 +0900915 private void updateImeWindowStatusLocked() {
satokdbf29502011-08-25 15:28:23 +0900916 setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700917 }
918
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900919 // ---------------------------------------------------------------------------------------
920 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
921 // 1) it comes from the system process
922 // 2) the calling process' user id is identical to the current user id IMMS thinks.
923 private boolean calledFromValidUser() {
924 final int uid = Binder.getCallingUid();
925 final int userId = UserHandle.getUserId(uid);
926 if (DEBUG) {
927 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
928 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
929 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +0900930 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
931 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900932 }
Kenny Guy2a764942014-04-02 13:29:20 +0100933 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900934 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900935 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +0900936
937 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
938 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
939 // must not manage background users' states in any functions.
940 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
941 // by a token.
942 if (mContext.checkCallingOrSelfPermission(
943 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
944 == PackageManager.PERMISSION_GRANTED) {
945 if (DEBUG) {
946 Slog.d(TAG, "--- Access granted because the calling process has "
947 + "the INTERACT_ACROSS_USERS_FULL permission");
948 }
949 return true;
950 }
Satoshi Kataoka0766eb02013-07-31 18:30:13 +0900951 Slog.w(TAG, "--- IPC called from background users. Ignore. \n"
952 + InputMethodUtils.getStackTrace());
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +0900953 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900954 }
955
956 private boolean bindCurrentInputMethodService(
957 Intent service, ServiceConnection conn, int flags) {
958 if (service == null || conn == null) {
959 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
960 return false;
961 }
Amith Yamasani27b89e62013-01-16 12:30:11 -0800962 return mContext.bindServiceAsUser(service, conn, flags,
963 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900964 }
965
satoke7c6998e2011-06-03 17:57:59 +0900966 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800967 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900968 // TODO: Make this work even for non-current users?
969 if (!calledFromValidUser()) {
970 return Collections.emptyList();
971 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800972 synchronized (mMethodMap) {
973 return new ArrayList<InputMethodInfo>(mMethodList);
974 }
975 }
976
satoke7c6998e2011-06-03 17:57:59 +0900977 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800978 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900979 // TODO: Make this work even for non-current users?
980 if (!calledFromValidUser()) {
981 return Collections.emptyList();
982 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800983 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900984 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800985 }
986 }
987
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900988 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +0900989 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900990 * @return enabled subtypes of the specified imi
991 */
satoke7c6998e2011-06-03 17:57:59 +0900992 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +0900993 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +0900994 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900995 // TODO: Make this work even for non-current users?
996 if (!calledFromValidUser()) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +0900997 return Collections.<InputMethodSubtype>emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900998 }
satok67ddf9c2010-11-17 09:45:54 +0900999 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001000 final InputMethodInfo imi;
1001 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001002 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001003 } else {
1004 imi = mMethodMap.get(imiId);
1005 }
1006 if (imi == null) {
1007 return Collections.<InputMethodSubtype>emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001008 }
1009 return mSettings.getEnabledInputMethodSubtypeListLocked(
1010 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001011 }
1012 }
1013
satoke7c6998e2011-06-03 17:57:59 +09001014 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015 public void addClient(IInputMethodClient client,
1016 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001017 if (!calledFromValidUser()) {
1018 return;
1019 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001020 synchronized (mMethodMap) {
1021 mClients.put(client.asBinder(), new ClientState(client,
1022 inputContext, uid, pid));
1023 }
1024 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001025
satoke7c6998e2011-06-03 17:57:59 +09001026 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001028 if (!calledFromValidUser()) {
1029 return;
1030 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001032 ClientState cs = mClients.remove(client.asBinder());
1033 if (cs != null) {
1034 clearClientSessionLocked(cs);
1035 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001036 }
1037 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001038
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001039 void executeOrSendMessage(IInterface target, Message msg) {
1040 if (target.asBinder() instanceof Binder) {
1041 mCaller.sendMessage(msg);
1042 } else {
1043 handleMessage(msg);
1044 msg.recycle();
1045 }
1046 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001047
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001048 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001049 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001050 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001051 + mCurClient.client.asBinder());
1052 if (mBoundToMethod) {
1053 mBoundToMethod = false;
1054 if (mCurMethod != null) {
1055 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1056 MSG_UNBIND_INPUT, mCurMethod));
1057 }
1058 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001059
1060 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1061 MSG_SET_ACTIVE, 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1063 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1064 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001065 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001066
The Android Open Source Project10592532009-03-18 17:39:46 -07001067 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001068 }
1069 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001070
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001071 private int getImeShowFlags() {
1072 int flags = 0;
1073 if (mShowForced) {
1074 flags |= InputMethod.SHOW_FORCED
1075 | InputMethod.SHOW_EXPLICIT;
1076 } else if (mShowExplicitlyRequested) {
1077 flags |= InputMethod.SHOW_EXPLICIT;
1078 }
1079 return flags;
1080 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001081
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001082 private int getAppShowFlags() {
1083 int flags = 0;
1084 if (mShowForced) {
1085 flags |= InputMethodManager.SHOW_FORCED;
1086 } else if (!mShowExplicitlyRequested) {
1087 flags |= InputMethodManager.SHOW_IMPLICIT;
1088 }
1089 return flags;
1090 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001091
Dianne Hackborn7663d802012-02-24 13:08:49 -08001092 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 if (!mBoundToMethod) {
1094 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1095 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1096 mBoundToMethod = true;
1097 }
1098 final SessionState session = mCurClient.curSession;
1099 if (initial) {
1100 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1101 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
1102 } else {
1103 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1104 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
1105 }
1106 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001107 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001108 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001109 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001110 return new InputBindResult(session.session,
1111 session.channel != null ? session.channel.dup() : null, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001112 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001114 InputBindResult startInputLocked(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001115 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001116 // If no method is currently selected, do nothing.
1117 if (mCurMethodId == null) {
1118 return mNoBinding;
1119 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001121 ClientState cs = mClients.get(client.asBinder());
1122 if (cs == null) {
1123 throw new IllegalArgumentException("unknown client "
1124 + client.asBinder());
1125 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001126
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001127 try {
1128 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1129 // Check with the window manager to make sure this client actually
1130 // has a window with focus. If not, reject. This is thread safe
1131 // because if the focus changes some time before or after, the
1132 // next client receiving focus that has any interest in input will
1133 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001134 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1136 return null;
1137 }
1138 } catch (RemoteException e) {
1139 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001140
Dianne Hackborn7663d802012-02-24 13:08:49 -08001141 return startInputUncheckedLocked(cs, inputContext, attribute, controlFlags);
1142 }
1143
1144 InputBindResult startInputUncheckedLocked(ClientState cs,
1145 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
1146 // If no method is currently selected, do nothing.
1147 if (mCurMethodId == null) {
1148 return mNoBinding;
1149 }
1150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001152 // Was the keyguard locked when switching over to the new client?
1153 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001154 // If the client is changing, we need to switch over to the new
1155 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001156 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001157 if (DEBUG) Slog.v(TAG, "switching to client: client = "
John Spurlocke0980502013-10-25 11:59:29 -04001158 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159
1160 // If the screen is on, inform the new client it is active
1161 if (mScreenOn) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001162 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
1163 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001164 }
1165 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001167 // Bump up the sequence for this client and attach it.
1168 mCurSeq++;
1169 if (mCurSeq <= 0) mCurSeq = 1;
1170 mCurClient = cs;
1171 mCurInputContext = inputContext;
1172 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001174 // Check if the input method is changing.
1175 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1176 if (cs.curSession != null) {
1177 // Fast case: if we are already connected to the input method,
1178 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001179 return attachNewInputLocked(
1180 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 }
1182 if (mHaveConnection) {
1183 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184 // Return to client, and we will get back with it when
1185 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001186 requestClientSessionLocked(cs);
1187 return new InputBindResult(null, null, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001188 } else if (SystemClock.uptimeMillis()
1189 < (mLastBindTime+TIME_TO_RECONNECT)) {
1190 // In this case we have connected to the service, but
1191 // don't yet have its interface. If it hasn't been too
1192 // long since we did the connection, we'll return to
1193 // the client and wait to get the service interface so
1194 // we can report back. If it has been too long, we want
1195 // to fall through so we can try a disconnect/reconnect
1196 // to see if we can get back in touch with the service.
Jeff Brownc28867a2013-03-26 15:42:39 -07001197 return new InputBindResult(null, null, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001199 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1200 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001201 }
1202 }
1203 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001204
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001205 return startInputInnerLocked();
1206 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001207
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001208 InputBindResult startInputInnerLocked() {
1209 if (mCurMethodId == null) {
1210 return mNoBinding;
1211 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001212
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001213 if (!mSystemReady) {
1214 // If the system is not yet ready, we shouldn't be running third
1215 // party code.
Jeff Brownc28867a2013-03-26 15:42:39 -07001216 return new InputBindResult(null, null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001217 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001219 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1220 if (info == null) {
1221 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1222 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001223
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001224 unbindCurrentMethodLocked(false, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1227 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001228 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1229 com.android.internal.R.string.input_method_binding_label);
1230 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1231 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001232 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001233 | Context.BIND_NOT_VISIBLE | Context.BIND_NOT_FOREGROUND
1234 | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 mLastBindTime = SystemClock.uptimeMillis();
1236 mHaveConnection = true;
1237 mCurId = info.getId();
1238 mCurToken = new Binder();
1239 try {
Craig Mautnerca0ac712013-03-14 09:43:02 -07001240 if (true || DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001241 mIWindowManager.addWindowToken(mCurToken,
1242 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1243 } catch (RemoteException e) {
1244 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001245 return new InputBindResult(null, null, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246 } else {
1247 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001248 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001249 + mCurIntent);
1250 }
1251 return null;
1252 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001253
satoke7c6998e2011-06-03 17:57:59 +09001254 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255 public InputBindResult startInput(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001256 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001257 if (!calledFromValidUser()) {
1258 return null;
1259 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001260 synchronized (mMethodMap) {
1261 final long ident = Binder.clearCallingIdentity();
1262 try {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001263 return startInputLocked(client, inputContext, attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 } finally {
1265 Binder.restoreCallingIdentity(ident);
1266 }
1267 }
1268 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001269
satoke7c6998e2011-06-03 17:57:59 +09001270 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001271 public void finishInput(IInputMethodClient client) {
1272 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001273
satoke7c6998e2011-06-03 17:57:59 +09001274 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001275 public void onServiceConnected(ComponentName name, IBinder service) {
1276 synchronized (mMethodMap) {
1277 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1278 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001279 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001280 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001281 unbindCurrentMethodLocked(false, false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001282 return;
1283 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001284 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001285 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1286 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001287 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001288 clearClientSessionLocked(mCurClient);
1289 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001290 }
1291 }
1292 }
1293 }
1294
Jeff Brownc28867a2013-03-26 15:42:39 -07001295 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1296 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001297 synchronized (mMethodMap) {
1298 if (mCurMethod != null && method != null
1299 && mCurMethod.asBinder() == method.asBinder()) {
1300 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001301 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001302 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001303 method, session, channel);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001304 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001305 if (res.method != null) {
1306 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
1307 MSG_BIND_METHOD, mCurClient.client, res));
1308 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001309 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310 }
1311 }
1312 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001313
1314 // Session abandoned. Close its associated input channel.
1315 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001316 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001317
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001318 void unbindCurrentMethodLocked(boolean reportToClient, boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001319 if (mVisibleBound) {
1320 mContext.unbindService(mVisibleConnection);
1321 mVisibleBound = false;
1322 }
1323
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001324 if (mHaveConnection) {
1325 mContext.unbindService(this);
1326 mHaveConnection = false;
1327 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001328
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001329 if (mCurToken != null) {
1330 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001331 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001332 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001333 // The current IME is shown. Hence an IME switch (transition) is happening.
1334 mWindowManagerService.saveLastInputMethodWindowForTransition();
1335 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001336 mIWindowManager.removeWindowToken(mCurToken);
1337 } catch (RemoteException e) {
1338 }
1339 mCurToken = null;
1340 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001341
The Android Open Source Project10592532009-03-18 17:39:46 -07001342 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001343 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001344
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001345 if (reportToClient && mCurClient != null) {
1346 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1347 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1348 }
1349 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001350
1351 void requestClientSessionLocked(ClientState cs) {
1352 if (!cs.sessionRequested) {
1353 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1354 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1355 cs.sessionRequested = true;
1356 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1357 MSG_CREATE_SESSION, mCurMethod, channels[1],
1358 new MethodCallback(this, mCurMethod, channels[0])));
1359 }
1360 }
1361
1362 void clearClientSessionLocked(ClientState cs) {
1363 finishSessionLocked(cs.curSession);
1364 cs.curSession = null;
1365 cs.sessionRequested = false;
1366 }
1367
1368 private void finishSessionLocked(SessionState sessionState) {
1369 if (sessionState != null) {
1370 if (sessionState.session != null) {
1371 try {
1372 sessionState.session.finishSession();
1373 } catch (RemoteException e) {
1374 Slog.w(TAG, "Session failed to close due to remote exception", e);
1375 setImeWindowVisibilityStatusHiddenLocked();
1376 }
1377 sessionState.session = null;
1378 }
1379 if (sessionState.channel != null) {
1380 sessionState.channel.dispose();
1381 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001382 }
1383 }
1384 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001385
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001386 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001387 if (mCurMethod != null) {
1388 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001389 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001390 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001391
Jeff Brownc28867a2013-03-26 15:42:39 -07001392 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001393 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001394 mCurMethod = null;
1395 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001396 if (mStatusBar != null) {
1397 mStatusBar.setIconVisibility("ime", false);
1398 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001399 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001400
satoke7c6998e2011-06-03 17:57:59 +09001401 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001402 public void onServiceDisconnected(ComponentName name) {
1403 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001404 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001405 + " mCurIntent=" + mCurIntent);
1406 if (mCurMethod != null && mCurIntent != null
1407 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001408 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001409 // We consider this to be a new bind attempt, since the system
1410 // should now try to restart the service for us.
1411 mLastBindTime = SystemClock.uptimeMillis();
1412 mShowRequested = mInputShown;
1413 mInputShown = false;
1414 if (mCurClient != null) {
1415 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1416 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1417 }
1418 }
1419 }
1420 }
1421
satokf9f01002011-05-19 21:31:50 +09001422 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001423 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001424 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001425 long ident = Binder.clearCallingIdentity();
1426 try {
1427 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001428 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001429 return;
1430 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001431
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001432 synchronized (mMethodMap) {
1433 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001434 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001435 if (mStatusBar != null) {
1436 mStatusBar.setIconVisibility("ime", false);
1437 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001438 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001439 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001440 CharSequence contentDescription = null;
1441 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001442 // Use PackageManager to load label
1443 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001444 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001445 mIPackageManager.getApplicationInfo(packageName, 0,
1446 mSettings.getCurrentUserId()));
1447 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001448 /* ignore */
1449 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001450 if (mStatusBar != null) {
1451 mStatusBar.setIcon("ime", packageName, iconId, 0,
1452 contentDescription != null
1453 ? contentDescription.toString() : null);
1454 mStatusBar.setIconVisibility("ime", true);
1455 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001456 }
1457 }
1458 } finally {
1459 Binder.restoreCallingIdentity(ident);
1460 }
1461 }
1462
satok7cfc0ed2011-06-20 21:29:36 +09001463 private boolean needsToShowImeSwitchOngoingNotification() {
1464 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04001465 if (mSwitchingDialog != null) return false;
satok2c93efc2012-04-02 19:33:47 +09001466 if (isScreenLocked()) return false;
satok7cfc0ed2011-06-20 21:29:36 +09001467 synchronized (mMethodMap) {
1468 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1469 final int N = imis.size();
satokb6359412011-06-28 17:47:41 +09001470 if (N > 2) return true;
1471 if (N < 1) return false;
1472 int nonAuxCount = 0;
1473 int auxCount = 0;
1474 InputMethodSubtype nonAuxSubtype = null;
1475 InputMethodSubtype auxSubtype = null;
satok7cfc0ed2011-06-20 21:29:36 +09001476 for(int i = 0; i < N; ++i) {
1477 final InputMethodInfo imi = imis.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001478 final List<InputMethodSubtype> subtypes =
1479 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satok7cfc0ed2011-06-20 21:29:36 +09001480 final int subtypeCount = subtypes.size();
1481 if (subtypeCount == 0) {
satokb6359412011-06-28 17:47:41 +09001482 ++nonAuxCount;
satok7cfc0ed2011-06-20 21:29:36 +09001483 } else {
1484 for (int j = 0; j < subtypeCount; ++j) {
satokb6359412011-06-28 17:47:41 +09001485 final InputMethodSubtype subtype = subtypes.get(j);
1486 if (!subtype.isAuxiliary()) {
1487 ++nonAuxCount;
1488 nonAuxSubtype = subtype;
1489 } else {
1490 ++auxCount;
1491 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001492 }
1493 }
1494 }
satok7cfc0ed2011-06-20 21:29:36 +09001495 }
satokb6359412011-06-28 17:47:41 +09001496 if (nonAuxCount > 1 || auxCount > 1) {
1497 return true;
1498 } else if (nonAuxCount == 1 && auxCount == 1) {
1499 if (nonAuxSubtype != null && auxSubtype != null
satok9747f892011-09-12 15:56:40 +09001500 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1501 || auxSubtype.overridesImplicitlyEnabledSubtype()
1502 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
satokb6359412011-06-28 17:47:41 +09001503 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1504 return false;
1505 }
1506 return true;
1507 }
1508 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001509 }
satok7cfc0ed2011-06-20 21:29:36 +09001510 }
1511
John Spurlocke0980502013-10-25 11:59:29 -04001512 private boolean isKeyguardLocked() {
1513 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1514 }
1515
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001516 // Caution! This method is called in this class. Handle multi-user carefully
satokdbf29502011-08-25 15:28:23 +09001517 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001518 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001519 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001520 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001521 try {
1522 if (token == null || mCurToken != token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001523 int uid = Binder.getCallingUid();
Joe Onorato857fd9b2011-01-27 15:08:35 -08001524 Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token);
satok06487a52010-10-29 11:37:18 +09001525 return;
1526 }
satok06487a52010-10-29 11:37:18 +09001527 synchronized (mMethodMap) {
John Spurlocke0980502013-10-25 11:59:29 -04001528 // apply policy for binder calls
1529 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
1530 vis = 0;
1531 }
Joe Onorato857fd9b2011-01-27 15:08:35 -08001532 mImeWindowVis = vis;
1533 mBackDisposition = backDisposition;
jungheang.lee217fd292013-02-26 16:53:22 +09001534 final boolean iconVisibility = ((vis & (InputMethodService.IME_ACTIVE)) != 0)
1535 && (mWindowManagerService.isHardKeyboardAvailable()
1536 || (vis & (InputMethodService.IME_VISIBLE)) != 0);
Jason Monkb605fec2014-05-02 17:04:10 -04001537 final boolean needsToShowImeSwitcher = iconVisibility
1538 && needsToShowImeSwitchOngoingNotification();
1539 if (mStatusBar != null) {
1540 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
1541 needsToShowImeSwitcher);
1542 }
satok5bc8e732011-07-22 21:07:23 +09001543 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Jason Monkb605fec2014-05-02 17:04:10 -04001544 if (imi != null && needsToShowImeSwitcher) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001545 // Used to load label
satok7cfc0ed2011-06-20 21:29:36 +09001546 final CharSequence title = mRes.getText(
1547 com.android.internal.R.string.select_input_method);
Satoshi Kataokab2827262013-07-04 19:43:14 +09001548 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
1549 mContext, imi, mCurrentSubtype);
satok5bc8e732011-07-22 21:07:23 +09001550
satok7cfc0ed2011-06-20 21:29:36 +09001551 mImeSwitcherNotification.setLatestEventInfo(
satok5bc8e732011-07-22 21:07:23 +09001552 mContext, title, summary, mImeSwitchPendingIntent);
Jason Monke463c742014-05-02 12:31:45 -04001553 if ((mNotificationManager != null)
1554 && !mWindowManagerService.hasNavigationBar()) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001555 if (DEBUG) {
Satoshi Kataokab2827262013-07-04 19:43:14 +09001556 Slog.d(TAG, "--- show notification: label = " + summary);
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001557 }
1558 mNotificationManager.notifyAsUser(null,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001559 com.android.internal.R.string.select_input_method,
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001560 mImeSwitcherNotification, UserHandle.ALL);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001561 mNotificationShown = true;
1562 }
satok7cfc0ed2011-06-20 21:29:36 +09001563 } else {
Dianne Hackborn661cd522011-08-22 00:26:20 -07001564 if (mNotificationShown && mNotificationManager != null) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001565 if (DEBUG) {
1566 Slog.d(TAG, "--- hide notification");
1567 }
1568 mNotificationManager.cancelAsUser(null,
1569 com.android.internal.R.string.select_input_method, UserHandle.ALL);
satok7cfc0ed2011-06-20 21:29:36 +09001570 mNotificationShown = false;
1571 }
1572 }
satok06487a52010-10-29 11:37:18 +09001573 }
1574 } finally {
1575 Binder.restoreCallingIdentity(ident);
1576 }
1577 }
1578
satoke7c6998e2011-06-03 17:57:59 +09001579 @Override
satokf9f01002011-05-19 21:31:50 +09001580 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001581 if (!calledFromValidUser()) {
1582 return;
1583 }
satokf9f01002011-05-19 21:31:50 +09001584 synchronized (mMethodMap) {
1585 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1586 for (int i = 0; i < spans.length; ++i) {
1587 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001588 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001589 mSecureSuggestionSpans.put(ss, currentImi);
1590 }
1591 }
1592 }
1593 }
1594
satoke7c6998e2011-06-03 17:57:59 +09001595 @Override
satokf9f01002011-05-19 21:31:50 +09001596 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001597 if (!calledFromValidUser()) {
1598 return false;
1599 }
satokf9f01002011-05-19 21:31:50 +09001600 synchronized (mMethodMap) {
1601 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1602 // TODO: Do not send the intent if the process of the targetImi is already dead.
1603 if (targetImi != null) {
1604 final String[] suggestions = span.getSuggestions();
1605 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001606 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001607 final Intent intent = new Intent();
1608 // Ensures that only a class in the original IME package will receive the
1609 // notification.
satok42c5a162011-05-26 16:46:14 +09001610 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001611 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1612 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1613 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1614 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001615 final long ident = Binder.clearCallingIdentity();
1616 try {
1617 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1618 } finally {
1619 Binder.restoreCallingIdentity(ident);
1620 }
satokf9f01002011-05-19 21:31:50 +09001621 return true;
1622 }
1623 }
1624 return false;
1625 }
1626
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001627 void updateFromSettingsLocked(boolean enabledMayChange) {
1628 if (enabledMayChange) {
1629 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1630 for (int i=0; i<enabled.size(); i++) {
1631 // We allow the user to select "disabled until used" apps, so if they
1632 // are enabling one of those here we now need to make it enabled.
1633 InputMethodInfo imm = enabled.get(i);
1634 try {
1635 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1636 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1637 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09001638 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001639 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001640 if (DEBUG) {
1641 Slog.d(TAG, "Update state(" + imm.getId()
1642 + "): DISABLED_UNTIL_USED -> DEFAULT");
1643 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001644 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1645 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001646 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
1647 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001648 }
1649 } catch (RemoteException e) {
1650 }
1651 }
1652 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001653 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1654 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1655 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1656 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001657 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001658 // There is no input method selected, try to choose new applicable input method.
1659 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001660 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001661 }
1662 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001663 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001664 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001665 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001666 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001667 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001668 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001669 }
satokf3db1af2010-11-23 13:34:33 +09001670 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001671 } else {
1672 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001673 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001674 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001675 }
1676 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001677
satokab751aa2010-09-14 19:17:36 +09001678 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001679 InputMethodInfo info = mMethodMap.get(id);
1680 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001681 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001683
satokd81e9502012-05-21 12:58:45 +09001684 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001685 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09001686 final int subtypeCount = info.getSubtypeCount();
1687 if (subtypeCount <= 0) {
1688 return;
satokcd7cd292010-11-20 15:46:23 +09001689 }
satokd81e9502012-05-21 12:58:45 +09001690 final InputMethodSubtype oldSubtype = mCurrentSubtype;
1691 final InputMethodSubtype newSubtype;
1692 if (subtypeId >= 0 && subtypeId < subtypeCount) {
1693 newSubtype = info.getSubtypeAt(subtypeId);
1694 } else {
1695 // If subtype is null, try to find the most applicable one from
1696 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001697 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09001698 }
1699 if (newSubtype == null || oldSubtype == null) {
1700 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
1701 + ", new subtype = " + newSubtype);
1702 return;
1703 }
1704 if (newSubtype != oldSubtype) {
1705 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1706 if (mCurMethod != null) {
1707 try {
1708 refreshImeWindowVisibilityLocked();
1709 mCurMethod.changeInputMethodSubtype(newSubtype);
1710 } catch (RemoteException e) {
1711 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
satokab751aa2010-09-14 19:17:36 +09001712 }
1713 }
1714 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001715 return;
1716 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001717
satokd81e9502012-05-21 12:58:45 +09001718 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001719 final long ident = Binder.clearCallingIdentity();
1720 try {
satokab751aa2010-09-14 19:17:36 +09001721 // Set a subtype to this input method.
1722 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001723 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1724 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1725 // because mCurMethodId is stored as a history in
1726 // setSelectedInputMethodAndSubtypeLocked().
1727 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001728
1729 if (ActivityManagerNative.isSystemReady()) {
1730 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001731 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001732 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07001733 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001734 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001735 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001736 } finally {
1737 Binder.restoreCallingIdentity(ident);
1738 }
1739 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001740
satok42c5a162011-05-26 16:46:14 +09001741 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001742 public boolean showSoftInput(IInputMethodClient client, int flags,
1743 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001744 if (!calledFromValidUser()) {
1745 return false;
1746 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001747 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001748 long ident = Binder.clearCallingIdentity();
1749 try {
1750 synchronized (mMethodMap) {
1751 if (mCurClient == null || client == null
1752 || mCurClient.client.asBinder() != client.asBinder()) {
1753 try {
1754 // We need to check if this is the current client with
1755 // focus in the window manager, to allow this call to
1756 // be made before input is started in it.
1757 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001758 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001759 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001760 }
1761 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001762 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001763 }
1764 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001765
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001766 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001767 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001768 }
1769 } finally {
1770 Binder.restoreCallingIdentity(ident);
1771 }
1772 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001773
The Android Open Source Project4df24232009-03-05 14:34:35 -08001774 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001775 mShowRequested = true;
1776 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1777 mShowExplicitlyRequested = true;
1778 }
1779 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1780 mShowExplicitlyRequested = true;
1781 mShowForced = true;
1782 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001783
Dianne Hackborncc278702009-09-02 23:07:23 -07001784 if (!mSystemReady) {
1785 return false;
1786 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001787
The Android Open Source Project4df24232009-03-05 14:34:35 -08001788 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001789 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001790 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001791 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1792 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1793 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001794 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001795 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001796 bindCurrentInputMethodService(
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001797 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE
1798 | Context.BIND_TREAT_LIKE_ACTIVITY);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001799 mVisibleBound = true;
1800 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001801 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001802 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09001803 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001804 // The client has asked to have the input method shown, but
1805 // we have been sitting here too long with a connection to the
1806 // service and no interface received, so let's disconnect/connect
1807 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001808 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001809 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09001810 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001811 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001812 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001813 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001814 } else {
1815 if (DEBUG) {
1816 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
1817 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
1818 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001819 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001820
The Android Open Source Project4df24232009-03-05 14:34:35 -08001821 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001822 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001823
satok42c5a162011-05-26 16:46:14 +09001824 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001825 public boolean hideSoftInput(IInputMethodClient client, int flags,
1826 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001827 if (!calledFromValidUser()) {
1828 return false;
1829 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001830 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001831 long ident = Binder.clearCallingIdentity();
1832 try {
1833 synchronized (mMethodMap) {
1834 if (mCurClient == null || client == null
1835 || mCurClient.client.asBinder() != client.asBinder()) {
1836 try {
1837 // We need to check if this is the current client with
1838 // focus in the window manager, to allow this call to
1839 // be made before input is started in it.
1840 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001841 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1842 + uid + ": " + client);
satok15452a42011-10-28 17:58:28 +09001843 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001844 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001845 }
1846 } catch (RemoteException e) {
satok15452a42011-10-28 17:58:28 +09001847 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001848 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001849 }
1850 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001851
Joe Onorato8a9b2202010-02-26 18:56:32 -08001852 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001853 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001854 }
1855 } finally {
1856 Binder.restoreCallingIdentity(ident);
1857 }
1858 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001859
The Android Open Source Project4df24232009-03-05 14:34:35 -08001860 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001861 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1862 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001863 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 -08001864 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001865 }
1866 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001867 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 -08001868 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001869 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001870 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001871 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001872 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1873 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1874 res = true;
1875 } else {
1876 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001877 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001878 if (mHaveConnection && mVisibleBound) {
1879 mContext.unbindService(mVisibleConnection);
1880 mVisibleBound = false;
1881 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001882 mInputShown = false;
1883 mShowRequested = false;
1884 mShowExplicitlyRequested = false;
1885 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001886 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001887 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001888
satok42c5a162011-05-26 16:46:14 +09001889 @Override
Dianne Hackborn7663d802012-02-24 13:08:49 -08001890 public InputBindResult windowGainedFocus(IInputMethodClient client, IBinder windowToken,
1891 int controlFlags, int softInputMode, int windowFlags,
1892 EditorInfo attribute, IInputContext inputContext) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09001893 // Needs to check the validity before clearing calling identity
1894 final boolean calledFromValidUser = calledFromValidUser();
1895
Dianne Hackborn7663d802012-02-24 13:08:49 -08001896 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001897 long ident = Binder.clearCallingIdentity();
1898 try {
1899 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001900 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
Dianne Hackborn7663d802012-02-24 13:08:49 -08001901 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001902 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08001903 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001904
Dianne Hackborn7663d802012-02-24 13:08:49 -08001905 ClientState cs = mClients.get(client.asBinder());
1906 if (cs == null) {
1907 throw new IllegalArgumentException("unknown client "
1908 + client.asBinder());
1909 }
1910
1911 try {
1912 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1913 // Check with the window manager to make sure this client actually
1914 // has a window with focus. If not, reject. This is thread safe
1915 // because if the focus changes some time before or after, the
1916 // next client receiving focus that has any interest in input will
1917 // be calling through here after that change happens.
1918 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
1919 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1920 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001921 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001922 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001923 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001924
Satoshi Kataoka8d033052012-11-19 17:30:40 +09001925 if (!calledFromValidUser) {
1926 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
1927 Slog.w(TAG, "If you want to interect with IME, you need "
1928 + "android.permission.INTERACT_ACROSS_USERS_FULL");
1929 hideCurrentInputLocked(0, null);
1930 return null;
1931 }
1932
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001933 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07001934 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09001935 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001936 if (attribute != null) {
1937 return startInputUncheckedLocked(cs, inputContext, attribute,
1938 controlFlags);
1939 }
1940 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001941 }
1942 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001943
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001944 // Should we auto-show the IME even if the caller has not
1945 // specified what should be done with it?
1946 // We only do this automatically if the window can resize
1947 // to accommodate the IME (so what the user sees will give
1948 // them good context without input information being obscured
1949 // by the IME) or if running on a large screen where there
1950 // is more room for the target window + IME.
1951 final boolean doAutoShow =
1952 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1953 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1954 || mRes.getConfiguration().isLayoutSizeAtLeast(
1955 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001956 final boolean isTextEditor =
1957 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
1958
1959 // We want to start input before showing the IME, but after closing
1960 // it. We want to do this after closing it to help the IME disappear
1961 // more quickly (not get stuck behind it initializing itself for the
1962 // new focused input, even if its window wants to hide the IME).
1963 boolean didStart = false;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001964
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001965 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1966 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001967 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001968 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1969 // There is no focus view, and this window will
1970 // be behind any soft input window, so hide the
1971 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001972 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001973 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001974 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001975 } else if (isTextEditor && doAutoShow && (softInputMode &
1976 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001977 // There is a focus view, and we are navigating forward
1978 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001979 // We only do this automatically if the window can resize
1980 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001981 // them good context without input information being obscured
1982 // by the IME) or if running on a large screen where there
1983 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001984 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001985 if (attribute != null) {
1986 res = startInputUncheckedLocked(cs, inputContext, attribute,
1987 controlFlags);
1988 didStart = true;
1989 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001990 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001991 }
1992 break;
1993 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1994 // Do nothing.
1995 break;
1996 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1997 if ((softInputMode &
1998 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001999 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002000 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002001 }
2002 break;
2003 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002004 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002005 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002006 break;
2007 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2008 if ((softInputMode &
2009 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002010 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002011 if (attribute != null) {
2012 res = startInputUncheckedLocked(cs, inputContext, attribute,
2013 controlFlags);
2014 didStart = true;
2015 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002016 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002017 }
2018 break;
2019 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002020 if (DEBUG) Slog.v(TAG, "Window asks to always 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 break;
2028 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002029
2030 if (!didStart && attribute != null) {
2031 res = startInputUncheckedLocked(cs, inputContext, attribute,
2032 controlFlags);
2033 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002034 }
2035 } finally {
2036 Binder.restoreCallingIdentity(ident);
2037 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002038
2039 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002040 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002041
satok42c5a162011-05-26 16:46:14 +09002042 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002043 public void showInputMethodPickerFromClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002044 if (!calledFromValidUser()) {
2045 return;
2046 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002047 synchronized (mMethodMap) {
2048 if (mCurClient == null || client == null
2049 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002050 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002051 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002052 }
2053
satok440aab52010-11-25 09:43:11 +09002054 // Always call subtype picker, because subtype picker is a superset of input method
2055 // picker.
satokab751aa2010-09-14 19:17:36 +09002056 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
2057 }
2058 }
2059
satok42c5a162011-05-26 16:46:14 +09002060 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002061 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002062 if (!calledFromValidUser()) {
2063 return;
2064 }
satok28203512010-11-24 11:06:49 +09002065 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2066 }
2067
satok42c5a162011-05-26 16:46:14 +09002068 @Override
satok28203512010-11-24 11:06:49 +09002069 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002070 if (!calledFromValidUser()) {
2071 return;
2072 }
satok28203512010-11-24 11:06:49 +09002073 synchronized (mMethodMap) {
2074 if (subtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002075 setInputMethodWithSubtypeId(token, id, InputMethodUtils.getSubtypeIdFromHashCode(
satok28203512010-11-24 11:06:49 +09002076 mMethodMap.get(id), subtype.hashCode()));
2077 } else {
2078 setInputMethod(token, id);
2079 }
2080 }
satokab751aa2010-09-14 19:17:36 +09002081 }
2082
satok42c5a162011-05-26 16:46:14 +09002083 @Override
satokb416a712010-11-25 20:42:14 +09002084 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002085 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002086 if (!calledFromValidUser()) {
2087 return;
2088 }
satokb416a712010-11-25 20:42:14 +09002089 synchronized (mMethodMap) {
2090 if (mCurClient == null || client == null
2091 || mCurClient.client.asBinder() != client.asBinder()) {
2092 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
2093 }
satok7fee71f2010-12-17 18:54:26 +09002094 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2095 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002096 }
2097 }
2098
satok4fc87d62011-05-20 16:13:43 +09002099 @Override
satok735cf382010-11-11 20:40:09 +09002100 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002101 if (!calledFromValidUser()) {
2102 return false;
2103 }
satok735cf382010-11-11 20:40:09 +09002104 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002105 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002106 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002107 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002108 lastImi = mMethodMap.get(lastIme.first);
2109 } else {
2110 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002111 }
satok4fc87d62011-05-20 16:13:43 +09002112 String targetLastImiId = null;
2113 int subtypeId = NOT_A_SUBTYPE_ID;
2114 if (lastIme != null && lastImi != null) {
2115 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
2116 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
2117 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2118 : mCurrentSubtype.hashCode();
2119 // If the last IME is the same as the current IME and the last subtype is not
2120 // defined, there is no need to switch to the last IME.
2121 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2122 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002123 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002124 }
2125 }
2126
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002127 if (TextUtils.isEmpty(targetLastImiId)
2128 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002129 // This is a safety net. If the currentSubtype can't be added to the history
2130 // and the framework couldn't find the last ime, we will make the last ime be
2131 // the most applicable enabled keyboard subtype of the system imes.
2132 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2133 if (enabled != null) {
2134 final int N = enabled.size();
2135 final String locale = mCurrentSubtype == null
2136 ? mRes.getConfiguration().locale.toString()
2137 : mCurrentSubtype.getLocale();
2138 for (int i = 0; i < N; ++i) {
2139 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002140 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002141 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002142 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2143 InputMethodUtils.getSubtypes(imi),
2144 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002145 if (keyboardSubtype != null) {
2146 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002147 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002148 imi, keyboardSubtype.hashCode());
2149 if(keyboardSubtype.getLocale().equals(locale)) {
2150 break;
2151 }
2152 }
2153 }
2154 }
2155 }
2156 }
2157
2158 if (!TextUtils.isEmpty(targetLastImiId)) {
2159 if (DEBUG) {
2160 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2161 + ", from: " + mCurMethodId + ", " + subtypeId);
2162 }
2163 setInputMethodWithSubtypeId(token, targetLastImiId, subtypeId);
2164 return true;
2165 } else {
2166 return false;
2167 }
satok735cf382010-11-11 20:40:09 +09002168 }
2169 }
2170
satoke7c6998e2011-06-03 17:57:59 +09002171 @Override
satok688bd472012-02-09 20:09:17 +09002172 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002173 if (!calledFromValidUser()) {
2174 return false;
2175 }
satok688bd472012-02-09 20:09:17 +09002176 synchronized (mMethodMap) {
Satoshi Kataokad787f692013-10-26 04:44:21 +09002177 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethod(
satok688bd472012-02-09 20:09:17 +09002178 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2179 if (nextSubtype == null) {
2180 return false;
2181 }
2182 setInputMethodWithSubtypeId(token, nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
2183 return true;
2184 }
2185 }
2186
2187 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002188 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2189 if (!calledFromValidUser()) {
2190 return false;
2191 }
2192 synchronized (mMethodMap) {
Satoshi Kataokad787f692013-10-26 04:44:21 +09002193 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethod(
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002194 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2195 if (nextSubtype == null) {
2196 return false;
2197 }
2198 return true;
2199 }
2200 }
2201
2202 @Override
satok68f1b782011-04-11 14:26:04 +09002203 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002204 if (!calledFromValidUser()) {
2205 return null;
2206 }
satok68f1b782011-04-11 14:26:04 +09002207 synchronized (mMethodMap) {
2208 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2209 // TODO: Handle the case of the last IME with no subtypes
2210 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2211 || TextUtils.isEmpty(lastIme.second)) return null;
2212 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2213 if (lastImi == null) return null;
2214 try {
2215 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002216 final int lastSubtypeId =
2217 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002218 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2219 return null;
2220 }
2221 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002222 } catch (NumberFormatException e) {
2223 return null;
2224 }
2225 }
2226 }
2227
satoke7c6998e2011-06-03 17:57:59 +09002228 @Override
satokee5e77c2011-09-02 18:50:15 +09002229 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002230 if (!calledFromValidUser()) {
2231 return;
2232 }
satok91e88122011-07-18 11:11:42 +09002233 // By this IPC call, only a process which shares the same uid with the IME can add
2234 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09002235 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09002236 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002237 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002238 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002239 final String[] packageInfos;
2240 try {
2241 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2242 } catch (RemoteException e) {
2243 Slog.e(TAG, "Failed to get package infos");
2244 return;
2245 }
satok91e88122011-07-18 11:11:42 +09002246 if (packageInfos != null) {
2247 final int packageNum = packageInfos.length;
2248 for (int i = 0; i < packageNum; ++i) {
2249 if (packageInfos[i].equals(imi.getPackageName())) {
2250 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002251 final long ident = Binder.clearCallingIdentity();
2252 try {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002253 buildInputMethodListLocked(mMethodList, mMethodMap,
2254 false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002255 } finally {
2256 Binder.restoreCallingIdentity(ident);
2257 }
satokee5e77c2011-09-02 18:50:15 +09002258 return;
satok91e88122011-07-18 11:11:42 +09002259 }
2260 }
2261 }
satoke7c6998e2011-06-03 17:57:59 +09002262 }
satokee5e77c2011-09-02 18:50:15 +09002263 return;
satoke7c6998e2011-06-03 17:57:59 +09002264 }
2265
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002266 @Override
2267 public int getInputMethodWindowVisibleHeight() {
2268 return mWindowManagerService.getInputMethodWindowVisibleHeight();
2269 }
2270
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002271 @Override
2272 public void notifyTextCommitted() {
2273 if (DEBUG) {
2274 Slog.d(TAG, "Got the notification of commitText");
2275 }
2276 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2277 if (imi != null) {
2278 mSwitchingController.onCommitText(imi, mCurrentSubtype);
2279 }
2280 }
2281
Yohei Yukawab7b79072014-03-25 11:02:00 +09002282 @Override
2283 public void setCursorAnchorMonitorMode(IBinder token, int monitorMode) {
2284 if (DEBUG) {
2285 Slog.d(TAG, "setCursorAnchorMonitorMode: monitorMode=" + monitorMode);
2286 }
2287 if (!calledFromValidUser()) {
2288 return;
2289 }
2290 synchronized (mMethodMap) {
2291 if (token == null || mCurToken != token) {
2292 if (DEBUG) {
2293 Slog.w(TAG, "Ignoring setCursorAnchorMonitorMode from uid "
2294 + Binder.getCallingUid() + " token: " + token);
2295 }
2296 return;
2297 }
2298 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIO(
2299 MSG_SET_CURSOR_ANCHOR_MONITOR_MODE, monitorMode, mCurClient));
2300 }
2301 }
2302
satok28203512010-11-24 11:06:49 +09002303 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002304 synchronized (mMethodMap) {
2305 if (token == null) {
2306 if (mContext.checkCallingOrSelfPermission(
2307 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2308 != PackageManager.PERMISSION_GRANTED) {
2309 throw new SecurityException(
2310 "Using null token requires permission "
2311 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2312 }
2313 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002314 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2315 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002316 return;
2317 }
2318
satokc5933802011-08-31 21:26:04 +09002319 final long ident = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002320 try {
satokab751aa2010-09-14 19:17:36 +09002321 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002322 } finally {
2323 Binder.restoreCallingIdentity(ident);
2324 }
2325 }
2326 }
2327
satok42c5a162011-05-26 16:46:14 +09002328 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002329 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002330 if (!calledFromValidUser()) {
2331 return;
2332 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002333 synchronized (mMethodMap) {
2334 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002335 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
2336 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002337 return;
2338 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002339 long ident = Binder.clearCallingIdentity();
2340 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002341 hideCurrentInputLocked(flags, null);
2342 } finally {
2343 Binder.restoreCallingIdentity(ident);
2344 }
2345 }
2346 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002347
satok42c5a162011-05-26 16:46:14 +09002348 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002349 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002350 if (!calledFromValidUser()) {
2351 return;
2352 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002353 synchronized (mMethodMap) {
2354 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002355 Slog.w(TAG, "Ignoring showMySoftInput of uid "
2356 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002357 return;
2358 }
2359 long ident = Binder.clearCallingIdentity();
2360 try {
2361 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002362 } finally {
2363 Binder.restoreCallingIdentity(ident);
2364 }
2365 }
2366 }
2367
2368 void setEnabledSessionInMainThread(SessionState session) {
2369 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002370 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002371 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002372 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002373 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002374 } catch (RemoteException e) {
2375 }
2376 }
2377 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002378 if (mEnabledSession != null && mEnabledSession.session != null) {
2379 try {
2380 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
2381 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
2382 } catch (RemoteException e) {
2383 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002384 }
2385 }
2386 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002387
satok42c5a162011-05-26 16:46:14 +09002388 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002389 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002390 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002391 switch (msg.what) {
2392 case MSG_SHOW_IM_PICKER:
2393 showInputMethodMenu();
2394 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002395
satokab751aa2010-09-14 19:17:36 +09002396 case MSG_SHOW_IM_SUBTYPE_PICKER:
2397 showInputMethodSubtypeMenu();
2398 return true;
2399
satok47a44912010-10-06 16:03:58 +09002400 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002401 args = (SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09002402 showInputMethodAndSubtypeEnabler((String)args.arg1);
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002403 args.recycle();
satok217f5482010-12-15 05:19:19 +09002404 return true;
2405
2406 case MSG_SHOW_IM_CONFIG:
2407 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002408 return true;
2409
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002410 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002411
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002412 case MSG_UNBIND_INPUT:
2413 try {
2414 ((IInputMethod)msg.obj).unbindInput();
2415 } catch (RemoteException e) {
2416 // There is nothing interesting about the method dying.
2417 }
2418 return true;
2419 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002420 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002421 try {
2422 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2423 } catch (RemoteException e) {
2424 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002425 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002426 return true;
2427 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002428 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002429 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002430 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002431 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002432 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002433 } catch (RemoteException e) {
2434 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002435 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002436 return true;
2437 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002438 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002439 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002440 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002441 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002442 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002443 } catch (RemoteException e) {
2444 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002445 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002446 return true;
2447 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002448 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002449 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002450 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002451 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2452 } catch (RemoteException e) {
2453 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002454 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002455 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002456 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002457 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002458 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002459 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002460 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002461 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002462 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002463 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002464 // Dispose the channel if the input method is not local to this process
2465 // because the remote proxy will get its own copy when unparceled.
2466 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002467 channel.dispose();
2468 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002469 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002470 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002471 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002472 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002473 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002474
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002475 case MSG_START_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002476 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002477 try {
2478 SessionState session = (SessionState)args.arg1;
2479 setEnabledSessionInMainThread(session);
2480 session.method.startInput((IInputContext)args.arg2,
2481 (EditorInfo)args.arg3);
2482 } catch (RemoteException e) {
2483 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002484 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002485 return true;
2486 case MSG_RESTART_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002487 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002488 try {
2489 SessionState session = (SessionState)args.arg1;
2490 setEnabledSessionInMainThread(session);
2491 session.method.restartInput((IInputContext)args.arg2,
2492 (EditorInfo)args.arg3);
2493 } catch (RemoteException e) {
2494 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002495 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002496 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002498 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002499
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002500 case MSG_UNBIND_METHOD:
2501 try {
2502 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
2503 } catch (RemoteException e) {
2504 // There is nothing interesting about the last client dying.
2505 }
2506 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002507 case MSG_BIND_METHOD: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002508 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002509 IInputMethodClient client = (IInputMethodClient)args.arg1;
2510 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002511 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002512 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002513 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002514 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002515 } finally {
2516 // Dispose the channel if the input method is not local to this process
2517 // because the remote proxy will get its own copy when unparceled.
2518 if (res.channel != null && Binder.isProxy(client)) {
2519 res.channel.dispose();
2520 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002521 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002522 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002523 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002524 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002525 case MSG_SET_ACTIVE:
2526 try {
2527 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2528 } catch (RemoteException e) {
2529 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2530 + ((ClientState)msg.obj).pid + " uid "
2531 + ((ClientState)msg.obj).uid);
2532 }
2533 return true;
Yohei Yukawab7b79072014-03-25 11:02:00 +09002534 case MSG_SET_CURSOR_ANCHOR_MONITOR_MODE:
2535 try {
2536 ((ClientState)msg.obj).client.setCursorAnchorMonitorMode(msg.arg1);
2537 } catch (RemoteException e) {
2538 Slog.w(TAG, "Got RemoteException sending setCursorAnchorMonitorMode "
2539 + "notification to pid " + ((ClientState)msg.obj).pid
2540 + " uid " + ((ClientState)msg.obj).uid);
2541 }
2542 return true;
satok01038492012-04-09 21:08:27 +09002543
2544 // --------------------------------------------------------------
2545 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
2546 mHardKeyboardListener.handleHardKeyboardStatusChange(
2547 msg.arg1 == 1, msg.arg2 == 1);
2548 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002549 }
2550 return false;
2551 }
2552
satokdc9ddae2011-10-06 12:22:36 +09002553 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002554 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
2555 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09002556 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002557 if (DEBUG) {
2558 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2559 }
satok723a27e2010-11-11 14:58:11 +09002560 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002561 return true;
2562 }
2563
2564 return false;
2565 }
2566
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002567 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002568 HashMap<String, InputMethodInfo> map, boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002569 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002570 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Satoshi Kataoka0766eb02013-07-31 18:30:13 +09002571 + " \n ------ \n" + InputMethodUtils.getStackTrace());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002572 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002573 list.clear();
2574 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002575
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002576 // Use for queryIntentServicesAsUser
2577 final PackageManager pm = mContext.getPackageManager();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002578 String disabledSysImes = mSettings.getDisabledSystemInputMethods();
Amith Yamasanie861ec12010-03-24 21:39:27 -07002579 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002580
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002581 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002582 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002583 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2584 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002585
satoke7c6998e2011-06-03 17:57:59 +09002586 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2587 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002588 for (int i = 0; i < services.size(); ++i) {
2589 ResolveInfo ri = services.get(i);
2590 ServiceInfo si = ri.serviceInfo;
2591 ComponentName compName = new ComponentName(si.packageName, si.name);
2592 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2593 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002594 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002595 + ": it does not require the permission "
2596 + android.Manifest.permission.BIND_INPUT_METHOD);
2597 continue;
2598 }
2599
Joe Onorato8a9b2202010-02-26 18:56:32 -08002600 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002601
2602 try {
satoke7c6998e2011-06-03 17:57:59 +09002603 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002604 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002605 final String id = p.getId();
2606 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002607
2608 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002609 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002610 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002611
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002612 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002613 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002614 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002615 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002616 }
2617 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002618
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002619 if (resetDefaultEnabledIme) {
2620 final ArrayList<InputMethodInfo> defaultEnabledIme =
2621 InputMethodUtils.getDefaultEnabledImes(mContext, mSystemReady, list);
2622 for (int i = 0; i < defaultEnabledIme.size(); ++i) {
2623 final InputMethodInfo imi = defaultEnabledIme.get(i);
2624 if (DEBUG) {
2625 Slog.d(TAG, "--- enable ime = " + imi);
2626 }
2627 setInputMethodEnabledLocked(imi.getId(), true);
2628 }
2629 }
2630
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002631 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09002632 if (!TextUtils.isEmpty(defaultImiId)) {
2633 if (!map.containsKey(defaultImiId)) {
2634 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
2635 if (chooseNewDefaultIMELocked()) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002636 updateFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09002637 }
2638 } else {
2639 // Double check that the default IME is certainly enabled.
2640 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002641 }
2642 }
Yohei Yukawac834a252014-05-21 22:42:32 +09002643
2644 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002645 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002646
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002647 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002648
satokab751aa2010-09-14 19:17:36 +09002649 private void showInputMethodMenu() {
2650 showInputMethodMenuInternal(false);
2651 }
2652
2653 private void showInputMethodSubtypeMenu() {
2654 showInputMethodMenuInternal(true);
2655 }
2656
satok217f5482010-12-15 05:19:19 +09002657 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002658 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002659 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002660 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2661 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002662 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002663 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002664 }
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002665 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok217f5482010-12-15 05:19:19 +09002666 }
2667
2668 private void showConfigureInputMethods() {
2669 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2670 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2671 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2672 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002673 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09002674 }
2675
satok2c93efc2012-04-02 19:33:47 +09002676 private boolean isScreenLocked() {
2677 return mKeyguardManager != null
2678 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
2679 }
satokab751aa2010-09-14 19:17:36 +09002680 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002681 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002682
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002683 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09002684 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002685
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002686 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002687 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002688 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002689
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002690 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002691 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09002692 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
2693 mContext);
satok7f35c8c2010-10-07 21:13:11 +09002694 if (immis == null || immis.size() == 0) {
2695 return;
2696 }
2697
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002698 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002699
satok688bd472012-02-09 20:09:17 +09002700 final List<ImeSubtypeListItem> imList =
Satoshi Kataokad787f692013-10-26 04:44:21 +09002701 mSwitchingController.getSortedInputMethodAndSubtypeList(
satok688bd472012-02-09 20:09:17 +09002702 showSubtypes, mInputShown, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09002703
satokc3690562012-01-10 20:14:43 +09002704 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002705 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09002706 if (currentSubtype != null) {
2707 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002708 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
2709 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09002710 }
2711 }
2712
Ken Wakasa761eb372011-03-04 19:06:18 +09002713 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09002714 mIms = new InputMethodInfo[N];
2715 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002716 int checkedItem = 0;
2717 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002718 final ImeSubtypeListItem item = imList.get(i);
2719 mIms[i] = item.mImi;
2720 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002721 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002722 int subtypeId = mSubtypeIds[i];
2723 if ((subtypeId == NOT_A_SUBTYPE_ID)
2724 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2725 || (subtypeId == lastInputMethodSubtypeId)) {
2726 checkedItem = i;
2727 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002728 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002729 }
Ken Wakasa05dbb652011-08-22 15:22:43 +09002730 final TypedArray a = context.obtainStyledAttributes(null,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002731 com.android.internal.R.styleable.DialogPreference,
2732 com.android.internal.R.attr.alertDialogStyle, 0);
2733 mDialogBuilder = new AlertDialog.Builder(context)
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002734 .setOnCancelListener(new OnCancelListener() {
satok42c5a162011-05-26 16:46:14 +09002735 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002736 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002737 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002738 }
2739 })
2740 .setIcon(a.getDrawable(
2741 com.android.internal.R.styleable.DialogPreference_dialogTitle));
2742 a.recycle();
satok01038492012-04-09 21:08:27 +09002743 final LayoutInflater inflater =
2744 (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2745 final View tv = inflater.inflate(
2746 com.android.internal.R.layout.input_method_switch_dialog_title, null);
2747 mDialogBuilder.setCustomTitle(tv);
2748
2749 // Setup layout for a toggle switch of the hardware keyboard
2750 mSwitchingDialogTitleView = tv;
2751 mSwitchingDialogTitleView.findViewById(
2752 com.android.internal.R.id.hard_keyboard_section).setVisibility(
2753 mWindowManagerService.isHardKeyboardAvailable() ?
2754 View.VISIBLE : View.GONE);
2755 final Switch hardKeySwitch = ((Switch)mSwitchingDialogTitleView.findViewById(
2756 com.android.internal.R.id.hard_keyboard_switch));
2757 hardKeySwitch.setChecked(mWindowManagerService.isHardKeyboardEnabled());
2758 hardKeySwitch.setOnCheckedChangeListener(
2759 new OnCheckedChangeListener() {
2760 @Override
2761 public void onCheckedChanged(
2762 CompoundButton buttonView, boolean isChecked) {
2763 mWindowManagerService.setHardKeyboardEnabled(isChecked);
Satoshi Kataoka04dd24d2013-01-18 13:44:37 +09002764 // Ensure that the input method dialog is dismissed when changing
2765 // the hardware keyboard state.
2766 hideInputMethodMenu();
satok01038492012-04-09 21:08:27 +09002767 }
2768 });
satokd87c2592010-09-29 11:52:06 +09002769
Ken Wakasa05dbb652011-08-22 15:22:43 +09002770 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(context,
2771 com.android.internal.R.layout.simple_list_item_2_single_choice, imList,
2772 checkedItem);
2773
2774 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002775 new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002776 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002777 public void onClick(DialogInterface dialog, int which) {
2778 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09002779 if (mIms == null || mIms.length <= which
2780 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002781 return;
2782 }
2783 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09002784 int subtypeId = mSubtypeIds[which];
Satoshi Kataokad2142962012-11-12 18:43:06 +09002785 adapter.mCheckedItem = which;
2786 adapter.notifyDataSetChanged();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002787 hideInputMethodMenu();
2788 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09002789 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09002790 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09002791 subtypeId = NOT_A_SUBTYPE_ID;
2792 }
2793 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002794 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08002795 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002796 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002797 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002798
satokbc81b692011-08-26 16:22:22 +09002799 if (showSubtypes && !isScreenLocked) {
satok82beadf2010-12-27 19:03:06 +09002800 mDialogBuilder.setPositiveButton(
2801 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09002802 new DialogInterface.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002803 @Override
satok7f35c8c2010-10-07 21:13:11 +09002804 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09002805 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09002806 }
2807 });
2808 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002809 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002810 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002811 mSwitchingDialog.getWindow().setType(
2812 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09002813 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
2814 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002815 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
Jason Monk807ef762014-05-08 15:47:46 -04002816 updateImeWindowStatusLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002817 mSwitchingDialog.show();
2818 }
2819 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002820
Ken Wakasa05dbb652011-08-22 15:22:43 +09002821 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
2822 private final LayoutInflater mInflater;
2823 private final int mTextViewResourceId;
2824 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09002825 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09002826 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
2827 List<ImeSubtypeListItem> itemsList, int checkedItem) {
2828 super(context, textViewResourceId, itemsList);
2829 mTextViewResourceId = textViewResourceId;
2830 mItemsList = itemsList;
2831 mCheckedItem = checkedItem;
2832 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2833 }
2834
2835 @Override
2836 public View getView(int position, View convertView, ViewGroup parent) {
2837 final View view = convertView != null ? convertView
2838 : mInflater.inflate(mTextViewResourceId, null);
2839 if (position < 0 || position >= mItemsList.size()) return view;
2840 final ImeSubtypeListItem item = mItemsList.get(position);
2841 final CharSequence imeName = item.mImeName;
2842 final CharSequence subtypeName = item.mSubtypeName;
2843 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
2844 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
2845 if (TextUtils.isEmpty(subtypeName)) {
2846 firstTextView.setText(imeName);
2847 secondTextView.setVisibility(View.GONE);
2848 } else {
2849 firstTextView.setText(subtypeName);
2850 secondTextView.setText(imeName);
2851 secondTextView.setVisibility(View.VISIBLE);
2852 }
2853 final RadioButton radioButton =
2854 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
2855 radioButton.setChecked(position == mCheckedItem);
2856 return view;
2857 }
2858 }
2859
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002860 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07002861 synchronized (mMethodMap) {
2862 hideInputMethodMenuLocked();
2863 }
2864 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002865
The Android Open Source Project10592532009-03-18 17:39:46 -07002866 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002867 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002868
The Android Open Source Project10592532009-03-18 17:39:46 -07002869 if (mSwitchingDialog != null) {
2870 mSwitchingDialog.dismiss();
2871 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002872 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002873
Jason Monk807ef762014-05-08 15:47:46 -04002874 updateImeWindowStatusLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -07002875 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07002876 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002877 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002879 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002880
satok42c5a162011-05-26 16:46:14 +09002881 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002882 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002883 // TODO: Make this work even for non-current users?
2884 if (!calledFromValidUser()) {
2885 return false;
2886 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002887 synchronized (mMethodMap) {
2888 if (mContext.checkCallingOrSelfPermission(
2889 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2890 != PackageManager.PERMISSION_GRANTED) {
2891 throw new SecurityException(
2892 "Requires permission "
2893 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2894 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002895
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002896 long ident = Binder.clearCallingIdentity();
2897 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002898 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002899 } finally {
2900 Binder.restoreCallingIdentity(ident);
2901 }
2902 }
2903 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002904
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002905 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
2906 // Make sure this is a valid input method.
2907 InputMethodInfo imm = mMethodMap.get(id);
2908 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09002909 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002910 }
2911
satokd87c2592010-09-29 11:52:06 +09002912 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
2913 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002914
satokd87c2592010-09-29 11:52:06 +09002915 if (enabled) {
2916 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
2917 if (pair.first.equals(id)) {
2918 // We are enabling this input method, but it is already enabled.
2919 // Nothing to do. The previous state was enabled.
2920 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002921 }
2922 }
satokd87c2592010-09-29 11:52:06 +09002923 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
2924 // Previous state was disabled.
2925 return false;
2926 } else {
2927 StringBuilder builder = new StringBuilder();
2928 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2929 builder, enabledInputMethodsList, id)) {
2930 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002931 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002932 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
2933 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
2934 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09002935 }
2936 // Previous state was enabled.
2937 return true;
2938 } else {
2939 // We are disabling the input method but it is already disabled.
2940 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002941 return false;
2942 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002943 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002944 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002945
satok723a27e2010-11-11 14:58:11 +09002946 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
2947 boolean setSubtypeOnly) {
2948 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002949 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09002950
2951 // Set Subtype here
2952 if (imi == null || subtypeId < 0) {
2953 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08002954 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09002955 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09002956 if (subtypeId < imi.getSubtypeCount()) {
2957 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
2958 mSettings.putSelectedSubtype(subtype.hashCode());
2959 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09002960 } else {
2961 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09002962 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002963 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09002964 }
satokab751aa2010-09-14 19:17:36 +09002965 }
satok723a27e2010-11-11 14:58:11 +09002966
satok4c0e7152012-06-20 20:08:44 +09002967 // Workaround.
2968 // ASEC is not ready in the IMMS constructor. Accordingly, forward-locked
2969 // IMEs are not recognized and considered uninstalled.
2970 // Actually, we can't move everything after SystemReady because
2971 // IMMS needs to run in the encryption lock screen. So, we just skip changing
2972 // the default IME here and try cheking the default IME again in systemReady().
2973 // TODO: Do nothing before system ready and implement a separated logic for
2974 // the encryption lock screen.
2975 // TODO: ASEC should be ready before IMMS is instantiated.
2976 if (mSystemReady && !setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09002977 // Set InputMethod here
2978 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
2979 }
2980 }
2981
2982 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
2983 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
2984 int lastSubtypeId = NOT_A_SUBTYPE_ID;
2985 // newDefaultIme is empty when there is no candidate for the selected IME.
2986 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
2987 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
2988 if (subtypeHashCode != null) {
2989 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002990 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok723a27e2010-11-11 14:58:11 +09002991 imi, Integer.valueOf(subtypeHashCode));
2992 } catch (NumberFormatException e) {
2993 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
2994 }
2995 }
2996 }
2997 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09002998 }
2999
satok4e4569d2010-11-19 18:45:53 +09003000 // If there are no selected shortcuts, tries finding the most applicable ones.
3001 private Pair<InputMethodInfo, InputMethodSubtype>
3002 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3003 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3004 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003005 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003006 boolean foundInSystemIME = false;
3007
3008 // Search applicable subtype for each InputMethodInfo
3009 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003010 final String imiId = imi.getId();
3011 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3012 continue;
3013 }
satokcd7cd292010-11-20 15:46:23 +09003014 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003015 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003016 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003017 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003018 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003019 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003020 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003021 }
satokdf31ae62011-01-15 06:19:44 +09003022 // 2. Search by the system locale from enabledSubtypes.
3023 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003024 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003025 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003026 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003027 }
satoka86f5e42011-09-02 17:12:42 +09003028 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003029 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003030 final ArrayList<InputMethodSubtype> subtypesForSearch =
3031 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003032 ? InputMethodUtils.getSubtypes(imi)
3033 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003034 // 4. Search by the current subtype's locale from all subtypes.
3035 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003036 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003037 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003038 }
3039 // 5. Search by the system locale from all subtypes.
3040 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003041 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003042 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003043 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003044 }
satokcd7cd292010-11-20 15:46:23 +09003045 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003046 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003047 // The current input method is the most applicable IME.
3048 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003049 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003050 break;
satok7599a7f2010-12-22 13:45:23 +09003051 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003052 // The system input method is 2nd applicable IME.
3053 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003054 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003055 if ((imi.getServiceInfo().applicationInfo.flags
3056 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3057 foundInSystemIME = true;
3058 }
satok4e4569d2010-11-19 18:45:53 +09003059 }
3060 }
3061 }
3062 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003063 if (mostApplicableIMI != null) {
3064 Slog.w(TAG, "Most applicable shortcut input method was:"
3065 + mostApplicableIMI.getId());
3066 if (mostApplicableSubtype != null) {
3067 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3068 + "," + mostApplicableSubtype.getMode() + ","
3069 + mostApplicableSubtype.getLocale());
3070 }
3071 }
satok4e4569d2010-11-19 18:45:53 +09003072 }
satokcd7cd292010-11-20 15:46:23 +09003073 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09003074 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09003075 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003076 } else {
3077 return null;
3078 }
3079 }
3080
satokab751aa2010-09-14 19:17:36 +09003081 /**
3082 * @return Return the current subtype of this input method.
3083 */
satok42c5a162011-05-26 16:46:14 +09003084 @Override
satokab751aa2010-09-14 19:17:36 +09003085 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003086 // TODO: Make this work even for non-current users?
3087 if (!calledFromValidUser()) {
3088 return null;
3089 }
3090 synchronized (mMethodMap) {
3091 return getCurrentInputMethodSubtypeLocked();
3092 }
3093 }
3094
3095 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003096 if (mCurMethodId == null) {
3097 return null;
3098 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003099 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003100 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3101 if (imi == null || imi.getSubtypeCount() == 0) {
3102 return null;
satok4e4569d2010-11-19 18:45:53 +09003103 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003104 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003105 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3106 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003107 if (subtypeId == NOT_A_SUBTYPE_ID) {
3108 // If there are no selected subtypes, the framework will try to find
3109 // the most applicable subtype from explicitly or implicitly enabled
3110 // subtypes.
3111 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003112 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003113 // If there is only one explicitly or implicitly enabled subtype,
3114 // just returns it.
3115 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3116 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3117 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003118 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003119 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003120 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003121 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003122 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003123 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3124 true);
satok4e4569d2010-11-19 18:45:53 +09003125 }
satok3ef8b292010-11-23 06:06:29 +09003126 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003127 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003128 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003129 }
3130 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003131 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003132 }
3133
satokf3db1af2010-11-23 13:34:33 +09003134 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
3135 InputMethodSubtype subtype) {
3136 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
3137 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
3138 } else {
3139 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3140 subtypes.add(subtype);
3141 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
3142 }
3143 }
3144
satok4e4569d2010-11-19 18:45:53 +09003145 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003146 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003147 @Override
satok4e4569d2010-11-19 18:45:53 +09003148 public List getShortcutInputMethodsAndSubtypes() {
3149 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09003150 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09003151 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003152 // If there are no selected shortcut subtypes, the framework will try to find
3153 // the most applicable subtype from all subtypes whose mode is
3154 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003155 Pair<InputMethodInfo, InputMethodSubtype> info =
3156 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003157 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003158 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003159 ret.add(info.first);
3160 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003161 }
satok3da92232011-01-11 22:46:30 +09003162 return ret;
satokf3db1af2010-11-23 13:34:33 +09003163 }
satokf3db1af2010-11-23 13:34:33 +09003164 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3165 ret.add(imi);
3166 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3167 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003168 }
3169 }
satokf3db1af2010-11-23 13:34:33 +09003170 return ret;
satok4e4569d2010-11-19 18:45:53 +09003171 }
3172 }
3173
satok42c5a162011-05-26 16:46:14 +09003174 @Override
satokb66d2872010-11-10 01:04:04 +09003175 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003176 // TODO: Make this work even for non-current users?
3177 if (!calledFromValidUser()) {
3178 return false;
3179 }
satokb66d2872010-11-10 01:04:04 +09003180 synchronized (mMethodMap) {
3181 if (subtype != null && mCurMethodId != null) {
3182 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003183 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003184 if (subtypeId != NOT_A_SUBTYPE_ID) {
3185 setInputMethodLocked(mCurMethodId, subtypeId);
3186 return true;
3187 }
3188 }
3189 return false;
3190 }
3191 }
3192
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003193 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003194 private static class InputMethodFileManager {
3195 private static final String SYSTEM_PATH = "system";
3196 private static final String INPUT_METHOD_PATH = "inputmethod";
3197 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3198 private static final String NODE_SUBTYPES = "subtypes";
3199 private static final String NODE_SUBTYPE = "subtype";
3200 private static final String NODE_IMI = "imi";
3201 private static final String ATTR_ID = "id";
3202 private static final String ATTR_LABEL = "label";
3203 private static final String ATTR_ICON = "icon";
3204 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3205 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3206 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3207 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3208 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3209 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003210 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
satoke7c6998e2011-06-03 17:57:59 +09003211 new HashMap<String, List<InputMethodSubtype>>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003212 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003213 if (methodMap == null) {
3214 throw new NullPointerException("methodMap is null");
3215 }
3216 mMethodMap = methodMap;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003217 final File systemDir = userId == UserHandle.USER_OWNER
3218 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3219 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003220 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3221 if (!inputMethodDir.mkdirs()) {
3222 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3223 }
3224 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3225 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3226 if (!subtypeFile.exists()) {
3227 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003228 writeAdditionalInputMethodSubtypes(
3229 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003230 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003231 readAdditionalInputMethodSubtypes(
3232 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003233 }
3234 }
3235
3236 private void deleteAllInputMethodSubtypes(String imiId) {
3237 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003238 mAdditionalSubtypesMap.remove(imiId);
3239 writeAdditionalInputMethodSubtypes(
3240 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003241 }
3242 }
3243
3244 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003245 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003246 synchronized (mMethodMap) {
3247 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3248 final int N = additionalSubtypes.length;
3249 for (int i = 0; i < N; ++i) {
3250 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003251 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003252 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003253 } else {
3254 Slog.w(TAG, "Duplicated subtype definition found: "
3255 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003256 }
3257 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003258 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3259 writeAdditionalInputMethodSubtypes(
3260 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003261 }
3262 }
3263
3264 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3265 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003266 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003267 }
3268 }
3269
3270 private static void writeAdditionalInputMethodSubtypes(
3271 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3272 HashMap<String, InputMethodInfo> methodMap) {
3273 // Safety net for the case that this function is called before methodMap is set.
3274 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3275 FileOutputStream fos = null;
3276 try {
3277 fos = subtypesFile.startWrite();
3278 final XmlSerializer out = new FastXmlSerializer();
3279 out.setOutput(fos, "utf-8");
3280 out.startDocument(null, true);
3281 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3282 out.startTag(null, NODE_SUBTYPES);
3283 for (String imiId : allSubtypes.keySet()) {
3284 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3285 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3286 continue;
3287 }
3288 out.startTag(null, NODE_IMI);
3289 out.attribute(null, ATTR_ID, imiId);
3290 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3291 final int N = subtypesList.size();
3292 for (int i = 0; i < N; ++i) {
3293 final InputMethodSubtype subtype = subtypesList.get(i);
3294 out.startTag(null, NODE_SUBTYPE);
3295 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3296 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3297 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3298 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3299 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3300 out.attribute(null, ATTR_IS_AUXILIARY,
3301 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3302 out.endTag(null, NODE_SUBTYPE);
3303 }
3304 out.endTag(null, NODE_IMI);
3305 }
3306 out.endTag(null, NODE_SUBTYPES);
3307 out.endDocument();
3308 subtypesFile.finishWrite(fos);
3309 } catch (java.io.IOException e) {
3310 Slog.w(TAG, "Error writing subtypes", e);
3311 if (fos != null) {
3312 subtypesFile.failWrite(fos);
3313 }
3314 }
3315 }
3316
3317 private static void readAdditionalInputMethodSubtypes(
3318 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3319 if (allSubtypes == null || subtypesFile == null) return;
3320 allSubtypes.clear();
3321 FileInputStream fis = null;
3322 try {
3323 fis = subtypesFile.openRead();
3324 final XmlPullParser parser = Xml.newPullParser();
3325 parser.setInput(fis, null);
3326 int type = parser.getEventType();
3327 // Skip parsing until START_TAG
3328 while ((type = parser.next()) != XmlPullParser.START_TAG
3329 && type != XmlPullParser.END_DOCUMENT) {}
3330 String firstNodeName = parser.getName();
3331 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3332 throw new XmlPullParserException("Xml doesn't start with subtypes");
3333 }
3334 final int depth =parser.getDepth();
3335 String currentImiId = null;
3336 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3337 while (((type = parser.next()) != XmlPullParser.END_TAG
3338 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3339 if (type != XmlPullParser.START_TAG)
3340 continue;
3341 final String nodeName = parser.getName();
3342 if (NODE_IMI.equals(nodeName)) {
3343 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3344 if (TextUtils.isEmpty(currentImiId)) {
3345 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3346 continue;
3347 }
3348 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
3349 allSubtypes.put(currentImiId, tempSubtypesArray);
3350 } else if (NODE_SUBTYPE.equals(nodeName)) {
3351 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3352 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3353 continue;
3354 }
3355 final int icon = Integer.valueOf(
3356 parser.getAttributeValue(null, ATTR_ICON));
3357 final int label = Integer.valueOf(
3358 parser.getAttributeValue(null, ATTR_LABEL));
3359 final String imeSubtypeLocale =
3360 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3361 final String imeSubtypeMode =
3362 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3363 final String imeSubtypeExtraValue =
3364 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003365 final boolean isAuxiliary = "1".equals(String.valueOf(
3366 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
satoke7c6998e2011-06-03 17:57:59 +09003367 final InputMethodSubtype subtype =
3368 new InputMethodSubtype(label, icon, imeSubtypeLocale,
3369 imeSubtypeMode, imeSubtypeExtraValue, isAuxiliary);
3370 tempSubtypesArray.add(subtype);
3371 }
3372 }
3373 } catch (XmlPullParserException e) {
3374 Slog.w(TAG, "Error reading subtypes: " + e);
3375 return;
3376 } catch (java.io.IOException e) {
3377 Slog.w(TAG, "Error reading subtypes: " + e);
3378 return;
3379 } catch (NumberFormatException e) {
3380 Slog.w(TAG, "Error reading subtypes: " + e);
3381 return;
3382 } finally {
3383 if (fis != null) {
3384 try {
3385 fis.close();
3386 } catch (java.io.IOException e1) {
3387 Slog.w(TAG, "Failed to close.");
3388 }
3389 }
3390 }
3391 }
3392 }
3393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003394 @Override
3395 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3396 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3397 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003398
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003399 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3400 + Binder.getCallingPid()
3401 + ", uid=" + Binder.getCallingUid());
3402 return;
3403 }
3404
3405 IInputMethod method;
3406 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003408 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003409
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003410 synchronized (mMethodMap) {
3411 p.println("Current Input Method Manager state:");
3412 int N = mMethodList.size();
3413 p.println(" Input Methods:");
3414 for (int i=0; i<N; i++) {
3415 InputMethodInfo info = mMethodList.get(i);
3416 p.println(" InputMethod #" + i + ":");
3417 info.dump(p, " ");
3418 }
3419 p.println(" Clients:");
3420 for (ClientState ci : mClients.values()) {
3421 p.println(" Client " + ci + ":");
3422 p.println(" client=" + ci.client);
3423 p.println(" inputContext=" + ci.inputContext);
3424 p.println(" sessionRequested=" + ci.sessionRequested);
3425 p.println(" curSession=" + ci.curSession);
3426 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003427 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003428 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003429 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3430 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003431 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3432 + " mBoundToMethod=" + mBoundToMethod);
3433 p.println(" mCurToken=" + mCurToken);
3434 p.println(" mCurIntent=" + mCurIntent);
3435 method = mCurMethod;
3436 p.println(" mCurMethod=" + mCurMethod);
3437 p.println(" mEnabledSession=" + mEnabledSession);
3438 p.println(" mShowRequested=" + mShowRequested
3439 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3440 + " mShowForced=" + mShowForced
3441 + " mInputShown=" + mInputShown);
Dianne Hackbornd4e6d462014-05-16 16:32:37 -07003442 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003443 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003444
Jeff Brownb88102f2010-09-08 11:49:43 -07003445 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003446 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003447 pw.flush();
3448 try {
3449 client.client.asBinder().dump(fd, args);
3450 } catch (RemoteException e) {
3451 p.println("Input method client dead: " + e);
3452 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003453 } else {
3454 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003455 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003456
Jeff Brownb88102f2010-09-08 11:49:43 -07003457 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003458 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003459 pw.flush();
3460 try {
3461 method.asBinder().dump(fd, args);
3462 } catch (RemoteException e) {
3463 p.println("Input method service dead: " + e);
3464 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003465 } else {
3466 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003467 }
3468 }
3469}