blob: 8fb2e9f484a1088dd7e8a04f28ab6aeec9001cc7 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 * use this file except in compliance with the License. You may obtain a copy of
5 * the License at
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007 * http://www.apache.org/licenses/LICENSE-2.0
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 * License for the specific language governing permissions and limitations under
13 * the License.
14 */
15
16package com.android.server;
17
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080018import com.android.internal.content.PackageMonitor;
Satoshi Kataokad7443c82013-10-15 17:45:43 +090019import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController;
Satoshi Kataokad787f692013-10-26 04:44:21 +090020import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController.ImeSubtypeListItem;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +090021import com.android.internal.inputmethod.InputMethodUtils;
22import com.android.internal.inputmethod.InputMethodUtils.InputMethodSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import com.android.internal.os.HandlerCaller;
Svetoslav Ganov758143e2012-08-06 16:40:27 -070024import com.android.internal.os.SomeArgs;
satoke7c6998e2011-06-03 17:57:59 +090025import com.android.internal.util.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import com.android.internal.view.IInputContext;
27import com.android.internal.view.IInputMethod;
Michael Wright52a53522013-03-14 10:59:38 -070028import com.android.internal.view.IInputSessionCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import com.android.internal.view.IInputMethodClient;
30import com.android.internal.view.IInputMethodManager;
31import com.android.internal.view.IInputMethodSession;
32import com.android.internal.view.InputBindResult;
Adam Lesinskief2ea1f2013-12-05 16:48:06 -080033import com.android.server.statusbar.StatusBarManagerService;
satok01038492012-04-09 21:08:27 +090034import com.android.server.wm.WindowManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035
satoke7c6998e2011-06-03 17:57:59 +090036import org.xmlpull.v1.XmlPullParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import org.xmlpull.v1.XmlPullParserException;
satoke7c6998e2011-06-03 17:57:59 +090038import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039
40import android.app.ActivityManagerNative;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090041import android.app.AppGlobals;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.app.AlertDialog;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090043import android.app.IUserSwitchObserver;
satokf90a33e2011-07-19 11:55:52 +090044import android.app.KeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +090045import android.app.Notification;
46import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070047import android.app.PendingIntent;
satok5b927c432012-05-01 20:09:34 +090048import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.content.ComponentName;
50import android.content.ContentResolver;
51import android.content.Context;
52import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.content.DialogInterface.OnCancelListener;
54import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090055import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070057import android.content.pm.ApplicationInfo;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090058import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.content.pm.PackageManager;
60import android.content.pm.ResolveInfo;
61import android.content.pm.ServiceInfo;
Amith Yamasani734983f2014-03-04 16:48:05 -080062import android.content.pm.UserInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070063import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.content.res.Resources;
65import android.content.res.TypedArray;
66import android.database.ContentObserver;
Joe Onorato857fd9b2011-01-27 15:08:35 -080067import android.inputmethodservice.InputMethodService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.os.Binder;
satoke7c6998e2011-06-03 17:57:59 +090069import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.os.Handler;
71import android.os.IBinder;
72import android.os.IInterface;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090073import android.os.IRemoteCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074import android.os.Message;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090075import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076import android.os.Parcel;
77import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080078import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079import android.os.ServiceManager;
80import android.os.SystemClock;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090081import android.os.UserHandle;
Amith Yamasani734983f2014-03-04 16:48:05 -080082import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083import android.provider.Settings;
84import android.text.TextUtils;
satokf9f01002011-05-19 21:31:50 +090085import android.text.style.SuggestionSpan;
Dianne Hackborn39606a02012-07-31 17:54:35 -070086import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +090088import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +090089import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090import android.util.PrintWriterPrinter;
91import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +090092import android.util.Slog;
93import android.util.Xml;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094import android.view.IWindowManager;
Jeff Brownc28867a2013-03-26 15:42:39 -070095import android.view.InputChannel;
Ken Wakasa05dbb652011-08-22 15:22:43 +090096import android.view.LayoutInflater;
97import android.view.View;
98import android.view.ViewGroup;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099import android.view.WindowManager;
satokab751aa2010-09-14 19:17:36 +0900100import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101import android.view.inputmethod.InputBinding;
102import android.view.inputmethod.InputMethod;
103import android.view.inputmethod.InputMethodInfo;
104import android.view.inputmethod.InputMethodManager;
satokab751aa2010-09-14 19:17:36 +0900105import android.view.inputmethod.InputMethodSubtype;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900106import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +0900107import android.widget.CompoundButton;
108import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900109import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +0900110import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900111import android.widget.TextView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112
satoke7c6998e2011-06-03 17:57:59 +0900113import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900115import java.io.FileInputStream;
116import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117import java.io.IOException;
118import java.io.PrintWriter;
119import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900120import java.util.Collections;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121import java.util.HashMap;
122import java.util.List;
satok5b927c432012-05-01 20:09:34 +0900123import java.util.Locale;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124
125/**
126 * This class provides a system service that manages input methods.
127 */
128public class InputMethodManagerService extends IInputMethodManager.Stub
129 implements ServiceConnection, Handler.Callback {
130 static final boolean DEBUG = false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700131 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132
133 static final int MSG_SHOW_IM_PICKER = 1;
satokab751aa2010-09-14 19:17:36 +0900134 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 2;
satok47a44912010-10-06 16:03:58 +0900135 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 3;
satok217f5482010-12-15 05:19:19 +0900136 static final int MSG_SHOW_IM_CONFIG = 4;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 static final int MSG_UNBIND_INPUT = 1000;
139 static final int MSG_BIND_INPUT = 1010;
140 static final int MSG_SHOW_SOFT_INPUT = 1020;
141 static final int MSG_HIDE_SOFT_INPUT = 1030;
142 static final int MSG_ATTACH_TOKEN = 1040;
143 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 static final int MSG_START_INPUT = 2000;
146 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 static final int MSG_UNBIND_METHOD = 3000;
149 static final int MSG_BIND_METHOD = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700150 static final int MSG_SET_ACTIVE = 3020;
Yohei Yukawab7b79072014-03-25 11:02:00 +0900151 static final int MSG_SET_CURSOR_ANCHOR_MONITOR_MODE = 3030;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900152 static final int MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER = 3040;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800153
satok01038492012-04-09 21:08:27 +0900154 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
155
Satoshi Kataokabcacc322013-10-21 17:57:27 -0700156 static final long TIME_TO_RECONNECT = 3 * 1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800157
satokf9f01002011-05-19 21:31:50 +0900158 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
159
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900160 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
satokb6359412011-06-28 17:47:41 +0900161 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900162
satok4e4569d2010-11-19 18:45:53 +0900163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800165 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900167 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 final IWindowManager mIWindowManager;
170 final HandlerCaller mCaller;
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700171 final boolean mHasFeature;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900172 private InputMethodFileManager mFileManager;
satok01038492012-04-09 21:08:27 +0900173 private final HardKeyboardListener mHardKeyboardListener;
174 private final WindowManagerService mWindowManagerService;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800175
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900176 final InputBindResult mNoBinding = new InputBindResult(null, null, null, -1, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 // All known input methods. mMethodMap also serves as the global
179 // lock for this class.
satokd87c2592010-09-29 11:52:06 +0900180 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<InputMethodInfo>();
181 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<String, InputMethodInfo>();
satokf9f01002011-05-19 21:31:50 +0900182 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
183 new LruCache<SuggestionSpan, InputMethodInfo>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Satoshi Kataokad787f692013-10-26 04:44:21 +0900184 private final InputMethodSubtypeSwitchingController mSwitchingController;
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
John Spurlocke0980502013-10-25 11:59:29 -0400315 // Was the keyguard locked when this client became current?
316 private boolean mCurClientInKeyguard;
317
satokab751aa2010-09-14 19:17:36 +0900318 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 * Set to true if our ServiceConnection is currently actively bound to
320 * a service (whether or not we have gotten its IBinder back yet).
321 */
322 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 /**
325 * Set if the client has asked for the input method to be shown.
326 */
327 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 /**
330 * Set if we were explicitly told to show the input method.
331 */
332 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800333
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 /**
335 * Set if we were forced to be shown.
336 */
337 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 /**
340 * Set if we last told the input method to show itself.
341 */
342 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 /**
345 * The Intent used to connect to the current input method.
346 */
347 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 /**
350 * The token we have made for the currently active input method, to
351 * identify it in the future.
352 */
353 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 /**
356 * If non-null, this is the input method service we are currently connected
357 * to.
358 */
359 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 /**
362 * Time that we last initiated a bind to the input method, to determine
363 * if we should try to disconnect and reconnect to it.
364 */
365 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800366
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367 /**
368 * Have we called mCurMethod.bindInput()?
369 */
370 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800371
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 /**
373 * Currently enabled session. Only touched by service thread, not
374 * protected by a lock.
375 */
376 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800377
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378 /**
379 * True if the screen is on. The value is true initially.
380 */
381 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800382
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900383 int mCurUserActionNotificationSequenceNumber = 0;
384
Joe Onorato857fd9b2011-01-27 15:08:35 -0800385 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
386 int mImeWindowVis;
387
Ken Wakasa05dbb652011-08-22 15:22:43 +0900388 private AlertDialog.Builder mDialogBuilder;
389 private AlertDialog mSwitchingDialog;
satok01038492012-04-09 21:08:27 +0900390 private View mSwitchingDialogTitleView;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900391 private InputMethodInfo[] mIms;
392 private int[] mSubtypeIds;
satok5b927c432012-05-01 20:09:34 +0900393 private Locale mLastSystemLocale;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900394 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
395 private final IPackageManager mIPackageManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800396
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 class SettingsObserver extends ContentObserver {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800398 String mLastEnabled = "";
399
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 SettingsObserver(Handler handler) {
401 super(handler);
402 ContentResolver resolver = mContext.getContentResolver();
403 resolver.registerContentObserver(Settings.Secure.getUriFor(
404 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
satokab751aa2010-09-14 19:17:36 +0900405 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokb6109bb2011-02-03 22:24:54 +0900406 Settings.Secure.ENABLED_INPUT_METHODS), false, this);
407 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokab751aa2010-09-14 19:17:36 +0900408 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 @Override public void onChange(boolean selfChange) {
412 synchronized (mMethodMap) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800413 boolean enabledChanged = false;
414 String newEnabled = mSettings.getEnabledInputMethodsStr();
415 if (!mLastEnabled.equals(newEnabled)) {
416 mLastEnabled = newEnabled;
417 enabledChanged = true;
418 }
419 updateFromSettingsLocked(enabledChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 }
421 }
422 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800423
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900424 class ImmsBroadcastReceiver extends android.content.BroadcastReceiver {
425 private void updateActive() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426 // Inform the current client of the change in active status
Dianne Hackborna6e41342012-05-22 16:30:34 -0700427 if (mCurClient != null && mCurClient.client != null) {
428 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
429 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 }
431 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900432
433 @Override
434 public void onReceive(Context context, Intent intent) {
435 final String action = intent.getAction();
436 if (Intent.ACTION_SCREEN_ON.equals(action)) {
437 mScreenOn = true;
438 refreshImeWindowVisibilityLocked();
439 updateActive();
440 return;
441 } else if (Intent.ACTION_SCREEN_OFF.equals(action)) {
442 mScreenOn = false;
443 setImeWindowVisibilityStatusHiddenLocked();
444 updateActive();
445 return;
446 } else if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
447 hideInputMethodMenu();
448 // No need to updateActive
449 return;
Amith Yamasani734983f2014-03-04 16:48:05 -0800450 } else if (Intent.ACTION_USER_ADDED.equals(action)
451 || Intent.ACTION_USER_REMOVED.equals(action)) {
Kenny Guy2a764942014-04-02 13:29:20 +0100452 updateCurrentProfileIds();
Amith Yamasani734983f2014-03-04 16:48:05 -0800453 return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900454 } else {
455 Slog.w(TAG, "Unexpected intent " + intent);
456 }
457 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800459
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800460 class MyPackageMonitor extends PackageMonitor {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900461 private boolean isChangingPackagesOfCurrentUser() {
462 final int userId = getChangingUserId();
463 final boolean retval = userId == mSettings.getCurrentUserId();
464 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900465 if (!retval) {
466 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
467 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900468 }
469 return retval;
470 }
471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800473 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900474 if (!isChangingPackagesOfCurrentUser()) {
475 return false;
476 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900478 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800479 final int N = mMethodList.size();
480 if (curInputMethodId != null) {
481 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800482 InputMethodInfo imi = mMethodList.get(i);
483 if (imi.getId().equals(curInputMethodId)) {
484 for (String pkg : packages) {
485 if (imi.getPackageName().equals(pkg)) {
486 if (!doit) {
487 return true;
488 }
satok723a27e2010-11-11 14:58:11 +0900489 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800490 chooseNewDefaultIMELocked();
491 return true;
492 }
493 }
494 }
495 }
496 }
497 }
498 return false;
499 }
500
501 @Override
502 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900503 if (!isChangingPackagesOfCurrentUser()) {
504 return;
505 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800506 synchronized (mMethodMap) {
507 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900508 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800509 final int N = mMethodList.size();
510 if (curInputMethodId != null) {
511 for (int i=0; i<N; i++) {
512 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900513 final String imiId = imi.getId();
514 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800515 curIm = imi;
516 }
satoke7c6998e2011-06-03 17:57:59 +0900517
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800518 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900519 if (isPackageModified(imi.getPackageName())) {
520 mFileManager.deleteAllInputMethodSubtypes(imiId);
521 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800522 if (change == PACKAGE_TEMPORARY_CHANGE
523 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800524 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800525 + imi.getComponent());
526 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 }
528 }
529 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800530
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900531 buildInputMethodListLocked(
532 mMethodList, mMethodMap, false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800533
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800535
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800536 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800537 int change = isPackageDisappearing(curIm.getPackageName());
538 if (change == PACKAGE_TEMPORARY_CHANGE
539 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800540 ServiceInfo si = null;
541 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900542 si = mIPackageManager.getServiceInfo(
543 curIm.getComponent(), 0, mSettings.getCurrentUserId());
544 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800545 }
546 if (si == null) {
547 // Uh oh, current input method is no longer around!
548 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800549 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
satok15452a42011-10-28 17:58:28 +0900550 setImeWindowVisibilityStatusHiddenLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800551 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800552 changed = true;
553 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800554 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900555 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800556 }
557 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800558 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800559 }
satokab751aa2010-09-14 19:17:36 +0900560
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800561 if (curIm == null) {
562 // We currently don't have a default input method... is
563 // one now available?
564 changed = chooseNewDefaultIMELocked();
565 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800566
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800567 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800568 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 }
570 }
571 }
572 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800573
Jeff Brownc28867a2013-03-26 15:42:39 -0700574 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900575 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -0700576 private final IInputMethod mMethod;
577 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800578
Jeff Brownc28867a2013-03-26 15:42:39 -0700579 MethodCallback(InputMethodManagerService imms, IInputMethod method,
580 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900581 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -0700582 mMethod = method;
583 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800585
satoke7c6998e2011-06-03 17:57:59 +0900586 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -0700587 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -0700588 long ident = Binder.clearCallingIdentity();
589 try {
590 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
591 } finally {
592 Binder.restoreCallingIdentity(ident);
593 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800594 }
595 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800596
satok01038492012-04-09 21:08:27 +0900597 private class HardKeyboardListener
598 implements WindowManagerService.OnHardKeyboardStatusChangeListener {
599 @Override
600 public void onHardKeyboardStatusChange(boolean available, boolean enabled) {
601 mHandler.sendMessage(mHandler.obtainMessage(
602 MSG_HARD_KEYBOARD_SWITCH_CHANGED, available ? 1 : 0, enabled ? 1 : 0));
603 }
604
605 public void handleHardKeyboardStatusChange(boolean available, boolean enabled) {
606 if (DEBUG) {
607 Slog.w(TAG, "HardKeyboardStatusChanged: available = " + available + ", enabled = "
608 + enabled);
609 }
610 synchronized(mMethodMap) {
611 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
612 && mSwitchingDialog.isShowing()) {
613 mSwitchingDialogTitleView.findViewById(
614 com.android.internal.R.id.hard_keyboard_section).setVisibility(
615 available ? View.VISIBLE : View.GONE);
616 }
617 }
618 }
619 }
620
621 public InputMethodManagerService(Context context, WindowManagerService windowManager) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900622 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800624 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800625 mHandler = new Handler(this);
626 mIWindowManager = IWindowManager.Stub.asInterface(
627 ServiceManager.getService(Context.WINDOW_SERVICE));
Mita Yuned218c72012-12-06 17:18:25 -0800628 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900629 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630 public void executeMessage(Message msg) {
631 handleMessage(msg);
632 }
Mita Yuned218c72012-12-06 17:18:25 -0800633 }, true /*asyncHandler*/);
satok01038492012-04-09 21:08:27 +0900634 mWindowManagerService = windowManager;
635 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700636 mHasFeature = context.getPackageManager().hasSystemFeature(
637 PackageManager.FEATURE_INPUT_METHODS);
satok7cfc0ed2011-06-20 21:29:36 +0900638
satok7cfc0ed2011-06-20 21:29:36 +0900639 mImeSwitcherNotification = new Notification();
640 mImeSwitcherNotification.icon = com.android.internal.R.drawable.ic_notification_ime_default;
641 mImeSwitcherNotification.when = 0;
642 mImeSwitcherNotification.flags = Notification.FLAG_ONGOING_EVENT;
643 mImeSwitcherNotification.tickerText = null;
644 mImeSwitcherNotification.defaults = 0; // please be quiet
645 mImeSwitcherNotification.sound = null;
646 mImeSwitcherNotification.vibrate = null;
Daniel Sandler590d5152012-06-14 16:10:13 -0400647
648 // Tag this notification specially so SystemUI knows it's important
John Spurlockfd7f1e02014-03-18 16:41:57 -0400649 mImeSwitcherNotification.extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
650 mImeSwitcherNotification.category = Notification.CATEGORY_SYSTEM;
Daniel Sandler590d5152012-06-14 16:10:13 -0400651
satok7cfc0ed2011-06-20 21:29:36 +0900652 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900653 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900654
655 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900656
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900657 final IntentFilter broadcastFilter = new IntentFilter();
658 broadcastFilter.addAction(Intent.ACTION_SCREEN_ON);
659 broadcastFilter.addAction(Intent.ACTION_SCREEN_OFF);
660 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Amith Yamasani734983f2014-03-04 16:48:05 -0800661 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
662 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900663 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800664
satok7cfc0ed2011-06-20 21:29:36 +0900665 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900666 int userId = 0;
667 try {
668 ActivityManagerNative.getDefault().registerUserSwitchObserver(
669 new IUserSwitchObserver.Stub() {
670 @Override
671 public void onUserSwitching(int newUserId, IRemoteCallback reply) {
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900672 synchronized(mMethodMap) {
673 switchUserLocked(newUserId);
674 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900675 if (reply != null) {
676 try {
677 reply.sendResult(null);
678 } catch (RemoteException e) {
679 }
680 }
681 }
682
683 @Override
684 public void onUserSwitchComplete(int newUserId) throws RemoteException {
685 }
686 });
687 userId = ActivityManagerNative.getDefault().getCurrentUser().id;
688 } catch (RemoteException e) {
689 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
690 }
satok81f8b7c2012-12-04 20:42:56 +0900691 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
satok913a8922010-08-26 21:53:41 +0900692
satokd87c2592010-09-29 11:52:06 +0900693 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900694 mSettings = new InputMethodSettings(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900695 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId);
Kenny Guy2a764942014-04-02 13:29:20 +0100696 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900697 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900698 synchronized (mMethodMap) {
699 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
700 mSettings, context);
701 }
satok0a1bcf42012-05-16 19:26:31 +0900702
703 // Just checking if defaultImiId is empty or not
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900704 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900705 if (DEBUG) {
706 Slog.d(TAG, "Initial default ime = " + defaultImiId);
707 }
satok0a1bcf42012-05-16 19:26:31 +0900708 mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
709
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900710 synchronized (mMethodMap) {
711 buildInputMethodListLocked(mMethodList, mMethodMap,
712 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
713 }
satokd87c2592010-09-29 11:52:06 +0900714 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800715
satok0a1bcf42012-05-16 19:26:31 +0900716 if (!mImeSelectedOnBoot) {
717 Slog.w(TAG, "No IME selected. Choose the most applicable IME.");
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900718 synchronized (mMethodMap) {
719 resetDefaultImeLocked(context);
720 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800721 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800722
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800723 mSettingsObserver = new SettingsObserver(mHandler);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900724 synchronized (mMethodMap) {
725 updateFromSettingsLocked(true);
726 }
satok5b927c432012-05-01 20:09:34 +0900727
728 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME
729 // according to the new system locale.
730 final IntentFilter filter = new IntentFilter();
731 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
732 mContext.registerReceiver(
733 new BroadcastReceiver() {
734 @Override
735 public void onReceive(Context context, Intent intent) {
736 synchronized(mMethodMap) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900737 resetStateIfCurrentLocaleChangedLocked();
satok5b927c432012-05-01 20:09:34 +0900738 }
739 }
740 }, filter);
741 }
742
satok5b927c432012-05-01 20:09:34 +0900743 private void resetDefaultImeLocked(Context context) {
744 // Do not reset the default (current) IME when it is a 3rd-party IME
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900745 if (mCurMethodId != null
746 && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +0900747 return;
748 }
749
750 InputMethodInfo defIm = null;
751 for (InputMethodInfo imi : mMethodList) {
752 if (defIm == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900753 if (InputMethodUtils.isValidSystemDefaultIme(
754 mSystemReady, imi, context)) {
satok5b927c432012-05-01 20:09:34 +0900755 defIm = imi;
756 Slog.i(TAG, "Selected default: " + imi.getId());
757 }
758 }
759 }
760 if (defIm == null && mMethodList.size() > 0) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900761 defIm = InputMethodUtils.getMostApplicableDefaultIME(
762 mSettings.getEnabledInputMethodListLocked());
satok5b927c432012-05-01 20:09:34 +0900763 Slog.i(TAG, "No default found, using " + defIm.getId());
764 }
765 if (defIm != null) {
766 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
767 }
768 }
769
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900770 private void resetAllInternalStateLocked(final boolean updateOnlyWhenLocaleChanged,
771 final boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900772 if (!mSystemReady) {
773 // not system ready
774 return;
775 }
776 final Locale newLocale = mRes.getConfiguration().locale;
777 if (!updateOnlyWhenLocaleChanged
778 || (newLocale != null && !newLocale.equals(mLastSystemLocale))) {
779 if (!updateOnlyWhenLocaleChanged) {
780 hideCurrentInputLocked(0, null);
781 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -0700782 unbindCurrentMethodLocked(true, false);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900783 }
784 if (DEBUG) {
785 Slog.i(TAG, "Locale has been changed to " + newLocale);
786 }
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900787 buildInputMethodListLocked(mMethodList, mMethodMap, resetDefaultEnabledIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900788 if (!updateOnlyWhenLocaleChanged) {
789 final String selectedImiId = mSettings.getSelectedInputMethod();
790 if (TextUtils.isEmpty(selectedImiId)) {
791 // This is the first time of the user switch and
792 // set the current ime to the proper one.
793 resetDefaultImeLocked(mContext);
794 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +0900795 } else {
796 // If the locale is changed, needs to reset the default ime
797 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900798 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800799 updateFromSettingsLocked(true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900800 mLastSystemLocale = newLocale;
801 if (!updateOnlyWhenLocaleChanged) {
802 try {
803 startInputInnerLocked();
804 } catch (RuntimeException e) {
805 Slog.w(TAG, "Unexpected exception", e);
806 }
807 }
808 }
809 }
810
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900811 private void resetStateIfCurrentLocaleChangedLocked() {
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900812 resetAllInternalStateLocked(true /* updateOnlyWhenLocaleChanged */,
813 true /* resetDefaultImeLocked */);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900814 }
815
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900816 private void switchUserLocked(int newUserId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900817 mSettings.setCurrentUserId(newUserId);
Kenny Guy2a764942014-04-02 13:29:20 +0100818 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900819 // InputMethodFileManager should be reset when the user is changed
820 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900821 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +0900822 // For secondary users, the list of enabled IMEs may not have been updated since the
823 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
824 // not be empty even if the IME has been uninstalled by the primary user.
825 // Even in such cases, IMMS works fine because it will find the most applicable
826 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900827 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900828 if (DEBUG) {
829 Slog.d(TAG, "Switch user: " + newUserId + " current ime = " + defaultImiId);
830 }
831 resetAllInternalStateLocked(false /* updateOnlyWhenLocaleChanged */,
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900832 initialUserSwitch /* needsToResetDefaultIme */);
833 if (initialUserSwitch) {
834 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mContext.getPackageManager(),
835 mSettings.getEnabledInputMethodListLocked());
836 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900837 }
838
Kenny Guy2a764942014-04-02 13:29:20 +0100839 void updateCurrentProfileIds() {
840 List<UserInfo> profiles =
841 UserManager.get(mContext).getProfiles(mSettings.getCurrentUserId());
842 int[] currentProfileIds = new int[profiles.size()]; // profiles will not be null
843 for (int i = 0; i < currentProfileIds.length; i++) {
844 currentProfileIds[i] = profiles.get(i).id;
Amith Yamasani734983f2014-03-04 16:48:05 -0800845 }
Kenny Guy2a764942014-04-02 13:29:20 +0100846 mSettings.setCurrentProfileIds(currentProfileIds);
Amith Yamasani734983f2014-03-04 16:48:05 -0800847 }
848
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800849 @Override
850 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
851 throws RemoteException {
852 try {
853 return super.onTransact(code, data, reply, flags);
854 } catch (RuntimeException e) {
855 // The input method manager only throws security exceptions, so let's
856 // log all others.
857 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -0700858 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800859 }
860 throw e;
861 }
862 }
863
Svetoslav Ganova0027152013-06-25 14:59:53 -0700864 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700865 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900866 if (DEBUG) {
867 Slog.d(TAG, "--- systemReady");
868 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700869 if (!mSystemReady) {
870 mSystemReady = true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900871 mKeyguardManager =
872 (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700873 mNotificationManager = (NotificationManager)
874 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
875 mStatusBar = statusBar;
876 statusBar.setIconVisibility("ime", false);
877 updateImeWindowStatusLocked();
satokb858c732011-07-22 19:54:34 +0900878 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
879 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +0900880 if (mShowOngoingImeSwitcherForPhones) {
881 mWindowManagerService.setOnHardKeyboardStatusChangeListener(
882 mHardKeyboardListener);
883 }
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900884 buildInputMethodListLocked(mMethodList, mMethodMap,
885 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satok0a1bcf42012-05-16 19:26:31 +0900886 if (!mImeSelectedOnBoot) {
887 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900888 resetStateIfCurrentLocaleChangedLocked();
889 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(
890 mContext.getPackageManager(),
891 mSettings.getEnabledInputMethodListLocked());
satok0a1bcf42012-05-16 19:26:31 +0900892 }
893 mLastSystemLocale = mRes.getConfiguration().locale;
Dianne Hackborncc278702009-09-02 23:07:23 -0700894 try {
895 startInputInnerLocked();
896 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800897 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700898 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700899 }
900 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800901 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800902
satok15452a42011-10-28 17:58:28 +0900903 private void setImeWindowVisibilityStatusHiddenLocked() {
904 mImeWindowVis = 0;
905 updateImeWindowStatusLocked();
906 }
907
satok3afd6c02011-11-18 08:38:19 +0900908 private void refreshImeWindowVisibilityLocked() {
909 final Configuration conf = mRes.getConfiguration();
910 final boolean haveHardKeyboard = conf.keyboard
911 != Configuration.KEYBOARD_NOKEYS;
912 final boolean hardKeyShown = haveHardKeyboard
913 && conf.hardKeyboardHidden
914 != Configuration.HARDKEYBOARDHIDDEN_YES;
John Spurlocke0980502013-10-25 11:59:29 -0400915
916 final boolean isScreenLocked = isKeyguardLocked();
917 final boolean inputActive = !isScreenLocked && (mInputShown || hardKeyShown);
John Spurlock36439b42013-10-23 16:36:47 -0400918 // We assume the softkeyboard is shown when the input is active as long as the
919 // hard keyboard is not shown.
920 final boolean inputVisible = inputActive && !hardKeyShown;
921 mImeWindowVis = (inputActive ? InputMethodService.IME_ACTIVE : 0)
922 | (inputVisible ? InputMethodService.IME_VISIBLE : 0);
satok3afd6c02011-11-18 08:38:19 +0900923 updateImeWindowStatusLocked();
924 }
925
satok15452a42011-10-28 17:58:28 +0900926 private void updateImeWindowStatusLocked() {
satokdbf29502011-08-25 15:28:23 +0900927 setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700928 }
929
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900930 // ---------------------------------------------------------------------------------------
931 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
932 // 1) it comes from the system process
933 // 2) the calling process' user id is identical to the current user id IMMS thinks.
934 private boolean calledFromValidUser() {
935 final int uid = Binder.getCallingUid();
936 final int userId = UserHandle.getUserId(uid);
937 if (DEBUG) {
938 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
939 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
940 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +0900941 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
942 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900943 }
Kenny Guy2a764942014-04-02 13:29:20 +0100944 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900945 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900946 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +0900947
948 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
949 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
950 // must not manage background users' states in any functions.
951 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
952 // by a token.
953 if (mContext.checkCallingOrSelfPermission(
954 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
955 == PackageManager.PERMISSION_GRANTED) {
956 if (DEBUG) {
957 Slog.d(TAG, "--- Access granted because the calling process has "
958 + "the INTERACT_ACROSS_USERS_FULL permission");
959 }
960 return true;
961 }
Satoshi Kataoka0766eb02013-07-31 18:30:13 +0900962 Slog.w(TAG, "--- IPC called from background users. Ignore. \n"
963 + InputMethodUtils.getStackTrace());
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +0900964 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900965 }
966
Yohei Yukawa22c97be2014-06-04 19:43:36 +0900967
968 /**
969 * Returns true iff the caller is identified to be the current input method with the token.
970 * @param token The window token given to the input method when it was started.
971 * @return true if and only if non-null valid token is specified.
972 */
973 private boolean calledWithValidToken(IBinder token) {
974 if (token == null || mCurToken != token) {
975 return false;
976 }
977 return true;
978 }
979
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900980 private boolean bindCurrentInputMethodService(
981 Intent service, ServiceConnection conn, int flags) {
982 if (service == null || conn == null) {
983 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
984 return false;
985 }
Amith Yamasani27b89e62013-01-16 12:30:11 -0800986 return mContext.bindServiceAsUser(service, conn, flags,
987 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900988 }
989
satoke7c6998e2011-06-03 17:57:59 +0900990 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800991 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900992 // TODO: Make this work even for non-current users?
993 if (!calledFromValidUser()) {
994 return Collections.emptyList();
995 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800996 synchronized (mMethodMap) {
997 return new ArrayList<InputMethodInfo>(mMethodList);
998 }
999 }
1000
satoke7c6998e2011-06-03 17:57:59 +09001001 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001002 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001003 // TODO: Make this work even for non-current users?
1004 if (!calledFromValidUser()) {
1005 return Collections.emptyList();
1006 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001008 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001009 }
1010 }
1011
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001012 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001013 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001014 * @return enabled subtypes of the specified imi
1015 */
satoke7c6998e2011-06-03 17:57:59 +09001016 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001017 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001018 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001019 // TODO: Make this work even for non-current users?
1020 if (!calledFromValidUser()) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001021 return Collections.<InputMethodSubtype>emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001022 }
satok67ddf9c2010-11-17 09:45:54 +09001023 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001024 final InputMethodInfo imi;
1025 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001026 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001027 } else {
1028 imi = mMethodMap.get(imiId);
1029 }
1030 if (imi == null) {
1031 return Collections.<InputMethodSubtype>emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001032 }
1033 return mSettings.getEnabledInputMethodSubtypeListLocked(
1034 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001035 }
1036 }
1037
satoke7c6998e2011-06-03 17:57:59 +09001038 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001039 public void addClient(IInputMethodClient client,
1040 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001041 if (!calledFromValidUser()) {
1042 return;
1043 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001044 synchronized (mMethodMap) {
1045 mClients.put(client.asBinder(), new ClientState(client,
1046 inputContext, uid, pid));
1047 }
1048 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001049
satoke7c6998e2011-06-03 17:57:59 +09001050 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001051 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001052 if (!calledFromValidUser()) {
1053 return;
1054 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001056 ClientState cs = mClients.remove(client.asBinder());
1057 if (cs != null) {
1058 clearClientSessionLocked(cs);
1059 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 }
1061 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001063 void executeOrSendMessage(IInterface target, Message msg) {
1064 if (target.asBinder() instanceof Binder) {
1065 mCaller.sendMessage(msg);
1066 } else {
1067 handleMessage(msg);
1068 msg.recycle();
1069 }
1070 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001071
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001072 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001074 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001075 + mCurClient.client.asBinder());
1076 if (mBoundToMethod) {
1077 mBoundToMethod = false;
1078 if (mCurMethod != null) {
1079 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1080 MSG_UNBIND_INPUT, mCurMethod));
1081 }
1082 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001083
1084 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1085 MSG_SET_ACTIVE, 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001086 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1087 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1088 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001089 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001090
The Android Open Source Project10592532009-03-18 17:39:46 -07001091 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 }
1093 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001094
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095 private int getImeShowFlags() {
1096 int flags = 0;
1097 if (mShowForced) {
1098 flags |= InputMethod.SHOW_FORCED
1099 | InputMethod.SHOW_EXPLICIT;
1100 } else if (mShowExplicitlyRequested) {
1101 flags |= InputMethod.SHOW_EXPLICIT;
1102 }
1103 return flags;
1104 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001106 private int getAppShowFlags() {
1107 int flags = 0;
1108 if (mShowForced) {
1109 flags |= InputMethodManager.SHOW_FORCED;
1110 } else if (!mShowExplicitlyRequested) {
1111 flags |= InputMethodManager.SHOW_IMPLICIT;
1112 }
1113 return flags;
1114 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001115
Dianne Hackborn7663d802012-02-24 13:08:49 -08001116 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001117 if (!mBoundToMethod) {
1118 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1119 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1120 mBoundToMethod = true;
1121 }
1122 final SessionState session = mCurClient.curSession;
1123 if (initial) {
1124 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1125 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
1126 } else {
1127 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1128 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
1129 }
1130 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001131 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001132 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001133 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001134 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001135 (session.channel != null ? session.channel.dup() : null),
1136 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001137 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001139 InputBindResult startInputLocked(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001140 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 // If no method is currently selected, do nothing.
1142 if (mCurMethodId == null) {
1143 return mNoBinding;
1144 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001145
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001146 ClientState cs = mClients.get(client.asBinder());
1147 if (cs == null) {
1148 throw new IllegalArgumentException("unknown client "
1149 + client.asBinder());
1150 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001152 try {
1153 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1154 // Check with the window manager to make sure this client actually
1155 // has a window with focus. If not, reject. This is thread safe
1156 // because if the focus changes some time before or after, the
1157 // next client receiving focus that has any interest in input will
1158 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001159 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1161 return null;
1162 }
1163 } catch (RemoteException e) {
1164 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001165
Dianne Hackborn7663d802012-02-24 13:08:49 -08001166 return startInputUncheckedLocked(cs, inputContext, attribute, controlFlags);
1167 }
1168
1169 InputBindResult startInputUncheckedLocked(ClientState cs,
1170 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
1171 // If no method is currently selected, do nothing.
1172 if (mCurMethodId == null) {
1173 return mNoBinding;
1174 }
1175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001176 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001177 // Was the keyguard locked when switching over to the new client?
1178 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001179 // If the client is changing, we need to switch over to the new
1180 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001181 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001182 if (DEBUG) Slog.v(TAG, "switching to client: client = "
John Spurlocke0980502013-10-25 11:59:29 -04001183 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184
1185 // If the screen is on, inform the new client it is active
1186 if (mScreenOn) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001187 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
1188 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 }
1190 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 // Bump up the sequence for this client and attach it.
1193 mCurSeq++;
1194 if (mCurSeq <= 0) mCurSeq = 1;
1195 mCurClient = cs;
1196 mCurInputContext = inputContext;
1197 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001198
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001199 // Check if the input method is changing.
1200 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1201 if (cs.curSession != null) {
1202 // Fast case: if we are already connected to the input method,
1203 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001204 return attachNewInputLocked(
1205 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206 }
1207 if (mHaveConnection) {
1208 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209 // Return to client, and we will get back with it when
1210 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001211 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001212 return new InputBindResult(null, null, mCurId, mCurSeq,
1213 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 } else if (SystemClock.uptimeMillis()
1215 < (mLastBindTime+TIME_TO_RECONNECT)) {
1216 // In this case we have connected to the service, but
1217 // don't yet have its interface. If it hasn't been too
1218 // long since we did the connection, we'll return to
1219 // the client and wait to get the service interface so
1220 // we can report back. If it has been too long, we want
1221 // to fall through so we can try a disconnect/reconnect
1222 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001223 return new InputBindResult(null, null, mCurId, mCurSeq,
1224 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001225 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001226 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1227 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 }
1229 }
1230 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001231
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001232 return startInputInnerLocked();
1233 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001234
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001235 InputBindResult startInputInnerLocked() {
1236 if (mCurMethodId == null) {
1237 return mNoBinding;
1238 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001239
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001240 if (!mSystemReady) {
1241 // If the system is not yet ready, we shouldn't be running third
1242 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001243 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1244 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001245 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001247 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1248 if (info == null) {
1249 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1250 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001251
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001252 unbindCurrentMethodLocked(false, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001254 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1255 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001256 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1257 com.android.internal.R.string.input_method_binding_label);
1258 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1259 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001260 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001261 | Context.BIND_NOT_VISIBLE | Context.BIND_NOT_FOREGROUND
1262 | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 mLastBindTime = SystemClock.uptimeMillis();
1264 mHaveConnection = true;
1265 mCurId = info.getId();
1266 mCurToken = new Binder();
1267 try {
Craig Mautnerca0ac712013-03-14 09:43:02 -07001268 if (true || DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001269 mIWindowManager.addWindowToken(mCurToken,
1270 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1271 } catch (RemoteException e) {
1272 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001273 return new InputBindResult(null, null, mCurId, mCurSeq,
1274 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001275 } else {
1276 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001277 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001278 + mCurIntent);
1279 }
1280 return null;
1281 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001282
satoke7c6998e2011-06-03 17:57:59 +09001283 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 public InputBindResult startInput(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001285 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001286 if (!calledFromValidUser()) {
1287 return null;
1288 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001289 synchronized (mMethodMap) {
1290 final long ident = Binder.clearCallingIdentity();
1291 try {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001292 return startInputLocked(client, inputContext, attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001293 } finally {
1294 Binder.restoreCallingIdentity(ident);
1295 }
1296 }
1297 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001298
satoke7c6998e2011-06-03 17:57:59 +09001299 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300 public void finishInput(IInputMethodClient client) {
1301 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001302
satoke7c6998e2011-06-03 17:57:59 +09001303 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001304 public void onServiceConnected(ComponentName name, IBinder service) {
1305 synchronized (mMethodMap) {
1306 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1307 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001308 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001309 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001310 unbindCurrentMethodLocked(false, false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001311 return;
1312 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001313 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001314 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1315 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001316 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001317 clearClientSessionLocked(mCurClient);
1318 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001319 }
1320 }
1321 }
1322 }
1323
Jeff Brownc28867a2013-03-26 15:42:39 -07001324 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1325 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326 synchronized (mMethodMap) {
1327 if (mCurMethod != null && method != null
1328 && mCurMethod.asBinder() == method.asBinder()) {
1329 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001330 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001331 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001332 method, session, channel);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001333 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001334 if (res.method != null) {
1335 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
1336 MSG_BIND_METHOD, mCurClient.client, res));
1337 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001338 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001339 }
1340 }
1341 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001342
1343 // Session abandoned. Close its associated input channel.
1344 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001345 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001346
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001347 void unbindCurrentMethodLocked(boolean reportToClient, boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001348 if (mVisibleBound) {
1349 mContext.unbindService(mVisibleConnection);
1350 mVisibleBound = false;
1351 }
1352
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001353 if (mHaveConnection) {
1354 mContext.unbindService(this);
1355 mHaveConnection = false;
1356 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001357
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001358 if (mCurToken != null) {
1359 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001360 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001361 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001362 // The current IME is shown. Hence an IME switch (transition) is happening.
1363 mWindowManagerService.saveLastInputMethodWindowForTransition();
1364 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001365 mIWindowManager.removeWindowToken(mCurToken);
1366 } catch (RemoteException e) {
1367 }
1368 mCurToken = null;
1369 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001370
The Android Open Source Project10592532009-03-18 17:39:46 -07001371 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001372 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001373
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001374 if (reportToClient && mCurClient != null) {
1375 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1376 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1377 }
1378 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001379
1380 void requestClientSessionLocked(ClientState cs) {
1381 if (!cs.sessionRequested) {
1382 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1383 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1384 cs.sessionRequested = true;
1385 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1386 MSG_CREATE_SESSION, mCurMethod, channels[1],
1387 new MethodCallback(this, mCurMethod, channels[0])));
1388 }
1389 }
1390
1391 void clearClientSessionLocked(ClientState cs) {
1392 finishSessionLocked(cs.curSession);
1393 cs.curSession = null;
1394 cs.sessionRequested = false;
1395 }
1396
1397 private void finishSessionLocked(SessionState sessionState) {
1398 if (sessionState != null) {
1399 if (sessionState.session != null) {
1400 try {
1401 sessionState.session.finishSession();
1402 } catch (RemoteException e) {
1403 Slog.w(TAG, "Session failed to close due to remote exception", e);
1404 setImeWindowVisibilityStatusHiddenLocked();
1405 }
1406 sessionState.session = null;
1407 }
1408 if (sessionState.channel != null) {
1409 sessionState.channel.dispose();
1410 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001411 }
1412 }
1413 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001414
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001415 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001416 if (mCurMethod != null) {
1417 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001418 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001419 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001420
Jeff Brownc28867a2013-03-26 15:42:39 -07001421 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001422 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001423 mCurMethod = null;
1424 }
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 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001429
satoke7c6998e2011-06-03 17:57:59 +09001430 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001431 public void onServiceDisconnected(ComponentName name) {
1432 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001433 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001434 + " mCurIntent=" + mCurIntent);
1435 if (mCurMethod != null && mCurIntent != null
1436 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001437 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001438 // We consider this to be a new bind attempt, since the system
1439 // should now try to restart the service for us.
1440 mLastBindTime = SystemClock.uptimeMillis();
1441 mShowRequested = mInputShown;
1442 mInputShown = false;
1443 if (mCurClient != null) {
1444 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1445 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1446 }
1447 }
1448 }
1449 }
1450
satokf9f01002011-05-19 21:31:50 +09001451 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001452 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
1453 long ident = Binder.clearCallingIdentity();
1454 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001455 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09001456 if (!calledWithValidToken(token)) {
1457 final int uid = Binder.getCallingUid();
1458 Slog.e(TAG, "Ignoring updateStatusIcon due to an invalid token. uid:" + uid
1459 + " token:" + token);
1460 return;
1461 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001462 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001463 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001464 if (mStatusBar != null) {
1465 mStatusBar.setIconVisibility("ime", false);
1466 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001467 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001468 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001469 CharSequence contentDescription = null;
1470 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001471 // Use PackageManager to load label
1472 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001473 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001474 mIPackageManager.getApplicationInfo(packageName, 0,
1475 mSettings.getCurrentUserId()));
1476 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001477 /* ignore */
1478 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001479 if (mStatusBar != null) {
1480 mStatusBar.setIcon("ime", packageName, iconId, 0,
1481 contentDescription != null
1482 ? contentDescription.toString() : null);
1483 mStatusBar.setIconVisibility("ime", true);
1484 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001485 }
1486 }
1487 } finally {
1488 Binder.restoreCallingIdentity(ident);
1489 }
1490 }
1491
satok7cfc0ed2011-06-20 21:29:36 +09001492 private boolean needsToShowImeSwitchOngoingNotification() {
1493 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04001494 if (mSwitchingDialog != null) return false;
satok2c93efc2012-04-02 19:33:47 +09001495 if (isScreenLocked()) return false;
satok7cfc0ed2011-06-20 21:29:36 +09001496 synchronized (mMethodMap) {
1497 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1498 final int N = imis.size();
satokb6359412011-06-28 17:47:41 +09001499 if (N > 2) return true;
1500 if (N < 1) return false;
1501 int nonAuxCount = 0;
1502 int auxCount = 0;
1503 InputMethodSubtype nonAuxSubtype = null;
1504 InputMethodSubtype auxSubtype = null;
satok7cfc0ed2011-06-20 21:29:36 +09001505 for(int i = 0; i < N; ++i) {
1506 final InputMethodInfo imi = imis.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001507 final List<InputMethodSubtype> subtypes =
1508 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satok7cfc0ed2011-06-20 21:29:36 +09001509 final int subtypeCount = subtypes.size();
1510 if (subtypeCount == 0) {
satokb6359412011-06-28 17:47:41 +09001511 ++nonAuxCount;
satok7cfc0ed2011-06-20 21:29:36 +09001512 } else {
1513 for (int j = 0; j < subtypeCount; ++j) {
satokb6359412011-06-28 17:47:41 +09001514 final InputMethodSubtype subtype = subtypes.get(j);
1515 if (!subtype.isAuxiliary()) {
1516 ++nonAuxCount;
1517 nonAuxSubtype = subtype;
1518 } else {
1519 ++auxCount;
1520 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001521 }
1522 }
1523 }
satok7cfc0ed2011-06-20 21:29:36 +09001524 }
satokb6359412011-06-28 17:47:41 +09001525 if (nonAuxCount > 1 || auxCount > 1) {
1526 return true;
1527 } else if (nonAuxCount == 1 && auxCount == 1) {
1528 if (nonAuxSubtype != null && auxSubtype != null
satok9747f892011-09-12 15:56:40 +09001529 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1530 || auxSubtype.overridesImplicitlyEnabledSubtype()
1531 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
satokb6359412011-06-28 17:47:41 +09001532 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1533 return false;
1534 }
1535 return true;
1536 }
1537 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001538 }
satok7cfc0ed2011-06-20 21:29:36 +09001539 }
1540
John Spurlocke0980502013-10-25 11:59:29 -04001541 private boolean isKeyguardLocked() {
1542 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1543 }
1544
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001545 // Caution! This method is called in this class. Handle multi-user carefully
satokdbf29502011-08-25 15:28:23 +09001546 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001547 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001548 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001549 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001550 try {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001551 if (!calledWithValidToken(token)) {
1552 final int uid = Binder.getCallingUid();
1553 Slog.e(TAG, "Ignoring setImeWindowStatus due to an invalid token. uid:" + uid
1554 + " token:" + token);
satok06487a52010-10-29 11:37:18 +09001555 return;
1556 }
satok06487a52010-10-29 11:37:18 +09001557 synchronized (mMethodMap) {
John Spurlocke0980502013-10-25 11:59:29 -04001558 // apply policy for binder calls
1559 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
1560 vis = 0;
1561 }
Joe Onorato857fd9b2011-01-27 15:08:35 -08001562 mImeWindowVis = vis;
1563 mBackDisposition = backDisposition;
jungheang.lee217fd292013-02-26 16:53:22 +09001564 final boolean iconVisibility = ((vis & (InputMethodService.IME_ACTIVE)) != 0)
1565 && (mWindowManagerService.isHardKeyboardAvailable()
1566 || (vis & (InputMethodService.IME_VISIBLE)) != 0);
Jason Monkb605fec2014-05-02 17:04:10 -04001567 final boolean needsToShowImeSwitcher = iconVisibility
1568 && needsToShowImeSwitchOngoingNotification();
1569 if (mStatusBar != null) {
1570 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
1571 needsToShowImeSwitcher);
1572 }
satok5bc8e732011-07-22 21:07:23 +09001573 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Jason Monkb605fec2014-05-02 17:04:10 -04001574 if (imi != null && needsToShowImeSwitcher) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001575 // Used to load label
satok7cfc0ed2011-06-20 21:29:36 +09001576 final CharSequence title = mRes.getText(
1577 com.android.internal.R.string.select_input_method);
Satoshi Kataokab2827262013-07-04 19:43:14 +09001578 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
1579 mContext, imi, mCurrentSubtype);
satok5bc8e732011-07-22 21:07:23 +09001580
satok7cfc0ed2011-06-20 21:29:36 +09001581 mImeSwitcherNotification.setLatestEventInfo(
satok5bc8e732011-07-22 21:07:23 +09001582 mContext, title, summary, mImeSwitchPendingIntent);
Jason Monke463c742014-05-02 12:31:45 -04001583 if ((mNotificationManager != null)
1584 && !mWindowManagerService.hasNavigationBar()) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001585 if (DEBUG) {
Satoshi Kataokab2827262013-07-04 19:43:14 +09001586 Slog.d(TAG, "--- show notification: label = " + summary);
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001587 }
1588 mNotificationManager.notifyAsUser(null,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001589 com.android.internal.R.string.select_input_method,
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001590 mImeSwitcherNotification, UserHandle.ALL);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001591 mNotificationShown = true;
1592 }
satok7cfc0ed2011-06-20 21:29:36 +09001593 } else {
Dianne Hackborn661cd522011-08-22 00:26:20 -07001594 if (mNotificationShown && mNotificationManager != null) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001595 if (DEBUG) {
1596 Slog.d(TAG, "--- hide notification");
1597 }
1598 mNotificationManager.cancelAsUser(null,
1599 com.android.internal.R.string.select_input_method, UserHandle.ALL);
satok7cfc0ed2011-06-20 21:29:36 +09001600 mNotificationShown = false;
1601 }
1602 }
satok06487a52010-10-29 11:37:18 +09001603 }
1604 } finally {
1605 Binder.restoreCallingIdentity(ident);
1606 }
1607 }
1608
satoke7c6998e2011-06-03 17:57:59 +09001609 @Override
satokf9f01002011-05-19 21:31:50 +09001610 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001611 if (!calledFromValidUser()) {
1612 return;
1613 }
satokf9f01002011-05-19 21:31:50 +09001614 synchronized (mMethodMap) {
1615 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1616 for (int i = 0; i < spans.length; ++i) {
1617 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001618 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001619 mSecureSuggestionSpans.put(ss, currentImi);
1620 }
1621 }
1622 }
1623 }
1624
satoke7c6998e2011-06-03 17:57:59 +09001625 @Override
satokf9f01002011-05-19 21:31:50 +09001626 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001627 if (!calledFromValidUser()) {
1628 return false;
1629 }
satokf9f01002011-05-19 21:31:50 +09001630 synchronized (mMethodMap) {
1631 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1632 // TODO: Do not send the intent if the process of the targetImi is already dead.
1633 if (targetImi != null) {
1634 final String[] suggestions = span.getSuggestions();
1635 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001636 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001637 final Intent intent = new Intent();
1638 // Ensures that only a class in the original IME package will receive the
1639 // notification.
satok42c5a162011-05-26 16:46:14 +09001640 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001641 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1642 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1643 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1644 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001645 final long ident = Binder.clearCallingIdentity();
1646 try {
1647 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1648 } finally {
1649 Binder.restoreCallingIdentity(ident);
1650 }
satokf9f01002011-05-19 21:31:50 +09001651 return true;
1652 }
1653 }
1654 return false;
1655 }
1656
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001657 void updateFromSettingsLocked(boolean enabledMayChange) {
1658 if (enabledMayChange) {
1659 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1660 for (int i=0; i<enabled.size(); i++) {
1661 // We allow the user to select "disabled until used" apps, so if they
1662 // are enabling one of those here we now need to make it enabled.
1663 InputMethodInfo imm = enabled.get(i);
1664 try {
1665 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1666 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1667 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09001668 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001669 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001670 if (DEBUG) {
1671 Slog.d(TAG, "Update state(" + imm.getId()
1672 + "): DISABLED_UNTIL_USED -> DEFAULT");
1673 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001674 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1675 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001676 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
1677 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001678 }
1679 } catch (RemoteException e) {
1680 }
1681 }
1682 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001683 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1684 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1685 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1686 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001687 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001688 // There is no input method selected, try to choose new applicable input method.
1689 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001690 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001691 }
1692 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001693 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001694 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001695 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001696 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001697 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001698 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001699 }
satokf3db1af2010-11-23 13:34:33 +09001700 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001701 } else {
1702 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001703 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001704 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001705 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09001706 // Here is not the perfect place to reset the switching controller. Ideally
1707 // mSwitchingController and mSettings should be able to share the same state.
1708 // TODO: Make sure that mSwitchingController and mSettings are sharing the
1709 // the same enabled IMEs list.
1710 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001711 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001712
satokab751aa2010-09-14 19:17:36 +09001713 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001714 InputMethodInfo info = mMethodMap.get(id);
1715 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001716 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001717 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001718
satokd81e9502012-05-21 12:58:45 +09001719 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001720 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09001721 final int subtypeCount = info.getSubtypeCount();
1722 if (subtypeCount <= 0) {
1723 return;
satokcd7cd292010-11-20 15:46:23 +09001724 }
satokd81e9502012-05-21 12:58:45 +09001725 final InputMethodSubtype oldSubtype = mCurrentSubtype;
1726 final InputMethodSubtype newSubtype;
1727 if (subtypeId >= 0 && subtypeId < subtypeCount) {
1728 newSubtype = info.getSubtypeAt(subtypeId);
1729 } else {
1730 // If subtype is null, try to find the most applicable one from
1731 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001732 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09001733 }
1734 if (newSubtype == null || oldSubtype == null) {
1735 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
1736 + ", new subtype = " + newSubtype);
1737 return;
1738 }
1739 if (newSubtype != oldSubtype) {
1740 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1741 if (mCurMethod != null) {
1742 try {
1743 refreshImeWindowVisibilityLocked();
1744 mCurMethod.changeInputMethodSubtype(newSubtype);
1745 } catch (RemoteException e) {
1746 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
satokab751aa2010-09-14 19:17:36 +09001747 }
1748 }
1749 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001750 return;
1751 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001752
satokd81e9502012-05-21 12:58:45 +09001753 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001754 final long ident = Binder.clearCallingIdentity();
1755 try {
satokab751aa2010-09-14 19:17:36 +09001756 // Set a subtype to this input method.
1757 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001758 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1759 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1760 // because mCurMethodId is stored as a history in
1761 // setSelectedInputMethodAndSubtypeLocked().
1762 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001763
1764 if (ActivityManagerNative.isSystemReady()) {
1765 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001766 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001767 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07001768 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001769 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001770 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001771 } finally {
1772 Binder.restoreCallingIdentity(ident);
1773 }
1774 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001775
satok42c5a162011-05-26 16:46:14 +09001776 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001777 public boolean showSoftInput(IInputMethodClient client, int flags,
1778 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001779 if (!calledFromValidUser()) {
1780 return false;
1781 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001782 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001783 long ident = Binder.clearCallingIdentity();
1784 try {
1785 synchronized (mMethodMap) {
1786 if (mCurClient == null || client == null
1787 || mCurClient.client.asBinder() != client.asBinder()) {
1788 try {
1789 // We need to check if this is the current client with
1790 // focus in the window manager, to allow this call to
1791 // be made before input is started in it.
1792 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001793 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001794 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001795 }
1796 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001797 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001798 }
1799 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001800
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001801 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001802 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001803 }
1804 } finally {
1805 Binder.restoreCallingIdentity(ident);
1806 }
1807 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001808
The Android Open Source Project4df24232009-03-05 14:34:35 -08001809 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001810 mShowRequested = true;
1811 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1812 mShowExplicitlyRequested = true;
1813 }
1814 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1815 mShowExplicitlyRequested = true;
1816 mShowForced = true;
1817 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001818
Dianne Hackborncc278702009-09-02 23:07:23 -07001819 if (!mSystemReady) {
1820 return false;
1821 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001822
The Android Open Source Project4df24232009-03-05 14:34:35 -08001823 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001824 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001825 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001826 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1827 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1828 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001829 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001830 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001831 bindCurrentInputMethodService(
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001832 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE
1833 | Context.BIND_TREAT_LIKE_ACTIVITY);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001834 mVisibleBound = true;
1835 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001836 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001837 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09001838 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001839 // The client has asked to have the input method shown, but
1840 // we have been sitting here too long with a connection to the
1841 // service and no interface received, so let's disconnect/connect
1842 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001843 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001844 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09001845 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001846 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001847 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001848 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001849 } else {
1850 if (DEBUG) {
1851 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
1852 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
1853 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001854 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001855
The Android Open Source Project4df24232009-03-05 14:34:35 -08001856 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001857 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001858
satok42c5a162011-05-26 16:46:14 +09001859 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001860 public boolean hideSoftInput(IInputMethodClient client, int flags,
1861 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001862 if (!calledFromValidUser()) {
1863 return false;
1864 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001865 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001866 long ident = Binder.clearCallingIdentity();
1867 try {
1868 synchronized (mMethodMap) {
1869 if (mCurClient == null || client == null
1870 || mCurClient.client.asBinder() != client.asBinder()) {
1871 try {
1872 // We need to check if this is the current client with
1873 // focus in the window manager, to allow this call to
1874 // be made before input is started in it.
1875 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001876 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1877 + uid + ": " + client);
satok15452a42011-10-28 17:58:28 +09001878 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001879 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001880 }
1881 } catch (RemoteException e) {
satok15452a42011-10-28 17:58:28 +09001882 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001883 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001884 }
1885 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001886
Joe Onorato8a9b2202010-02-26 18:56:32 -08001887 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001888 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001889 }
1890 } finally {
1891 Binder.restoreCallingIdentity(ident);
1892 }
1893 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001894
The Android Open Source Project4df24232009-03-05 14:34:35 -08001895 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001896 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1897 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001898 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 -08001899 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001900 }
1901 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001902 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 -08001903 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001904 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001905 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001906 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001907 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1908 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1909 res = true;
1910 } else {
1911 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001912 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001913 if (mHaveConnection && mVisibleBound) {
1914 mContext.unbindService(mVisibleConnection);
1915 mVisibleBound = false;
1916 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001917 mInputShown = false;
1918 mShowRequested = false;
1919 mShowExplicitlyRequested = false;
1920 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001921 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001922 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001923
satok42c5a162011-05-26 16:46:14 +09001924 @Override
Dianne Hackborn7663d802012-02-24 13:08:49 -08001925 public InputBindResult windowGainedFocus(IInputMethodClient client, IBinder windowToken,
1926 int controlFlags, int softInputMode, int windowFlags,
1927 EditorInfo attribute, IInputContext inputContext) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09001928 // Needs to check the validity before clearing calling identity
1929 final boolean calledFromValidUser = calledFromValidUser();
1930
Dianne Hackborn7663d802012-02-24 13:08:49 -08001931 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001932 long ident = Binder.clearCallingIdentity();
1933 try {
1934 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001935 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
Dianne Hackborn7663d802012-02-24 13:08:49 -08001936 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001937 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08001938 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001939
Dianne Hackborn7663d802012-02-24 13:08:49 -08001940 ClientState cs = mClients.get(client.asBinder());
1941 if (cs == null) {
1942 throw new IllegalArgumentException("unknown client "
1943 + client.asBinder());
1944 }
1945
1946 try {
1947 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1948 // Check with the window manager to make sure this client actually
1949 // has a window with focus. If not, reject. This is thread safe
1950 // because if the focus changes some time before or after, the
1951 // next client receiving focus that has any interest in input will
1952 // be calling through here after that change happens.
1953 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
1954 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1955 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001956 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001957 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001958 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001959
Satoshi Kataoka8d033052012-11-19 17:30:40 +09001960 if (!calledFromValidUser) {
1961 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
1962 Slog.w(TAG, "If you want to interect with IME, you need "
1963 + "android.permission.INTERACT_ACROSS_USERS_FULL");
1964 hideCurrentInputLocked(0, null);
1965 return null;
1966 }
1967
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001968 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07001969 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09001970 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001971 if (attribute != null) {
1972 return startInputUncheckedLocked(cs, inputContext, attribute,
1973 controlFlags);
1974 }
1975 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001976 }
1977 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001978
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001979 // Should we auto-show the IME even if the caller has not
1980 // specified what should be done with it?
1981 // We only do this automatically if the window can resize
1982 // to accommodate the IME (so what the user sees will give
1983 // them good context without input information being obscured
1984 // by the IME) or if running on a large screen where there
1985 // is more room for the target window + IME.
1986 final boolean doAutoShow =
1987 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1988 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1989 || mRes.getConfiguration().isLayoutSizeAtLeast(
1990 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001991 final boolean isTextEditor =
1992 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
1993
1994 // We want to start input before showing the IME, but after closing
1995 // it. We want to do this after closing it to help the IME disappear
1996 // more quickly (not get stuck behind it initializing itself for the
1997 // new focused input, even if its window wants to hide the IME).
1998 boolean didStart = false;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001999
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002000 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2001 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002002 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002003 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2004 // There is no focus view, and this window will
2005 // be behind any soft input window, so hide the
2006 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002007 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002008 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002009 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002010 } else if (isTextEditor && doAutoShow && (softInputMode &
2011 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002012 // There is a focus view, and we are navigating forward
2013 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002014 // We only do this automatically if the window can resize
2015 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002016 // them good context without input information being obscured
2017 // by the IME) or if running on a large screen where there
2018 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002019 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002020 if (attribute != null) {
2021 res = startInputUncheckedLocked(cs, inputContext, attribute,
2022 controlFlags);
2023 didStart = true;
2024 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002025 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002026 }
2027 break;
2028 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2029 // Do nothing.
2030 break;
2031 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2032 if ((softInputMode &
2033 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002034 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002035 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002036 }
2037 break;
2038 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002039 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002040 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002041 break;
2042 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2043 if ((softInputMode &
2044 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002045 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002046 if (attribute != null) {
2047 res = startInputUncheckedLocked(cs, inputContext, attribute,
2048 controlFlags);
2049 didStart = true;
2050 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002051 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002052 }
2053 break;
2054 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002055 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002056 if (attribute != null) {
2057 res = startInputUncheckedLocked(cs, inputContext, attribute,
2058 controlFlags);
2059 didStart = true;
2060 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002061 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002062 break;
2063 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002064
2065 if (!didStart && attribute != null) {
2066 res = startInputUncheckedLocked(cs, inputContext, attribute,
2067 controlFlags);
2068 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002069 }
2070 } finally {
2071 Binder.restoreCallingIdentity(ident);
2072 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002073
2074 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002075 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002076
satok42c5a162011-05-26 16:46:14 +09002077 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002078 public void showInputMethodPickerFromClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002079 if (!calledFromValidUser()) {
2080 return;
2081 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002082 synchronized (mMethodMap) {
2083 if (mCurClient == null || client == null
2084 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002085 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002086 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002087 }
2088
satok440aab52010-11-25 09:43:11 +09002089 // Always call subtype picker, because subtype picker is a superset of input method
2090 // picker.
satokab751aa2010-09-14 19:17:36 +09002091 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
2092 }
2093 }
2094
satok42c5a162011-05-26 16:46:14 +09002095 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002096 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002097 if (!calledFromValidUser()) {
2098 return;
2099 }
satok28203512010-11-24 11:06:49 +09002100 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2101 }
2102
satok42c5a162011-05-26 16:46:14 +09002103 @Override
satok28203512010-11-24 11:06:49 +09002104 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002105 if (!calledFromValidUser()) {
2106 return;
2107 }
satok28203512010-11-24 11:06:49 +09002108 synchronized (mMethodMap) {
2109 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002110 setInputMethodWithSubtypeIdLocked(token, id,
2111 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2112 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002113 } else {
2114 setInputMethod(token, id);
2115 }
2116 }
satokab751aa2010-09-14 19:17:36 +09002117 }
2118
satok42c5a162011-05-26 16:46:14 +09002119 @Override
satokb416a712010-11-25 20:42:14 +09002120 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002121 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002122 if (!calledFromValidUser()) {
2123 return;
2124 }
satokb416a712010-11-25 20:42:14 +09002125 synchronized (mMethodMap) {
2126 if (mCurClient == null || client == null
2127 || mCurClient.client.asBinder() != client.asBinder()) {
2128 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
2129 }
satok7fee71f2010-12-17 18:54:26 +09002130 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2131 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002132 }
2133 }
2134
satok4fc87d62011-05-20 16:13:43 +09002135 @Override
satok735cf382010-11-11 20:40:09 +09002136 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002137 if (!calledFromValidUser()) {
2138 return false;
2139 }
satok735cf382010-11-11 20:40:09 +09002140 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002141 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002142 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002143 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002144 lastImi = mMethodMap.get(lastIme.first);
2145 } else {
2146 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002147 }
satok4fc87d62011-05-20 16:13:43 +09002148 String targetLastImiId = null;
2149 int subtypeId = NOT_A_SUBTYPE_ID;
2150 if (lastIme != null && lastImi != null) {
2151 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
2152 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
2153 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2154 : mCurrentSubtype.hashCode();
2155 // If the last IME is the same as the current IME and the last subtype is not
2156 // defined, there is no need to switch to the last IME.
2157 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2158 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002159 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002160 }
2161 }
2162
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002163 if (TextUtils.isEmpty(targetLastImiId)
2164 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002165 // This is a safety net. If the currentSubtype can't be added to the history
2166 // and the framework couldn't find the last ime, we will make the last ime be
2167 // the most applicable enabled keyboard subtype of the system imes.
2168 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2169 if (enabled != null) {
2170 final int N = enabled.size();
2171 final String locale = mCurrentSubtype == null
2172 ? mRes.getConfiguration().locale.toString()
2173 : mCurrentSubtype.getLocale();
2174 for (int i = 0; i < N; ++i) {
2175 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002176 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002177 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002178 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2179 InputMethodUtils.getSubtypes(imi),
2180 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002181 if (keyboardSubtype != null) {
2182 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002183 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002184 imi, keyboardSubtype.hashCode());
2185 if(keyboardSubtype.getLocale().equals(locale)) {
2186 break;
2187 }
2188 }
2189 }
2190 }
2191 }
2192 }
2193
2194 if (!TextUtils.isEmpty(targetLastImiId)) {
2195 if (DEBUG) {
2196 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2197 + ", from: " + mCurMethodId + ", " + subtypeId);
2198 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002199 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002200 return true;
2201 } else {
2202 return false;
2203 }
satok735cf382010-11-11 20:40:09 +09002204 }
2205 }
2206
satoke7c6998e2011-06-03 17:57:59 +09002207 @Override
satok688bd472012-02-09 20:09:17 +09002208 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002209 if (!calledFromValidUser()) {
2210 return false;
2211 }
satok688bd472012-02-09 20:09:17 +09002212 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002213 if (!calledWithValidToken(token)) {
2214 final int uid = Binder.getCallingUid();
2215 Slog.e(TAG, "Ignoring switchToNextInputMethod due to an invalid token. uid:" + uid
2216 + " token:" + token);
2217 return false;
2218 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002219 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
satok688bd472012-02-09 20:09:17 +09002220 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2221 if (nextSubtype == null) {
2222 return false;
2223 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002224 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2225 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002226 return true;
2227 }
2228 }
2229
2230 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002231 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2232 if (!calledFromValidUser()) {
2233 return false;
2234 }
2235 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002236 if (!calledWithValidToken(token)) {
2237 final int uid = Binder.getCallingUid();
2238 Slog.e(TAG, "Ignoring shouldOfferSwitchingToNextInputMethod due to an invalid "
2239 + "token. uid:" + uid + " token:" + token);
2240 return false;
2241 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002242 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002243 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2244 if (nextSubtype == null) {
2245 return false;
2246 }
2247 return true;
2248 }
2249 }
2250
2251 @Override
satok68f1b782011-04-11 14:26:04 +09002252 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002253 if (!calledFromValidUser()) {
2254 return null;
2255 }
satok68f1b782011-04-11 14:26:04 +09002256 synchronized (mMethodMap) {
2257 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2258 // TODO: Handle the case of the last IME with no subtypes
2259 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2260 || TextUtils.isEmpty(lastIme.second)) return null;
2261 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2262 if (lastImi == null) return null;
2263 try {
2264 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002265 final int lastSubtypeId =
2266 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002267 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2268 return null;
2269 }
2270 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002271 } catch (NumberFormatException e) {
2272 return null;
2273 }
2274 }
2275 }
2276
satoke7c6998e2011-06-03 17:57:59 +09002277 @Override
satokee5e77c2011-09-02 18:50:15 +09002278 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002279 if (!calledFromValidUser()) {
2280 return;
2281 }
satok91e88122011-07-18 11:11:42 +09002282 // By this IPC call, only a process which shares the same uid with the IME can add
2283 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09002284 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09002285 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002286 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002287 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002288 final String[] packageInfos;
2289 try {
2290 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2291 } catch (RemoteException e) {
2292 Slog.e(TAG, "Failed to get package infos");
2293 return;
2294 }
satok91e88122011-07-18 11:11:42 +09002295 if (packageInfos != null) {
2296 final int packageNum = packageInfos.length;
2297 for (int i = 0; i < packageNum; ++i) {
2298 if (packageInfos[i].equals(imi.getPackageName())) {
2299 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002300 final long ident = Binder.clearCallingIdentity();
2301 try {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002302 buildInputMethodListLocked(mMethodList, mMethodMap,
2303 false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002304 } finally {
2305 Binder.restoreCallingIdentity(ident);
2306 }
satokee5e77c2011-09-02 18:50:15 +09002307 return;
satok91e88122011-07-18 11:11:42 +09002308 }
2309 }
2310 }
satoke7c6998e2011-06-03 17:57:59 +09002311 }
satokee5e77c2011-09-02 18:50:15 +09002312 return;
satoke7c6998e2011-06-03 17:57:59 +09002313 }
2314
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002315 @Override
2316 public int getInputMethodWindowVisibleHeight() {
2317 return mWindowManagerService.getInputMethodWindowVisibleHeight();
2318 }
2319
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002320 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002321 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002322 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002323 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002324 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002325 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002326 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
2327 if (DEBUG) {
2328 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
2329 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
2330 + " actual: " + sequenceNumber);
2331 }
2332 return;
2333 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002334 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2335 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09002336 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002337 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002338 }
2339 }
2340
Yohei Yukawab7b79072014-03-25 11:02:00 +09002341 @Override
2342 public void setCursorAnchorMonitorMode(IBinder token, int monitorMode) {
2343 if (DEBUG) {
2344 Slog.d(TAG, "setCursorAnchorMonitorMode: monitorMode=" + monitorMode);
2345 }
2346 if (!calledFromValidUser()) {
2347 return;
2348 }
2349 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002350 if (!calledWithValidToken(token)) {
2351 final int uid = Binder.getCallingUid();
2352 Slog.e(TAG, "Ignoring setCursorAnchorMonitorMode due to an invalid token. uid:"
2353 + uid + " token:" + token);
Yohei Yukawab7b79072014-03-25 11:02:00 +09002354 return;
2355 }
2356 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIO(
2357 MSG_SET_CURSOR_ANCHOR_MONITOR_MODE, monitorMode, mCurClient));
2358 }
2359 }
2360
satok28203512010-11-24 11:06:49 +09002361 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002362 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002363 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
2364 }
2365 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002366
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002367 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
2368 if (token == null) {
2369 if (mContext.checkCallingOrSelfPermission(
2370 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2371 != PackageManager.PERMISSION_GRANTED) {
2372 throw new SecurityException(
2373 "Using null token requires permission "
2374 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002375 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002376 } else if (mCurToken != token) {
2377 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2378 + " token: " + token);
2379 return;
2380 }
2381
2382 final long ident = Binder.clearCallingIdentity();
2383 try {
2384 setInputMethodLocked(id, subtypeId);
2385 } finally {
2386 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002387 }
2388 }
2389
satok42c5a162011-05-26 16:46:14 +09002390 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002391 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002392 if (!calledFromValidUser()) {
2393 return;
2394 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002395 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002396 if (!calledWithValidToken(token)) {
2397 final int uid = Binder.getCallingUid();
2398 Slog.e(TAG, "Ignoring hideInputMethod due to an invalid token. uid:"
2399 + uid + " token:" + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002400 return;
2401 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002402 long ident = Binder.clearCallingIdentity();
2403 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002404 hideCurrentInputLocked(flags, null);
2405 } finally {
2406 Binder.restoreCallingIdentity(ident);
2407 }
2408 }
2409 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002410
satok42c5a162011-05-26 16:46:14 +09002411 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002412 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002413 if (!calledFromValidUser()) {
2414 return;
2415 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002416 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002417 if (!calledWithValidToken(token)) {
2418 final int uid = Binder.getCallingUid();
2419 Slog.e(TAG, "Ignoring showMySoftInput due to an invalid token. uid:"
2420 + uid + " token:" + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002421 return;
2422 }
2423 long ident = Binder.clearCallingIdentity();
2424 try {
2425 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002426 } finally {
2427 Binder.restoreCallingIdentity(ident);
2428 }
2429 }
2430 }
2431
2432 void setEnabledSessionInMainThread(SessionState session) {
2433 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002434 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002435 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002436 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002437 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002438 } catch (RemoteException e) {
2439 }
2440 }
2441 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002442 if (mEnabledSession != null && mEnabledSession.session != null) {
2443 try {
2444 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
2445 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
2446 } catch (RemoteException e) {
2447 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002448 }
2449 }
2450 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002451
satok42c5a162011-05-26 16:46:14 +09002452 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002453 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002454 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002455 switch (msg.what) {
2456 case MSG_SHOW_IM_PICKER:
2457 showInputMethodMenu();
2458 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002459
satokab751aa2010-09-14 19:17:36 +09002460 case MSG_SHOW_IM_SUBTYPE_PICKER:
2461 showInputMethodSubtypeMenu();
2462 return true;
2463
satok47a44912010-10-06 16:03:58 +09002464 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002465 args = (SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09002466 showInputMethodAndSubtypeEnabler((String)args.arg1);
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002467 args.recycle();
satok217f5482010-12-15 05:19:19 +09002468 return true;
2469
2470 case MSG_SHOW_IM_CONFIG:
2471 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002472 return true;
2473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002474 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002475
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002476 case MSG_UNBIND_INPUT:
2477 try {
2478 ((IInputMethod)msg.obj).unbindInput();
2479 } catch (RemoteException e) {
2480 // There is nothing interesting about the method dying.
2481 }
2482 return true;
2483 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002484 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002485 try {
2486 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2487 } catch (RemoteException e) {
2488 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002489 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002490 return true;
2491 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002492 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002493 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002494 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002495 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002496 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002497 } catch (RemoteException e) {
2498 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002499 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002500 return true;
2501 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002502 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002503 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002504 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002505 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002506 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002507 } catch (RemoteException e) {
2508 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002509 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002510 return true;
2511 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002512 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002513 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002514 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002515 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2516 } catch (RemoteException e) {
2517 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002518 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002519 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002520 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002521 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002522 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002523 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002524 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002525 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002526 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002527 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002528 // Dispose the channel if the input method is not local to this process
2529 // because the remote proxy will get its own copy when unparceled.
2530 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002531 channel.dispose();
2532 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002533 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002534 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002535 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002536 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002537 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002538
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002539 case MSG_START_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002540 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002541 try {
2542 SessionState session = (SessionState)args.arg1;
2543 setEnabledSessionInMainThread(session);
2544 session.method.startInput((IInputContext)args.arg2,
2545 (EditorInfo)args.arg3);
2546 } catch (RemoteException e) {
2547 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002548 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002549 return true;
2550 case MSG_RESTART_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002551 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002552 try {
2553 SessionState session = (SessionState)args.arg1;
2554 setEnabledSessionInMainThread(session);
2555 session.method.restartInput((IInputContext)args.arg2,
2556 (EditorInfo)args.arg3);
2557 } catch (RemoteException e) {
2558 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002559 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002560 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002561
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002562 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002563
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002564 case MSG_UNBIND_METHOD:
2565 try {
2566 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
2567 } catch (RemoteException e) {
2568 // There is nothing interesting about the last client dying.
2569 }
2570 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002571 case MSG_BIND_METHOD: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002572 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002573 IInputMethodClient client = (IInputMethodClient)args.arg1;
2574 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002575 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002576 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002577 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002578 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002579 } finally {
2580 // Dispose the channel if the input method is not local to this process
2581 // because the remote proxy will get its own copy when unparceled.
2582 if (res.channel != null && Binder.isProxy(client)) {
2583 res.channel.dispose();
2584 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002585 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002586 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002587 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002588 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002589 case MSG_SET_ACTIVE:
2590 try {
2591 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2592 } catch (RemoteException e) {
2593 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2594 + ((ClientState)msg.obj).pid + " uid "
2595 + ((ClientState)msg.obj).uid);
2596 }
2597 return true;
Yohei Yukawab7b79072014-03-25 11:02:00 +09002598 case MSG_SET_CURSOR_ANCHOR_MONITOR_MODE:
2599 try {
2600 ((ClientState)msg.obj).client.setCursorAnchorMonitorMode(msg.arg1);
2601 } catch (RemoteException e) {
2602 Slog.w(TAG, "Got RemoteException sending setCursorAnchorMonitorMode "
2603 + "notification to pid " + ((ClientState)msg.obj).pid
2604 + " uid " + ((ClientState)msg.obj).uid);
2605 }
2606 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002607 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
2608 final int sequenceNumber = msg.arg1;
2609 final IInputMethodClient client = (IInputMethodClient)msg.obj;
2610 try {
2611 client.setUserActionNotificationSequenceNumber(sequenceNumber);
2612 } catch (RemoteException e) {
2613 Slog.w(TAG, "Got RemoteException sending "
2614 + "setUserActionNotificationSequenceNumber("
2615 + sequenceNumber + ") notification to pid "
2616 + ((ClientState)msg.obj).pid + " uid "
2617 + ((ClientState)msg.obj).uid);
2618 }
2619 return true;
2620 }
satok01038492012-04-09 21:08:27 +09002621
2622 // --------------------------------------------------------------
2623 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
2624 mHardKeyboardListener.handleHardKeyboardStatusChange(
2625 msg.arg1 == 1, msg.arg2 == 1);
2626 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002627 }
2628 return false;
2629 }
2630
satokdc9ddae2011-10-06 12:22:36 +09002631 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002632 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
2633 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09002634 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002635 if (DEBUG) {
2636 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2637 }
satok723a27e2010-11-11 14:58:11 +09002638 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002639 return true;
2640 }
2641
2642 return false;
2643 }
2644
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002645 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002646 HashMap<String, InputMethodInfo> map, boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002647 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002648 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Satoshi Kataoka0766eb02013-07-31 18:30:13 +09002649 + " \n ------ \n" + InputMethodUtils.getStackTrace());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002650 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002651 list.clear();
2652 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002653
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002654 // Use for queryIntentServicesAsUser
2655 final PackageManager pm = mContext.getPackageManager();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002656 String disabledSysImes = mSettings.getDisabledSystemInputMethods();
Amith Yamasanie861ec12010-03-24 21:39:27 -07002657 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002658
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002659 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002660 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002661 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2662 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002663
satoke7c6998e2011-06-03 17:57:59 +09002664 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2665 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002666 for (int i = 0; i < services.size(); ++i) {
2667 ResolveInfo ri = services.get(i);
2668 ServiceInfo si = ri.serviceInfo;
2669 ComponentName compName = new ComponentName(si.packageName, si.name);
2670 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2671 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002672 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002673 + ": it does not require the permission "
2674 + android.Manifest.permission.BIND_INPUT_METHOD);
2675 continue;
2676 }
2677
Joe Onorato8a9b2202010-02-26 18:56:32 -08002678 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002679
2680 try {
satoke7c6998e2011-06-03 17:57:59 +09002681 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002682 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002683 final String id = p.getId();
2684 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002685
2686 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002687 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002688 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002689
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002690 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002691 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002692 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002693 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002694 }
2695 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002696
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002697 if (resetDefaultEnabledIme) {
2698 final ArrayList<InputMethodInfo> defaultEnabledIme =
2699 InputMethodUtils.getDefaultEnabledImes(mContext, mSystemReady, list);
2700 for (int i = 0; i < defaultEnabledIme.size(); ++i) {
2701 final InputMethodInfo imi = defaultEnabledIme.get(i);
2702 if (DEBUG) {
2703 Slog.d(TAG, "--- enable ime = " + imi);
2704 }
2705 setInputMethodEnabledLocked(imi.getId(), true);
2706 }
2707 }
2708
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002709 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09002710 if (!TextUtils.isEmpty(defaultImiId)) {
2711 if (!map.containsKey(defaultImiId)) {
2712 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
2713 if (chooseNewDefaultIMELocked()) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002714 updateFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09002715 }
2716 } else {
2717 // Double check that the default IME is certainly enabled.
2718 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002719 }
2720 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09002721 // Here is not the perfect place to reset the switching controller. Ideally
2722 // mSwitchingController and mSettings should be able to share the same state.
2723 // TODO: Make sure that mSwitchingController and mSettings are sharing the
2724 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09002725 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002726 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002727
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002728 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002729
satokab751aa2010-09-14 19:17:36 +09002730 private void showInputMethodMenu() {
2731 showInputMethodMenuInternal(false);
2732 }
2733
2734 private void showInputMethodSubtypeMenu() {
2735 showInputMethodMenuInternal(true);
2736 }
2737
satok217f5482010-12-15 05:19:19 +09002738 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002739 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002740 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002741 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2742 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002743 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002744 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002745 }
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002746 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok217f5482010-12-15 05:19:19 +09002747 }
2748
2749 private void showConfigureInputMethods() {
2750 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2751 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2752 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2753 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002754 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09002755 }
2756
satok2c93efc2012-04-02 19:33:47 +09002757 private boolean isScreenLocked() {
2758 return mKeyguardManager != null
2759 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
2760 }
satokab751aa2010-09-14 19:17:36 +09002761 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002762 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002763
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002764 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09002765 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002766
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002767 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002768 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002769 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002770
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002771 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002772 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09002773 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
2774 mContext);
satok7f35c8c2010-10-07 21:13:11 +09002775 if (immis == null || immis.size() == 0) {
2776 return;
2777 }
2778
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002779 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002780
satok688bd472012-02-09 20:09:17 +09002781 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002782 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
satok688bd472012-02-09 20:09:17 +09002783 showSubtypes, mInputShown, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09002784
satokc3690562012-01-10 20:14:43 +09002785 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002786 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09002787 if (currentSubtype != null) {
2788 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002789 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
2790 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09002791 }
2792 }
2793
Ken Wakasa761eb372011-03-04 19:06:18 +09002794 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09002795 mIms = new InputMethodInfo[N];
2796 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002797 int checkedItem = 0;
2798 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002799 final ImeSubtypeListItem item = imList.get(i);
2800 mIms[i] = item.mImi;
2801 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002802 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002803 int subtypeId = mSubtypeIds[i];
2804 if ((subtypeId == NOT_A_SUBTYPE_ID)
2805 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2806 || (subtypeId == lastInputMethodSubtypeId)) {
2807 checkedItem = i;
2808 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002809 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002810 }
Ken Wakasa05dbb652011-08-22 15:22:43 +09002811 final TypedArray a = context.obtainStyledAttributes(null,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002812 com.android.internal.R.styleable.DialogPreference,
2813 com.android.internal.R.attr.alertDialogStyle, 0);
2814 mDialogBuilder = new AlertDialog.Builder(context)
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002815 .setOnCancelListener(new OnCancelListener() {
satok42c5a162011-05-26 16:46:14 +09002816 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002817 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002818 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002819 }
2820 })
2821 .setIcon(a.getDrawable(
2822 com.android.internal.R.styleable.DialogPreference_dialogTitle));
2823 a.recycle();
satok01038492012-04-09 21:08:27 +09002824 final LayoutInflater inflater =
2825 (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2826 final View tv = inflater.inflate(
2827 com.android.internal.R.layout.input_method_switch_dialog_title, null);
2828 mDialogBuilder.setCustomTitle(tv);
2829
2830 // Setup layout for a toggle switch of the hardware keyboard
2831 mSwitchingDialogTitleView = tv;
2832 mSwitchingDialogTitleView.findViewById(
2833 com.android.internal.R.id.hard_keyboard_section).setVisibility(
2834 mWindowManagerService.isHardKeyboardAvailable() ?
2835 View.VISIBLE : View.GONE);
2836 final Switch hardKeySwitch = ((Switch)mSwitchingDialogTitleView.findViewById(
2837 com.android.internal.R.id.hard_keyboard_switch));
2838 hardKeySwitch.setChecked(mWindowManagerService.isHardKeyboardEnabled());
2839 hardKeySwitch.setOnCheckedChangeListener(
2840 new OnCheckedChangeListener() {
2841 @Override
2842 public void onCheckedChanged(
2843 CompoundButton buttonView, boolean isChecked) {
2844 mWindowManagerService.setHardKeyboardEnabled(isChecked);
Satoshi Kataoka04dd24d2013-01-18 13:44:37 +09002845 // Ensure that the input method dialog is dismissed when changing
2846 // the hardware keyboard state.
2847 hideInputMethodMenu();
satok01038492012-04-09 21:08:27 +09002848 }
2849 });
satokd87c2592010-09-29 11:52:06 +09002850
Ken Wakasa05dbb652011-08-22 15:22:43 +09002851 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(context,
2852 com.android.internal.R.layout.simple_list_item_2_single_choice, imList,
2853 checkedItem);
2854
2855 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002856 new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002857 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002858 public void onClick(DialogInterface dialog, int which) {
2859 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09002860 if (mIms == null || mIms.length <= which
2861 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002862 return;
2863 }
2864 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09002865 int subtypeId = mSubtypeIds[which];
Satoshi Kataokad2142962012-11-12 18:43:06 +09002866 adapter.mCheckedItem = which;
2867 adapter.notifyDataSetChanged();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002868 hideInputMethodMenu();
2869 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09002870 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09002871 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09002872 subtypeId = NOT_A_SUBTYPE_ID;
2873 }
2874 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002875 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08002876 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002877 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002878 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002879
satokbc81b692011-08-26 16:22:22 +09002880 if (showSubtypes && !isScreenLocked) {
satok82beadf2010-12-27 19:03:06 +09002881 mDialogBuilder.setPositiveButton(
2882 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09002883 new DialogInterface.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002884 @Override
satok7f35c8c2010-10-07 21:13:11 +09002885 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09002886 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09002887 }
2888 });
2889 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002890 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002891 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002892 mSwitchingDialog.getWindow().setType(
2893 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09002894 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
2895 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002896 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
Jason Monk807ef762014-05-08 15:47:46 -04002897 updateImeWindowStatusLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002898 mSwitchingDialog.show();
2899 }
2900 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002901
Ken Wakasa05dbb652011-08-22 15:22:43 +09002902 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
2903 private final LayoutInflater mInflater;
2904 private final int mTextViewResourceId;
2905 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09002906 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09002907 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
2908 List<ImeSubtypeListItem> itemsList, int checkedItem) {
2909 super(context, textViewResourceId, itemsList);
2910 mTextViewResourceId = textViewResourceId;
2911 mItemsList = itemsList;
2912 mCheckedItem = checkedItem;
2913 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2914 }
2915
2916 @Override
2917 public View getView(int position, View convertView, ViewGroup parent) {
2918 final View view = convertView != null ? convertView
2919 : mInflater.inflate(mTextViewResourceId, null);
2920 if (position < 0 || position >= mItemsList.size()) return view;
2921 final ImeSubtypeListItem item = mItemsList.get(position);
2922 final CharSequence imeName = item.mImeName;
2923 final CharSequence subtypeName = item.mSubtypeName;
2924 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
2925 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
2926 if (TextUtils.isEmpty(subtypeName)) {
2927 firstTextView.setText(imeName);
2928 secondTextView.setVisibility(View.GONE);
2929 } else {
2930 firstTextView.setText(subtypeName);
2931 secondTextView.setText(imeName);
2932 secondTextView.setVisibility(View.VISIBLE);
2933 }
2934 final RadioButton radioButton =
2935 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
2936 radioButton.setChecked(position == mCheckedItem);
2937 return view;
2938 }
2939 }
2940
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002941 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07002942 synchronized (mMethodMap) {
2943 hideInputMethodMenuLocked();
2944 }
2945 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002946
The Android Open Source Project10592532009-03-18 17:39:46 -07002947 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002948 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002949
The Android Open Source Project10592532009-03-18 17:39:46 -07002950 if (mSwitchingDialog != null) {
2951 mSwitchingDialog.dismiss();
2952 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002953 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002954
Jason Monk807ef762014-05-08 15:47:46 -04002955 updateImeWindowStatusLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -07002956 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07002957 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002958 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002959
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002960 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002961
satok42c5a162011-05-26 16:46:14 +09002962 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002963 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002964 // TODO: Make this work even for non-current users?
2965 if (!calledFromValidUser()) {
2966 return false;
2967 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002968 synchronized (mMethodMap) {
2969 if (mContext.checkCallingOrSelfPermission(
2970 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2971 != PackageManager.PERMISSION_GRANTED) {
2972 throw new SecurityException(
2973 "Requires permission "
2974 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2975 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002976
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002977 long ident = Binder.clearCallingIdentity();
2978 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002979 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002980 } finally {
2981 Binder.restoreCallingIdentity(ident);
2982 }
2983 }
2984 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002985
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002986 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
2987 // Make sure this is a valid input method.
2988 InputMethodInfo imm = mMethodMap.get(id);
2989 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09002990 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002991 }
2992
satokd87c2592010-09-29 11:52:06 +09002993 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
2994 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002995
satokd87c2592010-09-29 11:52:06 +09002996 if (enabled) {
2997 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
2998 if (pair.first.equals(id)) {
2999 // We are enabling this input method, but it is already enabled.
3000 // Nothing to do. The previous state was enabled.
3001 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003002 }
3003 }
satokd87c2592010-09-29 11:52:06 +09003004 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3005 // Previous state was disabled.
3006 return false;
3007 } else {
3008 StringBuilder builder = new StringBuilder();
3009 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3010 builder, enabledInputMethodsList, id)) {
3011 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003012 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003013 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3014 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3015 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003016 }
3017 // Previous state was enabled.
3018 return true;
3019 } else {
3020 // We are disabling the input method but it is already disabled.
3021 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003022 return false;
3023 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003024 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003025 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003026
satok723a27e2010-11-11 14:58:11 +09003027 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3028 boolean setSubtypeOnly) {
3029 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003030 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003031
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003032 mCurUserActionNotificationSequenceNumber =
3033 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3034 if (DEBUG) {
3035 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3036 + mCurUserActionNotificationSequenceNumber);
3037 }
3038
3039 if (mCurClient != null && mCurClient.client != null) {
3040 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3041 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
3042 mCurUserActionNotificationSequenceNumber, mCurClient.client));
3043 }
3044
satok723a27e2010-11-11 14:58:11 +09003045 // Set Subtype here
3046 if (imi == null || subtypeId < 0) {
3047 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003048 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003049 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003050 if (subtypeId < imi.getSubtypeCount()) {
3051 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3052 mSettings.putSelectedSubtype(subtype.hashCode());
3053 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003054 } else {
3055 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003056 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003057 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003058 }
satokab751aa2010-09-14 19:17:36 +09003059 }
satok723a27e2010-11-11 14:58:11 +09003060
satok4c0e7152012-06-20 20:08:44 +09003061 // Workaround.
3062 // ASEC is not ready in the IMMS constructor. Accordingly, forward-locked
3063 // IMEs are not recognized and considered uninstalled.
3064 // Actually, we can't move everything after SystemReady because
3065 // IMMS needs to run in the encryption lock screen. So, we just skip changing
3066 // the default IME here and try cheking the default IME again in systemReady().
3067 // TODO: Do nothing before system ready and implement a separated logic for
3068 // the encryption lock screen.
3069 // TODO: ASEC should be ready before IMMS is instantiated.
3070 if (mSystemReady && !setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003071 // Set InputMethod here
3072 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3073 }
3074 }
3075
3076 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3077 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3078 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3079 // newDefaultIme is empty when there is no candidate for the selected IME.
3080 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3081 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3082 if (subtypeHashCode != null) {
3083 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003084 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok723a27e2010-11-11 14:58:11 +09003085 imi, Integer.valueOf(subtypeHashCode));
3086 } catch (NumberFormatException e) {
3087 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3088 }
3089 }
3090 }
3091 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003092 }
3093
satok4e4569d2010-11-19 18:45:53 +09003094 // If there are no selected shortcuts, tries finding the most applicable ones.
3095 private Pair<InputMethodInfo, InputMethodSubtype>
3096 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3097 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3098 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003099 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003100 boolean foundInSystemIME = false;
3101
3102 // Search applicable subtype for each InputMethodInfo
3103 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003104 final String imiId = imi.getId();
3105 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3106 continue;
3107 }
satokcd7cd292010-11-20 15:46:23 +09003108 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003109 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003110 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003111 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003112 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003113 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003114 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003115 }
satokdf31ae62011-01-15 06:19:44 +09003116 // 2. Search by the system locale from enabledSubtypes.
3117 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003118 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003119 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003120 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003121 }
satoka86f5e42011-09-02 17:12:42 +09003122 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003123 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003124 final ArrayList<InputMethodSubtype> subtypesForSearch =
3125 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003126 ? InputMethodUtils.getSubtypes(imi)
3127 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003128 // 4. Search by the current subtype's locale from all subtypes.
3129 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003130 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003131 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003132 }
3133 // 5. Search by the system locale from all subtypes.
3134 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003135 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003136 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003137 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003138 }
satokcd7cd292010-11-20 15:46:23 +09003139 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003140 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003141 // The current input method is the most applicable IME.
3142 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003143 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003144 break;
satok7599a7f2010-12-22 13:45:23 +09003145 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003146 // The system input method is 2nd applicable IME.
3147 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003148 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003149 if ((imi.getServiceInfo().applicationInfo.flags
3150 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3151 foundInSystemIME = true;
3152 }
satok4e4569d2010-11-19 18:45:53 +09003153 }
3154 }
3155 }
3156 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003157 if (mostApplicableIMI != null) {
3158 Slog.w(TAG, "Most applicable shortcut input method was:"
3159 + mostApplicableIMI.getId());
3160 if (mostApplicableSubtype != null) {
3161 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3162 + "," + mostApplicableSubtype.getMode() + ","
3163 + mostApplicableSubtype.getLocale());
3164 }
3165 }
satok4e4569d2010-11-19 18:45:53 +09003166 }
satokcd7cd292010-11-20 15:46:23 +09003167 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09003168 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09003169 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003170 } else {
3171 return null;
3172 }
3173 }
3174
satokab751aa2010-09-14 19:17:36 +09003175 /**
3176 * @return Return the current subtype of this input method.
3177 */
satok42c5a162011-05-26 16:46:14 +09003178 @Override
satokab751aa2010-09-14 19:17:36 +09003179 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003180 // TODO: Make this work even for non-current users?
3181 if (!calledFromValidUser()) {
3182 return null;
3183 }
3184 synchronized (mMethodMap) {
3185 return getCurrentInputMethodSubtypeLocked();
3186 }
3187 }
3188
3189 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003190 if (mCurMethodId == null) {
3191 return null;
3192 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003193 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003194 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3195 if (imi == null || imi.getSubtypeCount() == 0) {
3196 return null;
satok4e4569d2010-11-19 18:45:53 +09003197 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003198 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003199 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3200 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003201 if (subtypeId == NOT_A_SUBTYPE_ID) {
3202 // If there are no selected subtypes, the framework will try to find
3203 // the most applicable subtype from explicitly or implicitly enabled
3204 // subtypes.
3205 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003206 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003207 // If there is only one explicitly or implicitly enabled subtype,
3208 // just returns it.
3209 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3210 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3211 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003212 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003213 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003214 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003215 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003216 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003217 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3218 true);
satok4e4569d2010-11-19 18:45:53 +09003219 }
satok3ef8b292010-11-23 06:06:29 +09003220 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003221 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003222 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003223 }
3224 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003225 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003226 }
3227
satokf3db1af2010-11-23 13:34:33 +09003228 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
3229 InputMethodSubtype subtype) {
3230 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
3231 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
3232 } else {
3233 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3234 subtypes.add(subtype);
3235 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
3236 }
3237 }
3238
satok4e4569d2010-11-19 18:45:53 +09003239 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003240 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003241 @Override
satok4e4569d2010-11-19 18:45:53 +09003242 public List getShortcutInputMethodsAndSubtypes() {
3243 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09003244 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09003245 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003246 // If there are no selected shortcut subtypes, the framework will try to find
3247 // the most applicable subtype from all subtypes whose mode is
3248 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003249 Pair<InputMethodInfo, InputMethodSubtype> info =
3250 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003251 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003252 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003253 ret.add(info.first);
3254 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003255 }
satok3da92232011-01-11 22:46:30 +09003256 return ret;
satokf3db1af2010-11-23 13:34:33 +09003257 }
satokf3db1af2010-11-23 13:34:33 +09003258 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3259 ret.add(imi);
3260 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3261 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003262 }
3263 }
satokf3db1af2010-11-23 13:34:33 +09003264 return ret;
satok4e4569d2010-11-19 18:45:53 +09003265 }
3266 }
3267
satok42c5a162011-05-26 16:46:14 +09003268 @Override
satokb66d2872010-11-10 01:04:04 +09003269 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003270 // TODO: Make this work even for non-current users?
3271 if (!calledFromValidUser()) {
3272 return false;
3273 }
satokb66d2872010-11-10 01:04:04 +09003274 synchronized (mMethodMap) {
3275 if (subtype != null && mCurMethodId != null) {
3276 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003277 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003278 if (subtypeId != NOT_A_SUBTYPE_ID) {
3279 setInputMethodLocked(mCurMethodId, subtypeId);
3280 return true;
3281 }
3282 }
3283 return false;
3284 }
3285 }
3286
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003287 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003288 private static class InputMethodFileManager {
3289 private static final String SYSTEM_PATH = "system";
3290 private static final String INPUT_METHOD_PATH = "inputmethod";
3291 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3292 private static final String NODE_SUBTYPES = "subtypes";
3293 private static final String NODE_SUBTYPE = "subtype";
3294 private static final String NODE_IMI = "imi";
3295 private static final String ATTR_ID = "id";
3296 private static final String ATTR_LABEL = "label";
3297 private static final String ATTR_ICON = "icon";
3298 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3299 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3300 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3301 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3302 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3303 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003304 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
satoke7c6998e2011-06-03 17:57:59 +09003305 new HashMap<String, List<InputMethodSubtype>>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003306 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003307 if (methodMap == null) {
3308 throw new NullPointerException("methodMap is null");
3309 }
3310 mMethodMap = methodMap;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003311 final File systemDir = userId == UserHandle.USER_OWNER
3312 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3313 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003314 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3315 if (!inputMethodDir.mkdirs()) {
3316 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3317 }
3318 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3319 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3320 if (!subtypeFile.exists()) {
3321 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003322 writeAdditionalInputMethodSubtypes(
3323 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003324 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003325 readAdditionalInputMethodSubtypes(
3326 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003327 }
3328 }
3329
3330 private void deleteAllInputMethodSubtypes(String imiId) {
3331 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003332 mAdditionalSubtypesMap.remove(imiId);
3333 writeAdditionalInputMethodSubtypes(
3334 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003335 }
3336 }
3337
3338 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003339 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003340 synchronized (mMethodMap) {
3341 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3342 final int N = additionalSubtypes.length;
3343 for (int i = 0; i < N; ++i) {
3344 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003345 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003346 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003347 } else {
3348 Slog.w(TAG, "Duplicated subtype definition found: "
3349 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003350 }
3351 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003352 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3353 writeAdditionalInputMethodSubtypes(
3354 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003355 }
3356 }
3357
3358 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3359 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003360 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003361 }
3362 }
3363
3364 private static void writeAdditionalInputMethodSubtypes(
3365 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3366 HashMap<String, InputMethodInfo> methodMap) {
3367 // Safety net for the case that this function is called before methodMap is set.
3368 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3369 FileOutputStream fos = null;
3370 try {
3371 fos = subtypesFile.startWrite();
3372 final XmlSerializer out = new FastXmlSerializer();
3373 out.setOutput(fos, "utf-8");
3374 out.startDocument(null, true);
3375 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3376 out.startTag(null, NODE_SUBTYPES);
3377 for (String imiId : allSubtypes.keySet()) {
3378 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3379 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3380 continue;
3381 }
3382 out.startTag(null, NODE_IMI);
3383 out.attribute(null, ATTR_ID, imiId);
3384 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3385 final int N = subtypesList.size();
3386 for (int i = 0; i < N; ++i) {
3387 final InputMethodSubtype subtype = subtypesList.get(i);
3388 out.startTag(null, NODE_SUBTYPE);
3389 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3390 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3391 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3392 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3393 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3394 out.attribute(null, ATTR_IS_AUXILIARY,
3395 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3396 out.endTag(null, NODE_SUBTYPE);
3397 }
3398 out.endTag(null, NODE_IMI);
3399 }
3400 out.endTag(null, NODE_SUBTYPES);
3401 out.endDocument();
3402 subtypesFile.finishWrite(fos);
3403 } catch (java.io.IOException e) {
3404 Slog.w(TAG, "Error writing subtypes", e);
3405 if (fos != null) {
3406 subtypesFile.failWrite(fos);
3407 }
3408 }
3409 }
3410
3411 private static void readAdditionalInputMethodSubtypes(
3412 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3413 if (allSubtypes == null || subtypesFile == null) return;
3414 allSubtypes.clear();
3415 FileInputStream fis = null;
3416 try {
3417 fis = subtypesFile.openRead();
3418 final XmlPullParser parser = Xml.newPullParser();
3419 parser.setInput(fis, null);
3420 int type = parser.getEventType();
3421 // Skip parsing until START_TAG
3422 while ((type = parser.next()) != XmlPullParser.START_TAG
3423 && type != XmlPullParser.END_DOCUMENT) {}
3424 String firstNodeName = parser.getName();
3425 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3426 throw new XmlPullParserException("Xml doesn't start with subtypes");
3427 }
3428 final int depth =parser.getDepth();
3429 String currentImiId = null;
3430 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3431 while (((type = parser.next()) != XmlPullParser.END_TAG
3432 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3433 if (type != XmlPullParser.START_TAG)
3434 continue;
3435 final String nodeName = parser.getName();
3436 if (NODE_IMI.equals(nodeName)) {
3437 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3438 if (TextUtils.isEmpty(currentImiId)) {
3439 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3440 continue;
3441 }
3442 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
3443 allSubtypes.put(currentImiId, tempSubtypesArray);
3444 } else if (NODE_SUBTYPE.equals(nodeName)) {
3445 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3446 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3447 continue;
3448 }
3449 final int icon = Integer.valueOf(
3450 parser.getAttributeValue(null, ATTR_ICON));
3451 final int label = Integer.valueOf(
3452 parser.getAttributeValue(null, ATTR_LABEL));
3453 final String imeSubtypeLocale =
3454 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3455 final String imeSubtypeMode =
3456 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3457 final String imeSubtypeExtraValue =
3458 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003459 final boolean isAuxiliary = "1".equals(String.valueOf(
3460 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
satoke7c6998e2011-06-03 17:57:59 +09003461 final InputMethodSubtype subtype =
3462 new InputMethodSubtype(label, icon, imeSubtypeLocale,
3463 imeSubtypeMode, imeSubtypeExtraValue, isAuxiliary);
3464 tempSubtypesArray.add(subtype);
3465 }
3466 }
3467 } catch (XmlPullParserException e) {
3468 Slog.w(TAG, "Error reading subtypes: " + e);
3469 return;
3470 } catch (java.io.IOException e) {
3471 Slog.w(TAG, "Error reading subtypes: " + e);
3472 return;
3473 } catch (NumberFormatException e) {
3474 Slog.w(TAG, "Error reading subtypes: " + e);
3475 return;
3476 } finally {
3477 if (fis != null) {
3478 try {
3479 fis.close();
3480 } catch (java.io.IOException e1) {
3481 Slog.w(TAG, "Failed to close.");
3482 }
3483 }
3484 }
3485 }
3486 }
3487
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003488 @Override
3489 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3490 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3491 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003492
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003493 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3494 + Binder.getCallingPid()
3495 + ", uid=" + Binder.getCallingUid());
3496 return;
3497 }
3498
3499 IInputMethod method;
3500 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003501
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003502 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003504 synchronized (mMethodMap) {
3505 p.println("Current Input Method Manager state:");
3506 int N = mMethodList.size();
3507 p.println(" Input Methods:");
3508 for (int i=0; i<N; i++) {
3509 InputMethodInfo info = mMethodList.get(i);
3510 p.println(" InputMethod #" + i + ":");
3511 info.dump(p, " ");
3512 }
3513 p.println(" Clients:");
3514 for (ClientState ci : mClients.values()) {
3515 p.println(" Client " + ci + ":");
3516 p.println(" client=" + ci.client);
3517 p.println(" inputContext=" + ci.inputContext);
3518 p.println(" sessionRequested=" + ci.sessionRequested);
3519 p.println(" curSession=" + ci.curSession);
3520 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003521 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003522 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003523 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3524 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003525 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3526 + " mBoundToMethod=" + mBoundToMethod);
3527 p.println(" mCurToken=" + mCurToken);
3528 p.println(" mCurIntent=" + mCurIntent);
3529 method = mCurMethod;
3530 p.println(" mCurMethod=" + mCurMethod);
3531 p.println(" mEnabledSession=" + mEnabledSession);
3532 p.println(" mShowRequested=" + mShowRequested
3533 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3534 + " mShowForced=" + mShowForced
3535 + " mInputShown=" + mInputShown);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003536 p.println(" mCurUserActionNotificationSequenceNumber="
3537 + mCurUserActionNotificationSequenceNumber);
Dianne Hackbornd4e6d462014-05-16 16:32:37 -07003538 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003539 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003540
Jeff Brownb88102f2010-09-08 11:49:43 -07003541 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003542 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003543 pw.flush();
3544 try {
3545 client.client.asBinder().dump(fd, args);
3546 } catch (RemoteException e) {
3547 p.println("Input method client dead: " + e);
3548 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003549 } else {
3550 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003551 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003552
Jeff Brownb88102f2010-09-08 11:49:43 -07003553 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003554 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003555 pw.flush();
3556 try {
3557 method.asBinder().dump(fd, args);
3558 } catch (RemoteException e) {
3559 p.println("Input method service dead: " + e);
3560 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003561 } else {
3562 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003563 }
3564 }
3565}