blob: 753a14ac8514f73bc2b090055adb1f8eb5606e41 [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 Kataoka8e303cc2013-01-11 15:55:28 +090020import com.android.internal.inputmethod.InputMethodUtils;
21import com.android.internal.inputmethod.InputMethodUtils.InputMethodSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import com.android.internal.os.HandlerCaller;
Svetoslav Ganov758143e2012-08-06 16:40:27 -070023import com.android.internal.os.SomeArgs;
satoke7c6998e2011-06-03 17:57:59 +090024import com.android.internal.util.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import com.android.internal.view.IInputContext;
26import com.android.internal.view.IInputMethod;
Michael Wright52a53522013-03-14 10:59:38 -070027import com.android.internal.view.IInputSessionCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import com.android.internal.view.IInputMethodClient;
29import com.android.internal.view.IInputMethodManager;
30import com.android.internal.view.IInputMethodSession;
31import com.android.internal.view.InputBindResult;
Dianne Hackborna924dc0d2011-02-17 14:22:17 -080032import com.android.server.EventLogTags;
satok01038492012-04-09 21:08:27 +090033import com.android.server.wm.WindowManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034
satoke7c6998e2011-06-03 17:57:59 +090035import org.xmlpull.v1.XmlPullParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import org.xmlpull.v1.XmlPullParserException;
satoke7c6998e2011-06-03 17:57:59 +090037import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038
39import android.app.ActivityManagerNative;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090040import android.app.AppGlobals;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.app.AlertDialog;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090042import android.app.IUserSwitchObserver;
satokf90a33e2011-07-19 11:55:52 +090043import android.app.KeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +090044import android.app.Notification;
45import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070046import android.app.PendingIntent;
satok5b927c432012-05-01 20:09:34 +090047import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.content.ComponentName;
49import android.content.ContentResolver;
50import android.content.Context;
51import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.content.DialogInterface.OnCancelListener;
53import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090054import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070056import android.content.pm.ApplicationInfo;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090057import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058import android.content.pm.PackageManager;
59import android.content.pm.ResolveInfo;
60import android.content.pm.ServiceInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070061import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062import android.content.res.Resources;
63import android.content.res.TypedArray;
64import android.database.ContentObserver;
Joe Onorato857fd9b2011-01-27 15:08:35 -080065import android.inputmethodservice.InputMethodService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066import android.os.Binder;
satoke7c6998e2011-06-03 17:57:59 +090067import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.os.Handler;
69import android.os.IBinder;
70import android.os.IInterface;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090071import android.os.IRemoteCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072import android.os.Message;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090073import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074import android.os.Parcel;
75import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080076import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import android.os.ServiceManager;
78import android.os.SystemClock;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090079import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080import android.provider.Settings;
81import android.text.TextUtils;
satokf9f01002011-05-19 21:31:50 +090082import android.text.style.SuggestionSpan;
Dianne Hackborn39606a02012-07-31 17:54:35 -070083import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +090085import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +090086import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087import android.util.PrintWriterPrinter;
88import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +090089import android.util.Slog;
90import android.util.Xml;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091import android.view.IWindowManager;
Jeff Brownc28867a2013-03-26 15:42:39 -070092import android.view.InputChannel;
Ken Wakasa05dbb652011-08-22 15:22:43 +090093import android.view.LayoutInflater;
94import android.view.View;
95import android.view.ViewGroup;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096import android.view.WindowManager;
satokab751aa2010-09-14 19:17:36 +090097import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098import android.view.inputmethod.InputBinding;
99import android.view.inputmethod.InputMethod;
100import android.view.inputmethod.InputMethodInfo;
101import android.view.inputmethod.InputMethodManager;
satokab751aa2010-09-14 19:17:36 +0900102import android.view.inputmethod.InputMethodSubtype;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900103import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +0900104import android.widget.CompoundButton;
105import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900106import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +0900107import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900108import android.widget.TextView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109
satoke7c6998e2011-06-03 17:57:59 +0900110import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900112import java.io.FileInputStream;
113import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114import java.io.IOException;
115import java.io.PrintWriter;
116import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900117import java.util.Collections;
Ken Wakasa761eb372011-03-04 19:06:18 +0900118import java.util.Comparator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119import java.util.HashMap;
satok7f35c8c2010-10-07 21:13:11 +0900120import java.util.HashSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121import java.util.List;
satok5b927c432012-05-01 20:09:34 +0900122import java.util.Locale;
satok913a8922010-08-26 21:53:41 +0900123import java.util.TreeMap;
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;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800151
satok01038492012-04-09 21:08:27 +0900152 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 static final long TIME_TO_RECONNECT = 10*1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800155
satokf9f01002011-05-19 21:31:50 +0900156 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
157
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900158 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
satokb6359412011-06-28 17:47:41 +0900159 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900160
satok4e4569d2010-11-19 18:45:53 +0900161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800163 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900165 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 final IWindowManager mIWindowManager;
168 final HandlerCaller mCaller;
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700169 final boolean mHasFeature;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900170 private InputMethodFileManager mFileManager;
171 private InputMethodAndSubtypeListManager mImListManager;
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 Kataokad7443c82013-10-15 17:45:43 +0900183 private final InputMethodSubtypeSwitchingController mSwitchingController =
184 new InputMethodSubtypeSwitchingController();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800185
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700186 // Used to bring IME service up to visible adjustment while it is being shown.
187 final ServiceConnection mVisibleConnection = new ServiceConnection() {
188 @Override public void onServiceConnected(ComponentName name, IBinder service) {
189 }
190
191 @Override public void onServiceDisconnected(ComponentName name) {
192 }
193 };
194 boolean mVisibleBound = false;
195
satok7cfc0ed2011-06-20 21:29:36 +0900196 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700197 private NotificationManager mNotificationManager;
198 private KeyguardManager mKeyguardManager;
199 private StatusBarManagerService mStatusBar;
200 private Notification mImeSwitcherNotification;
201 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900202 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900203 private boolean mNotificationShown;
satok0a1bcf42012-05-16 19:26:31 +0900204 private final boolean mImeSelectedOnBoot;
satok7cfc0ed2011-06-20 21:29:36 +0900205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 class SessionState {
207 final ClientState client;
208 final IInputMethod method;
Jeff Brownc28867a2013-03-26 15:42:39 -0700209
210 IInputMethodSession session;
211 InputChannel channel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 @Override
214 public String toString() {
215 return "SessionState{uid " + client.uid + " pid " + client.pid
216 + " method " + Integer.toHexString(
217 System.identityHashCode(method))
218 + " session " + Integer.toHexString(
219 System.identityHashCode(session))
Jeff Brownc28867a2013-03-26 15:42:39 -0700220 + " channel " + channel
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 + "}";
222 }
223
224 SessionState(ClientState _client, IInputMethod _method,
Jeff Brownc28867a2013-03-26 15:42:39 -0700225 IInputMethodSession _session, InputChannel _channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 client = _client;
227 method = _method;
228 session = _session;
Jeff Brownc28867a2013-03-26 15:42:39 -0700229 channel = _channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 }
231 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800232
Jeff Brownc28867a2013-03-26 15:42:39 -0700233 static final class ClientState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 final IInputMethodClient client;
235 final IInputContext inputContext;
236 final int uid;
237 final int pid;
238 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800239
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 boolean sessionRequested;
241 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800242
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 @Override
244 public String toString() {
245 return "ClientState{" + Integer.toHexString(
246 System.identityHashCode(this)) + " uid " + uid
247 + " pid " + pid + "}";
248 }
249
250 ClientState(IInputMethodClient _client, IInputContext _inputContext,
251 int _uid, int _pid) {
252 client = _client;
253 inputContext = _inputContext;
254 uid = _uid;
255 pid = _pid;
256 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
257 }
258 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 final HashMap<IBinder, ClientState> mClients
261 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700264 * Set once the system is ready to run third party code.
265 */
266 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800267
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700268 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 * Id of the currently selected input method.
270 */
271 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 /**
274 * The current binding sequence number, incremented every time there is
275 * a new bind performed.
276 */
277 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 /**
280 * The client that is currently bound to an input method.
281 */
282 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700285 * The last window token that gained focus.
286 */
287 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800288
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700289 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 * The input context last provided by the current client.
291 */
292 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800293
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 /**
295 * The attributes last provided by the current client.
296 */
297 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 /**
300 * The input method ID of the input method service that we are currently
301 * connected to or in the process of connecting to.
302 */
303 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800304
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 /**
satokab751aa2010-09-14 19:17:36 +0900306 * The current subtype of the current input method.
307 */
308 private InputMethodSubtype mCurrentSubtype;
309
satok4e4569d2010-11-19 18:45:53 +0900310 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900311 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
312 mShortcutInputMethodsAndSubtypes =
313 new HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>();
satokab751aa2010-09-14 19:17:36 +0900314
315 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 * Set to true if our ServiceConnection is currently actively bound to
317 * a service (whether or not we have gotten its IBinder back yet).
318 */
319 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321 /**
322 * Set if the client has asked for the input method to be shown.
323 */
324 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 /**
327 * Set if we were explicitly told to show the input method.
328 */
329 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 /**
332 * Set if we were forced to be shown.
333 */
334 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800335
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 /**
337 * Set if we last told the input method to show itself.
338 */
339 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 /**
342 * The Intent used to connect to the current input method.
343 */
344 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 /**
347 * The token we have made for the currently active input method, to
348 * identify it in the future.
349 */
350 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800351
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 /**
353 * If non-null, this is the input method service we are currently connected
354 * to.
355 */
356 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800357
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 /**
359 * Time that we last initiated a bind to the input method, to determine
360 * if we should try to disconnect and reconnect to it.
361 */
362 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 /**
365 * Have we called mCurMethod.bindInput()?
366 */
367 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800368
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369 /**
370 * Currently enabled session. Only touched by service thread, not
371 * protected by a lock.
372 */
373 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 /**
376 * True if the screen is on. The value is true initially.
377 */
378 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800379
Joe Onorato857fd9b2011-01-27 15:08:35 -0800380 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
381 int mImeWindowVis;
382
Ken Wakasa05dbb652011-08-22 15:22:43 +0900383 private AlertDialog.Builder mDialogBuilder;
384 private AlertDialog mSwitchingDialog;
satok01038492012-04-09 21:08:27 +0900385 private View mSwitchingDialogTitleView;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900386 private InputMethodInfo[] mIms;
387 private int[] mSubtypeIds;
satok5b927c432012-05-01 20:09:34 +0900388 private Locale mLastSystemLocale;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900389 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
390 private final IPackageManager mIPackageManager;
John Spurlockbc7b6fc2012-11-14 08:51:07 -0500391 private boolean mInputBoundToKeyguard;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800392
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 class SettingsObserver extends ContentObserver {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800394 String mLastEnabled = "";
395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 SettingsObserver(Handler handler) {
397 super(handler);
398 ContentResolver resolver = mContext.getContentResolver();
399 resolver.registerContentObserver(Settings.Secure.getUriFor(
400 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
satokab751aa2010-09-14 19:17:36 +0900401 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokb6109bb2011-02-03 22:24:54 +0900402 Settings.Secure.ENABLED_INPUT_METHODS), false, this);
403 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokab751aa2010-09-14 19:17:36 +0900404 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407 @Override public void onChange(boolean selfChange) {
408 synchronized (mMethodMap) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800409 boolean enabledChanged = false;
410 String newEnabled = mSettings.getEnabledInputMethodsStr();
411 if (!mLastEnabled.equals(newEnabled)) {
412 mLastEnabled = newEnabled;
413 enabledChanged = true;
414 }
415 updateFromSettingsLocked(enabledChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 }
417 }
418 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800419
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900420 class ImmsBroadcastReceiver extends android.content.BroadcastReceiver {
421 private void updateActive() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 // Inform the current client of the change in active status
Dianne Hackborna6e41342012-05-22 16:30:34 -0700423 if (mCurClient != null && mCurClient.client != null) {
424 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
425 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426 }
427 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900428
429 @Override
430 public void onReceive(Context context, Intent intent) {
431 final String action = intent.getAction();
432 if (Intent.ACTION_SCREEN_ON.equals(action)) {
433 mScreenOn = true;
434 refreshImeWindowVisibilityLocked();
435 updateActive();
436 return;
437 } else if (Intent.ACTION_SCREEN_OFF.equals(action)) {
438 mScreenOn = false;
439 setImeWindowVisibilityStatusHiddenLocked();
440 updateActive();
441 return;
442 } else if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
443 hideInputMethodMenu();
444 // No need to updateActive
445 return;
446 } else {
447 Slog.w(TAG, "Unexpected intent " + intent);
448 }
449 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800451
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800452 class MyPackageMonitor extends PackageMonitor {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900453 private boolean isChangingPackagesOfCurrentUser() {
454 final int userId = getChangingUserId();
455 final boolean retval = userId == mSettings.getCurrentUserId();
456 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900457 if (!retval) {
458 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
459 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900460 }
461 return retval;
462 }
463
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800465 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900466 if (!isChangingPackagesOfCurrentUser()) {
467 return false;
468 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900470 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471 final int N = mMethodList.size();
472 if (curInputMethodId != null) {
473 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800474 InputMethodInfo imi = mMethodList.get(i);
475 if (imi.getId().equals(curInputMethodId)) {
476 for (String pkg : packages) {
477 if (imi.getPackageName().equals(pkg)) {
478 if (!doit) {
479 return true;
480 }
satok723a27e2010-11-11 14:58:11 +0900481 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800482 chooseNewDefaultIMELocked();
483 return true;
484 }
485 }
486 }
487 }
488 }
489 }
490 return false;
491 }
492
493 @Override
494 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900495 if (!isChangingPackagesOfCurrentUser()) {
496 return;
497 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800498 synchronized (mMethodMap) {
499 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900500 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800501 final int N = mMethodList.size();
502 if (curInputMethodId != null) {
503 for (int i=0; i<N; i++) {
504 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900505 final String imiId = imi.getId();
506 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800507 curIm = imi;
508 }
satoke7c6998e2011-06-03 17:57:59 +0900509
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800510 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900511 if (isPackageModified(imi.getPackageName())) {
512 mFileManager.deleteAllInputMethodSubtypes(imiId);
513 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800514 if (change == PACKAGE_TEMPORARY_CHANGE
515 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800516 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800517 + imi.getComponent());
518 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519 }
520 }
521 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800522
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900523 buildInputMethodListLocked(
524 mMethodList, mMethodMap, false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800525
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800527
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800528 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800529 int change = isPackageDisappearing(curIm.getPackageName());
530 if (change == PACKAGE_TEMPORARY_CHANGE
531 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800532 ServiceInfo si = null;
533 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900534 si = mIPackageManager.getServiceInfo(
535 curIm.getComponent(), 0, mSettings.getCurrentUserId());
536 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800537 }
538 if (si == null) {
539 // Uh oh, current input method is no longer around!
540 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800541 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
satok15452a42011-10-28 17:58:28 +0900542 setImeWindowVisibilityStatusHiddenLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800543 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800544 changed = true;
545 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800546 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900547 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800548 }
549 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800550 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800551 }
satokab751aa2010-09-14 19:17:36 +0900552
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800553 if (curIm == null) {
554 // We currently don't have a default input method... is
555 // one now available?
556 changed = chooseNewDefaultIMELocked();
557 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800558
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800559 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800560 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561 }
562 }
563 }
564 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800565
Jeff Brownc28867a2013-03-26 15:42:39 -0700566 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900567 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -0700568 private final IInputMethod mMethod;
569 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800570
Jeff Brownc28867a2013-03-26 15:42:39 -0700571 MethodCallback(InputMethodManagerService imms, IInputMethod method,
572 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900573 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -0700574 mMethod = method;
575 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800576 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800577
satoke7c6998e2011-06-03 17:57:59 +0900578 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -0700579 public void sessionCreated(IInputMethodSession session) {
580 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800581 }
582 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800583
satok01038492012-04-09 21:08:27 +0900584 private class HardKeyboardListener
585 implements WindowManagerService.OnHardKeyboardStatusChangeListener {
586 @Override
587 public void onHardKeyboardStatusChange(boolean available, boolean enabled) {
588 mHandler.sendMessage(mHandler.obtainMessage(
589 MSG_HARD_KEYBOARD_SWITCH_CHANGED, available ? 1 : 0, enabled ? 1 : 0));
590 }
591
592 public void handleHardKeyboardStatusChange(boolean available, boolean enabled) {
593 if (DEBUG) {
594 Slog.w(TAG, "HardKeyboardStatusChanged: available = " + available + ", enabled = "
595 + enabled);
596 }
597 synchronized(mMethodMap) {
598 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
599 && mSwitchingDialog.isShowing()) {
600 mSwitchingDialogTitleView.findViewById(
601 com.android.internal.R.id.hard_keyboard_section).setVisibility(
602 available ? View.VISIBLE : View.GONE);
603 }
604 }
605 }
606 }
607
608 public InputMethodManagerService(Context context, WindowManagerService windowManager) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900609 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800611 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 mHandler = new Handler(this);
613 mIWindowManager = IWindowManager.Stub.asInterface(
614 ServiceManager.getService(Context.WINDOW_SERVICE));
Mita Yuned218c72012-12-06 17:18:25 -0800615 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900616 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 public void executeMessage(Message msg) {
618 handleMessage(msg);
619 }
Mita Yuned218c72012-12-06 17:18:25 -0800620 }, true /*asyncHandler*/);
satok01038492012-04-09 21:08:27 +0900621 mWindowManagerService = windowManager;
622 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700623 mHasFeature = context.getPackageManager().hasSystemFeature(
624 PackageManager.FEATURE_INPUT_METHODS);
satok7cfc0ed2011-06-20 21:29:36 +0900625
satok7cfc0ed2011-06-20 21:29:36 +0900626 mImeSwitcherNotification = new Notification();
627 mImeSwitcherNotification.icon = com.android.internal.R.drawable.ic_notification_ime_default;
628 mImeSwitcherNotification.when = 0;
629 mImeSwitcherNotification.flags = Notification.FLAG_ONGOING_EVENT;
630 mImeSwitcherNotification.tickerText = null;
631 mImeSwitcherNotification.defaults = 0; // please be quiet
632 mImeSwitcherNotification.sound = null;
633 mImeSwitcherNotification.vibrate = null;
Daniel Sandler590d5152012-06-14 16:10:13 -0400634
635 // Tag this notification specially so SystemUI knows it's important
636 mImeSwitcherNotification.kind = new String[] { "android.system.imeswitcher" };
637
satok7cfc0ed2011-06-20 21:29:36 +0900638 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900639 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900640
641 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900642
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900643 final IntentFilter broadcastFilter = new IntentFilter();
644 broadcastFilter.addAction(Intent.ACTION_SCREEN_ON);
645 broadcastFilter.addAction(Intent.ACTION_SCREEN_OFF);
646 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
647 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800648
satok7cfc0ed2011-06-20 21:29:36 +0900649 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900650 int userId = 0;
651 try {
652 ActivityManagerNative.getDefault().registerUserSwitchObserver(
653 new IUserSwitchObserver.Stub() {
654 @Override
655 public void onUserSwitching(int newUserId, IRemoteCallback reply) {
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900656 synchronized(mMethodMap) {
657 switchUserLocked(newUserId);
658 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900659 if (reply != null) {
660 try {
661 reply.sendResult(null);
662 } catch (RemoteException e) {
663 }
664 }
665 }
666
667 @Override
668 public void onUserSwitchComplete(int newUserId) throws RemoteException {
669 }
670 });
671 userId = ActivityManagerNative.getDefault().getCurrentUser().id;
672 } catch (RemoteException e) {
673 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
674 }
satok81f8b7c2012-12-04 20:42:56 +0900675 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
satok913a8922010-08-26 21:53:41 +0900676
satokd87c2592010-09-29 11:52:06 +0900677 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900678 mSettings = new InputMethodSettings(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900679 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId);
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900680 mFileManager = new InputMethodFileManager(mMethodMap, userId);
681 mImListManager = new InputMethodAndSubtypeListManager(context, this);
satok0a1bcf42012-05-16 19:26:31 +0900682
683 // Just checking if defaultImiId is empty or not
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900684 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900685 if (DEBUG) {
686 Slog.d(TAG, "Initial default ime = " + defaultImiId);
687 }
satok0a1bcf42012-05-16 19:26:31 +0900688 mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
689
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900690 buildInputMethodListLocked(mMethodList, mMethodMap,
691 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satokd87c2592010-09-29 11:52:06 +0900692 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693
satok0a1bcf42012-05-16 19:26:31 +0900694 if (!mImeSelectedOnBoot) {
695 Slog.w(TAG, "No IME selected. Choose the most applicable IME.");
satok5b927c432012-05-01 20:09:34 +0900696 resetDefaultImeLocked(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800697 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800698
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800699 mSettingsObserver = new SettingsObserver(mHandler);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800700 updateFromSettingsLocked(true);
satok5b927c432012-05-01 20:09:34 +0900701
702 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME
703 // according to the new system locale.
704 final IntentFilter filter = new IntentFilter();
705 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
706 mContext.registerReceiver(
707 new BroadcastReceiver() {
708 @Override
709 public void onReceive(Context context, Intent intent) {
710 synchronized(mMethodMap) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900711 resetStateIfCurrentLocaleChangedLocked();
satok5b927c432012-05-01 20:09:34 +0900712 }
713 }
714 }, filter);
715 }
716
satok5b927c432012-05-01 20:09:34 +0900717 private void resetDefaultImeLocked(Context context) {
718 // Do not reset the default (current) IME when it is a 3rd-party IME
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900719 if (mCurMethodId != null
720 && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +0900721 return;
722 }
723
724 InputMethodInfo defIm = null;
725 for (InputMethodInfo imi : mMethodList) {
726 if (defIm == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900727 if (InputMethodUtils.isValidSystemDefaultIme(
728 mSystemReady, imi, context)) {
satok5b927c432012-05-01 20:09:34 +0900729 defIm = imi;
730 Slog.i(TAG, "Selected default: " + imi.getId());
731 }
732 }
733 }
734 if (defIm == null && mMethodList.size() > 0) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900735 defIm = InputMethodUtils.getMostApplicableDefaultIME(
736 mSettings.getEnabledInputMethodListLocked());
satok5b927c432012-05-01 20:09:34 +0900737 Slog.i(TAG, "No default found, using " + defIm.getId());
738 }
739 if (defIm != null) {
740 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
741 }
742 }
743
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900744 private void resetAllInternalStateLocked(final boolean updateOnlyWhenLocaleChanged,
745 final boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900746 if (!mSystemReady) {
747 // not system ready
748 return;
749 }
750 final Locale newLocale = mRes.getConfiguration().locale;
751 if (!updateOnlyWhenLocaleChanged
752 || (newLocale != null && !newLocale.equals(mLastSystemLocale))) {
753 if (!updateOnlyWhenLocaleChanged) {
754 hideCurrentInputLocked(0, null);
755 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -0700756 unbindCurrentMethodLocked(true, false);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900757 }
758 if (DEBUG) {
759 Slog.i(TAG, "Locale has been changed to " + newLocale);
760 }
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900761 // InputMethodAndSubtypeListManager should be reset when the locale is changed.
762 mImListManager = new InputMethodAndSubtypeListManager(mContext, this);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900763 buildInputMethodListLocked(mMethodList, mMethodMap, resetDefaultEnabledIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900764 if (!updateOnlyWhenLocaleChanged) {
765 final String selectedImiId = mSettings.getSelectedInputMethod();
766 if (TextUtils.isEmpty(selectedImiId)) {
767 // This is the first time of the user switch and
768 // set the current ime to the proper one.
769 resetDefaultImeLocked(mContext);
770 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +0900771 } else {
772 // If the locale is changed, needs to reset the default ime
773 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900774 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800775 updateFromSettingsLocked(true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900776 mLastSystemLocale = newLocale;
777 if (!updateOnlyWhenLocaleChanged) {
778 try {
779 startInputInnerLocked();
780 } catch (RuntimeException e) {
781 Slog.w(TAG, "Unexpected exception", e);
782 }
783 }
784 }
785 }
786
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900787 private void resetStateIfCurrentLocaleChangedLocked() {
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900788 resetAllInternalStateLocked(true /* updateOnlyWhenLocaleChanged */,
789 true /* resetDefaultImeLocked */);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900790 }
791
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900792 private void switchUserLocked(int newUserId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900793 mSettings.setCurrentUserId(newUserId);
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900794 // InputMethodFileManager should be reset when the user is changed
795 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900796 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +0900797 // For secondary users, the list of enabled IMEs may not have been updated since the
798 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
799 // not be empty even if the IME has been uninstalled by the primary user.
800 // Even in such cases, IMMS works fine because it will find the most applicable
801 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900802 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900803 if (DEBUG) {
804 Slog.d(TAG, "Switch user: " + newUserId + " current ime = " + defaultImiId);
805 }
806 resetAllInternalStateLocked(false /* updateOnlyWhenLocaleChanged */,
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900807 initialUserSwitch /* needsToResetDefaultIme */);
808 if (initialUserSwitch) {
809 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mContext.getPackageManager(),
810 mSettings.getEnabledInputMethodListLocked());
811 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900812 }
813
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800814 @Override
815 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
816 throws RemoteException {
817 try {
818 return super.onTransact(code, data, reply, flags);
819 } catch (RuntimeException e) {
820 // The input method manager only throws security exceptions, so let's
821 // log all others.
822 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -0700823 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800824 }
825 throw e;
826 }
827 }
828
Svetoslav Ganova0027152013-06-25 14:59:53 -0700829 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700830 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900831 if (DEBUG) {
832 Slog.d(TAG, "--- systemReady");
833 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700834 if (!mSystemReady) {
835 mSystemReady = true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900836 mKeyguardManager =
837 (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700838 mNotificationManager = (NotificationManager)
839 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
840 mStatusBar = statusBar;
841 statusBar.setIconVisibility("ime", false);
842 updateImeWindowStatusLocked();
satokb858c732011-07-22 19:54:34 +0900843 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
844 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +0900845 if (mShowOngoingImeSwitcherForPhones) {
846 mWindowManagerService.setOnHardKeyboardStatusChangeListener(
847 mHardKeyboardListener);
848 }
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900849 buildInputMethodListLocked(mMethodList, mMethodMap,
850 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satok0a1bcf42012-05-16 19:26:31 +0900851 if (!mImeSelectedOnBoot) {
852 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900853 resetStateIfCurrentLocaleChangedLocked();
854 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(
855 mContext.getPackageManager(),
856 mSettings.getEnabledInputMethodListLocked());
satok0a1bcf42012-05-16 19:26:31 +0900857 }
858 mLastSystemLocale = mRes.getConfiguration().locale;
Dianne Hackborncc278702009-09-02 23:07:23 -0700859 try {
860 startInputInnerLocked();
861 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800862 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700863 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700864 }
865 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800866 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800867
satok15452a42011-10-28 17:58:28 +0900868 private void setImeWindowVisibilityStatusHiddenLocked() {
869 mImeWindowVis = 0;
870 updateImeWindowStatusLocked();
871 }
872
satok3afd6c02011-11-18 08:38:19 +0900873 private void refreshImeWindowVisibilityLocked() {
874 final Configuration conf = mRes.getConfiguration();
875 final boolean haveHardKeyboard = conf.keyboard
876 != Configuration.KEYBOARD_NOKEYS;
877 final boolean hardKeyShown = haveHardKeyboard
878 && conf.hardKeyboardHidden
879 != Configuration.HARDKEYBOARDHIDDEN_YES;
John Spurlockbc7b6fc2012-11-14 08:51:07 -0500880 final boolean isScreenLocked =
881 mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
882 final boolean isScreenSecurelyLocked =
883 isScreenLocked && mKeyguardManager.isKeyguardSecure();
884 final boolean inputShown = mInputShown && (!isScreenLocked || mInputBoundToKeyguard);
885 mImeWindowVis = (!isScreenSecurelyLocked && (inputShown || hardKeyShown)) ?
satok3afd6c02011-11-18 08:38:19 +0900886 (InputMethodService.IME_ACTIVE | InputMethodService.IME_VISIBLE) : 0;
887 updateImeWindowStatusLocked();
888 }
889
satok15452a42011-10-28 17:58:28 +0900890 private void updateImeWindowStatusLocked() {
satokdbf29502011-08-25 15:28:23 +0900891 setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700892 }
893
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900894 // ---------------------------------------------------------------------------------------
895 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
896 // 1) it comes from the system process
897 // 2) the calling process' user id is identical to the current user id IMMS thinks.
898 private boolean calledFromValidUser() {
899 final int uid = Binder.getCallingUid();
900 final int userId = UserHandle.getUserId(uid);
901 if (DEBUG) {
902 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
903 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
904 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +0900905 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
906 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900907 }
908 if (uid == Process.SYSTEM_UID || userId == mSettings.getCurrentUserId()) {
909 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900910 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +0900911
912 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
913 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
914 // must not manage background users' states in any functions.
915 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
916 // by a token.
917 if (mContext.checkCallingOrSelfPermission(
918 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
919 == PackageManager.PERMISSION_GRANTED) {
920 if (DEBUG) {
921 Slog.d(TAG, "--- Access granted because the calling process has "
922 + "the INTERACT_ACROSS_USERS_FULL permission");
923 }
924 return true;
925 }
Satoshi Kataoka0766eb02013-07-31 18:30:13 +0900926 Slog.w(TAG, "--- IPC called from background users. Ignore. \n"
927 + InputMethodUtils.getStackTrace());
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +0900928 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900929 }
930
931 private boolean bindCurrentInputMethodService(
932 Intent service, ServiceConnection conn, int flags) {
933 if (service == null || conn == null) {
934 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
935 return false;
936 }
Amith Yamasani27b89e62013-01-16 12:30:11 -0800937 return mContext.bindServiceAsUser(service, conn, flags,
938 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900939 }
940
satoke7c6998e2011-06-03 17:57:59 +0900941 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800942 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900943 // TODO: Make this work even for non-current users?
944 if (!calledFromValidUser()) {
945 return Collections.emptyList();
946 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800947 synchronized (mMethodMap) {
948 return new ArrayList<InputMethodInfo>(mMethodList);
949 }
950 }
951
satoke7c6998e2011-06-03 17:57:59 +0900952 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900954 // TODO: Make this work even for non-current users?
955 if (!calledFromValidUser()) {
956 return Collections.emptyList();
957 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800958 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900959 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800960 }
961 }
962
satokbb4aa062011-01-19 21:40:27 +0900963 private HashMap<InputMethodInfo, List<InputMethodSubtype>>
964 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked() {
965 HashMap<InputMethodInfo, List<InputMethodSubtype>> enabledInputMethodAndSubtypes =
966 new HashMap<InputMethodInfo, List<InputMethodSubtype>>();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900967 for (InputMethodInfo imi: mSettings.getEnabledInputMethodListLocked()) {
satokbb4aa062011-01-19 21:40:27 +0900968 enabledInputMethodAndSubtypes.put(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900969 imi, mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true));
satokbb4aa062011-01-19 21:40:27 +0900970 }
971 return enabledInputMethodAndSubtypes;
972 }
973
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900974 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +0900975 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900976 * @return enabled subtypes of the specified imi
977 */
satoke7c6998e2011-06-03 17:57:59 +0900978 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +0900979 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +0900980 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900981 // TODO: Make this work even for non-current users?
982 if (!calledFromValidUser()) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +0900983 return Collections.<InputMethodSubtype>emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900984 }
satok67ddf9c2010-11-17 09:45:54 +0900985 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +0900986 final InputMethodInfo imi;
987 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900988 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +0900989 } else {
990 imi = mMethodMap.get(imiId);
991 }
992 if (imi == null) {
993 return Collections.<InputMethodSubtype>emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900994 }
995 return mSettings.getEnabledInputMethodSubtypeListLocked(
996 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +0900997 }
998 }
999
satoke7c6998e2011-06-03 17:57:59 +09001000 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001 public void addClient(IInputMethodClient client,
1002 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001003 if (!calledFromValidUser()) {
1004 return;
1005 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001006 synchronized (mMethodMap) {
1007 mClients.put(client.asBinder(), new ClientState(client,
1008 inputContext, uid, pid));
1009 }
1010 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001011
satoke7c6998e2011-06-03 17:57:59 +09001012 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001013 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001014 if (!calledFromValidUser()) {
1015 return;
1016 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001017 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001018 ClientState cs = mClients.remove(client.asBinder());
1019 if (cs != null) {
1020 clearClientSessionLocked(cs);
1021 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001022 }
1023 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001024
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 void executeOrSendMessage(IInterface target, Message msg) {
1026 if (target.asBinder() instanceof Binder) {
1027 mCaller.sendMessage(msg);
1028 } else {
1029 handleMessage(msg);
1030 msg.recycle();
1031 }
1032 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001033
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001034 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001036 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 + mCurClient.client.asBinder());
1038 if (mBoundToMethod) {
1039 mBoundToMethod = false;
1040 if (mCurMethod != null) {
1041 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1042 MSG_UNBIND_INPUT, mCurMethod));
1043 }
1044 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001045
1046 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1047 MSG_SET_ACTIVE, 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001048 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1049 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1050 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001051 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001052
The Android Open Source Project10592532009-03-18 17:39:46 -07001053 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001054 }
1055 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001056
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001057 private int getImeShowFlags() {
1058 int flags = 0;
1059 if (mShowForced) {
1060 flags |= InputMethod.SHOW_FORCED
1061 | InputMethod.SHOW_EXPLICIT;
1062 } else if (mShowExplicitlyRequested) {
1063 flags |= InputMethod.SHOW_EXPLICIT;
1064 }
1065 return flags;
1066 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001067
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001068 private int getAppShowFlags() {
1069 int flags = 0;
1070 if (mShowForced) {
1071 flags |= InputMethodManager.SHOW_FORCED;
1072 } else if (!mShowExplicitlyRequested) {
1073 flags |= InputMethodManager.SHOW_IMPLICIT;
1074 }
1075 return flags;
1076 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001077
Dianne Hackborn7663d802012-02-24 13:08:49 -08001078 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001079 if (!mBoundToMethod) {
1080 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1081 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1082 mBoundToMethod = true;
1083 }
1084 final SessionState session = mCurClient.curSession;
1085 if (initial) {
1086 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1087 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
1088 } else {
1089 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1090 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
1091 }
1092 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001093 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001094 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001096 return new InputBindResult(session.session,
1097 session.channel != null ? session.channel.dup() : null, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001098 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001099
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001100 InputBindResult startInputLocked(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001101 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001102 // If no method is currently selected, do nothing.
1103 if (mCurMethodId == null) {
1104 return mNoBinding;
1105 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001107 ClientState cs = mClients.get(client.asBinder());
1108 if (cs == null) {
1109 throw new IllegalArgumentException("unknown client "
1110 + client.asBinder());
1111 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001113 try {
1114 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1115 // Check with the window manager to make sure this client actually
1116 // has a window with focus. If not, reject. This is thread safe
1117 // because if the focus changes some time before or after, the
1118 // next client receiving focus that has any interest in input will
1119 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001120 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001121 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1122 return null;
1123 }
1124 } catch (RemoteException e) {
1125 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001126
Dianne Hackborn7663d802012-02-24 13:08:49 -08001127 return startInputUncheckedLocked(cs, inputContext, attribute, controlFlags);
1128 }
1129
1130 InputBindResult startInputUncheckedLocked(ClientState cs,
1131 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
1132 // If no method is currently selected, do nothing.
1133 if (mCurMethodId == null) {
1134 return mNoBinding;
1135 }
1136
John Spurlockbc7b6fc2012-11-14 08:51:07 -05001137 if (mCurClient == null) {
1138 mInputBoundToKeyguard = mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1139 if (DEBUG) {
1140 Slog.v(TAG, "New bind. keyguard = " + mInputBoundToKeyguard);
1141 }
1142 }
1143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 if (mCurClient != cs) {
1145 // If the client is changing, we need to switch over to the new
1146 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001147 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001148 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001149 + cs.client.asBinder());
1150
1151 // If the screen is on, inform the new client it is active
1152 if (mScreenOn) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001153 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
1154 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001155 }
1156 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001158 // Bump up the sequence for this client and attach it.
1159 mCurSeq++;
1160 if (mCurSeq <= 0) mCurSeq = 1;
1161 mCurClient = cs;
1162 mCurInputContext = inputContext;
1163 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001165 // Check if the input method is changing.
1166 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1167 if (cs.curSession != null) {
1168 // Fast case: if we are already connected to the input method,
1169 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001170 return attachNewInputLocked(
1171 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001172 }
1173 if (mHaveConnection) {
1174 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001175 // Return to client, and we will get back with it when
1176 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001177 requestClientSessionLocked(cs);
1178 return new InputBindResult(null, null, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001179 } else if (SystemClock.uptimeMillis()
1180 < (mLastBindTime+TIME_TO_RECONNECT)) {
1181 // In this case we have connected to the service, but
1182 // don't yet have its interface. If it hasn't been too
1183 // long since we did the connection, we'll return to
1184 // the client and wait to get the service interface so
1185 // we can report back. If it has been too long, we want
1186 // to fall through so we can try a disconnect/reconnect
1187 // to see if we can get back in touch with the service.
Jeff Brownc28867a2013-03-26 15:42:39 -07001188 return new InputBindResult(null, null, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001190 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1191 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 }
1193 }
1194 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001195
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001196 return startInputInnerLocked();
1197 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001198
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001199 InputBindResult startInputInnerLocked() {
1200 if (mCurMethodId == null) {
1201 return mNoBinding;
1202 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001203
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001204 if (!mSystemReady) {
1205 // If the system is not yet ready, we shouldn't be running third
1206 // party code.
Jeff Brownc28867a2013-03-26 15:42:39 -07001207 return new InputBindResult(null, null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001208 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1211 if (info == null) {
1212 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1213 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001214
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001215 unbindCurrentMethodLocked(false, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001217 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1218 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001219 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1220 com.android.internal.R.string.input_method_binding_label);
1221 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1222 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001223 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornc8230512013-07-13 21:32:12 -07001224 | Context.BIND_NOT_VISIBLE | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001225 mLastBindTime = SystemClock.uptimeMillis();
1226 mHaveConnection = true;
1227 mCurId = info.getId();
1228 mCurToken = new Binder();
1229 try {
Craig Mautnerca0ac712013-03-14 09:43:02 -07001230 if (true || DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001231 mIWindowManager.addWindowToken(mCurToken,
1232 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1233 } catch (RemoteException e) {
1234 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001235 return new InputBindResult(null, null, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001236 } else {
1237 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001238 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001239 + mCurIntent);
1240 }
1241 return null;
1242 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001243
satoke7c6998e2011-06-03 17:57:59 +09001244 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 public InputBindResult startInput(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001246 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001247 if (!calledFromValidUser()) {
1248 return null;
1249 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001250 synchronized (mMethodMap) {
1251 final long ident = Binder.clearCallingIdentity();
1252 try {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001253 return startInputLocked(client, inputContext, attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001254 } finally {
1255 Binder.restoreCallingIdentity(ident);
1256 }
1257 }
1258 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001259
satoke7c6998e2011-06-03 17:57:59 +09001260 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001261 public void finishInput(IInputMethodClient client) {
1262 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001263
satoke7c6998e2011-06-03 17:57:59 +09001264 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001265 public void onServiceConnected(ComponentName name, IBinder service) {
1266 synchronized (mMethodMap) {
1267 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1268 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001269 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001270 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001271 unbindCurrentMethodLocked(false, false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001272 return;
1273 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001274 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001275 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1276 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001278 clearClientSessionLocked(mCurClient);
1279 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001280 }
1281 }
1282 }
1283 }
1284
Jeff Brownc28867a2013-03-26 15:42:39 -07001285 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1286 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001287 synchronized (mMethodMap) {
1288 if (mCurMethod != null && method != null
1289 && mCurMethod.asBinder() == method.asBinder()) {
1290 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001291 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001293 method, session, channel);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001294 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001295 if (res.method != null) {
1296 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
1297 MSG_BIND_METHOD, mCurClient.client, res));
1298 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001299 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300 }
1301 }
1302 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001303
1304 // Session abandoned. Close its associated input channel.
1305 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001306 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001307
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001308 void unbindCurrentMethodLocked(boolean reportToClient, boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001309 if (mVisibleBound) {
1310 mContext.unbindService(mVisibleConnection);
1311 mVisibleBound = false;
1312 }
1313
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001314 if (mHaveConnection) {
1315 mContext.unbindService(this);
1316 mHaveConnection = false;
1317 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001318
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001319 if (mCurToken != null) {
1320 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001321 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001322 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001323 // The current IME is shown. Hence an IME switch (transition) is happening.
1324 mWindowManagerService.saveLastInputMethodWindowForTransition();
1325 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001326 mIWindowManager.removeWindowToken(mCurToken);
1327 } catch (RemoteException e) {
1328 }
1329 mCurToken = null;
1330 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001331
The Android Open Source Project10592532009-03-18 17:39:46 -07001332 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001333 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001334
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001335 if (reportToClient && mCurClient != null) {
1336 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1337 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1338 }
1339 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001340
1341 void requestClientSessionLocked(ClientState cs) {
1342 if (!cs.sessionRequested) {
1343 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1344 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1345 cs.sessionRequested = true;
1346 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1347 MSG_CREATE_SESSION, mCurMethod, channels[1],
1348 new MethodCallback(this, mCurMethod, channels[0])));
1349 }
1350 }
1351
1352 void clearClientSessionLocked(ClientState cs) {
1353 finishSessionLocked(cs.curSession);
1354 cs.curSession = null;
1355 cs.sessionRequested = false;
1356 }
1357
1358 private void finishSessionLocked(SessionState sessionState) {
1359 if (sessionState != null) {
1360 if (sessionState.session != null) {
1361 try {
1362 sessionState.session.finishSession();
1363 } catch (RemoteException e) {
1364 Slog.w(TAG, "Session failed to close due to remote exception", e);
1365 setImeWindowVisibilityStatusHiddenLocked();
1366 }
1367 sessionState.session = null;
1368 }
1369 if (sessionState.channel != null) {
1370 sessionState.channel.dispose();
1371 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001372 }
1373 }
1374 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001375
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001376 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001377 if (mCurMethod != null) {
1378 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001379 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001380 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001381
Jeff Brownc28867a2013-03-26 15:42:39 -07001382 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001383 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001384 mCurMethod = null;
1385 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001386 if (mStatusBar != null) {
1387 mStatusBar.setIconVisibility("ime", false);
1388 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001389 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001390
satoke7c6998e2011-06-03 17:57:59 +09001391 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001392 public void onServiceDisconnected(ComponentName name) {
1393 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001394 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001395 + " mCurIntent=" + mCurIntent);
1396 if (mCurMethod != null && mCurIntent != null
1397 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001398 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001399 // We consider this to be a new bind attempt, since the system
1400 // should now try to restart the service for us.
1401 mLastBindTime = SystemClock.uptimeMillis();
1402 mShowRequested = mInputShown;
1403 mInputShown = false;
1404 if (mCurClient != null) {
1405 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1406 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1407 }
1408 }
1409 }
1410 }
1411
satokf9f01002011-05-19 21:31:50 +09001412 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001413 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001414 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001415 long ident = Binder.clearCallingIdentity();
1416 try {
1417 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001418 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001419 return;
1420 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001421
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001422 synchronized (mMethodMap) {
1423 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001424 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001425 if (mStatusBar != null) {
1426 mStatusBar.setIconVisibility("ime", false);
1427 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001428 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001429 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001430 CharSequence contentDescription = null;
1431 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001432 // Use PackageManager to load label
1433 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001434 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001435 mIPackageManager.getApplicationInfo(packageName, 0,
1436 mSettings.getCurrentUserId()));
1437 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001438 /* ignore */
1439 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001440 if (mStatusBar != null) {
1441 mStatusBar.setIcon("ime", packageName, iconId, 0,
1442 contentDescription != null
1443 ? contentDescription.toString() : null);
1444 mStatusBar.setIconVisibility("ime", true);
1445 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001446 }
1447 }
1448 } finally {
1449 Binder.restoreCallingIdentity(ident);
1450 }
1451 }
1452
satok7cfc0ed2011-06-20 21:29:36 +09001453 private boolean needsToShowImeSwitchOngoingNotification() {
1454 if (!mShowOngoingImeSwitcherForPhones) return false;
satok2c93efc2012-04-02 19:33:47 +09001455 if (isScreenLocked()) return false;
satok7cfc0ed2011-06-20 21:29:36 +09001456 synchronized (mMethodMap) {
1457 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1458 final int N = imis.size();
satokb6359412011-06-28 17:47:41 +09001459 if (N > 2) return true;
1460 if (N < 1) return false;
1461 int nonAuxCount = 0;
1462 int auxCount = 0;
1463 InputMethodSubtype nonAuxSubtype = null;
1464 InputMethodSubtype auxSubtype = null;
satok7cfc0ed2011-06-20 21:29:36 +09001465 for(int i = 0; i < N; ++i) {
1466 final InputMethodInfo imi = imis.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001467 final List<InputMethodSubtype> subtypes =
1468 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satok7cfc0ed2011-06-20 21:29:36 +09001469 final int subtypeCount = subtypes.size();
1470 if (subtypeCount == 0) {
satokb6359412011-06-28 17:47:41 +09001471 ++nonAuxCount;
satok7cfc0ed2011-06-20 21:29:36 +09001472 } else {
1473 for (int j = 0; j < subtypeCount; ++j) {
satokb6359412011-06-28 17:47:41 +09001474 final InputMethodSubtype subtype = subtypes.get(j);
1475 if (!subtype.isAuxiliary()) {
1476 ++nonAuxCount;
1477 nonAuxSubtype = subtype;
1478 } else {
1479 ++auxCount;
1480 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001481 }
1482 }
1483 }
satok7cfc0ed2011-06-20 21:29:36 +09001484 }
satokb6359412011-06-28 17:47:41 +09001485 if (nonAuxCount > 1 || auxCount > 1) {
1486 return true;
1487 } else if (nonAuxCount == 1 && auxCount == 1) {
1488 if (nonAuxSubtype != null && auxSubtype != null
satok9747f892011-09-12 15:56:40 +09001489 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1490 || auxSubtype.overridesImplicitlyEnabledSubtype()
1491 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
satokb6359412011-06-28 17:47:41 +09001492 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1493 return false;
1494 }
1495 return true;
1496 }
1497 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001498 }
satok7cfc0ed2011-06-20 21:29:36 +09001499 }
1500
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001501 // Caution! This method is called in this class. Handle multi-user carefully
satokdbf29502011-08-25 15:28:23 +09001502 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001503 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001504 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001505 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001506 try {
1507 if (token == null || mCurToken != token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001508 int uid = Binder.getCallingUid();
Joe Onorato857fd9b2011-01-27 15:08:35 -08001509 Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token);
satok06487a52010-10-29 11:37:18 +09001510 return;
1511 }
1512
1513 synchronized (mMethodMap) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001514 mImeWindowVis = vis;
1515 mBackDisposition = backDisposition;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001516 if (mStatusBar != null) {
1517 mStatusBar.setImeWindowStatus(token, vis, backDisposition);
1518 }
jungheang.lee217fd292013-02-26 16:53:22 +09001519 final boolean iconVisibility = ((vis & (InputMethodService.IME_ACTIVE)) != 0)
1520 && (mWindowManagerService.isHardKeyboardAvailable()
1521 || (vis & (InputMethodService.IME_VISIBLE)) != 0);
satok5bc8e732011-07-22 21:07:23 +09001522 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1523 if (imi != null && iconVisibility && needsToShowImeSwitchOngoingNotification()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001524 // Used to load label
satok7cfc0ed2011-06-20 21:29:36 +09001525 final CharSequence title = mRes.getText(
1526 com.android.internal.R.string.select_input_method);
Satoshi Kataokab2827262013-07-04 19:43:14 +09001527 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
1528 mContext, imi, mCurrentSubtype);
satok5bc8e732011-07-22 21:07:23 +09001529
satok7cfc0ed2011-06-20 21:29:36 +09001530 mImeSwitcherNotification.setLatestEventInfo(
satok5bc8e732011-07-22 21:07:23 +09001531 mContext, title, summary, mImeSwitchPendingIntent);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001532 if (mNotificationManager != null) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001533 if (DEBUG) {
Satoshi Kataokab2827262013-07-04 19:43:14 +09001534 Slog.d(TAG, "--- show notification: label = " + summary);
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001535 }
1536 mNotificationManager.notifyAsUser(null,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001537 com.android.internal.R.string.select_input_method,
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001538 mImeSwitcherNotification, UserHandle.ALL);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001539 mNotificationShown = true;
1540 }
satok7cfc0ed2011-06-20 21:29:36 +09001541 } else {
Dianne Hackborn661cd522011-08-22 00:26:20 -07001542 if (mNotificationShown && mNotificationManager != null) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001543 if (DEBUG) {
1544 Slog.d(TAG, "--- hide notification");
1545 }
1546 mNotificationManager.cancelAsUser(null,
1547 com.android.internal.R.string.select_input_method, UserHandle.ALL);
satok7cfc0ed2011-06-20 21:29:36 +09001548 mNotificationShown = false;
1549 }
1550 }
satok06487a52010-10-29 11:37:18 +09001551 }
1552 } finally {
1553 Binder.restoreCallingIdentity(ident);
1554 }
1555 }
1556
satoke7c6998e2011-06-03 17:57:59 +09001557 @Override
satokf9f01002011-05-19 21:31:50 +09001558 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001559 if (!calledFromValidUser()) {
1560 return;
1561 }
satokf9f01002011-05-19 21:31:50 +09001562 synchronized (mMethodMap) {
1563 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1564 for (int i = 0; i < spans.length; ++i) {
1565 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001566 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001567 mSecureSuggestionSpans.put(ss, currentImi);
1568 }
1569 }
1570 }
1571 }
1572
satoke7c6998e2011-06-03 17:57:59 +09001573 @Override
satokf9f01002011-05-19 21:31:50 +09001574 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001575 if (!calledFromValidUser()) {
1576 return false;
1577 }
satokf9f01002011-05-19 21:31:50 +09001578 synchronized (mMethodMap) {
1579 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1580 // TODO: Do not send the intent if the process of the targetImi is already dead.
1581 if (targetImi != null) {
1582 final String[] suggestions = span.getSuggestions();
1583 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001584 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001585 final Intent intent = new Intent();
1586 // Ensures that only a class in the original IME package will receive the
1587 // notification.
satok42c5a162011-05-26 16:46:14 +09001588 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001589 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1590 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1591 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1592 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001593 final long ident = Binder.clearCallingIdentity();
1594 try {
1595 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1596 } finally {
1597 Binder.restoreCallingIdentity(ident);
1598 }
satokf9f01002011-05-19 21:31:50 +09001599 return true;
1600 }
1601 }
1602 return false;
1603 }
1604
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001605 void updateFromSettingsLocked(boolean enabledMayChange) {
1606 if (enabledMayChange) {
1607 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1608 for (int i=0; i<enabled.size(); i++) {
1609 // We allow the user to select "disabled until used" apps, so if they
1610 // are enabling one of those here we now need to make it enabled.
1611 InputMethodInfo imm = enabled.get(i);
1612 try {
1613 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1614 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1615 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09001616 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001617 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001618 if (DEBUG) {
1619 Slog.d(TAG, "Update state(" + imm.getId()
1620 + "): DISABLED_UNTIL_USED -> DEFAULT");
1621 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001622 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1623 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001624 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
1625 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001626 }
1627 } catch (RemoteException e) {
1628 }
1629 }
1630 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001631 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1632 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1633 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1634 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001635 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001636 // There is no input method selected, try to choose new applicable input method.
1637 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001638 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001639 }
1640 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001641 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001642 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001643 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001644 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001645 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001646 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001647 }
satokf3db1af2010-11-23 13:34:33 +09001648 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001649 } else {
1650 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001651 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001652 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001653 }
1654 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001655
satokab751aa2010-09-14 19:17:36 +09001656 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001657 InputMethodInfo info = mMethodMap.get(id);
1658 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001659 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001660 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001661
satokd81e9502012-05-21 12:58:45 +09001662 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001663 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09001664 final int subtypeCount = info.getSubtypeCount();
1665 if (subtypeCount <= 0) {
1666 return;
satokcd7cd292010-11-20 15:46:23 +09001667 }
satokd81e9502012-05-21 12:58:45 +09001668 final InputMethodSubtype oldSubtype = mCurrentSubtype;
1669 final InputMethodSubtype newSubtype;
1670 if (subtypeId >= 0 && subtypeId < subtypeCount) {
1671 newSubtype = info.getSubtypeAt(subtypeId);
1672 } else {
1673 // If subtype is null, try to find the most applicable one from
1674 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001675 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09001676 }
1677 if (newSubtype == null || oldSubtype == null) {
1678 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
1679 + ", new subtype = " + newSubtype);
1680 return;
1681 }
1682 if (newSubtype != oldSubtype) {
1683 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1684 if (mCurMethod != null) {
1685 try {
1686 refreshImeWindowVisibilityLocked();
1687 mCurMethod.changeInputMethodSubtype(newSubtype);
1688 } catch (RemoteException e) {
1689 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
satokab751aa2010-09-14 19:17:36 +09001690 }
1691 }
1692 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001693 return;
1694 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001695
satokd81e9502012-05-21 12:58:45 +09001696 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001697 final long ident = Binder.clearCallingIdentity();
1698 try {
satokab751aa2010-09-14 19:17:36 +09001699 // Set a subtype to this input method.
1700 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001701 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1702 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1703 // because mCurMethodId is stored as a history in
1704 // setSelectedInputMethodAndSubtypeLocked().
1705 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001706
1707 if (ActivityManagerNative.isSystemReady()) {
1708 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001709 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001710 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07001711 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001712 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001713 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001714 } finally {
1715 Binder.restoreCallingIdentity(ident);
1716 }
1717 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001718
satok42c5a162011-05-26 16:46:14 +09001719 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001720 public boolean showSoftInput(IInputMethodClient client, int flags,
1721 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001722 if (!calledFromValidUser()) {
1723 return false;
1724 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001725 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001726 long ident = Binder.clearCallingIdentity();
1727 try {
1728 synchronized (mMethodMap) {
1729 if (mCurClient == null || client == null
1730 || mCurClient.client.asBinder() != client.asBinder()) {
1731 try {
1732 // We need to check if this is the current client with
1733 // focus in the window manager, to allow this call to
1734 // be made before input is started in it.
1735 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001736 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001737 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001738 }
1739 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001740 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001741 }
1742 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001743
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001744 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001745 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001746 }
1747 } finally {
1748 Binder.restoreCallingIdentity(ident);
1749 }
1750 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001751
The Android Open Source Project4df24232009-03-05 14:34:35 -08001752 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001753 mShowRequested = true;
1754 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1755 mShowExplicitlyRequested = true;
1756 }
1757 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1758 mShowExplicitlyRequested = true;
1759 mShowForced = true;
1760 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001761
Dianne Hackborncc278702009-09-02 23:07:23 -07001762 if (!mSystemReady) {
1763 return false;
1764 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001765
The Android Open Source Project4df24232009-03-05 14:34:35 -08001766 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001767 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001768 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001769 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1770 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1771 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001772 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001773 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001774 bindCurrentInputMethodService(
1775 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001776 mVisibleBound = true;
1777 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001778 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001779 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09001780 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001781 // The client has asked to have the input method shown, but
1782 // we have been sitting here too long with a connection to the
1783 // service and no interface received, so let's disconnect/connect
1784 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001785 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001786 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09001787 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001788 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001789 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001790 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001791 } else {
1792 if (DEBUG) {
1793 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
1794 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
1795 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001796 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001797
The Android Open Source Project4df24232009-03-05 14:34:35 -08001798 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001799 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001800
satok42c5a162011-05-26 16:46:14 +09001801 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001802 public boolean hideSoftInput(IInputMethodClient client, int flags,
1803 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001804 if (!calledFromValidUser()) {
1805 return false;
1806 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001807 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001808 long ident = Binder.clearCallingIdentity();
1809 try {
1810 synchronized (mMethodMap) {
1811 if (mCurClient == null || client == null
1812 || mCurClient.client.asBinder() != client.asBinder()) {
1813 try {
1814 // We need to check if this is the current client with
1815 // focus in the window manager, to allow this call to
1816 // be made before input is started in it.
1817 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001818 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1819 + uid + ": " + client);
satok15452a42011-10-28 17:58:28 +09001820 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001821 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001822 }
1823 } catch (RemoteException e) {
satok15452a42011-10-28 17:58:28 +09001824 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001825 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001826 }
1827 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001828
Joe Onorato8a9b2202010-02-26 18:56:32 -08001829 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001830 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001831 }
1832 } finally {
1833 Binder.restoreCallingIdentity(ident);
1834 }
1835 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001836
The Android Open Source Project4df24232009-03-05 14:34:35 -08001837 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001838 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1839 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001840 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 -08001841 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001842 }
1843 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001844 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 -08001845 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001846 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001847 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001848 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001849 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1850 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1851 res = true;
1852 } else {
1853 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001854 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001855 if (mHaveConnection && mVisibleBound) {
1856 mContext.unbindService(mVisibleConnection);
1857 mVisibleBound = false;
1858 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001859 mInputShown = false;
1860 mShowRequested = false;
1861 mShowExplicitlyRequested = false;
1862 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001863 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001864 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001865
satok42c5a162011-05-26 16:46:14 +09001866 @Override
Dianne Hackborn7663d802012-02-24 13:08:49 -08001867 public InputBindResult windowGainedFocus(IInputMethodClient client, IBinder windowToken,
1868 int controlFlags, int softInputMode, int windowFlags,
1869 EditorInfo attribute, IInputContext inputContext) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09001870 // Needs to check the validity before clearing calling identity
1871 final boolean calledFromValidUser = calledFromValidUser();
1872
Dianne Hackborn7663d802012-02-24 13:08:49 -08001873 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001874 long ident = Binder.clearCallingIdentity();
1875 try {
1876 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001877 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
Dianne Hackborn7663d802012-02-24 13:08:49 -08001878 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001879 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08001880 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001881
Dianne Hackborn7663d802012-02-24 13:08:49 -08001882 ClientState cs = mClients.get(client.asBinder());
1883 if (cs == null) {
1884 throw new IllegalArgumentException("unknown client "
1885 + client.asBinder());
1886 }
1887
1888 try {
1889 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1890 // Check with the window manager to make sure this client actually
1891 // has a window with focus. If not, reject. This is thread safe
1892 // because if the focus changes some time before or after, the
1893 // next client receiving focus that has any interest in input will
1894 // be calling through here after that change happens.
1895 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
1896 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1897 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001898 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001899 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001900 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001901
Satoshi Kataoka8d033052012-11-19 17:30:40 +09001902 if (!calledFromValidUser) {
1903 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
1904 Slog.w(TAG, "If you want to interect with IME, you need "
1905 + "android.permission.INTERACT_ACROSS_USERS_FULL");
1906 hideCurrentInputLocked(0, null);
1907 return null;
1908 }
1909
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001910 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07001911 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09001912 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001913 if (attribute != null) {
1914 return startInputUncheckedLocked(cs, inputContext, attribute,
1915 controlFlags);
1916 }
1917 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001918 }
1919 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001920
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001921 // Should we auto-show the IME even if the caller has not
1922 // specified what should be done with it?
1923 // We only do this automatically if the window can resize
1924 // to accommodate the IME (so what the user sees will give
1925 // them good context without input information being obscured
1926 // by the IME) or if running on a large screen where there
1927 // is more room for the target window + IME.
1928 final boolean doAutoShow =
1929 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1930 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1931 || mRes.getConfiguration().isLayoutSizeAtLeast(
1932 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001933 final boolean isTextEditor =
1934 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
1935
1936 // We want to start input before showing the IME, but after closing
1937 // it. We want to do this after closing it to help the IME disappear
1938 // more quickly (not get stuck behind it initializing itself for the
1939 // new focused input, even if its window wants to hide the IME).
1940 boolean didStart = false;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001941
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001942 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1943 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001944 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001945 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1946 // There is no focus view, and this window will
1947 // be behind any soft input window, so hide the
1948 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001949 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001950 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001951 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001952 } else if (isTextEditor && doAutoShow && (softInputMode &
1953 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001954 // There is a focus view, and we are navigating forward
1955 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001956 // We only do this automatically if the window can resize
1957 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001958 // them good context without input information being obscured
1959 // by the IME) or if running on a large screen where there
1960 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001961 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001962 if (attribute != null) {
1963 res = startInputUncheckedLocked(cs, inputContext, attribute,
1964 controlFlags);
1965 didStart = true;
1966 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001967 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001968 }
1969 break;
1970 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1971 // Do nothing.
1972 break;
1973 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1974 if ((softInputMode &
1975 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001976 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001977 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001978 }
1979 break;
1980 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001981 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001982 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001983 break;
1984 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1985 if ((softInputMode &
1986 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001987 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001988 if (attribute != null) {
1989 res = startInputUncheckedLocked(cs, inputContext, attribute,
1990 controlFlags);
1991 didStart = true;
1992 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001993 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001994 }
1995 break;
1996 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001997 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001998 if (attribute != null) {
1999 res = startInputUncheckedLocked(cs, inputContext, attribute,
2000 controlFlags);
2001 didStart = true;
2002 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002003 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002004 break;
2005 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002006
2007 if (!didStart && attribute != null) {
2008 res = startInputUncheckedLocked(cs, inputContext, attribute,
2009 controlFlags);
2010 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002011 }
2012 } finally {
2013 Binder.restoreCallingIdentity(ident);
2014 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002015
2016 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002017 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002018
satok42c5a162011-05-26 16:46:14 +09002019 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002020 public void showInputMethodPickerFromClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002021 if (!calledFromValidUser()) {
2022 return;
2023 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002024 synchronized (mMethodMap) {
2025 if (mCurClient == null || client == null
2026 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002027 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002028 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002029 }
2030
satok440aab52010-11-25 09:43:11 +09002031 // Always call subtype picker, because subtype picker is a superset of input method
2032 // picker.
satokab751aa2010-09-14 19:17:36 +09002033 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
2034 }
2035 }
2036
satok42c5a162011-05-26 16:46:14 +09002037 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002038 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002039 if (!calledFromValidUser()) {
2040 return;
2041 }
satok28203512010-11-24 11:06:49 +09002042 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2043 }
2044
satok42c5a162011-05-26 16:46:14 +09002045 @Override
satok28203512010-11-24 11:06:49 +09002046 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002047 if (!calledFromValidUser()) {
2048 return;
2049 }
satok28203512010-11-24 11:06:49 +09002050 synchronized (mMethodMap) {
2051 if (subtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002052 setInputMethodWithSubtypeId(token, id, InputMethodUtils.getSubtypeIdFromHashCode(
satok28203512010-11-24 11:06:49 +09002053 mMethodMap.get(id), subtype.hashCode()));
2054 } else {
2055 setInputMethod(token, id);
2056 }
2057 }
satokab751aa2010-09-14 19:17:36 +09002058 }
2059
satok42c5a162011-05-26 16:46:14 +09002060 @Override
satokb416a712010-11-25 20:42:14 +09002061 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002062 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002063 if (!calledFromValidUser()) {
2064 return;
2065 }
satokb416a712010-11-25 20:42:14 +09002066 synchronized (mMethodMap) {
2067 if (mCurClient == null || client == null
2068 || mCurClient.client.asBinder() != client.asBinder()) {
2069 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
2070 }
satok7fee71f2010-12-17 18:54:26 +09002071 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2072 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002073 }
2074 }
2075
satok4fc87d62011-05-20 16:13:43 +09002076 @Override
satok735cf382010-11-11 20:40:09 +09002077 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002078 if (!calledFromValidUser()) {
2079 return false;
2080 }
satok735cf382010-11-11 20:40:09 +09002081 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002082 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002083 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002084 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002085 lastImi = mMethodMap.get(lastIme.first);
2086 } else {
2087 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002088 }
satok4fc87d62011-05-20 16:13:43 +09002089 String targetLastImiId = null;
2090 int subtypeId = NOT_A_SUBTYPE_ID;
2091 if (lastIme != null && lastImi != null) {
2092 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
2093 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
2094 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2095 : mCurrentSubtype.hashCode();
2096 // If the last IME is the same as the current IME and the last subtype is not
2097 // defined, there is no need to switch to the last IME.
2098 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2099 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002100 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002101 }
2102 }
2103
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002104 if (TextUtils.isEmpty(targetLastImiId)
2105 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002106 // This is a safety net. If the currentSubtype can't be added to the history
2107 // and the framework couldn't find the last ime, we will make the last ime be
2108 // the most applicable enabled keyboard subtype of the system imes.
2109 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2110 if (enabled != null) {
2111 final int N = enabled.size();
2112 final String locale = mCurrentSubtype == null
2113 ? mRes.getConfiguration().locale.toString()
2114 : mCurrentSubtype.getLocale();
2115 for (int i = 0; i < N; ++i) {
2116 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002117 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002118 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002119 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2120 InputMethodUtils.getSubtypes(imi),
2121 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002122 if (keyboardSubtype != null) {
2123 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002124 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002125 imi, keyboardSubtype.hashCode());
2126 if(keyboardSubtype.getLocale().equals(locale)) {
2127 break;
2128 }
2129 }
2130 }
2131 }
2132 }
2133 }
2134
2135 if (!TextUtils.isEmpty(targetLastImiId)) {
2136 if (DEBUG) {
2137 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2138 + ", from: " + mCurMethodId + ", " + subtypeId);
2139 }
2140 setInputMethodWithSubtypeId(token, targetLastImiId, subtypeId);
2141 return true;
2142 } else {
2143 return false;
2144 }
satok735cf382010-11-11 20:40:09 +09002145 }
2146 }
2147
satoke7c6998e2011-06-03 17:57:59 +09002148 @Override
satok688bd472012-02-09 20:09:17 +09002149 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002150 if (!calledFromValidUser()) {
2151 return false;
2152 }
satok688bd472012-02-09 20:09:17 +09002153 synchronized (mMethodMap) {
2154 final ImeSubtypeListItem nextSubtype = mImListManager.getNextInputMethod(
2155 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2156 if (nextSubtype == null) {
2157 return false;
2158 }
2159 setInputMethodWithSubtypeId(token, nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
2160 return true;
2161 }
2162 }
2163
2164 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002165 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2166 if (!calledFromValidUser()) {
2167 return false;
2168 }
2169 synchronized (mMethodMap) {
2170 final ImeSubtypeListItem nextSubtype = mImListManager.getNextInputMethod(
2171 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2172 if (nextSubtype == null) {
2173 return false;
2174 }
2175 return true;
2176 }
2177 }
2178
2179 @Override
satok68f1b782011-04-11 14:26:04 +09002180 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002181 if (!calledFromValidUser()) {
2182 return null;
2183 }
satok68f1b782011-04-11 14:26:04 +09002184 synchronized (mMethodMap) {
2185 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2186 // TODO: Handle the case of the last IME with no subtypes
2187 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2188 || TextUtils.isEmpty(lastIme.second)) return null;
2189 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2190 if (lastImi == null) return null;
2191 try {
2192 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002193 final int lastSubtypeId =
2194 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002195 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2196 return null;
2197 }
2198 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002199 } catch (NumberFormatException e) {
2200 return null;
2201 }
2202 }
2203 }
2204
satoke7c6998e2011-06-03 17:57:59 +09002205 @Override
satokee5e77c2011-09-02 18:50:15 +09002206 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002207 if (!calledFromValidUser()) {
2208 return;
2209 }
satok91e88122011-07-18 11:11:42 +09002210 // By this IPC call, only a process which shares the same uid with the IME can add
2211 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09002212 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09002213 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002214 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002215 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002216 final String[] packageInfos;
2217 try {
2218 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2219 } catch (RemoteException e) {
2220 Slog.e(TAG, "Failed to get package infos");
2221 return;
2222 }
satok91e88122011-07-18 11:11:42 +09002223 if (packageInfos != null) {
2224 final int packageNum = packageInfos.length;
2225 for (int i = 0; i < packageNum; ++i) {
2226 if (packageInfos[i].equals(imi.getPackageName())) {
2227 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002228 final long ident = Binder.clearCallingIdentity();
2229 try {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002230 buildInputMethodListLocked(mMethodList, mMethodMap,
2231 false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002232 } finally {
2233 Binder.restoreCallingIdentity(ident);
2234 }
satokee5e77c2011-09-02 18:50:15 +09002235 return;
satok91e88122011-07-18 11:11:42 +09002236 }
2237 }
2238 }
satoke7c6998e2011-06-03 17:57:59 +09002239 }
satokee5e77c2011-09-02 18:50:15 +09002240 return;
satoke7c6998e2011-06-03 17:57:59 +09002241 }
2242
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002243 @Override
2244 public int getInputMethodWindowVisibleHeight() {
2245 return mWindowManagerService.getInputMethodWindowVisibleHeight();
2246 }
2247
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002248 @Override
2249 public void notifyTextCommitted() {
2250 if (DEBUG) {
2251 Slog.d(TAG, "Got the notification of commitText");
2252 }
2253 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2254 if (imi != null) {
2255 mSwitchingController.onCommitText(imi, mCurrentSubtype);
2256 }
2257 }
2258
satok28203512010-11-24 11:06:49 +09002259 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002260 synchronized (mMethodMap) {
2261 if (token == null) {
2262 if (mContext.checkCallingOrSelfPermission(
2263 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2264 != PackageManager.PERMISSION_GRANTED) {
2265 throw new SecurityException(
2266 "Using null token requires permission "
2267 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2268 }
2269 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002270 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2271 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002272 return;
2273 }
2274
satokc5933802011-08-31 21:26:04 +09002275 final long ident = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002276 try {
satokab751aa2010-09-14 19:17:36 +09002277 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002278 } finally {
2279 Binder.restoreCallingIdentity(ident);
2280 }
2281 }
2282 }
2283
satok42c5a162011-05-26 16:46:14 +09002284 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002285 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002286 if (!calledFromValidUser()) {
2287 return;
2288 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002289 synchronized (mMethodMap) {
2290 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002291 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
2292 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002293 return;
2294 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002295 long ident = Binder.clearCallingIdentity();
2296 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002297 hideCurrentInputLocked(flags, null);
2298 } finally {
2299 Binder.restoreCallingIdentity(ident);
2300 }
2301 }
2302 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002303
satok42c5a162011-05-26 16:46:14 +09002304 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002305 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002306 if (!calledFromValidUser()) {
2307 return;
2308 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002309 synchronized (mMethodMap) {
2310 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002311 Slog.w(TAG, "Ignoring showMySoftInput of uid "
2312 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002313 return;
2314 }
2315 long ident = Binder.clearCallingIdentity();
2316 try {
2317 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002318 } finally {
2319 Binder.restoreCallingIdentity(ident);
2320 }
2321 }
2322 }
2323
2324 void setEnabledSessionInMainThread(SessionState session) {
2325 if (mEnabledSession != session) {
2326 if (mEnabledSession != null) {
2327 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002328 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002329 mEnabledSession.method.setSessionEnabled(
2330 mEnabledSession.session, false);
2331 } catch (RemoteException e) {
2332 }
2333 }
2334 mEnabledSession = session;
2335 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002336 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002337 session.method.setSessionEnabled(
2338 session.session, true);
2339 } catch (RemoteException e) {
2340 }
2341 }
2342 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002343
satok42c5a162011-05-26 16:46:14 +09002344 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002345 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002346 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002347 switch (msg.what) {
2348 case MSG_SHOW_IM_PICKER:
2349 showInputMethodMenu();
2350 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002351
satokab751aa2010-09-14 19:17:36 +09002352 case MSG_SHOW_IM_SUBTYPE_PICKER:
2353 showInputMethodSubtypeMenu();
2354 return true;
2355
satok47a44912010-10-06 16:03:58 +09002356 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002357 args = (SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09002358 showInputMethodAndSubtypeEnabler((String)args.arg1);
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002359 args.recycle();
satok217f5482010-12-15 05:19:19 +09002360 return true;
2361
2362 case MSG_SHOW_IM_CONFIG:
2363 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002364 return true;
2365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002366 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002367
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002368 case MSG_UNBIND_INPUT:
2369 try {
2370 ((IInputMethod)msg.obj).unbindInput();
2371 } catch (RemoteException e) {
2372 // There is nothing interesting about the method dying.
2373 }
2374 return true;
2375 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002376 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002377 try {
2378 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2379 } catch (RemoteException e) {
2380 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002381 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002382 return true;
2383 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002384 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002385 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002386 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002387 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002388 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002389 } catch (RemoteException e) {
2390 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002391 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002392 return true;
2393 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002394 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002395 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002396 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002397 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002398 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002399 } catch (RemoteException e) {
2400 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002401 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002402 return true;
2403 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002404 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002405 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002406 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002407 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2408 } catch (RemoteException e) {
2409 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002410 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002411 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002412 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002413 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002414 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002415 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002416 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002417 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002418 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002419 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002420 // Dispose the channel if the input method is not local to this process
2421 // because the remote proxy will get its own copy when unparceled.
2422 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002423 channel.dispose();
2424 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002425 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002426 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002427 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002428 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002429 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002431 case MSG_START_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002432 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002433 try {
2434 SessionState session = (SessionState)args.arg1;
2435 setEnabledSessionInMainThread(session);
2436 session.method.startInput((IInputContext)args.arg2,
2437 (EditorInfo)args.arg3);
2438 } catch (RemoteException e) {
2439 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002440 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002441 return true;
2442 case MSG_RESTART_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002443 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002444 try {
2445 SessionState session = (SessionState)args.arg1;
2446 setEnabledSessionInMainThread(session);
2447 session.method.restartInput((IInputContext)args.arg2,
2448 (EditorInfo)args.arg3);
2449 } catch (RemoteException e) {
2450 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002451 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002452 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002454 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002456 case MSG_UNBIND_METHOD:
2457 try {
2458 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
2459 } catch (RemoteException e) {
2460 // There is nothing interesting about the last client dying.
2461 }
2462 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002463 case MSG_BIND_METHOD: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002464 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002465 IInputMethodClient client = (IInputMethodClient)args.arg1;
2466 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002467 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002468 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002469 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002470 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002471 } finally {
2472 // Dispose the channel if the input method is not local to this process
2473 // because the remote proxy will get its own copy when unparceled.
2474 if (res.channel != null && Binder.isProxy(client)) {
2475 res.channel.dispose();
2476 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002477 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002478 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002479 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002480 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002481 case MSG_SET_ACTIVE:
2482 try {
2483 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2484 } catch (RemoteException e) {
2485 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2486 + ((ClientState)msg.obj).pid + " uid "
2487 + ((ClientState)msg.obj).uid);
2488 }
2489 return true;
satok01038492012-04-09 21:08:27 +09002490
2491 // --------------------------------------------------------------
2492 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
2493 mHardKeyboardListener.handleHardKeyboardStatusChange(
2494 msg.arg1 == 1, msg.arg2 == 1);
2495 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002496 }
2497 return false;
2498 }
2499
satokdc9ddae2011-10-06 12:22:36 +09002500 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002501 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
2502 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09002503 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002504 if (DEBUG) {
2505 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2506 }
satok723a27e2010-11-11 14:58:11 +09002507 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002508 return true;
2509 }
2510
2511 return false;
2512 }
2513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002514 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002515 HashMap<String, InputMethodInfo> map, boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002516 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002517 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Satoshi Kataoka0766eb02013-07-31 18:30:13 +09002518 + " \n ------ \n" + InputMethodUtils.getStackTrace());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002519 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002520 list.clear();
2521 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002522
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002523 // Use for queryIntentServicesAsUser
2524 final PackageManager pm = mContext.getPackageManager();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002525 String disabledSysImes = mSettings.getDisabledSystemInputMethods();
Amith Yamasanie861ec12010-03-24 21:39:27 -07002526 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002527
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002528 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002529 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002530 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2531 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002532
satoke7c6998e2011-06-03 17:57:59 +09002533 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2534 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002535 for (int i = 0; i < services.size(); ++i) {
2536 ResolveInfo ri = services.get(i);
2537 ServiceInfo si = ri.serviceInfo;
2538 ComponentName compName = new ComponentName(si.packageName, si.name);
2539 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2540 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002541 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002542 + ": it does not require the permission "
2543 + android.Manifest.permission.BIND_INPUT_METHOD);
2544 continue;
2545 }
2546
Joe Onorato8a9b2202010-02-26 18:56:32 -08002547 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002548
2549 try {
satoke7c6998e2011-06-03 17:57:59 +09002550 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002551 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002552 final String id = p.getId();
2553 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002554
2555 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002556 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002557 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002558
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002559 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002560 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002561 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002562 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002563 }
2564 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002565
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002566 if (resetDefaultEnabledIme) {
2567 final ArrayList<InputMethodInfo> defaultEnabledIme =
2568 InputMethodUtils.getDefaultEnabledImes(mContext, mSystemReady, list);
2569 for (int i = 0; i < defaultEnabledIme.size(); ++i) {
2570 final InputMethodInfo imi = defaultEnabledIme.get(i);
2571 if (DEBUG) {
2572 Slog.d(TAG, "--- enable ime = " + imi);
2573 }
2574 setInputMethodEnabledLocked(imi.getId(), true);
2575 }
2576 }
2577
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002578 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09002579 if (!TextUtils.isEmpty(defaultImiId)) {
2580 if (!map.containsKey(defaultImiId)) {
2581 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
2582 if (chooseNewDefaultIMELocked()) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002583 updateFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09002584 }
2585 } else {
2586 // Double check that the default IME is certainly enabled.
2587 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002588 }
2589 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002590 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002591
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002592 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002593
satokab751aa2010-09-14 19:17:36 +09002594 private void showInputMethodMenu() {
2595 showInputMethodMenuInternal(false);
2596 }
2597
2598 private void showInputMethodSubtypeMenu() {
2599 showInputMethodMenuInternal(true);
2600 }
2601
satok217f5482010-12-15 05:19:19 +09002602 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002603 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002604 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002605 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2606 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002607 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002608 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002609 }
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002610 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok217f5482010-12-15 05:19:19 +09002611 }
2612
2613 private void showConfigureInputMethods() {
2614 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2615 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2616 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2617 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002618 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09002619 }
2620
satok2c93efc2012-04-02 19:33:47 +09002621 private boolean isScreenLocked() {
2622 return mKeyguardManager != null
2623 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
2624 }
satokab751aa2010-09-14 19:17:36 +09002625 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002626 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002627
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002628 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09002629 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002630
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002631 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002632 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002633 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002634
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002635 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002636 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
2637 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
satok7f35c8c2010-10-07 21:13:11 +09002638 if (immis == null || immis.size() == 0) {
2639 return;
2640 }
2641
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002642 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002643
satok688bd472012-02-09 20:09:17 +09002644 final List<ImeSubtypeListItem> imList =
2645 mImListManager.getSortedInputMethodAndSubtypeList(
2646 showSubtypes, mInputShown, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09002647
satokc3690562012-01-10 20:14:43 +09002648 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002649 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09002650 if (currentSubtype != null) {
2651 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002652 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
2653 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09002654 }
2655 }
2656
Ken Wakasa761eb372011-03-04 19:06:18 +09002657 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09002658 mIms = new InputMethodInfo[N];
2659 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002660 int checkedItem = 0;
2661 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002662 final ImeSubtypeListItem item = imList.get(i);
2663 mIms[i] = item.mImi;
2664 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002665 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002666 int subtypeId = mSubtypeIds[i];
2667 if ((subtypeId == NOT_A_SUBTYPE_ID)
2668 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2669 || (subtypeId == lastInputMethodSubtypeId)) {
2670 checkedItem = i;
2671 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002672 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002673 }
Ken Wakasa05dbb652011-08-22 15:22:43 +09002674 final TypedArray a = context.obtainStyledAttributes(null,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002675 com.android.internal.R.styleable.DialogPreference,
2676 com.android.internal.R.attr.alertDialogStyle, 0);
2677 mDialogBuilder = new AlertDialog.Builder(context)
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002678 .setOnCancelListener(new OnCancelListener() {
satok42c5a162011-05-26 16:46:14 +09002679 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002680 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002681 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002682 }
2683 })
2684 .setIcon(a.getDrawable(
2685 com.android.internal.R.styleable.DialogPreference_dialogTitle));
2686 a.recycle();
satok01038492012-04-09 21:08:27 +09002687 final LayoutInflater inflater =
2688 (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2689 final View tv = inflater.inflate(
2690 com.android.internal.R.layout.input_method_switch_dialog_title, null);
2691 mDialogBuilder.setCustomTitle(tv);
2692
2693 // Setup layout for a toggle switch of the hardware keyboard
2694 mSwitchingDialogTitleView = tv;
2695 mSwitchingDialogTitleView.findViewById(
2696 com.android.internal.R.id.hard_keyboard_section).setVisibility(
2697 mWindowManagerService.isHardKeyboardAvailable() ?
2698 View.VISIBLE : View.GONE);
2699 final Switch hardKeySwitch = ((Switch)mSwitchingDialogTitleView.findViewById(
2700 com.android.internal.R.id.hard_keyboard_switch));
2701 hardKeySwitch.setChecked(mWindowManagerService.isHardKeyboardEnabled());
2702 hardKeySwitch.setOnCheckedChangeListener(
2703 new OnCheckedChangeListener() {
2704 @Override
2705 public void onCheckedChanged(
2706 CompoundButton buttonView, boolean isChecked) {
2707 mWindowManagerService.setHardKeyboardEnabled(isChecked);
Satoshi Kataoka04dd24d2013-01-18 13:44:37 +09002708 // Ensure that the input method dialog is dismissed when changing
2709 // the hardware keyboard state.
2710 hideInputMethodMenu();
satok01038492012-04-09 21:08:27 +09002711 }
2712 });
satokd87c2592010-09-29 11:52:06 +09002713
Ken Wakasa05dbb652011-08-22 15:22:43 +09002714 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(context,
2715 com.android.internal.R.layout.simple_list_item_2_single_choice, imList,
2716 checkedItem);
2717
2718 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002719 new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002720 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002721 public void onClick(DialogInterface dialog, int which) {
2722 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09002723 if (mIms == null || mIms.length <= which
2724 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002725 return;
2726 }
2727 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09002728 int subtypeId = mSubtypeIds[which];
Satoshi Kataokad2142962012-11-12 18:43:06 +09002729 adapter.mCheckedItem = which;
2730 adapter.notifyDataSetChanged();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002731 hideInputMethodMenu();
2732 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09002733 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09002734 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09002735 subtypeId = NOT_A_SUBTYPE_ID;
2736 }
2737 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002738 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08002739 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002740 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002741 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002742
satokbc81b692011-08-26 16:22:22 +09002743 if (showSubtypes && !isScreenLocked) {
satok82beadf2010-12-27 19:03:06 +09002744 mDialogBuilder.setPositiveButton(
2745 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09002746 new DialogInterface.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002747 @Override
satok7f35c8c2010-10-07 21:13:11 +09002748 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09002749 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09002750 }
2751 });
2752 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002753 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002754 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002755 mSwitchingDialog.getWindow().setType(
2756 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09002757 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
2758 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002759 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002760 mSwitchingDialog.show();
2761 }
2762 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002763
satok93d744d2012-05-09 17:14:08 +09002764 private static class ImeSubtypeListItem implements Comparable<ImeSubtypeListItem> {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002765 public final CharSequence mImeName;
2766 public final CharSequence mSubtypeName;
2767 public final InputMethodInfo mImi;
2768 public final int mSubtypeId;
satok93d744d2012-05-09 17:14:08 +09002769 private final boolean mIsSystemLocale;
2770 private final boolean mIsSystemLanguage;
2771
Ken Wakasa05dbb652011-08-22 15:22:43 +09002772 public ImeSubtypeListItem(CharSequence imeName, CharSequence subtypeName,
satok93d744d2012-05-09 17:14:08 +09002773 InputMethodInfo imi, int subtypeId, String subtypeLocale, String systemLocale) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002774 mImeName = imeName;
2775 mSubtypeName = subtypeName;
2776 mImi = imi;
2777 mSubtypeId = subtypeId;
satok93d744d2012-05-09 17:14:08 +09002778 if (TextUtils.isEmpty(subtypeLocale)) {
2779 mIsSystemLocale = false;
2780 mIsSystemLanguage = false;
2781 } else {
2782 mIsSystemLocale = subtypeLocale.equals(systemLocale);
2783 mIsSystemLanguage = mIsSystemLocale
2784 || subtypeLocale.startsWith(systemLocale.substring(0, 2));
2785 }
2786 }
2787
2788 @Override
2789 public int compareTo(ImeSubtypeListItem other) {
2790 if (TextUtils.isEmpty(mImeName)) {
2791 return 1;
2792 }
2793 if (TextUtils.isEmpty(other.mImeName)) {
2794 return -1;
2795 }
2796 if (!TextUtils.equals(mImeName, other.mImeName)) {
2797 return mImeName.toString().compareTo(other.mImeName.toString());
2798 }
2799 if (TextUtils.equals(mSubtypeName, other.mSubtypeName)) {
2800 return 0;
2801 }
2802 if (mIsSystemLocale) {
2803 return -1;
2804 }
2805 if (other.mIsSystemLocale) {
2806 return 1;
2807 }
2808 if (mIsSystemLanguage) {
2809 return -1;
2810 }
2811 if (other.mIsSystemLanguage) {
2812 return 1;
2813 }
2814 if (TextUtils.isEmpty(mSubtypeName)) {
2815 return 1;
2816 }
2817 if (TextUtils.isEmpty(other.mSubtypeName)) {
2818 return -1;
2819 }
2820 return mSubtypeName.toString().compareTo(other.mSubtypeName.toString());
Ken Wakasa05dbb652011-08-22 15:22:43 +09002821 }
2822 }
2823
2824 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
2825 private final LayoutInflater mInflater;
2826 private final int mTextViewResourceId;
2827 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09002828 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09002829 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
2830 List<ImeSubtypeListItem> itemsList, int checkedItem) {
2831 super(context, textViewResourceId, itemsList);
2832 mTextViewResourceId = textViewResourceId;
2833 mItemsList = itemsList;
2834 mCheckedItem = checkedItem;
2835 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2836 }
2837
2838 @Override
2839 public View getView(int position, View convertView, ViewGroup parent) {
2840 final View view = convertView != null ? convertView
2841 : mInflater.inflate(mTextViewResourceId, null);
2842 if (position < 0 || position >= mItemsList.size()) return view;
2843 final ImeSubtypeListItem item = mItemsList.get(position);
2844 final CharSequence imeName = item.mImeName;
2845 final CharSequence subtypeName = item.mSubtypeName;
2846 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
2847 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
2848 if (TextUtils.isEmpty(subtypeName)) {
2849 firstTextView.setText(imeName);
2850 secondTextView.setVisibility(View.GONE);
2851 } else {
2852 firstTextView.setText(subtypeName);
2853 secondTextView.setText(imeName);
2854 secondTextView.setVisibility(View.VISIBLE);
2855 }
2856 final RadioButton radioButton =
2857 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
2858 radioButton.setChecked(position == mCheckedItem);
2859 return view;
2860 }
2861 }
2862
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002863 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07002864 synchronized (mMethodMap) {
2865 hideInputMethodMenuLocked();
2866 }
2867 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002868
The Android Open Source Project10592532009-03-18 17:39:46 -07002869 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002870 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002871
The Android Open Source Project10592532009-03-18 17:39:46 -07002872 if (mSwitchingDialog != null) {
2873 mSwitchingDialog.dismiss();
2874 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002875 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002876
The Android Open Source Project10592532009-03-18 17:39:46 -07002877 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07002878 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002879 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002880
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002881 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002882
satok42c5a162011-05-26 16:46:14 +09002883 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002884 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002885 // TODO: Make this work even for non-current users?
2886 if (!calledFromValidUser()) {
2887 return false;
2888 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002889 synchronized (mMethodMap) {
2890 if (mContext.checkCallingOrSelfPermission(
2891 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2892 != PackageManager.PERMISSION_GRANTED) {
2893 throw new SecurityException(
2894 "Requires permission "
2895 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2896 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002897
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002898 long ident = Binder.clearCallingIdentity();
2899 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002900 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002901 } finally {
2902 Binder.restoreCallingIdentity(ident);
2903 }
2904 }
2905 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002906
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002907 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
2908 // Make sure this is a valid input method.
2909 InputMethodInfo imm = mMethodMap.get(id);
2910 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09002911 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002912 }
2913
satokd87c2592010-09-29 11:52:06 +09002914 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
2915 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002916
satokd87c2592010-09-29 11:52:06 +09002917 if (enabled) {
2918 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
2919 if (pair.first.equals(id)) {
2920 // We are enabling this input method, but it is already enabled.
2921 // Nothing to do. The previous state was enabled.
2922 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002923 }
2924 }
satokd87c2592010-09-29 11:52:06 +09002925 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
2926 // Previous state was disabled.
2927 return false;
2928 } else {
2929 StringBuilder builder = new StringBuilder();
2930 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2931 builder, enabledInputMethodsList, id)) {
2932 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002933 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002934 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
2935 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
2936 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09002937 }
2938 // Previous state was enabled.
2939 return true;
2940 } else {
2941 // We are disabling the input method but it is already disabled.
2942 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002943 return false;
2944 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002945 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002946 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002947
satok723a27e2010-11-11 14:58:11 +09002948 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
2949 boolean setSubtypeOnly) {
2950 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002951 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09002952
2953 // Set Subtype here
2954 if (imi == null || subtypeId < 0) {
2955 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08002956 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09002957 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09002958 if (subtypeId < imi.getSubtypeCount()) {
2959 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
2960 mSettings.putSelectedSubtype(subtype.hashCode());
2961 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09002962 } else {
2963 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09002964 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002965 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09002966 }
satokab751aa2010-09-14 19:17:36 +09002967 }
satok723a27e2010-11-11 14:58:11 +09002968
satok4c0e7152012-06-20 20:08:44 +09002969 // Workaround.
2970 // ASEC is not ready in the IMMS constructor. Accordingly, forward-locked
2971 // IMEs are not recognized and considered uninstalled.
2972 // Actually, we can't move everything after SystemReady because
2973 // IMMS needs to run in the encryption lock screen. So, we just skip changing
2974 // the default IME here and try cheking the default IME again in systemReady().
2975 // TODO: Do nothing before system ready and implement a separated logic for
2976 // the encryption lock screen.
2977 // TODO: ASEC should be ready before IMMS is instantiated.
2978 if (mSystemReady && !setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09002979 // Set InputMethod here
2980 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
2981 }
2982 }
2983
2984 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
2985 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
2986 int lastSubtypeId = NOT_A_SUBTYPE_ID;
2987 // newDefaultIme is empty when there is no candidate for the selected IME.
2988 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
2989 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
2990 if (subtypeHashCode != null) {
2991 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002992 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok723a27e2010-11-11 14:58:11 +09002993 imi, Integer.valueOf(subtypeHashCode));
2994 } catch (NumberFormatException e) {
2995 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
2996 }
2997 }
2998 }
2999 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003000 }
3001
satok4e4569d2010-11-19 18:45:53 +09003002 // If there are no selected shortcuts, tries finding the most applicable ones.
3003 private Pair<InputMethodInfo, InputMethodSubtype>
3004 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3005 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3006 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003007 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003008 boolean foundInSystemIME = false;
3009
3010 // Search applicable subtype for each InputMethodInfo
3011 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003012 final String imiId = imi.getId();
3013 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3014 continue;
3015 }
satokcd7cd292010-11-20 15:46:23 +09003016 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003017 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003018 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003019 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003020 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003021 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003022 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003023 }
satokdf31ae62011-01-15 06:19:44 +09003024 // 2. Search by the system locale from enabledSubtypes.
3025 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003026 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003027 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003028 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003029 }
satoka86f5e42011-09-02 17:12:42 +09003030 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003031 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003032 final ArrayList<InputMethodSubtype> subtypesForSearch =
3033 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003034 ? InputMethodUtils.getSubtypes(imi)
3035 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003036 // 4. Search by the current subtype's locale from all subtypes.
3037 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003038 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003039 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003040 }
3041 // 5. Search by the system locale from all subtypes.
3042 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003043 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003044 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003045 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003046 }
satokcd7cd292010-11-20 15:46:23 +09003047 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003048 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003049 // The current input method is the most applicable IME.
3050 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003051 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003052 break;
satok7599a7f2010-12-22 13:45:23 +09003053 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003054 // The system input method is 2nd applicable IME.
3055 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003056 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003057 if ((imi.getServiceInfo().applicationInfo.flags
3058 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3059 foundInSystemIME = true;
3060 }
satok4e4569d2010-11-19 18:45:53 +09003061 }
3062 }
3063 }
3064 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003065 if (mostApplicableIMI != null) {
3066 Slog.w(TAG, "Most applicable shortcut input method was:"
3067 + mostApplicableIMI.getId());
3068 if (mostApplicableSubtype != null) {
3069 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3070 + "," + mostApplicableSubtype.getMode() + ","
3071 + mostApplicableSubtype.getLocale());
3072 }
3073 }
satok4e4569d2010-11-19 18:45:53 +09003074 }
satokcd7cd292010-11-20 15:46:23 +09003075 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09003076 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09003077 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003078 } else {
3079 return null;
3080 }
3081 }
3082
satokab751aa2010-09-14 19:17:36 +09003083 /**
3084 * @return Return the current subtype of this input method.
3085 */
satok42c5a162011-05-26 16:46:14 +09003086 @Override
satokab751aa2010-09-14 19:17:36 +09003087 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003088 // TODO: Make this work even for non-current users?
3089 if (!calledFromValidUser()) {
3090 return null;
3091 }
3092 synchronized (mMethodMap) {
3093 return getCurrentInputMethodSubtypeLocked();
3094 }
3095 }
3096
3097 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003098 if (mCurMethodId == null) {
3099 return null;
3100 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003101 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003102 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3103 if (imi == null || imi.getSubtypeCount() == 0) {
3104 return null;
satok4e4569d2010-11-19 18:45:53 +09003105 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003106 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003107 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3108 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003109 if (subtypeId == NOT_A_SUBTYPE_ID) {
3110 // If there are no selected subtypes, the framework will try to find
3111 // the most applicable subtype from explicitly or implicitly enabled
3112 // subtypes.
3113 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003114 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003115 // If there is only one explicitly or implicitly enabled subtype,
3116 // just returns it.
3117 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3118 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3119 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003120 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003121 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003122 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003123 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003124 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003125 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3126 true);
satok4e4569d2010-11-19 18:45:53 +09003127 }
satok3ef8b292010-11-23 06:06:29 +09003128 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003129 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003130 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003131 }
3132 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003133 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003134 }
3135
satokf3db1af2010-11-23 13:34:33 +09003136 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
3137 InputMethodSubtype subtype) {
3138 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
3139 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
3140 } else {
3141 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3142 subtypes.add(subtype);
3143 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
3144 }
3145 }
3146
satok4e4569d2010-11-19 18:45:53 +09003147 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003148 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003149 @Override
satok4e4569d2010-11-19 18:45:53 +09003150 public List getShortcutInputMethodsAndSubtypes() {
3151 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09003152 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09003153 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003154 // If there are no selected shortcut subtypes, the framework will try to find
3155 // the most applicable subtype from all subtypes whose mode is
3156 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003157 Pair<InputMethodInfo, InputMethodSubtype> info =
3158 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003159 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003160 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003161 ret.add(info.first);
3162 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003163 }
satok3da92232011-01-11 22:46:30 +09003164 return ret;
satokf3db1af2010-11-23 13:34:33 +09003165 }
satokf3db1af2010-11-23 13:34:33 +09003166 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3167 ret.add(imi);
3168 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3169 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003170 }
3171 }
satokf3db1af2010-11-23 13:34:33 +09003172 return ret;
satok4e4569d2010-11-19 18:45:53 +09003173 }
3174 }
3175
satok42c5a162011-05-26 16:46:14 +09003176 @Override
satokb66d2872010-11-10 01:04:04 +09003177 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003178 // TODO: Make this work even for non-current users?
3179 if (!calledFromValidUser()) {
3180 return false;
3181 }
satokb66d2872010-11-10 01:04:04 +09003182 synchronized (mMethodMap) {
3183 if (subtype != null && mCurMethodId != null) {
3184 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003185 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003186 if (subtypeId != NOT_A_SUBTYPE_ID) {
3187 setInputMethodLocked(mCurMethodId, subtypeId);
3188 return true;
3189 }
3190 }
3191 return false;
3192 }
3193 }
3194
satok688bd472012-02-09 20:09:17 +09003195 private static class InputMethodAndSubtypeListManager {
3196 private final Context mContext;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003197 // Used to load label
satok688bd472012-02-09 20:09:17 +09003198 private final PackageManager mPm;
3199 private final InputMethodManagerService mImms;
satok93d744d2012-05-09 17:14:08 +09003200 private final String mSystemLocaleStr;
satok688bd472012-02-09 20:09:17 +09003201 public InputMethodAndSubtypeListManager(Context context, InputMethodManagerService imms) {
3202 mContext = context;
3203 mPm = context.getPackageManager();
3204 mImms = imms;
satok0a1bcf42012-05-16 19:26:31 +09003205 final Locale locale = context.getResources().getConfiguration().locale;
3206 mSystemLocaleStr = locale != null ? locale.toString() : "";
satok688bd472012-02-09 20:09:17 +09003207 }
3208
3209 private final TreeMap<InputMethodInfo, List<InputMethodSubtype>> mSortedImmis =
3210 new TreeMap<InputMethodInfo, List<InputMethodSubtype>>(
3211 new Comparator<InputMethodInfo>() {
3212 @Override
3213 public int compare(InputMethodInfo imi1, InputMethodInfo imi2) {
3214 if (imi2 == null) return 0;
3215 if (imi1 == null) return 1;
3216 if (mPm == null) {
3217 return imi1.getId().compareTo(imi2.getId());
3218 }
3219 CharSequence imiId1 = imi1.loadLabel(mPm) + "/" + imi1.getId();
3220 CharSequence imiId2 = imi2.loadLabel(mPm) + "/" + imi2.getId();
3221 return imiId1.toString().compareTo(imiId2.toString());
3222 }
3223 });
3224
3225 public ImeSubtypeListItem getNextInputMethod(
3226 boolean onlyCurrentIme, InputMethodInfo imi, InputMethodSubtype subtype) {
3227 if (imi == null) {
3228 return null;
3229 }
3230 final List<ImeSubtypeListItem> imList = getSortedInputMethodAndSubtypeList();
3231 if (imList.size() <= 1) {
3232 return null;
3233 }
3234 final int N = imList.size();
3235 final int currentSubtypeId = subtype != null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003236 ? InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode())
satok688bd472012-02-09 20:09:17 +09003237 : NOT_A_SUBTYPE_ID;
3238 for (int i = 0; i < N; ++i) {
3239 final ImeSubtypeListItem isli = imList.get(i);
3240 if (isli.mImi.equals(imi) && isli.mSubtypeId == currentSubtypeId) {
3241 if (!onlyCurrentIme) {
3242 return imList.get((i + 1) % N);
3243 }
3244 for (int j = 0; j < N - 1; ++j) {
3245 final ImeSubtypeListItem candidate = imList.get((i + j + 1) % N);
3246 if (candidate.mImi.equals(imi)) {
3247 return candidate;
3248 }
3249 }
3250 return null;
3251 }
3252 }
3253 return null;
3254 }
3255
3256 public List<ImeSubtypeListItem> getSortedInputMethodAndSubtypeList() {
3257 return getSortedInputMethodAndSubtypeList(true, false, false);
3258 }
3259
3260 public List<ImeSubtypeListItem> getSortedInputMethodAndSubtypeList(boolean showSubtypes,
3261 boolean inputShown, boolean isScreenLocked) {
3262 final ArrayList<ImeSubtypeListItem> imList = new ArrayList<ImeSubtypeListItem>();
3263 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
3264 mImms.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
3265 if (immis == null || immis.size() == 0) {
3266 return Collections.emptyList();
3267 }
3268 mSortedImmis.clear();
3269 mSortedImmis.putAll(immis);
3270 for (InputMethodInfo imi : mSortedImmis.keySet()) {
3271 if (imi == null) continue;
3272 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypeList = immis.get(imi);
3273 HashSet<String> enabledSubtypeSet = new HashSet<String>();
3274 for (InputMethodSubtype subtype: explicitlyOrImplicitlyEnabledSubtypeList) {
3275 enabledSubtypeSet.add(String.valueOf(subtype.hashCode()));
3276 }
satok688bd472012-02-09 20:09:17 +09003277 final CharSequence imeLabel = imi.loadLabel(mPm);
3278 if (showSubtypes && enabledSubtypeSet.size() > 0) {
3279 final int subtypeCount = imi.getSubtypeCount();
3280 if (DEBUG) {
3281 Slog.v(TAG, "Add subtypes: " + subtypeCount + ", " + imi.getId());
3282 }
3283 for (int j = 0; j < subtypeCount; ++j) {
3284 final InputMethodSubtype subtype = imi.getSubtypeAt(j);
3285 final String subtypeHashCode = String.valueOf(subtype.hashCode());
3286 // We show all enabled IMEs and subtypes when an IME is shown.
3287 if (enabledSubtypeSet.contains(subtypeHashCode)
3288 && ((inputShown && !isScreenLocked) || !subtype.isAuxiliary())) {
3289 final CharSequence subtypeLabel =
3290 subtype.overridesImplicitlyEnabledSubtype() ? null
3291 : subtype.getDisplayName(mContext, imi.getPackageName(),
3292 imi.getServiceInfo().applicationInfo);
satok93d744d2012-05-09 17:14:08 +09003293 imList.add(new ImeSubtypeListItem(imeLabel, subtypeLabel, imi, j,
3294 subtype.getLocale(), mSystemLocaleStr));
satok688bd472012-02-09 20:09:17 +09003295
3296 // Removing this subtype from enabledSubtypeSet because we no longer
3297 // need to add an entry of this subtype to imList to avoid duplicated
3298 // entries.
3299 enabledSubtypeSet.remove(subtypeHashCode);
3300 }
3301 }
3302 } else {
satok93d744d2012-05-09 17:14:08 +09003303 imList.add(new ImeSubtypeListItem(imeLabel, null, imi, NOT_A_SUBTYPE_ID,
3304 null, mSystemLocaleStr));
satok688bd472012-02-09 20:09:17 +09003305 }
3306 }
satok93d744d2012-05-09 17:14:08 +09003307 Collections.sort(imList);
satok688bd472012-02-09 20:09:17 +09003308 return imList;
3309 }
3310 }
3311
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003312 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003313 private static class InputMethodFileManager {
3314 private static final String SYSTEM_PATH = "system";
3315 private static final String INPUT_METHOD_PATH = "inputmethod";
3316 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3317 private static final String NODE_SUBTYPES = "subtypes";
3318 private static final String NODE_SUBTYPE = "subtype";
3319 private static final String NODE_IMI = "imi";
3320 private static final String ATTR_ID = "id";
3321 private static final String ATTR_LABEL = "label";
3322 private static final String ATTR_ICON = "icon";
3323 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3324 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3325 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3326 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3327 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3328 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003329 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
satoke7c6998e2011-06-03 17:57:59 +09003330 new HashMap<String, List<InputMethodSubtype>>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003331 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003332 if (methodMap == null) {
3333 throw new NullPointerException("methodMap is null");
3334 }
3335 mMethodMap = methodMap;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003336 final File systemDir = userId == UserHandle.USER_OWNER
3337 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3338 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003339 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3340 if (!inputMethodDir.mkdirs()) {
3341 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3342 }
3343 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3344 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3345 if (!subtypeFile.exists()) {
3346 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003347 writeAdditionalInputMethodSubtypes(
3348 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003349 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003350 readAdditionalInputMethodSubtypes(
3351 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003352 }
3353 }
3354
3355 private void deleteAllInputMethodSubtypes(String imiId) {
3356 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003357 mAdditionalSubtypesMap.remove(imiId);
3358 writeAdditionalInputMethodSubtypes(
3359 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003360 }
3361 }
3362
3363 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003364 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003365 synchronized (mMethodMap) {
3366 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3367 final int N = additionalSubtypes.length;
3368 for (int i = 0; i < N; ++i) {
3369 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003370 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003371 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003372 } else {
3373 Slog.w(TAG, "Duplicated subtype definition found: "
3374 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003375 }
3376 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003377 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3378 writeAdditionalInputMethodSubtypes(
3379 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003380 }
3381 }
3382
3383 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3384 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003385 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003386 }
3387 }
3388
3389 private static void writeAdditionalInputMethodSubtypes(
3390 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3391 HashMap<String, InputMethodInfo> methodMap) {
3392 // Safety net for the case that this function is called before methodMap is set.
3393 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3394 FileOutputStream fos = null;
3395 try {
3396 fos = subtypesFile.startWrite();
3397 final XmlSerializer out = new FastXmlSerializer();
3398 out.setOutput(fos, "utf-8");
3399 out.startDocument(null, true);
3400 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3401 out.startTag(null, NODE_SUBTYPES);
3402 for (String imiId : allSubtypes.keySet()) {
3403 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3404 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3405 continue;
3406 }
3407 out.startTag(null, NODE_IMI);
3408 out.attribute(null, ATTR_ID, imiId);
3409 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3410 final int N = subtypesList.size();
3411 for (int i = 0; i < N; ++i) {
3412 final InputMethodSubtype subtype = subtypesList.get(i);
3413 out.startTag(null, NODE_SUBTYPE);
3414 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3415 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3416 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3417 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3418 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3419 out.attribute(null, ATTR_IS_AUXILIARY,
3420 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3421 out.endTag(null, NODE_SUBTYPE);
3422 }
3423 out.endTag(null, NODE_IMI);
3424 }
3425 out.endTag(null, NODE_SUBTYPES);
3426 out.endDocument();
3427 subtypesFile.finishWrite(fos);
3428 } catch (java.io.IOException e) {
3429 Slog.w(TAG, "Error writing subtypes", e);
3430 if (fos != null) {
3431 subtypesFile.failWrite(fos);
3432 }
3433 }
3434 }
3435
3436 private static void readAdditionalInputMethodSubtypes(
3437 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3438 if (allSubtypes == null || subtypesFile == null) return;
3439 allSubtypes.clear();
3440 FileInputStream fis = null;
3441 try {
3442 fis = subtypesFile.openRead();
3443 final XmlPullParser parser = Xml.newPullParser();
3444 parser.setInput(fis, null);
3445 int type = parser.getEventType();
3446 // Skip parsing until START_TAG
3447 while ((type = parser.next()) != XmlPullParser.START_TAG
3448 && type != XmlPullParser.END_DOCUMENT) {}
3449 String firstNodeName = parser.getName();
3450 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3451 throw new XmlPullParserException("Xml doesn't start with subtypes");
3452 }
3453 final int depth =parser.getDepth();
3454 String currentImiId = null;
3455 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3456 while (((type = parser.next()) != XmlPullParser.END_TAG
3457 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3458 if (type != XmlPullParser.START_TAG)
3459 continue;
3460 final String nodeName = parser.getName();
3461 if (NODE_IMI.equals(nodeName)) {
3462 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3463 if (TextUtils.isEmpty(currentImiId)) {
3464 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3465 continue;
3466 }
3467 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
3468 allSubtypes.put(currentImiId, tempSubtypesArray);
3469 } else if (NODE_SUBTYPE.equals(nodeName)) {
3470 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3471 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3472 continue;
3473 }
3474 final int icon = Integer.valueOf(
3475 parser.getAttributeValue(null, ATTR_ICON));
3476 final int label = Integer.valueOf(
3477 parser.getAttributeValue(null, ATTR_LABEL));
3478 final String imeSubtypeLocale =
3479 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3480 final String imeSubtypeMode =
3481 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3482 final String imeSubtypeExtraValue =
3483 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003484 final boolean isAuxiliary = "1".equals(String.valueOf(
3485 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
satoke7c6998e2011-06-03 17:57:59 +09003486 final InputMethodSubtype subtype =
3487 new InputMethodSubtype(label, icon, imeSubtypeLocale,
3488 imeSubtypeMode, imeSubtypeExtraValue, isAuxiliary);
3489 tempSubtypesArray.add(subtype);
3490 }
3491 }
3492 } catch (XmlPullParserException e) {
3493 Slog.w(TAG, "Error reading subtypes: " + e);
3494 return;
3495 } catch (java.io.IOException e) {
3496 Slog.w(TAG, "Error reading subtypes: " + e);
3497 return;
3498 } catch (NumberFormatException e) {
3499 Slog.w(TAG, "Error reading subtypes: " + e);
3500 return;
3501 } finally {
3502 if (fis != null) {
3503 try {
3504 fis.close();
3505 } catch (java.io.IOException e1) {
3506 Slog.w(TAG, "Failed to close.");
3507 }
3508 }
3509 }
3510 }
3511 }
3512
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003513 @Override
3514 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3515 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3516 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003517
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003518 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3519 + Binder.getCallingPid()
3520 + ", uid=" + Binder.getCallingUid());
3521 return;
3522 }
3523
3524 IInputMethod method;
3525 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003526
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003527 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003528
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003529 synchronized (mMethodMap) {
3530 p.println("Current Input Method Manager state:");
3531 int N = mMethodList.size();
3532 p.println(" Input Methods:");
3533 for (int i=0; i<N; i++) {
3534 InputMethodInfo info = mMethodList.get(i);
3535 p.println(" InputMethod #" + i + ":");
3536 info.dump(p, " ");
3537 }
3538 p.println(" Clients:");
3539 for (ClientState ci : mClients.values()) {
3540 p.println(" Client " + ci + ":");
3541 p.println(" client=" + ci.client);
3542 p.println(" inputContext=" + ci.inputContext);
3543 p.println(" sessionRequested=" + ci.sessionRequested);
3544 p.println(" curSession=" + ci.curSession);
3545 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003546 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003547 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003548 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3549 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003550 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3551 + " mBoundToMethod=" + mBoundToMethod);
3552 p.println(" mCurToken=" + mCurToken);
3553 p.println(" mCurIntent=" + mCurIntent);
3554 method = mCurMethod;
3555 p.println(" mCurMethod=" + mCurMethod);
3556 p.println(" mEnabledSession=" + mEnabledSession);
3557 p.println(" mShowRequested=" + mShowRequested
3558 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3559 + " mShowForced=" + mShowForced
3560 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07003561 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003562 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003563
Jeff Brownb88102f2010-09-08 11:49:43 -07003564 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003565 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003566 pw.flush();
3567 try {
3568 client.client.asBinder().dump(fd, args);
3569 } catch (RemoteException e) {
3570 p.println("Input method client dead: " + e);
3571 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003572 } else {
3573 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003574 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003575
Jeff Brownb88102f2010-09-08 11:49:43 -07003576 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003577 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003578 pw.flush();
3579 try {
3580 method.asBinder().dump(fd, args);
3581 } catch (RemoteException e) {
3582 p.println("Input method service dead: " + e);
3583 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003584 } else {
3585 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003586 }
3587 }
3588}