blob: 0863bd6e91281d84118b4d2fbf5125604dd109cd [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 Kataoka8e303cc52013-01-11 15:55:28 +090019import com.android.internal.inputmethod.InputMethodUtils;
20import com.android.internal.inputmethod.InputMethodUtils.InputMethodSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import com.android.internal.os.HandlerCaller;
Svetoslav Ganov758143e2012-08-06 16:40:27 -070022import com.android.internal.os.SomeArgs;
satoke7c6998e2011-06-03 17:57:59 +090023import com.android.internal.util.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import com.android.internal.view.IInputContext;
25import com.android.internal.view.IInputMethod;
Michael Wright52a53522013-03-14 10:59:38 -070026import com.android.internal.view.IInputSessionCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import com.android.internal.view.IInputMethodClient;
28import com.android.internal.view.IInputMethodManager;
29import com.android.internal.view.IInputMethodSession;
30import com.android.internal.view.InputBindResult;
Dianne Hackborna924dc0d2011-02-17 14:22:17 -080031import com.android.server.EventLogTags;
satok01038492012-04-09 21:08:27 +090032import com.android.server.wm.WindowManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033
satoke7c6998e2011-06-03 17:57:59 +090034import org.xmlpull.v1.XmlPullParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import org.xmlpull.v1.XmlPullParserException;
satoke7c6998e2011-06-03 17:57:59 +090036import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037
38import android.app.ActivityManagerNative;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090039import android.app.AppGlobals;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.app.AlertDialog;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090041import android.app.IUserSwitchObserver;
satokf90a33e2011-07-19 11:55:52 +090042import android.app.KeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +090043import android.app.Notification;
44import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070045import android.app.PendingIntent;
satok5b927c432012-05-01 20:09:34 +090046import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import android.content.ComponentName;
48import android.content.ContentResolver;
49import android.content.Context;
50import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.content.DialogInterface.OnCancelListener;
52import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090053import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070055import android.content.pm.ApplicationInfo;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090056import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057import android.content.pm.PackageManager;
58import android.content.pm.ResolveInfo;
59import android.content.pm.ServiceInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070060import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.content.res.Resources;
62import android.content.res.TypedArray;
63import android.database.ContentObserver;
Joe Onorato857fd9b2011-01-27 15:08:35 -080064import android.inputmethodservice.InputMethodService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.os.Binder;
satoke7c6998e2011-06-03 17:57:59 +090066import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.os.Handler;
68import android.os.IBinder;
69import android.os.IInterface;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090070import android.os.IRemoteCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.os.Message;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090072import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073import android.os.Parcel;
74import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080075import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076import android.os.ServiceManager;
77import android.os.SystemClock;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090078import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079import android.provider.Settings;
80import android.text.TextUtils;
satokf9f01002011-05-19 21:31:50 +090081import android.text.style.SuggestionSpan;
Dianne Hackborn39606a02012-07-31 17:54:35 -070082import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +090084import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +090085import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086import android.util.PrintWriterPrinter;
87import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +090088import android.util.Slog;
89import android.util.Xml;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090import android.view.IWindowManager;
Ken Wakasa05dbb652011-08-22 15:22:43 +090091import android.view.LayoutInflater;
92import android.view.View;
93import android.view.ViewGroup;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094import android.view.WindowManager;
satokab751aa2010-09-14 19:17:36 +090095import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096import android.view.inputmethod.InputBinding;
97import android.view.inputmethod.InputMethod;
98import android.view.inputmethod.InputMethodInfo;
99import android.view.inputmethod.InputMethodManager;
satokab751aa2010-09-14 19:17:36 +0900100import android.view.inputmethod.InputMethodSubtype;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900101import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +0900102import android.widget.CompoundButton;
103import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900104import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +0900105import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900106import android.widget.TextView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107
satoke7c6998e2011-06-03 17:57:59 +0900108import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900110import java.io.FileInputStream;
111import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112import java.io.IOException;
113import java.io.PrintWriter;
114import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900115import java.util.Collections;
Ken Wakasa761eb372011-03-04 19:06:18 +0900116import java.util.Comparator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117import java.util.HashMap;
satok7f35c8c2010-10-07 21:13:11 +0900118import java.util.HashSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119import java.util.List;
satok5b927c432012-05-01 20:09:34 +0900120import java.util.Locale;
satok913a8922010-08-26 21:53:41 +0900121import java.util.TreeMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122
123/**
124 * This class provides a system service that manages input methods.
125 */
126public class InputMethodManagerService extends IInputMethodManager.Stub
127 implements ServiceConnection, Handler.Callback {
128 static final boolean DEBUG = false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700129 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130
131 static final int MSG_SHOW_IM_PICKER = 1;
satokab751aa2010-09-14 19:17:36 +0900132 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 2;
satok47a44912010-10-06 16:03:58 +0900133 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 3;
satok217f5482010-12-15 05:19:19 +0900134 static final int MSG_SHOW_IM_CONFIG = 4;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 static final int MSG_UNBIND_INPUT = 1000;
137 static final int MSG_BIND_INPUT = 1010;
138 static final int MSG_SHOW_SOFT_INPUT = 1020;
139 static final int MSG_HIDE_SOFT_INPUT = 1030;
140 static final int MSG_ATTACH_TOKEN = 1040;
141 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 static final int MSG_START_INPUT = 2000;
144 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800145
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 static final int MSG_UNBIND_METHOD = 3000;
147 static final int MSG_BIND_METHOD = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700148 static final int MSG_SET_ACTIVE = 3020;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800149
satok01038492012-04-09 21:08:27 +0900150 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 static final long TIME_TO_RECONNECT = 10*1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800153
satokf9f01002011-05-19 21:31:50 +0900154 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
155
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +0900156 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
satokb6359412011-06-28 17:47:41 +0900157 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +0900158
satok4e4569d2010-11-19 18:45:53 +0900159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800161 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900163 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 final IWindowManager mIWindowManager;
166 final HandlerCaller mCaller;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900167 private InputMethodFileManager mFileManager;
168 private InputMethodAndSubtypeListManager mImListManager;
satok01038492012-04-09 21:08:27 +0900169 private final HardKeyboardListener mHardKeyboardListener;
170 private final WindowManagerService mWindowManagerService;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 final InputBindResult mNoBinding = new InputBindResult(null, null, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 // All known input methods. mMethodMap also serves as the global
175 // lock for this class.
satokd87c2592010-09-29 11:52:06 +0900176 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<InputMethodInfo>();
177 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<String, InputMethodInfo>();
satokf9f01002011-05-19 21:31:50 +0900178 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
179 new LruCache<SuggestionSpan, InputMethodInfo>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800180
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700181 // Used to bring IME service up to visible adjustment while it is being shown.
182 final ServiceConnection mVisibleConnection = new ServiceConnection() {
183 @Override public void onServiceConnected(ComponentName name, IBinder service) {
184 }
185
186 @Override public void onServiceDisconnected(ComponentName name) {
187 }
188 };
189 boolean mVisibleBound = false;
190
satok7cfc0ed2011-06-20 21:29:36 +0900191 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700192 private NotificationManager mNotificationManager;
193 private KeyguardManager mKeyguardManager;
194 private StatusBarManagerService mStatusBar;
195 private Notification mImeSwitcherNotification;
196 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900197 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900198 private boolean mNotificationShown;
satok0a1bcf42012-05-16 19:26:31 +0900199 private final boolean mImeSelectedOnBoot;
satok7cfc0ed2011-06-20 21:29:36 +0900200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 class SessionState {
202 final ClientState client;
203 final IInputMethod method;
204 final IInputMethodSession session;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 @Override
207 public String toString() {
208 return "SessionState{uid " + client.uid + " pid " + client.pid
209 + " method " + Integer.toHexString(
210 System.identityHashCode(method))
211 + " session " + Integer.toHexString(
212 System.identityHashCode(session))
213 + "}";
214 }
215
216 SessionState(ClientState _client, IInputMethod _method,
217 IInputMethodSession _session) {
218 client = _client;
219 method = _method;
220 session = _session;
221 }
222 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 class ClientState {
225 final IInputMethodClient client;
226 final IInputContext inputContext;
227 final int uid;
228 final int pid;
229 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 boolean sessionRequested;
232 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 @Override
235 public String toString() {
236 return "ClientState{" + Integer.toHexString(
237 System.identityHashCode(this)) + " uid " + uid
238 + " pid " + pid + "}";
239 }
240
241 ClientState(IInputMethodClient _client, IInputContext _inputContext,
242 int _uid, int _pid) {
243 client = _client;
244 inputContext = _inputContext;
245 uid = _uid;
246 pid = _pid;
247 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
248 }
249 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 final HashMap<IBinder, ClientState> mClients
252 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700255 * Set once the system is ready to run third party code.
256 */
257 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800258
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700259 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 * Id of the currently selected input method.
261 */
262 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 /**
265 * The current binding sequence number, incremented every time there is
266 * a new bind performed.
267 */
268 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 /**
271 * The client that is currently bound to an input method.
272 */
273 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800274
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700276 * The last window token that gained focus.
277 */
278 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800279
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700280 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 * The input context last provided by the current client.
282 */
283 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 /**
286 * The attributes last provided by the current client.
287 */
288 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 /**
291 * The input method ID of the input method service that we are currently
292 * connected to or in the process of connecting to.
293 */
294 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296 /**
satokab751aa2010-09-14 19:17:36 +0900297 * The current subtype of the current input method.
298 */
299 private InputMethodSubtype mCurrentSubtype;
300
satok4e4569d2010-11-19 18:45:53 +0900301 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900302 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
303 mShortcutInputMethodsAndSubtypes =
304 new HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>();
satokab751aa2010-09-14 19:17:36 +0900305
306 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 * Set to true if our ServiceConnection is currently actively bound to
308 * a service (whether or not we have gotten its IBinder back yet).
309 */
310 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 /**
313 * Set if the client has asked for the input method to be shown.
314 */
315 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800316
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 /**
318 * Set if we were explicitly told to show the input method.
319 */
320 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322 /**
323 * Set if we were forced to be shown.
324 */
325 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 /**
328 * Set if we last told the input method to show itself.
329 */
330 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332 /**
333 * The Intent used to connect to the current input method.
334 */
335 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 /**
338 * The token we have made for the currently active input method, to
339 * identify it in the future.
340 */
341 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 /**
344 * If non-null, this is the input method service we are currently connected
345 * to.
346 */
347 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 /**
350 * Time that we last initiated a bind to the input method, to determine
351 * if we should try to disconnect and reconnect to it.
352 */
353 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 /**
356 * Have we called mCurMethod.bindInput()?
357 */
358 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 /**
361 * Currently enabled session. Only touched by service thread, not
362 * protected by a lock.
363 */
364 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 /**
367 * True if the screen is on. The value is true initially.
368 */
369 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800370
Joe Onorato857fd9b2011-01-27 15:08:35 -0800371 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
372 int mImeWindowVis;
373
Ken Wakasa05dbb652011-08-22 15:22:43 +0900374 private AlertDialog.Builder mDialogBuilder;
375 private AlertDialog mSwitchingDialog;
satok01038492012-04-09 21:08:27 +0900376 private View mSwitchingDialogTitleView;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900377 private InputMethodInfo[] mIms;
378 private int[] mSubtypeIds;
satok5b927c432012-05-01 20:09:34 +0900379 private Locale mLastSystemLocale;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900380 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
381 private final IPackageManager mIPackageManager;
John Spurlockbc7b6fc2012-11-14 08:51:07 -0500382 private boolean mInputBoundToKeyguard;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800383
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 class SettingsObserver extends ContentObserver {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800385 String mLastEnabled = "";
386
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 SettingsObserver(Handler handler) {
388 super(handler);
389 ContentResolver resolver = mContext.getContentResolver();
390 resolver.registerContentObserver(Settings.Secure.getUriFor(
391 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
satokab751aa2010-09-14 19:17:36 +0900392 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokb6109bb2011-02-03 22:24:54 +0900393 Settings.Secure.ENABLED_INPUT_METHODS), false, this);
394 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokab751aa2010-09-14 19:17:36 +0900395 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398 @Override public void onChange(boolean selfChange) {
399 synchronized (mMethodMap) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800400 boolean enabledChanged = false;
401 String newEnabled = mSettings.getEnabledInputMethodsStr();
402 if (!mLastEnabled.equals(newEnabled)) {
403 mLastEnabled = newEnabled;
404 enabledChanged = true;
405 }
406 updateFromSettingsLocked(enabledChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407 }
408 }
409 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800410
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900411 class ImmsBroadcastReceiver extends android.content.BroadcastReceiver {
412 private void updateActive() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 // Inform the current client of the change in active status
Dianne Hackborna6e41342012-05-22 16:30:34 -0700414 if (mCurClient != null && mCurClient.client != null) {
415 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
416 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 }
418 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900419
420 @Override
421 public void onReceive(Context context, Intent intent) {
422 final String action = intent.getAction();
423 if (Intent.ACTION_SCREEN_ON.equals(action)) {
424 mScreenOn = true;
425 refreshImeWindowVisibilityLocked();
426 updateActive();
427 return;
428 } else if (Intent.ACTION_SCREEN_OFF.equals(action)) {
429 mScreenOn = false;
430 setImeWindowVisibilityStatusHiddenLocked();
431 updateActive();
432 return;
433 } else if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
434 hideInputMethodMenu();
435 // No need to updateActive
436 return;
437 } else {
438 Slog.w(TAG, "Unexpected intent " + intent);
439 }
440 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800442
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800443 class MyPackageMonitor extends PackageMonitor {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900444 private boolean isChangingPackagesOfCurrentUser() {
445 final int userId = getChangingUserId();
446 final boolean retval = userId == mSettings.getCurrentUserId();
447 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900448 if (!retval) {
449 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
450 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900451 }
452 return retval;
453 }
454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800456 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900457 if (!isChangingPackagesOfCurrentUser()) {
458 return false;
459 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900461 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 final int N = mMethodList.size();
463 if (curInputMethodId != null) {
464 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800465 InputMethodInfo imi = mMethodList.get(i);
466 if (imi.getId().equals(curInputMethodId)) {
467 for (String pkg : packages) {
468 if (imi.getPackageName().equals(pkg)) {
469 if (!doit) {
470 return true;
471 }
satok723a27e2010-11-11 14:58:11 +0900472 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800473 chooseNewDefaultIMELocked();
474 return true;
475 }
476 }
477 }
478 }
479 }
480 }
481 return false;
482 }
483
484 @Override
485 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900486 if (!isChangingPackagesOfCurrentUser()) {
487 return;
488 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800489 synchronized (mMethodMap) {
490 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900491 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800492 final int N = mMethodList.size();
493 if (curInputMethodId != null) {
494 for (int i=0; i<N; i++) {
495 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900496 final String imiId = imi.getId();
497 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800498 curIm = imi;
499 }
satoke7c6998e2011-06-03 17:57:59 +0900500
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800501 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900502 if (isPackageModified(imi.getPackageName())) {
503 mFileManager.deleteAllInputMethodSubtypes(imiId);
504 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800505 if (change == PACKAGE_TEMPORARY_CHANGE
506 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800507 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800508 + imi.getComponent());
509 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510 }
511 }
512 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800513
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900514 buildInputMethodListLocked(
515 mMethodList, mMethodMap, false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800516
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800518
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800519 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800520 int change = isPackageDisappearing(curIm.getPackageName());
521 if (change == PACKAGE_TEMPORARY_CHANGE
522 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800523 ServiceInfo si = null;
524 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900525 si = mIPackageManager.getServiceInfo(
526 curIm.getComponent(), 0, mSettings.getCurrentUserId());
527 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800528 }
529 if (si == null) {
530 // Uh oh, current input method is no longer around!
531 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800532 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
satok15452a42011-10-28 17:58:28 +0900533 setImeWindowVisibilityStatusHiddenLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800534 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800535 changed = true;
536 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800537 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900538 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800539 }
540 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800541 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800542 }
satokab751aa2010-09-14 19:17:36 +0900543
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800544 if (curIm == null) {
545 // We currently don't have a default input method... is
546 // one now available?
547 changed = chooseNewDefaultIMELocked();
548 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800549
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800550 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800551 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 }
553 }
554 }
555 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800556
Michael Wright52a53522013-03-14 10:59:38 -0700557 private static class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900558 private final IInputMethod mMethod;
559 private final InputMethodManagerService mParentIMMS;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800560
Jean Chalarde0d32a62011-10-20 20:36:07 +0900561 MethodCallback(final IInputMethod method, final InputMethodManagerService imms) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 mMethod = method;
Jean Chalarde0d32a62011-10-20 20:36:07 +0900563 mParentIMMS = imms;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800565
satoke7c6998e2011-06-03 17:57:59 +0900566 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 public void sessionCreated(IInputMethodSession session) throws RemoteException {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900568 mParentIMMS.onSessionCreated(mMethod, session);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 }
570 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800571
satok01038492012-04-09 21:08:27 +0900572 private class HardKeyboardListener
573 implements WindowManagerService.OnHardKeyboardStatusChangeListener {
574 @Override
575 public void onHardKeyboardStatusChange(boolean available, boolean enabled) {
576 mHandler.sendMessage(mHandler.obtainMessage(
577 MSG_HARD_KEYBOARD_SWITCH_CHANGED, available ? 1 : 0, enabled ? 1 : 0));
578 }
579
580 public void handleHardKeyboardStatusChange(boolean available, boolean enabled) {
581 if (DEBUG) {
582 Slog.w(TAG, "HardKeyboardStatusChanged: available = " + available + ", enabled = "
583 + enabled);
584 }
585 synchronized(mMethodMap) {
586 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
587 && mSwitchingDialog.isShowing()) {
588 mSwitchingDialogTitleView.findViewById(
589 com.android.internal.R.id.hard_keyboard_section).setVisibility(
590 available ? View.VISIBLE : View.GONE);
591 }
592 }
593 }
594 }
595
596 public InputMethodManagerService(Context context, WindowManagerService windowManager) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900597 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800598 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800599 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600 mHandler = new Handler(this);
601 mIWindowManager = IWindowManager.Stub.asInterface(
602 ServiceManager.getService(Context.WINDOW_SERVICE));
Mita Yuned218c72012-12-06 17:18:25 -0800603 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900604 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 public void executeMessage(Message msg) {
606 handleMessage(msg);
607 }
Mita Yuned218c72012-12-06 17:18:25 -0800608 }, true /*asyncHandler*/);
satok01038492012-04-09 21:08:27 +0900609 mWindowManagerService = windowManager;
610 mHardKeyboardListener = new HardKeyboardListener();
satok7cfc0ed2011-06-20 21:29:36 +0900611
satok7cfc0ed2011-06-20 21:29:36 +0900612 mImeSwitcherNotification = new Notification();
613 mImeSwitcherNotification.icon = com.android.internal.R.drawable.ic_notification_ime_default;
614 mImeSwitcherNotification.when = 0;
615 mImeSwitcherNotification.flags = Notification.FLAG_ONGOING_EVENT;
616 mImeSwitcherNotification.tickerText = null;
617 mImeSwitcherNotification.defaults = 0; // please be quiet
618 mImeSwitcherNotification.sound = null;
619 mImeSwitcherNotification.vibrate = null;
Daniel Sandler590d5152012-06-14 16:10:13 -0400620
621 // Tag this notification specially so SystemUI knows it's important
622 mImeSwitcherNotification.kind = new String[] { "android.system.imeswitcher" };
623
satok7cfc0ed2011-06-20 21:29:36 +0900624 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900625 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900626
627 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900628
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900629 final IntentFilter broadcastFilter = new IntentFilter();
630 broadcastFilter.addAction(Intent.ACTION_SCREEN_ON);
631 broadcastFilter.addAction(Intent.ACTION_SCREEN_OFF);
632 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
633 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800634
satok7cfc0ed2011-06-20 21:29:36 +0900635 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900636 int userId = 0;
637 try {
638 ActivityManagerNative.getDefault().registerUserSwitchObserver(
639 new IUserSwitchObserver.Stub() {
640 @Override
641 public void onUserSwitching(int newUserId, IRemoteCallback reply) {
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900642 synchronized(mMethodMap) {
643 switchUserLocked(newUserId);
644 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900645 if (reply != null) {
646 try {
647 reply.sendResult(null);
648 } catch (RemoteException e) {
649 }
650 }
651 }
652
653 @Override
654 public void onUserSwitchComplete(int newUserId) throws RemoteException {
655 }
656 });
657 userId = ActivityManagerNative.getDefault().getCurrentUser().id;
658 } catch (RemoteException e) {
659 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
660 }
satok81f8b7c2012-12-04 20:42:56 +0900661 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
satok913a8922010-08-26 21:53:41 +0900662
satokd87c2592010-09-29 11:52:06 +0900663 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900664 mSettings = new InputMethodSettings(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900665 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId);
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900666 mFileManager = new InputMethodFileManager(mMethodMap, userId);
667 mImListManager = new InputMethodAndSubtypeListManager(context, this);
satok0a1bcf42012-05-16 19:26:31 +0900668
669 // Just checking if defaultImiId is empty or not
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900670 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900671 if (DEBUG) {
672 Slog.d(TAG, "Initial default ime = " + defaultImiId);
673 }
satok0a1bcf42012-05-16 19:26:31 +0900674 mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
675
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900676 buildInputMethodListLocked(mMethodList, mMethodMap,
677 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satokd87c2592010-09-29 11:52:06 +0900678 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800679
satok0a1bcf42012-05-16 19:26:31 +0900680 if (!mImeSelectedOnBoot) {
681 Slog.w(TAG, "No IME selected. Choose the most applicable IME.");
satok5b927c432012-05-01 20:09:34 +0900682 resetDefaultImeLocked(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800683 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800684
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800685 mSettingsObserver = new SettingsObserver(mHandler);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800686 updateFromSettingsLocked(true);
satok5b927c432012-05-01 20:09:34 +0900687
688 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME
689 // according to the new system locale.
690 final IntentFilter filter = new IntentFilter();
691 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
692 mContext.registerReceiver(
693 new BroadcastReceiver() {
694 @Override
695 public void onReceive(Context context, Intent intent) {
696 synchronized(mMethodMap) {
697 checkCurrentLocaleChangedLocked();
698 }
699 }
700 }, filter);
701 }
702
satok5b927c432012-05-01 20:09:34 +0900703 private void resetDefaultImeLocked(Context context) {
704 // Do not reset the default (current) IME when it is a 3rd-party IME
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +0900705 if (mCurMethodId != null
706 && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +0900707 return;
708 }
709
710 InputMethodInfo defIm = null;
711 for (InputMethodInfo imi : mMethodList) {
712 if (defIm == null) {
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +0900713 if (InputMethodUtils.isValidSystemDefaultIme(
714 mSystemReady, imi, context)) {
satok5b927c432012-05-01 20:09:34 +0900715 defIm = imi;
716 Slog.i(TAG, "Selected default: " + imi.getId());
717 }
718 }
719 }
720 if (defIm == null && mMethodList.size() > 0) {
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +0900721 defIm = InputMethodUtils.getMostApplicableDefaultIME(
722 mSettings.getEnabledInputMethodListLocked());
satok5b927c432012-05-01 20:09:34 +0900723 Slog.i(TAG, "No default found, using " + defIm.getId());
724 }
725 if (defIm != null) {
726 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
727 }
728 }
729
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900730 private void resetAllInternalStateLocked(final boolean updateOnlyWhenLocaleChanged,
731 final boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900732 if (!mSystemReady) {
733 // not system ready
734 return;
735 }
736 final Locale newLocale = mRes.getConfiguration().locale;
737 if (!updateOnlyWhenLocaleChanged
738 || (newLocale != null && !newLocale.equals(mLastSystemLocale))) {
739 if (!updateOnlyWhenLocaleChanged) {
740 hideCurrentInputLocked(0, null);
741 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -0700742 unbindCurrentMethodLocked(true, false);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900743 }
744 if (DEBUG) {
745 Slog.i(TAG, "Locale has been changed to " + newLocale);
746 }
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900747 // InputMethodAndSubtypeListManager should be reset when the locale is changed.
748 mImListManager = new InputMethodAndSubtypeListManager(mContext, this);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900749 buildInputMethodListLocked(mMethodList, mMethodMap, resetDefaultEnabledIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900750 if (!updateOnlyWhenLocaleChanged) {
751 final String selectedImiId = mSettings.getSelectedInputMethod();
752 if (TextUtils.isEmpty(selectedImiId)) {
753 // This is the first time of the user switch and
754 // set the current ime to the proper one.
755 resetDefaultImeLocked(mContext);
756 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +0900757 } else {
758 // If the locale is changed, needs to reset the default ime
759 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900760 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800761 updateFromSettingsLocked(true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900762 mLastSystemLocale = newLocale;
763 if (!updateOnlyWhenLocaleChanged) {
764 try {
765 startInputInnerLocked();
766 } catch (RuntimeException e) {
767 Slog.w(TAG, "Unexpected exception", e);
768 }
769 }
770 }
771 }
772
773 private void checkCurrentLocaleChangedLocked() {
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900774 resetAllInternalStateLocked(true /* updateOnlyWhenLocaleChanged */,
775 true /* resetDefaultImeLocked */);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900776 }
777
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900778 private void switchUserLocked(int newUserId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900779 mSettings.setCurrentUserId(newUserId);
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900780 // InputMethodFileManager should be reset when the user is changed
781 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900782 final String defaultImiId = mSettings.getSelectedInputMethod();
783 final boolean needsToResetDefaultIme = TextUtils.isEmpty(defaultImiId);
784 if (DEBUG) {
785 Slog.d(TAG, "Switch user: " + newUserId + " current ime = " + defaultImiId);
786 }
787 resetAllInternalStateLocked(false /* updateOnlyWhenLocaleChanged */,
788 needsToResetDefaultIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900789 }
790
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800791 @Override
792 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
793 throws RemoteException {
794 try {
795 return super.onTransact(code, data, reply, flags);
796 } catch (RuntimeException e) {
797 // The input method manager only throws security exceptions, so let's
798 // log all others.
799 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800800 Slog.e(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800801 }
802 throw e;
803 }
804 }
805
Dianne Hackborn661cd522011-08-22 00:26:20 -0700806 public void systemReady(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700807 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900808 if (DEBUG) {
809 Slog.d(TAG, "--- systemReady");
810 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700811 if (!mSystemReady) {
812 mSystemReady = true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900813 mKeyguardManager =
814 (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700815 mNotificationManager = (NotificationManager)
816 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
817 mStatusBar = statusBar;
818 statusBar.setIconVisibility("ime", false);
819 updateImeWindowStatusLocked();
satokb858c732011-07-22 19:54:34 +0900820 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
821 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +0900822 if (mShowOngoingImeSwitcherForPhones) {
823 mWindowManagerService.setOnHardKeyboardStatusChangeListener(
824 mHardKeyboardListener);
825 }
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900826 buildInputMethodListLocked(mMethodList, mMethodMap,
827 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satok0a1bcf42012-05-16 19:26:31 +0900828 if (!mImeSelectedOnBoot) {
829 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
830 checkCurrentLocaleChangedLocked();
831 }
832 mLastSystemLocale = mRes.getConfiguration().locale;
Dianne Hackborncc278702009-09-02 23:07:23 -0700833 try {
834 startInputInnerLocked();
835 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800836 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700837 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700838 }
839 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800840 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800841
satok15452a42011-10-28 17:58:28 +0900842 private void setImeWindowVisibilityStatusHiddenLocked() {
843 mImeWindowVis = 0;
844 updateImeWindowStatusLocked();
845 }
846
satok3afd6c02011-11-18 08:38:19 +0900847 private void refreshImeWindowVisibilityLocked() {
848 final Configuration conf = mRes.getConfiguration();
849 final boolean haveHardKeyboard = conf.keyboard
850 != Configuration.KEYBOARD_NOKEYS;
851 final boolean hardKeyShown = haveHardKeyboard
852 && conf.hardKeyboardHidden
853 != Configuration.HARDKEYBOARDHIDDEN_YES;
John Spurlockbc7b6fc2012-11-14 08:51:07 -0500854 final boolean isScreenLocked =
855 mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
856 final boolean isScreenSecurelyLocked =
857 isScreenLocked && mKeyguardManager.isKeyguardSecure();
858 final boolean inputShown = mInputShown && (!isScreenLocked || mInputBoundToKeyguard);
859 mImeWindowVis = (!isScreenSecurelyLocked && (inputShown || hardKeyShown)) ?
satok3afd6c02011-11-18 08:38:19 +0900860 (InputMethodService.IME_ACTIVE | InputMethodService.IME_VISIBLE) : 0;
861 updateImeWindowStatusLocked();
862 }
863
satok15452a42011-10-28 17:58:28 +0900864 private void updateImeWindowStatusLocked() {
satokdbf29502011-08-25 15:28:23 +0900865 setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700866 }
867
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900868 // ---------------------------------------------------------------------------------------
869 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
870 // 1) it comes from the system process
871 // 2) the calling process' user id is identical to the current user id IMMS thinks.
872 private boolean calledFromValidUser() {
873 final int uid = Binder.getCallingUid();
874 final int userId = UserHandle.getUserId(uid);
875 if (DEBUG) {
876 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
877 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
878 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataokac86884c2012-10-09 15:20:29 +0900879 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900880 }
881 if (uid == Process.SYSTEM_UID || userId == mSettings.getCurrentUserId()) {
882 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900883 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +0900884
885 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
886 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
887 // must not manage background users' states in any functions.
888 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
889 // by a token.
890 if (mContext.checkCallingOrSelfPermission(
891 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
892 == PackageManager.PERMISSION_GRANTED) {
893 if (DEBUG) {
894 Slog.d(TAG, "--- Access granted because the calling process has "
895 + "the INTERACT_ACROSS_USERS_FULL permission");
896 }
897 return true;
898 }
899 Slog.w(TAG, "--- IPC called from background users. Ignore. \n" + getStackTrace());
900 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900901 }
902
903 private boolean bindCurrentInputMethodService(
904 Intent service, ServiceConnection conn, int flags) {
905 if (service == null || conn == null) {
906 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
907 return false;
908 }
Amith Yamasani27b89e62013-01-16 12:30:11 -0800909 return mContext.bindServiceAsUser(service, conn, flags,
910 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900911 }
912
satoke7c6998e2011-06-03 17:57:59 +0900913 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800914 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900915 // TODO: Make this work even for non-current users?
916 if (!calledFromValidUser()) {
917 return Collections.emptyList();
918 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919 synchronized (mMethodMap) {
920 return new ArrayList<InputMethodInfo>(mMethodList);
921 }
922 }
923
satoke7c6998e2011-06-03 17:57:59 +0900924 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800925 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900926 // TODO: Make this work even for non-current users?
927 if (!calledFromValidUser()) {
928 return Collections.emptyList();
929 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800930 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900931 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800932 }
933 }
934
satokbb4aa062011-01-19 21:40:27 +0900935 private HashMap<InputMethodInfo, List<InputMethodSubtype>>
936 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked() {
937 HashMap<InputMethodInfo, List<InputMethodSubtype>> enabledInputMethodAndSubtypes =
938 new HashMap<InputMethodInfo, List<InputMethodSubtype>>();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900939 for (InputMethodInfo imi: mSettings.getEnabledInputMethodListLocked()) {
satokbb4aa062011-01-19 21:40:27 +0900940 enabledInputMethodAndSubtypes.put(
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +0900941 imi, mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true));
satokbb4aa062011-01-19 21:40:27 +0900942 }
943 return enabledInputMethodAndSubtypes;
944 }
945
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +0900946 /**
947 * @param imi if null, returns enabled subtypes for the current imi
948 * @return enabled subtypes of the specified imi
949 */
satoke7c6998e2011-06-03 17:57:59 +0900950 @Override
satok16331c82010-12-20 23:48:46 +0900951 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi,
952 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900953 // TODO: Make this work even for non-current users?
954 if (!calledFromValidUser()) {
955 return Collections.emptyList();
956 }
satok67ddf9c2010-11-17 09:45:54 +0900957 synchronized (mMethodMap) {
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +0900958 if (imi == null && mCurMethodId != null) {
959 imi = mMethodMap.get(mCurMethodId);
960 }
961 return mSettings.getEnabledInputMethodSubtypeListLocked(
962 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +0900963 }
964 }
965
satoke7c6998e2011-06-03 17:57:59 +0900966 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800967 public void addClient(IInputMethodClient client,
968 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900969 if (!calledFromValidUser()) {
970 return;
971 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800972 synchronized (mMethodMap) {
973 mClients.put(client.asBinder(), new ClientState(client,
974 inputContext, uid, pid));
975 }
976 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800977
satoke7c6998e2011-06-03 17:57:59 +0900978 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800979 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900980 if (!calledFromValidUser()) {
981 return;
982 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800983 synchronized (mMethodMap) {
984 mClients.remove(client.asBinder());
985 }
986 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800987
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988 void executeOrSendMessage(IInterface target, Message msg) {
989 if (target.asBinder() instanceof Binder) {
990 mCaller.sendMessage(msg);
991 } else {
992 handleMessage(msg);
993 msg.recycle();
994 }
995 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800996
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700997 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800998 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800999 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001000 + mCurClient.client.asBinder());
1001 if (mBoundToMethod) {
1002 mBoundToMethod = false;
1003 if (mCurMethod != null) {
1004 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1005 MSG_UNBIND_INPUT, mCurMethod));
1006 }
1007 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001008
1009 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1010 MSG_SET_ACTIVE, 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1012 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1013 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001014 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001015
The Android Open Source Project10592532009-03-18 17:39:46 -07001016 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001017 }
1018 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001019
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001020 private int getImeShowFlags() {
1021 int flags = 0;
1022 if (mShowForced) {
1023 flags |= InputMethod.SHOW_FORCED
1024 | InputMethod.SHOW_EXPLICIT;
1025 } else if (mShowExplicitlyRequested) {
1026 flags |= InputMethod.SHOW_EXPLICIT;
1027 }
1028 return flags;
1029 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001030
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031 private int getAppShowFlags() {
1032 int flags = 0;
1033 if (mShowForced) {
1034 flags |= InputMethodManager.SHOW_FORCED;
1035 } else if (!mShowExplicitlyRequested) {
1036 flags |= InputMethodManager.SHOW_IMPLICIT;
1037 }
1038 return flags;
1039 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001040
Dianne Hackborn7663d802012-02-24 13:08:49 -08001041 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 if (!mBoundToMethod) {
1043 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1044 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1045 mBoundToMethod = true;
1046 }
1047 final SessionState session = mCurClient.curSession;
1048 if (initial) {
1049 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1050 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
1051 } else {
1052 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1053 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
1054 }
1055 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001056 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001057 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001058 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001059 return new InputBindResult(session.session, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062 InputBindResult startInputLocked(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001063 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064 // If no method is currently selected, do nothing.
1065 if (mCurMethodId == null) {
1066 return mNoBinding;
1067 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001068
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069 ClientState cs = mClients.get(client.asBinder());
1070 if (cs == null) {
1071 throw new IllegalArgumentException("unknown client "
1072 + client.asBinder());
1073 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001074
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001075 try {
1076 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1077 // Check with the window manager to make sure this client actually
1078 // has a window with focus. If not, reject. This is thread safe
1079 // because if the focus changes some time before or after, the
1080 // next client receiving focus that has any interest in input will
1081 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001082 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1084 return null;
1085 }
1086 } catch (RemoteException e) {
1087 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001088
Dianne Hackborn7663d802012-02-24 13:08:49 -08001089 return startInputUncheckedLocked(cs, inputContext, attribute, controlFlags);
1090 }
1091
1092 InputBindResult startInputUncheckedLocked(ClientState cs,
1093 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
1094 // If no method is currently selected, do nothing.
1095 if (mCurMethodId == null) {
1096 return mNoBinding;
1097 }
1098
John Spurlockbc7b6fc2012-11-14 08:51:07 -05001099 if (mCurClient == null) {
1100 mInputBoundToKeyguard = mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1101 if (DEBUG) {
1102 Slog.v(TAG, "New bind. keyguard = " + mInputBoundToKeyguard);
1103 }
1104 }
1105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001106 if (mCurClient != cs) {
1107 // If the client is changing, we need to switch over to the new
1108 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001109 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001110 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 + cs.client.asBinder());
1112
1113 // If the screen is on, inform the new client it is active
1114 if (mScreenOn) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001115 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
1116 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001117 }
1118 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001119
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120 // Bump up the sequence for this client and attach it.
1121 mCurSeq++;
1122 if (mCurSeq <= 0) mCurSeq = 1;
1123 mCurClient = cs;
1124 mCurInputContext = inputContext;
1125 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001126
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001127 // Check if the input method is changing.
1128 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1129 if (cs.curSession != null) {
1130 // Fast case: if we are already connected to the input method,
1131 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001132 return attachNewInputLocked(
1133 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001134 }
1135 if (mHaveConnection) {
1136 if (mCurMethod != null) {
1137 if (!cs.sessionRequested) {
1138 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001139 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001140 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1141 MSG_CREATE_SESSION, mCurMethod,
Jean Chalarde0d32a62011-10-20 20:36:07 +09001142 new MethodCallback(mCurMethod, this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 }
1144 // Return to client, and we will get back with it when
1145 // we have had a session made for it.
1146 return new InputBindResult(null, mCurId, mCurSeq);
1147 } else if (SystemClock.uptimeMillis()
1148 < (mLastBindTime+TIME_TO_RECONNECT)) {
1149 // In this case we have connected to the service, but
1150 // don't yet have its interface. If it hasn't been too
1151 // long since we did the connection, we'll return to
1152 // the client and wait to get the service interface so
1153 // we can report back. If it has been too long, we want
1154 // to fall through so we can try a disconnect/reconnect
1155 // to see if we can get back in touch with the service.
1156 return new InputBindResult(null, mCurId, mCurSeq);
1157 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001158 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1159 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160 }
1161 }
1162 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001163
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001164 return startInputInnerLocked();
1165 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001166
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001167 InputBindResult startInputInnerLocked() {
1168 if (mCurMethodId == null) {
1169 return mNoBinding;
1170 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001171
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001172 if (!mSystemReady) {
1173 // If the system is not yet ready, we shouldn't be running third
1174 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -07001175 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001176 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001178 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1179 if (info == null) {
1180 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1181 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001182
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001183 unbindCurrentMethodLocked(false, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1186 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001187 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1188 com.android.internal.R.string.input_method_binding_label);
1189 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1190 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001191 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001192 | Context.BIND_NOT_VISIBLE)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001193 mLastBindTime = SystemClock.uptimeMillis();
1194 mHaveConnection = true;
1195 mCurId = info.getId();
1196 mCurToken = new Binder();
1197 try {
Craig Mautnerca0ac712013-03-14 09:43:02 -07001198 if (true || DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001199 mIWindowManager.addWindowToken(mCurToken,
1200 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1201 } catch (RemoteException e) {
1202 }
1203 return new InputBindResult(null, mCurId, mCurSeq);
1204 } else {
1205 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001206 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001207 + mCurIntent);
1208 }
1209 return null;
1210 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001211
satoke7c6998e2011-06-03 17:57:59 +09001212 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 public InputBindResult startInput(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001214 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001215 if (!calledFromValidUser()) {
1216 return null;
1217 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001218 synchronized (mMethodMap) {
1219 final long ident = Binder.clearCallingIdentity();
1220 try {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001221 return startInputLocked(client, inputContext, attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222 } finally {
1223 Binder.restoreCallingIdentity(ident);
1224 }
1225 }
1226 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001227
satoke7c6998e2011-06-03 17:57:59 +09001228 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 public void finishInput(IInputMethodClient client) {
1230 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001231
satoke7c6998e2011-06-03 17:57:59 +09001232 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001233 public void onServiceConnected(ComponentName name, IBinder service) {
1234 synchronized (mMethodMap) {
1235 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1236 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001237 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001238 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001239 unbindCurrentMethodLocked(false, false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001240 return;
1241 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001242 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001243 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1244 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001246 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -07001247 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001248 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -07001249 MSG_CREATE_SESSION, mCurMethod,
Jean Chalarde0d32a62011-10-20 20:36:07 +09001250 new MethodCallback(mCurMethod, this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001251 }
1252 }
1253 }
1254 }
1255
1256 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
1257 synchronized (mMethodMap) {
1258 if (mCurMethod != null && method != null
1259 && mCurMethod.asBinder() == method.asBinder()) {
1260 if (mCurClient != null) {
1261 mCurClient.curSession = new SessionState(mCurClient,
1262 method, session);
1263 mCurClient.sessionRequested = false;
Dianne Hackborn7663d802012-02-24 13:08:49 -08001264 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001265 if (res.method != null) {
1266 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
1267 MSG_BIND_METHOD, mCurClient.client, res));
1268 }
1269 }
1270 }
1271 }
1272 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001273
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001274 void unbindCurrentMethodLocked(boolean reportToClient, boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001275 if (mVisibleBound) {
1276 mContext.unbindService(mVisibleConnection);
1277 mVisibleBound = false;
1278 }
1279
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001280 if (mHaveConnection) {
1281 mContext.unbindService(this);
1282 mHaveConnection = false;
1283 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001284
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001285 if (mCurToken != null) {
1286 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001287 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001288 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001289 // The current IME is shown. Hence an IME switch (transition) is happening.
1290 mWindowManagerService.saveLastInputMethodWindowForTransition();
1291 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001292 mIWindowManager.removeWindowToken(mCurToken);
1293 } catch (RemoteException e) {
1294 }
1295 mCurToken = null;
1296 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001297
The Android Open Source Project10592532009-03-18 17:39:46 -07001298 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001299 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001300
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001301 if (reportToClient && mCurClient != null) {
1302 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1303 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1304 }
1305 }
1306
Devin Taylor0c33ed22010-02-23 13:26:46 -06001307 private void finishSession(SessionState sessionState) {
1308 if (sessionState != null && sessionState.session != null) {
1309 try {
1310 sessionState.session.finishSession();
1311 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -07001312 Slog.w(TAG, "Session failed to close due to remote exception", e);
satok15452a42011-10-28 17:58:28 +09001313 setImeWindowVisibilityStatusHiddenLocked();
Devin Taylor0c33ed22010-02-23 13:26:46 -06001314 }
1315 }
1316 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001317
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001318 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001319 if (mCurMethod != null) {
1320 for (ClientState cs : mClients.values()) {
1321 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001322 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001323 cs.curSession = null;
1324 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001325
1326 finishSession(mEnabledSession);
1327 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001328 mCurMethod = null;
1329 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001330 if (mStatusBar != null) {
1331 mStatusBar.setIconVisibility("ime", false);
1332 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001333 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001334
satoke7c6998e2011-06-03 17:57:59 +09001335 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001336 public void onServiceDisconnected(ComponentName name) {
1337 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001338 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001339 + " mCurIntent=" + mCurIntent);
1340 if (mCurMethod != null && mCurIntent != null
1341 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001342 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001343 // We consider this to be a new bind attempt, since the system
1344 // should now try to restart the service for us.
1345 mLastBindTime = SystemClock.uptimeMillis();
1346 mShowRequested = mInputShown;
1347 mInputShown = false;
1348 if (mCurClient != null) {
1349 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1350 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1351 }
1352 }
1353 }
1354 }
1355
satokf9f01002011-05-19 21:31:50 +09001356 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001357 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001358 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001359 long ident = Binder.clearCallingIdentity();
1360 try {
1361 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001362 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001363 return;
1364 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001366 synchronized (mMethodMap) {
1367 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001368 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001369 if (mStatusBar != null) {
1370 mStatusBar.setIconVisibility("ime", false);
1371 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001372 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001373 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001374 CharSequence contentDescription = null;
1375 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001376 // Use PackageManager to load label
1377 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001378 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001379 mIPackageManager.getApplicationInfo(packageName, 0,
1380 mSettings.getCurrentUserId()));
1381 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001382 /* ignore */
1383 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001384 if (mStatusBar != null) {
1385 mStatusBar.setIcon("ime", packageName, iconId, 0,
1386 contentDescription != null
1387 ? contentDescription.toString() : null);
1388 mStatusBar.setIconVisibility("ime", true);
1389 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001390 }
1391 }
1392 } finally {
1393 Binder.restoreCallingIdentity(ident);
1394 }
1395 }
1396
satok7cfc0ed2011-06-20 21:29:36 +09001397 private boolean needsToShowImeSwitchOngoingNotification() {
1398 if (!mShowOngoingImeSwitcherForPhones) return false;
satok2c93efc2012-04-02 19:33:47 +09001399 if (isScreenLocked()) return false;
satok7cfc0ed2011-06-20 21:29:36 +09001400 synchronized (mMethodMap) {
1401 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1402 final int N = imis.size();
satokb6359412011-06-28 17:47:41 +09001403 if (N > 2) return true;
1404 if (N < 1) return false;
1405 int nonAuxCount = 0;
1406 int auxCount = 0;
1407 InputMethodSubtype nonAuxSubtype = null;
1408 InputMethodSubtype auxSubtype = null;
satok7cfc0ed2011-06-20 21:29:36 +09001409 for(int i = 0; i < N; ++i) {
1410 final InputMethodInfo imi = imis.get(i);
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09001411 final List<InputMethodSubtype> subtypes =
1412 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satok7cfc0ed2011-06-20 21:29:36 +09001413 final int subtypeCount = subtypes.size();
1414 if (subtypeCount == 0) {
satokb6359412011-06-28 17:47:41 +09001415 ++nonAuxCount;
satok7cfc0ed2011-06-20 21:29:36 +09001416 } else {
1417 for (int j = 0; j < subtypeCount; ++j) {
satokb6359412011-06-28 17:47:41 +09001418 final InputMethodSubtype subtype = subtypes.get(j);
1419 if (!subtype.isAuxiliary()) {
1420 ++nonAuxCount;
1421 nonAuxSubtype = subtype;
1422 } else {
1423 ++auxCount;
1424 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001425 }
1426 }
1427 }
satok7cfc0ed2011-06-20 21:29:36 +09001428 }
satokb6359412011-06-28 17:47:41 +09001429 if (nonAuxCount > 1 || auxCount > 1) {
1430 return true;
1431 } else if (nonAuxCount == 1 && auxCount == 1) {
1432 if (nonAuxSubtype != null && auxSubtype != null
satok9747f892011-09-12 15:56:40 +09001433 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1434 || auxSubtype.overridesImplicitlyEnabledSubtype()
1435 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
satokb6359412011-06-28 17:47:41 +09001436 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1437 return false;
1438 }
1439 return true;
1440 }
1441 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001442 }
satok7cfc0ed2011-06-20 21:29:36 +09001443 }
1444
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001445 // Caution! This method is called in this class. Handle multi-user carefully
satokdbf29502011-08-25 15:28:23 +09001446 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001447 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001448 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001449 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001450 try {
1451 if (token == null || mCurToken != token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001452 int uid = Binder.getCallingUid();
Joe Onorato857fd9b2011-01-27 15:08:35 -08001453 Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token);
satok06487a52010-10-29 11:37:18 +09001454 return;
1455 }
1456
1457 synchronized (mMethodMap) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001458 mImeWindowVis = vis;
1459 mBackDisposition = backDisposition;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001460 if (mStatusBar != null) {
1461 mStatusBar.setImeWindowStatus(token, vis, backDisposition);
1462 }
satok7cfc0ed2011-06-20 21:29:36 +09001463 final boolean iconVisibility = (vis & InputMethodService.IME_ACTIVE) != 0;
satok5bc8e732011-07-22 21:07:23 +09001464 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1465 if (imi != null && iconVisibility && needsToShowImeSwitchOngoingNotification()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001466 // Used to load label
satok7cfc0ed2011-06-20 21:29:36 +09001467 final PackageManager pm = mContext.getPackageManager();
satok7cfc0ed2011-06-20 21:29:36 +09001468 final CharSequence title = mRes.getText(
1469 com.android.internal.R.string.select_input_method);
satok5bc8e732011-07-22 21:07:23 +09001470 final CharSequence imiLabel = imi.loadLabel(pm);
1471 final CharSequence summary = mCurrentSubtype != null
1472 ? TextUtils.concat(mCurrentSubtype.getDisplayName(mContext,
1473 imi.getPackageName(), imi.getServiceInfo().applicationInfo),
1474 (TextUtils.isEmpty(imiLabel) ?
Ken Wakasa05dbb652011-08-22 15:22:43 +09001475 "" : " - " + imiLabel))
satok5bc8e732011-07-22 21:07:23 +09001476 : imiLabel;
1477
satok7cfc0ed2011-06-20 21:29:36 +09001478 mImeSwitcherNotification.setLatestEventInfo(
satok5bc8e732011-07-22 21:07:23 +09001479 mContext, title, summary, mImeSwitchPendingIntent);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001480 if (mNotificationManager != null) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001481 if (DEBUG) {
1482 Slog.d(TAG, "--- show notification: label = " + imiLabel
1483 + ", summary = " + summary);
1484 }
1485 mNotificationManager.notifyAsUser(null,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001486 com.android.internal.R.string.select_input_method,
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001487 mImeSwitcherNotification, UserHandle.ALL);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001488 mNotificationShown = true;
1489 }
satok7cfc0ed2011-06-20 21:29:36 +09001490 } else {
Dianne Hackborn661cd522011-08-22 00:26:20 -07001491 if (mNotificationShown && mNotificationManager != null) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001492 if (DEBUG) {
1493 Slog.d(TAG, "--- hide notification");
1494 }
1495 mNotificationManager.cancelAsUser(null,
1496 com.android.internal.R.string.select_input_method, UserHandle.ALL);
satok7cfc0ed2011-06-20 21:29:36 +09001497 mNotificationShown = false;
1498 }
1499 }
satok06487a52010-10-29 11:37:18 +09001500 }
1501 } finally {
1502 Binder.restoreCallingIdentity(ident);
1503 }
1504 }
1505
satoke7c6998e2011-06-03 17:57:59 +09001506 @Override
satokf9f01002011-05-19 21:31:50 +09001507 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001508 if (!calledFromValidUser()) {
1509 return;
1510 }
satokf9f01002011-05-19 21:31:50 +09001511 synchronized (mMethodMap) {
1512 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1513 for (int i = 0; i < spans.length; ++i) {
1514 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001515 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001516 mSecureSuggestionSpans.put(ss, currentImi);
1517 }
1518 }
1519 }
1520 }
1521
satoke7c6998e2011-06-03 17:57:59 +09001522 @Override
satokf9f01002011-05-19 21:31:50 +09001523 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001524 if (!calledFromValidUser()) {
1525 return false;
1526 }
satokf9f01002011-05-19 21:31:50 +09001527 synchronized (mMethodMap) {
1528 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1529 // TODO: Do not send the intent if the process of the targetImi is already dead.
1530 if (targetImi != null) {
1531 final String[] suggestions = span.getSuggestions();
1532 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001533 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001534 final Intent intent = new Intent();
1535 // Ensures that only a class in the original IME package will receive the
1536 // notification.
satok42c5a162011-05-26 16:46:14 +09001537 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001538 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1539 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1540 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1541 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001542 final long ident = Binder.clearCallingIdentity();
1543 try {
1544 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1545 } finally {
1546 Binder.restoreCallingIdentity(ident);
1547 }
satokf9f01002011-05-19 21:31:50 +09001548 return true;
1549 }
1550 }
1551 return false;
1552 }
1553
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001554 void updateFromSettingsLocked(boolean enabledMayChange) {
1555 if (enabledMayChange) {
1556 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1557 for (int i=0; i<enabled.size(); i++) {
1558 // We allow the user to select "disabled until used" apps, so if they
1559 // are enabling one of those here we now need to make it enabled.
1560 InputMethodInfo imm = enabled.get(i);
1561 try {
1562 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1563 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1564 mSettings.getCurrentUserId());
1565 if (ai.enabledSetting
1566 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
1567 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1568 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
1569 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId());
1570 }
1571 } catch (RemoteException e) {
1572 }
1573 }
1574 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001575 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1576 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1577 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1578 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001579 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001580 // There is no input method selected, try to choose new applicable input method.
1581 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001582 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001583 }
1584 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001585 try {
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09001586 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001587 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001588 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001589 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001590 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001591 }
satokf3db1af2010-11-23 13:34:33 +09001592 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001593 } else {
1594 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001595 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001596 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001597 }
1598 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001599
satokab751aa2010-09-14 19:17:36 +09001600 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001601 InputMethodInfo info = mMethodMap.get(id);
1602 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001603 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001604 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001605
satokd81e9502012-05-21 12:58:45 +09001606 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001607 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09001608 final int subtypeCount = info.getSubtypeCount();
1609 if (subtypeCount <= 0) {
1610 return;
satokcd7cd292010-11-20 15:46:23 +09001611 }
satokd81e9502012-05-21 12:58:45 +09001612 final InputMethodSubtype oldSubtype = mCurrentSubtype;
1613 final InputMethodSubtype newSubtype;
1614 if (subtypeId >= 0 && subtypeId < subtypeCount) {
1615 newSubtype = info.getSubtypeAt(subtypeId);
1616 } else {
1617 // If subtype is null, try to find the most applicable one from
1618 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001619 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09001620 }
1621 if (newSubtype == null || oldSubtype == null) {
1622 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
1623 + ", new subtype = " + newSubtype);
1624 return;
1625 }
1626 if (newSubtype != oldSubtype) {
1627 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1628 if (mCurMethod != null) {
1629 try {
1630 refreshImeWindowVisibilityLocked();
1631 mCurMethod.changeInputMethodSubtype(newSubtype);
1632 } catch (RemoteException e) {
1633 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
satokab751aa2010-09-14 19:17:36 +09001634 }
1635 }
1636 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001637 return;
1638 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001639
satokd81e9502012-05-21 12:58:45 +09001640 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001641 final long ident = Binder.clearCallingIdentity();
1642 try {
satokab751aa2010-09-14 19:17:36 +09001643 // Set a subtype to this input method.
1644 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001645 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1646 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1647 // because mCurMethodId is stored as a history in
1648 // setSelectedInputMethodAndSubtypeLocked().
1649 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001650
1651 if (ActivityManagerNative.isSystemReady()) {
1652 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001653 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001654 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07001655 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001656 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001657 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001658 } finally {
1659 Binder.restoreCallingIdentity(ident);
1660 }
1661 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001662
satok42c5a162011-05-26 16:46:14 +09001663 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001664 public boolean showSoftInput(IInputMethodClient client, int flags,
1665 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001666 if (!calledFromValidUser()) {
1667 return false;
1668 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001669 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001670 long ident = Binder.clearCallingIdentity();
1671 try {
1672 synchronized (mMethodMap) {
1673 if (mCurClient == null || client == null
1674 || mCurClient.client.asBinder() != client.asBinder()) {
1675 try {
1676 // We need to check if this is the current client with
1677 // focus in the window manager, to allow this call to
1678 // be made before input is started in it.
1679 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001680 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001681 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682 }
1683 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001684 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001685 }
1686 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001687
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001688 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001689 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001690 }
1691 } finally {
1692 Binder.restoreCallingIdentity(ident);
1693 }
1694 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001695
The Android Open Source Project4df24232009-03-05 14:34:35 -08001696 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001697 mShowRequested = true;
1698 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1699 mShowExplicitlyRequested = true;
1700 }
1701 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1702 mShowExplicitlyRequested = true;
1703 mShowForced = true;
1704 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001705
Dianne Hackborncc278702009-09-02 23:07:23 -07001706 if (!mSystemReady) {
1707 return false;
1708 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001709
The Android Open Source Project4df24232009-03-05 14:34:35 -08001710 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001711 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001712 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001713 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1714 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1715 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001716 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001717 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001718 bindCurrentInputMethodService(
1719 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001720 mVisibleBound = true;
1721 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001722 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001723 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09001724 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001725 // The client has asked to have the input method shown, but
1726 // we have been sitting here too long with a connection to the
1727 // service and no interface received, so let's disconnect/connect
1728 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001729 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001730 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09001731 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001732 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001733 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001734 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001735 } else {
1736 if (DEBUG) {
1737 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
1738 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
1739 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001740 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001741
The Android Open Source Project4df24232009-03-05 14:34:35 -08001742 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001743 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001744
satok42c5a162011-05-26 16:46:14 +09001745 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001746 public boolean hideSoftInput(IInputMethodClient client, int flags,
1747 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001748 if (!calledFromValidUser()) {
1749 return false;
1750 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001751 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001752 long ident = Binder.clearCallingIdentity();
1753 try {
1754 synchronized (mMethodMap) {
1755 if (mCurClient == null || client == null
1756 || mCurClient.client.asBinder() != client.asBinder()) {
1757 try {
1758 // We need to check if this is the current client with
1759 // focus in the window manager, to allow this call to
1760 // be made before input is started in it.
1761 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001762 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1763 + uid + ": " + client);
satok15452a42011-10-28 17:58:28 +09001764 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001765 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001766 }
1767 } catch (RemoteException e) {
satok15452a42011-10-28 17:58:28 +09001768 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001769 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001770 }
1771 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001772
Joe Onorato8a9b2202010-02-26 18:56:32 -08001773 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001774 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001775 }
1776 } finally {
1777 Binder.restoreCallingIdentity(ident);
1778 }
1779 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001780
The Android Open Source Project4df24232009-03-05 14:34:35 -08001781 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001782 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1783 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001784 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 -08001785 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001786 }
1787 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001788 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 -08001789 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001790 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001791 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001792 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001793 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1794 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1795 res = true;
1796 } else {
1797 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001798 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001799 if (mHaveConnection && mVisibleBound) {
1800 mContext.unbindService(mVisibleConnection);
1801 mVisibleBound = false;
1802 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001803 mInputShown = false;
1804 mShowRequested = false;
1805 mShowExplicitlyRequested = false;
1806 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001807 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001808 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001809
satok42c5a162011-05-26 16:46:14 +09001810 @Override
Dianne Hackborn7663d802012-02-24 13:08:49 -08001811 public InputBindResult windowGainedFocus(IInputMethodClient client, IBinder windowToken,
1812 int controlFlags, int softInputMode, int windowFlags,
1813 EditorInfo attribute, IInputContext inputContext) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09001814 // Needs to check the validity before clearing calling identity
1815 final boolean calledFromValidUser = calledFromValidUser();
1816
Dianne Hackborn7663d802012-02-24 13:08:49 -08001817 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001818 long ident = Binder.clearCallingIdentity();
1819 try {
1820 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001821 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
Dianne Hackborn7663d802012-02-24 13:08:49 -08001822 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001823 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08001824 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001825
Dianne Hackborn7663d802012-02-24 13:08:49 -08001826 ClientState cs = mClients.get(client.asBinder());
1827 if (cs == null) {
1828 throw new IllegalArgumentException("unknown client "
1829 + client.asBinder());
1830 }
1831
1832 try {
1833 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1834 // Check with the window manager to make sure this client actually
1835 // has a window with focus. If not, reject. This is thread safe
1836 // because if the focus changes some time before or after, the
1837 // next client receiving focus that has any interest in input will
1838 // be calling through here after that change happens.
1839 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
1840 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1841 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001842 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001843 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001844 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001845
Satoshi Kataoka8d033052012-11-19 17:30:40 +09001846 if (!calledFromValidUser) {
1847 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
1848 Slog.w(TAG, "If you want to interect with IME, you need "
1849 + "android.permission.INTERACT_ACROSS_USERS_FULL");
1850 hideCurrentInputLocked(0, null);
1851 return null;
1852 }
1853
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001854 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07001855 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09001856 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001857 if (attribute != null) {
1858 return startInputUncheckedLocked(cs, inputContext, attribute,
1859 controlFlags);
1860 }
1861 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001862 }
1863 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001864
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001865 // Should we auto-show the IME even if the caller has not
1866 // specified what should be done with it?
1867 // We only do this automatically if the window can resize
1868 // to accommodate the IME (so what the user sees will give
1869 // them good context without input information being obscured
1870 // by the IME) or if running on a large screen where there
1871 // is more room for the target window + IME.
1872 final boolean doAutoShow =
1873 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1874 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1875 || mRes.getConfiguration().isLayoutSizeAtLeast(
1876 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001877 final boolean isTextEditor =
1878 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
1879
1880 // We want to start input before showing the IME, but after closing
1881 // it. We want to do this after closing it to help the IME disappear
1882 // more quickly (not get stuck behind it initializing itself for the
1883 // new focused input, even if its window wants to hide the IME).
1884 boolean didStart = false;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001885
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001886 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1887 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001888 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001889 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1890 // There is no focus view, and this window will
1891 // be behind any soft input window, so hide the
1892 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001893 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001894 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001895 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001896 } else if (isTextEditor && doAutoShow && (softInputMode &
1897 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001898 // There is a focus view, and we are navigating forward
1899 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001900 // We only do this automatically if the window can resize
1901 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001902 // them good context without input information being obscured
1903 // by the IME) or if running on a large screen where there
1904 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001905 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001906 if (attribute != null) {
1907 res = startInputUncheckedLocked(cs, inputContext, attribute,
1908 controlFlags);
1909 didStart = true;
1910 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001911 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001912 }
1913 break;
1914 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1915 // Do nothing.
1916 break;
1917 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1918 if ((softInputMode &
1919 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001920 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001921 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001922 }
1923 break;
1924 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001925 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001926 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001927 break;
1928 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1929 if ((softInputMode &
1930 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001931 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001932 if (attribute != null) {
1933 res = startInputUncheckedLocked(cs, inputContext, attribute,
1934 controlFlags);
1935 didStart = true;
1936 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001937 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001938 }
1939 break;
1940 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001941 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001942 if (attribute != null) {
1943 res = startInputUncheckedLocked(cs, inputContext, attribute,
1944 controlFlags);
1945 didStart = true;
1946 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001947 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001948 break;
1949 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001950
1951 if (!didStart && attribute != null) {
1952 res = startInputUncheckedLocked(cs, inputContext, attribute,
1953 controlFlags);
1954 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001955 }
1956 } finally {
1957 Binder.restoreCallingIdentity(ident);
1958 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001959
1960 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001961 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001962
satok42c5a162011-05-26 16:46:14 +09001963 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001964 public void showInputMethodPickerFromClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001965 if (!calledFromValidUser()) {
1966 return;
1967 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001968 synchronized (mMethodMap) {
1969 if (mCurClient == null || client == null
1970 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001971 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001972 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001973 }
1974
satok440aab52010-11-25 09:43:11 +09001975 // Always call subtype picker, because subtype picker is a superset of input method
1976 // picker.
satokab751aa2010-09-14 19:17:36 +09001977 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1978 }
1979 }
1980
satok42c5a162011-05-26 16:46:14 +09001981 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001982 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001983 if (!calledFromValidUser()) {
1984 return;
1985 }
satok28203512010-11-24 11:06:49 +09001986 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
1987 }
1988
satok42c5a162011-05-26 16:46:14 +09001989 @Override
satok28203512010-11-24 11:06:49 +09001990 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001991 if (!calledFromValidUser()) {
1992 return;
1993 }
satok28203512010-11-24 11:06:49 +09001994 synchronized (mMethodMap) {
1995 if (subtype != null) {
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09001996 setInputMethodWithSubtypeId(token, id, InputMethodUtils.getSubtypeIdFromHashCode(
satok28203512010-11-24 11:06:49 +09001997 mMethodMap.get(id), subtype.hashCode()));
1998 } else {
1999 setInputMethod(token, id);
2000 }
2001 }
satokab751aa2010-09-14 19:17:36 +09002002 }
2003
satok42c5a162011-05-26 16:46:14 +09002004 @Override
satokb416a71e2010-11-25 20:42:14 +09002005 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002006 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002007 if (!calledFromValidUser()) {
2008 return;
2009 }
satokb416a71e2010-11-25 20:42:14 +09002010 synchronized (mMethodMap) {
2011 if (mCurClient == null || client == null
2012 || mCurClient.client.asBinder() != client.asBinder()) {
2013 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
2014 }
satok7fee71f2010-12-17 18:54:26 +09002015 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2016 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a71e2010-11-25 20:42:14 +09002017 }
2018 }
2019
satok4fc87d62011-05-20 16:13:43 +09002020 @Override
satok735cf382010-11-11 20:40:09 +09002021 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002022 if (!calledFromValidUser()) {
2023 return false;
2024 }
satok735cf382010-11-11 20:40:09 +09002025 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002026 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002027 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002028 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002029 lastImi = mMethodMap.get(lastIme.first);
2030 } else {
2031 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002032 }
satok4fc87d62011-05-20 16:13:43 +09002033 String targetLastImiId = null;
2034 int subtypeId = NOT_A_SUBTYPE_ID;
2035 if (lastIme != null && lastImi != null) {
2036 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
2037 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
2038 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2039 : mCurrentSubtype.hashCode();
2040 // If the last IME is the same as the current IME and the last subtype is not
2041 // defined, there is no need to switch to the last IME.
2042 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2043 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002044 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002045 }
2046 }
2047
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002048 if (TextUtils.isEmpty(targetLastImiId)
2049 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002050 // This is a safety net. If the currentSubtype can't be added to the history
2051 // and the framework couldn't find the last ime, we will make the last ime be
2052 // the most applicable enabled keyboard subtype of the system imes.
2053 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2054 if (enabled != null) {
2055 final int N = enabled.size();
2056 final String locale = mCurrentSubtype == null
2057 ? mRes.getConfiguration().locale.toString()
2058 : mCurrentSubtype.getLocale();
2059 for (int i = 0; i < N; ++i) {
2060 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002061 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002062 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002063 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2064 InputMethodUtils.getSubtypes(imi),
2065 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002066 if (keyboardSubtype != null) {
2067 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002068 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002069 imi, keyboardSubtype.hashCode());
2070 if(keyboardSubtype.getLocale().equals(locale)) {
2071 break;
2072 }
2073 }
2074 }
2075 }
2076 }
2077 }
2078
2079 if (!TextUtils.isEmpty(targetLastImiId)) {
2080 if (DEBUG) {
2081 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2082 + ", from: " + mCurMethodId + ", " + subtypeId);
2083 }
2084 setInputMethodWithSubtypeId(token, targetLastImiId, subtypeId);
2085 return true;
2086 } else {
2087 return false;
2088 }
satok735cf382010-11-11 20:40:09 +09002089 }
2090 }
2091
satoke7c6998e2011-06-03 17:57:59 +09002092 @Override
satok688bd472012-02-09 20:09:17 +09002093 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002094 if (!calledFromValidUser()) {
2095 return false;
2096 }
satok688bd472012-02-09 20:09:17 +09002097 synchronized (mMethodMap) {
2098 final ImeSubtypeListItem nextSubtype = mImListManager.getNextInputMethod(
2099 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2100 if (nextSubtype == null) {
2101 return false;
2102 }
2103 setInputMethodWithSubtypeId(token, nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
2104 return true;
2105 }
2106 }
2107
2108 @Override
satok68f1b782011-04-11 14:26:04 +09002109 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002110 if (!calledFromValidUser()) {
2111 return null;
2112 }
satok68f1b782011-04-11 14:26:04 +09002113 synchronized (mMethodMap) {
2114 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2115 // TODO: Handle the case of the last IME with no subtypes
2116 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2117 || TextUtils.isEmpty(lastIme.second)) return null;
2118 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2119 if (lastImi == null) return null;
2120 try {
2121 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002122 final int lastSubtypeId =
2123 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002124 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2125 return null;
2126 }
2127 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002128 } catch (NumberFormatException e) {
2129 return null;
2130 }
2131 }
2132 }
2133
satoke7c6998e2011-06-03 17:57:59 +09002134 @Override
satokee5e77c2011-09-02 18:50:15 +09002135 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002136 if (!calledFromValidUser()) {
2137 return;
2138 }
satok91e88122011-07-18 11:11:42 +09002139 // By this IPC call, only a process which shares the same uid with the IME can add
2140 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09002141 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09002142 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002143 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002144 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002145 final String[] packageInfos;
2146 try {
2147 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2148 } catch (RemoteException e) {
2149 Slog.e(TAG, "Failed to get package infos");
2150 return;
2151 }
satok91e88122011-07-18 11:11:42 +09002152 if (packageInfos != null) {
2153 final int packageNum = packageInfos.length;
2154 for (int i = 0; i < packageNum; ++i) {
2155 if (packageInfos[i].equals(imi.getPackageName())) {
2156 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002157 final long ident = Binder.clearCallingIdentity();
2158 try {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002159 buildInputMethodListLocked(mMethodList, mMethodMap,
2160 false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002161 } finally {
2162 Binder.restoreCallingIdentity(ident);
2163 }
satokee5e77c2011-09-02 18:50:15 +09002164 return;
satok91e88122011-07-18 11:11:42 +09002165 }
2166 }
2167 }
satoke7c6998e2011-06-03 17:57:59 +09002168 }
satokee5e77c2011-09-02 18:50:15 +09002169 return;
satoke7c6998e2011-06-03 17:57:59 +09002170 }
2171
satok28203512010-11-24 11:06:49 +09002172 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002173 synchronized (mMethodMap) {
2174 if (token == null) {
2175 if (mContext.checkCallingOrSelfPermission(
2176 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2177 != PackageManager.PERMISSION_GRANTED) {
2178 throw new SecurityException(
2179 "Using null token requires permission "
2180 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2181 }
2182 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002183 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2184 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002185 return;
2186 }
2187
satokc5933802011-08-31 21:26:04 +09002188 final long ident = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002189 try {
satokab751aa2010-09-14 19:17:36 +09002190 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002191 } finally {
2192 Binder.restoreCallingIdentity(ident);
2193 }
2194 }
2195 }
2196
satok42c5a162011-05-26 16:46:14 +09002197 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002198 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002199 if (!calledFromValidUser()) {
2200 return;
2201 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002202 synchronized (mMethodMap) {
2203 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002204 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
2205 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002206 return;
2207 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002208 long ident = Binder.clearCallingIdentity();
2209 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002210 hideCurrentInputLocked(flags, null);
2211 } finally {
2212 Binder.restoreCallingIdentity(ident);
2213 }
2214 }
2215 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002216
satok42c5a162011-05-26 16:46:14 +09002217 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002218 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002219 if (!calledFromValidUser()) {
2220 return;
2221 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002222 synchronized (mMethodMap) {
2223 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002224 Slog.w(TAG, "Ignoring showMySoftInput of uid "
2225 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002226 return;
2227 }
2228 long ident = Binder.clearCallingIdentity();
2229 try {
2230 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002231 } finally {
2232 Binder.restoreCallingIdentity(ident);
2233 }
2234 }
2235 }
2236
2237 void setEnabledSessionInMainThread(SessionState session) {
2238 if (mEnabledSession != session) {
2239 if (mEnabledSession != null) {
2240 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002241 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002242 mEnabledSession.method.setSessionEnabled(
2243 mEnabledSession.session, false);
2244 } catch (RemoteException e) {
2245 }
2246 }
2247 mEnabledSession = session;
2248 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002249 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002250 session.method.setSessionEnabled(
2251 session.session, true);
2252 } catch (RemoteException e) {
2253 }
2254 }
2255 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002256
satok42c5a162011-05-26 16:46:14 +09002257 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002258 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002259 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002260 switch (msg.what) {
2261 case MSG_SHOW_IM_PICKER:
2262 showInputMethodMenu();
2263 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002264
satokab751aa2010-09-14 19:17:36 +09002265 case MSG_SHOW_IM_SUBTYPE_PICKER:
2266 showInputMethodSubtypeMenu();
2267 return true;
2268
satok47a44912010-10-06 16:03:58 +09002269 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002270 args = (SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09002271 showInputMethodAndSubtypeEnabler((String)args.arg1);
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002272 args.recycle();
satok217f5482010-12-15 05:19:19 +09002273 return true;
2274
2275 case MSG_SHOW_IM_CONFIG:
2276 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002277 return true;
2278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002279 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002281 case MSG_UNBIND_INPUT:
2282 try {
2283 ((IInputMethod)msg.obj).unbindInput();
2284 } catch (RemoteException e) {
2285 // There is nothing interesting about the method dying.
2286 }
2287 return true;
2288 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002289 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002290 try {
2291 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2292 } catch (RemoteException e) {
2293 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002294 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002295 return true;
2296 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002297 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002298 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002299 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002300 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002301 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002302 } catch (RemoteException e) {
2303 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002304 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002305 return true;
2306 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002307 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002308 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002309 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002310 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002311 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002312 } catch (RemoteException e) {
2313 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002314 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002315 return true;
2316 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002317 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002318 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002319 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002320 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2321 } catch (RemoteException e) {
2322 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002323 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002324 return true;
2325 case MSG_CREATE_SESSION:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002326 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002327 try {
2328 ((IInputMethod)args.arg1).createSession(
Michael Wright52a53522013-03-14 10:59:38 -07002329 (IInputSessionCallback)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002330 } catch (RemoteException e) {
2331 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002332 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002333 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002334 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002335
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002336 case MSG_START_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002337 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002338 try {
2339 SessionState session = (SessionState)args.arg1;
2340 setEnabledSessionInMainThread(session);
2341 session.method.startInput((IInputContext)args.arg2,
2342 (EditorInfo)args.arg3);
2343 } catch (RemoteException e) {
2344 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002345 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002346 return true;
2347 case MSG_RESTART_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002348 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002349 try {
2350 SessionState session = (SessionState)args.arg1;
2351 setEnabledSessionInMainThread(session);
2352 session.method.restartInput((IInputContext)args.arg2,
2353 (EditorInfo)args.arg3);
2354 } catch (RemoteException e) {
2355 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002356 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002357 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002359 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002361 case MSG_UNBIND_METHOD:
2362 try {
2363 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
2364 } catch (RemoteException e) {
2365 // There is nothing interesting about the last client dying.
2366 }
2367 return true;
2368 case MSG_BIND_METHOD:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002369 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002370 try {
2371 ((IInputMethodClient)args.arg1).onBindMethod(
2372 (InputBindResult)args.arg2);
2373 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002374 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002375 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002376 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002377 return true;
Dianne Hackborna6e41342012-05-22 16:30:34 -07002378 case MSG_SET_ACTIVE:
2379 try {
2380 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2381 } catch (RemoteException e) {
2382 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2383 + ((ClientState)msg.obj).pid + " uid "
2384 + ((ClientState)msg.obj).uid);
2385 }
2386 return true;
satok01038492012-04-09 21:08:27 +09002387
2388 // --------------------------------------------------------------
2389 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
2390 mHardKeyboardListener.handleHardKeyboardStatusChange(
2391 msg.arg1 == 1, msg.arg2 == 1);
2392 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002393 }
2394 return false;
2395 }
2396
satokdc9ddae2011-10-06 12:22:36 +09002397 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002398 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
2399 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09002400 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002401 if (DEBUG) {
2402 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2403 }
satok723a27e2010-11-11 14:58:11 +09002404 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002405 return true;
2406 }
2407
2408 return false;
2409 }
2410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002411 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002412 HashMap<String, InputMethodInfo> map, boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002413 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002414 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
2415 + " \n ------ \n" + getStackTrace());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002416 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002417 list.clear();
2418 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002419
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002420 // Use for queryIntentServicesAsUser
2421 final PackageManager pm = mContext.getPackageManager();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002422 String disabledSysImes = mSettings.getDisabledSystemInputMethods();
Amith Yamasanie861ec12010-03-24 21:39:27 -07002423 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002424
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002425 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002426 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002427 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2428 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002429
satoke7c6998e2011-06-03 17:57:59 +09002430 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2431 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002432 for (int i = 0; i < services.size(); ++i) {
2433 ResolveInfo ri = services.get(i);
2434 ServiceInfo si = ri.serviceInfo;
2435 ComponentName compName = new ComponentName(si.packageName, si.name);
2436 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2437 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002438 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002439 + ": it does not require the permission "
2440 + android.Manifest.permission.BIND_INPUT_METHOD);
2441 continue;
2442 }
2443
Joe Onorato8a9b2202010-02-26 18:56:32 -08002444 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002445
2446 try {
satoke7c6998e2011-06-03 17:57:59 +09002447 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002448 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002449 final String id = p.getId();
2450 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002451
2452 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002453 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002454 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002456 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002457 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002458 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002459 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002460 }
2461 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002462
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002463 if (resetDefaultEnabledIme) {
2464 final ArrayList<InputMethodInfo> defaultEnabledIme =
2465 InputMethodUtils.getDefaultEnabledImes(mContext, mSystemReady, list);
2466 for (int i = 0; i < defaultEnabledIme.size(); ++i) {
2467 final InputMethodInfo imi = defaultEnabledIme.get(i);
2468 if (DEBUG) {
2469 Slog.d(TAG, "--- enable ime = " + imi);
2470 }
2471 setInputMethodEnabledLocked(imi.getId(), true);
2472 }
2473 }
2474
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002475 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09002476 if (!TextUtils.isEmpty(defaultImiId)) {
2477 if (!map.containsKey(defaultImiId)) {
2478 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
2479 if (chooseNewDefaultIMELocked()) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002480 updateFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09002481 }
2482 } else {
2483 // Double check that the default IME is certainly enabled.
2484 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002485 }
2486 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002487 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002488
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002489 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002490
satokab751aa2010-09-14 19:17:36 +09002491 private void showInputMethodMenu() {
2492 showInputMethodMenuInternal(false);
2493 }
2494
2495 private void showInputMethodSubtypeMenu() {
2496 showInputMethodMenuInternal(true);
2497 }
2498
satok217f5482010-12-15 05:19:19 +09002499 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002500 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002501 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002502 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2503 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002504 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002505 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002506 }
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002507 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok217f5482010-12-15 05:19:19 +09002508 }
2509
2510 private void showConfigureInputMethods() {
2511 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2512 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2513 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2514 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002515 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09002516 }
2517
satok2c93efc2012-04-02 19:33:47 +09002518 private boolean isScreenLocked() {
2519 return mKeyguardManager != null
2520 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
2521 }
satokab751aa2010-09-14 19:17:36 +09002522 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002523 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002524
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002525 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09002526 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002527
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002528 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002529 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002530 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002531
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002532 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002533 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
2534 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
satok7f35c8c2010-10-07 21:13:11 +09002535 if (immis == null || immis.size() == 0) {
2536 return;
2537 }
2538
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002539 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002540
satok688bd472012-02-09 20:09:17 +09002541 final List<ImeSubtypeListItem> imList =
2542 mImListManager.getSortedInputMethodAndSubtypeList(
2543 showSubtypes, mInputShown, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09002544
satokc3690562012-01-10 20:14:43 +09002545 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002546 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09002547 if (currentSubtype != null) {
2548 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002549 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
2550 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09002551 }
2552 }
2553
Ken Wakasa761eb372011-03-04 19:06:18 +09002554 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09002555 mIms = new InputMethodInfo[N];
2556 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002557 int checkedItem = 0;
2558 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002559 final ImeSubtypeListItem item = imList.get(i);
2560 mIms[i] = item.mImi;
2561 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002562 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002563 int subtypeId = mSubtypeIds[i];
2564 if ((subtypeId == NOT_A_SUBTYPE_ID)
2565 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2566 || (subtypeId == lastInputMethodSubtypeId)) {
2567 checkedItem = i;
2568 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002569 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002570 }
Ken Wakasa05dbb652011-08-22 15:22:43 +09002571 final TypedArray a = context.obtainStyledAttributes(null,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002572 com.android.internal.R.styleable.DialogPreference,
2573 com.android.internal.R.attr.alertDialogStyle, 0);
2574 mDialogBuilder = new AlertDialog.Builder(context)
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002575 .setOnCancelListener(new OnCancelListener() {
satok42c5a162011-05-26 16:46:14 +09002576 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002577 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002578 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002579 }
2580 })
2581 .setIcon(a.getDrawable(
2582 com.android.internal.R.styleable.DialogPreference_dialogTitle));
2583 a.recycle();
satok01038492012-04-09 21:08:27 +09002584 final LayoutInflater inflater =
2585 (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2586 final View tv = inflater.inflate(
2587 com.android.internal.R.layout.input_method_switch_dialog_title, null);
2588 mDialogBuilder.setCustomTitle(tv);
2589
2590 // Setup layout for a toggle switch of the hardware keyboard
2591 mSwitchingDialogTitleView = tv;
2592 mSwitchingDialogTitleView.findViewById(
2593 com.android.internal.R.id.hard_keyboard_section).setVisibility(
2594 mWindowManagerService.isHardKeyboardAvailable() ?
2595 View.VISIBLE : View.GONE);
2596 final Switch hardKeySwitch = ((Switch)mSwitchingDialogTitleView.findViewById(
2597 com.android.internal.R.id.hard_keyboard_switch));
2598 hardKeySwitch.setChecked(mWindowManagerService.isHardKeyboardEnabled());
2599 hardKeySwitch.setOnCheckedChangeListener(
2600 new OnCheckedChangeListener() {
2601 @Override
2602 public void onCheckedChanged(
2603 CompoundButton buttonView, boolean isChecked) {
2604 mWindowManagerService.setHardKeyboardEnabled(isChecked);
Satoshi Kataoka04dd24d2013-01-18 13:44:37 +09002605 // Ensure that the input method dialog is dismissed when changing
2606 // the hardware keyboard state.
2607 hideInputMethodMenu();
satok01038492012-04-09 21:08:27 +09002608 }
2609 });
satokd87c2592010-09-29 11:52:06 +09002610
Ken Wakasa05dbb652011-08-22 15:22:43 +09002611 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(context,
2612 com.android.internal.R.layout.simple_list_item_2_single_choice, imList,
2613 checkedItem);
2614
2615 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002616 new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002617 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002618 public void onClick(DialogInterface dialog, int which) {
2619 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09002620 if (mIms == null || mIms.length <= which
2621 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002622 return;
2623 }
2624 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09002625 int subtypeId = mSubtypeIds[which];
Satoshi Kataokad2142962012-11-12 18:43:06 +09002626 adapter.mCheckedItem = which;
2627 adapter.notifyDataSetChanged();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002628 hideInputMethodMenu();
2629 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09002630 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09002631 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09002632 subtypeId = NOT_A_SUBTYPE_ID;
2633 }
2634 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002635 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08002636 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002637 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002638 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002639
satokbc81b692011-08-26 16:22:22 +09002640 if (showSubtypes && !isScreenLocked) {
satok82beadf2010-12-27 19:03:06 +09002641 mDialogBuilder.setPositiveButton(
2642 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09002643 new DialogInterface.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002644 @Override
satok7f35c8c2010-10-07 21:13:11 +09002645 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09002646 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09002647 }
2648 });
2649 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002650 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002651 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002652 mSwitchingDialog.getWindow().setType(
2653 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09002654 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
2655 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002656 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002657 mSwitchingDialog.show();
2658 }
2659 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002660
satok93d744d2012-05-09 17:14:08 +09002661 private static class ImeSubtypeListItem implements Comparable<ImeSubtypeListItem> {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002662 public final CharSequence mImeName;
2663 public final CharSequence mSubtypeName;
2664 public final InputMethodInfo mImi;
2665 public final int mSubtypeId;
satok93d744d2012-05-09 17:14:08 +09002666 private final boolean mIsSystemLocale;
2667 private final boolean mIsSystemLanguage;
2668
Ken Wakasa05dbb652011-08-22 15:22:43 +09002669 public ImeSubtypeListItem(CharSequence imeName, CharSequence subtypeName,
satok93d744d2012-05-09 17:14:08 +09002670 InputMethodInfo imi, int subtypeId, String subtypeLocale, String systemLocale) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002671 mImeName = imeName;
2672 mSubtypeName = subtypeName;
2673 mImi = imi;
2674 mSubtypeId = subtypeId;
satok93d744d2012-05-09 17:14:08 +09002675 if (TextUtils.isEmpty(subtypeLocale)) {
2676 mIsSystemLocale = false;
2677 mIsSystemLanguage = false;
2678 } else {
2679 mIsSystemLocale = subtypeLocale.equals(systemLocale);
2680 mIsSystemLanguage = mIsSystemLocale
2681 || subtypeLocale.startsWith(systemLocale.substring(0, 2));
2682 }
2683 }
2684
2685 @Override
2686 public int compareTo(ImeSubtypeListItem other) {
2687 if (TextUtils.isEmpty(mImeName)) {
2688 return 1;
2689 }
2690 if (TextUtils.isEmpty(other.mImeName)) {
2691 return -1;
2692 }
2693 if (!TextUtils.equals(mImeName, other.mImeName)) {
2694 return mImeName.toString().compareTo(other.mImeName.toString());
2695 }
2696 if (TextUtils.equals(mSubtypeName, other.mSubtypeName)) {
2697 return 0;
2698 }
2699 if (mIsSystemLocale) {
2700 return -1;
2701 }
2702 if (other.mIsSystemLocale) {
2703 return 1;
2704 }
2705 if (mIsSystemLanguage) {
2706 return -1;
2707 }
2708 if (other.mIsSystemLanguage) {
2709 return 1;
2710 }
2711 if (TextUtils.isEmpty(mSubtypeName)) {
2712 return 1;
2713 }
2714 if (TextUtils.isEmpty(other.mSubtypeName)) {
2715 return -1;
2716 }
2717 return mSubtypeName.toString().compareTo(other.mSubtypeName.toString());
Ken Wakasa05dbb652011-08-22 15:22:43 +09002718 }
2719 }
2720
2721 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
2722 private final LayoutInflater mInflater;
2723 private final int mTextViewResourceId;
2724 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09002725 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09002726 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
2727 List<ImeSubtypeListItem> itemsList, int checkedItem) {
2728 super(context, textViewResourceId, itemsList);
2729 mTextViewResourceId = textViewResourceId;
2730 mItemsList = itemsList;
2731 mCheckedItem = checkedItem;
2732 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2733 }
2734
2735 @Override
2736 public View getView(int position, View convertView, ViewGroup parent) {
2737 final View view = convertView != null ? convertView
2738 : mInflater.inflate(mTextViewResourceId, null);
2739 if (position < 0 || position >= mItemsList.size()) return view;
2740 final ImeSubtypeListItem item = mItemsList.get(position);
2741 final CharSequence imeName = item.mImeName;
2742 final CharSequence subtypeName = item.mSubtypeName;
2743 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
2744 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
2745 if (TextUtils.isEmpty(subtypeName)) {
2746 firstTextView.setText(imeName);
2747 secondTextView.setVisibility(View.GONE);
2748 } else {
2749 firstTextView.setText(subtypeName);
2750 secondTextView.setText(imeName);
2751 secondTextView.setVisibility(View.VISIBLE);
2752 }
2753 final RadioButton radioButton =
2754 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
2755 radioButton.setChecked(position == mCheckedItem);
2756 return view;
2757 }
2758 }
2759
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002760 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07002761 synchronized (mMethodMap) {
2762 hideInputMethodMenuLocked();
2763 }
2764 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002765
The Android Open Source Project10592532009-03-18 17:39:46 -07002766 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002767 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002768
The Android Open Source Project10592532009-03-18 17:39:46 -07002769 if (mSwitchingDialog != null) {
2770 mSwitchingDialog.dismiss();
2771 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002772 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002773
The Android Open Source Project10592532009-03-18 17:39:46 -07002774 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07002775 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002776 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002777
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002778 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002779
satok42c5a162011-05-26 16:46:14 +09002780 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002781 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002782 // TODO: Make this work even for non-current users?
2783 if (!calledFromValidUser()) {
2784 return false;
2785 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002786 synchronized (mMethodMap) {
2787 if (mContext.checkCallingOrSelfPermission(
2788 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2789 != PackageManager.PERMISSION_GRANTED) {
2790 throw new SecurityException(
2791 "Requires permission "
2792 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2793 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002794
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002795 long ident = Binder.clearCallingIdentity();
2796 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002797 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002798 } finally {
2799 Binder.restoreCallingIdentity(ident);
2800 }
2801 }
2802 }
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002803
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002804 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
2805 // Make sure this is a valid input method.
2806 InputMethodInfo imm = mMethodMap.get(id);
2807 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09002808 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002809 }
2810
satokd87c2592010-09-29 11:52:06 +09002811 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
2812 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002813
satokd87c2592010-09-29 11:52:06 +09002814 if (enabled) {
2815 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
2816 if (pair.first.equals(id)) {
2817 // We are enabling this input method, but it is already enabled.
2818 // Nothing to do. The previous state was enabled.
2819 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002820 }
2821 }
satokd87c2592010-09-29 11:52:06 +09002822 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
2823 // Previous state was disabled.
2824 return false;
2825 } else {
2826 StringBuilder builder = new StringBuilder();
2827 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2828 builder, enabledInputMethodsList, id)) {
2829 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002830 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002831 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
2832 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
2833 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09002834 }
2835 // Previous state was enabled.
2836 return true;
2837 } else {
2838 // We are disabling the input method but it is already disabled.
2839 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002840 return false;
2841 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002842 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002843 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002844
satok723a27e2010-11-11 14:58:11 +09002845 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
2846 boolean setSubtypeOnly) {
2847 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002848 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09002849
2850 // Set Subtype here
2851 if (imi == null || subtypeId < 0) {
2852 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08002853 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09002854 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09002855 if (subtypeId < imi.getSubtypeCount()) {
2856 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
2857 mSettings.putSelectedSubtype(subtype.hashCode());
2858 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09002859 } else {
2860 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09002861 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002862 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09002863 }
satokab751aa2010-09-14 19:17:36 +09002864 }
satok723a27e2010-11-11 14:58:11 +09002865
satok4c0e7152012-06-20 20:08:44 +09002866 // Workaround.
2867 // ASEC is not ready in the IMMS constructor. Accordingly, forward-locked
2868 // IMEs are not recognized and considered uninstalled.
2869 // Actually, we can't move everything after SystemReady because
2870 // IMMS needs to run in the encryption lock screen. So, we just skip changing
2871 // the default IME here and try cheking the default IME again in systemReady().
2872 // TODO: Do nothing before system ready and implement a separated logic for
2873 // the encryption lock screen.
2874 // TODO: ASEC should be ready before IMMS is instantiated.
2875 if (mSystemReady && !setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09002876 // Set InputMethod here
2877 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
2878 }
2879 }
2880
2881 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
2882 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
2883 int lastSubtypeId = NOT_A_SUBTYPE_ID;
2884 // newDefaultIme is empty when there is no candidate for the selected IME.
2885 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
2886 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
2887 if (subtypeHashCode != null) {
2888 try {
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002889 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok723a27e2010-11-11 14:58:11 +09002890 imi, Integer.valueOf(subtypeHashCode));
2891 } catch (NumberFormatException e) {
2892 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
2893 }
2894 }
2895 }
2896 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09002897 }
2898
satok4e4569d2010-11-19 18:45:53 +09002899 // If there are no selected shortcuts, tries finding the most applicable ones.
2900 private Pair<InputMethodInfo, InputMethodSubtype>
2901 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
2902 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2903 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09002904 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09002905 boolean foundInSystemIME = false;
2906
2907 // Search applicable subtype for each InputMethodInfo
2908 for (InputMethodInfo imi: imis) {
satok7599a7fb2010-12-22 13:45:23 +09002909 final String imiId = imi.getId();
2910 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
2911 continue;
2912 }
satokcd7cd292010-11-20 15:46:23 +09002913 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09002914 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002915 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09002916 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09002917 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002918 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002919 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09002920 }
satokdf31ae62011-01-15 06:19:44 +09002921 // 2. Search by the system locale from enabledSubtypes.
2922 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09002923 if (subtype == null) {
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002924 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002925 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002926 }
satoka86f5e42011-09-02 17:12:42 +09002927 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002928 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09002929 final ArrayList<InputMethodSubtype> subtypesForSearch =
2930 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002931 ? InputMethodUtils.getSubtypes(imi)
2932 : overridingImplicitlyEnabledSubtypes;
satok7599a7fb2010-12-22 13:45:23 +09002933 // 4. Search by the current subtype's locale from all subtypes.
2934 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002935 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09002936 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7fb2010-12-22 13:45:23 +09002937 }
2938 // 5. Search by the system locale from all subtypes.
2939 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09002940 if (subtype == null) {
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002941 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09002942 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002943 }
satokcd7cd292010-11-20 15:46:23 +09002944 if (subtype != null) {
satok7599a7fb2010-12-22 13:45:23 +09002945 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09002946 // The current input method is the most applicable IME.
2947 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002948 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09002949 break;
satok7599a7fb2010-12-22 13:45:23 +09002950 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09002951 // The system input method is 2nd applicable IME.
2952 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002953 mostApplicableSubtype = subtype;
satok7599a7fb2010-12-22 13:45:23 +09002954 if ((imi.getServiceInfo().applicationInfo.flags
2955 & ApplicationInfo.FLAG_SYSTEM) != 0) {
2956 foundInSystemIME = true;
2957 }
satok4e4569d2010-11-19 18:45:53 +09002958 }
2959 }
2960 }
2961 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09002962 if (mostApplicableIMI != null) {
2963 Slog.w(TAG, "Most applicable shortcut input method was:"
2964 + mostApplicableIMI.getId());
2965 if (mostApplicableSubtype != null) {
2966 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
2967 + "," + mostApplicableSubtype.getMode() + ","
2968 + mostApplicableSubtype.getLocale());
2969 }
2970 }
satok4e4569d2010-11-19 18:45:53 +09002971 }
satokcd7cd292010-11-20 15:46:23 +09002972 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09002973 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09002974 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09002975 } else {
2976 return null;
2977 }
2978 }
2979
satokab751aa2010-09-14 19:17:36 +09002980 /**
2981 * @return Return the current subtype of this input method.
2982 */
satok42c5a162011-05-26 16:46:14 +09002983 @Override
satokab751aa2010-09-14 19:17:36 +09002984 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002985 // TODO: Make this work even for non-current users?
2986 if (!calledFromValidUser()) {
2987 return null;
2988 }
2989 synchronized (mMethodMap) {
2990 return getCurrentInputMethodSubtypeLocked();
2991 }
2992 }
2993
2994 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09002995 if (mCurMethodId == null) {
2996 return null;
2997 }
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002998 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002999 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3000 if (imi == null || imi.getSubtypeCount() == 0) {
3001 return null;
satok4e4569d2010-11-19 18:45:53 +09003002 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003003 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09003004 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3005 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003006 if (subtypeId == NOT_A_SUBTYPE_ID) {
3007 // If there are no selected subtypes, the framework will try to find
3008 // the most applicable subtype from explicitly or implicitly enabled
3009 // subtypes.
3010 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09003011 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003012 // If there is only one explicitly or implicitly enabled subtype,
3013 // just returns it.
3014 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3015 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3016 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09003017 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003018 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09003019 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003020 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09003021 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003022 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3023 true);
satok4e4569d2010-11-19 18:45:53 +09003024 }
satok3ef8b292010-11-23 06:06:29 +09003025 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003026 } else {
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09003027 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003028 }
3029 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003030 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003031 }
3032
satokf3db1af2010-11-23 13:34:33 +09003033 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
3034 InputMethodSubtype subtype) {
3035 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
3036 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
3037 } else {
3038 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3039 subtypes.add(subtype);
3040 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
3041 }
3042 }
3043
satok4e4569d2010-11-19 18:45:53 +09003044 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003045 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003046 @Override
satok4e4569d2010-11-19 18:45:53 +09003047 public List getShortcutInputMethodsAndSubtypes() {
3048 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09003049 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09003050 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003051 // If there are no selected shortcut subtypes, the framework will try to find
3052 // the most applicable subtype from all subtypes whose mode is
3053 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003054 Pair<InputMethodInfo, InputMethodSubtype> info =
3055 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09003056 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7fb2010-12-22 13:45:23 +09003057 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003058 ret.add(info.first);
3059 ret.add(info.second);
satok7599a7fb2010-12-22 13:45:23 +09003060 }
satok3da92232011-01-11 22:46:30 +09003061 return ret;
satokf3db1af2010-11-23 13:34:33 +09003062 }
satokf3db1af2010-11-23 13:34:33 +09003063 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3064 ret.add(imi);
3065 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3066 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003067 }
3068 }
satokf3db1af2010-11-23 13:34:33 +09003069 return ret;
satok4e4569d2010-11-19 18:45:53 +09003070 }
3071 }
3072
satok42c5a162011-05-26 16:46:14 +09003073 @Override
satokb66d2872010-11-10 01:04:04 +09003074 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003075 // TODO: Make this work even for non-current users?
3076 if (!calledFromValidUser()) {
3077 return false;
3078 }
satokb66d2872010-11-10 01:04:04 +09003079 synchronized (mMethodMap) {
3080 if (subtype != null && mCurMethodId != null) {
3081 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09003082 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003083 if (subtypeId != NOT_A_SUBTYPE_ID) {
3084 setInputMethodLocked(mCurMethodId, subtypeId);
3085 return true;
3086 }
3087 }
3088 return false;
3089 }
3090 }
3091
satok688bd472012-02-09 20:09:17 +09003092 private static class InputMethodAndSubtypeListManager {
3093 private final Context mContext;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003094 // Used to load label
satok688bd472012-02-09 20:09:17 +09003095 private final PackageManager mPm;
3096 private final InputMethodManagerService mImms;
satok93d744d2012-05-09 17:14:08 +09003097 private final String mSystemLocaleStr;
satok688bd472012-02-09 20:09:17 +09003098 public InputMethodAndSubtypeListManager(Context context, InputMethodManagerService imms) {
3099 mContext = context;
3100 mPm = context.getPackageManager();
3101 mImms = imms;
satok0a1bcf42012-05-16 19:26:31 +09003102 final Locale locale = context.getResources().getConfiguration().locale;
3103 mSystemLocaleStr = locale != null ? locale.toString() : "";
satok688bd472012-02-09 20:09:17 +09003104 }
3105
3106 private final TreeMap<InputMethodInfo, List<InputMethodSubtype>> mSortedImmis =
3107 new TreeMap<InputMethodInfo, List<InputMethodSubtype>>(
3108 new Comparator<InputMethodInfo>() {
3109 @Override
3110 public int compare(InputMethodInfo imi1, InputMethodInfo imi2) {
3111 if (imi2 == null) return 0;
3112 if (imi1 == null) return 1;
3113 if (mPm == null) {
3114 return imi1.getId().compareTo(imi2.getId());
3115 }
3116 CharSequence imiId1 = imi1.loadLabel(mPm) + "/" + imi1.getId();
3117 CharSequence imiId2 = imi2.loadLabel(mPm) + "/" + imi2.getId();
3118 return imiId1.toString().compareTo(imiId2.toString());
3119 }
3120 });
3121
3122 public ImeSubtypeListItem getNextInputMethod(
3123 boolean onlyCurrentIme, InputMethodInfo imi, InputMethodSubtype subtype) {
3124 if (imi == null) {
3125 return null;
3126 }
3127 final List<ImeSubtypeListItem> imList = getSortedInputMethodAndSubtypeList();
3128 if (imList.size() <= 1) {
3129 return null;
3130 }
3131 final int N = imList.size();
3132 final int currentSubtypeId = subtype != null
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09003133 ? InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode())
satok688bd472012-02-09 20:09:17 +09003134 : NOT_A_SUBTYPE_ID;
3135 for (int i = 0; i < N; ++i) {
3136 final ImeSubtypeListItem isli = imList.get(i);
3137 if (isli.mImi.equals(imi) && isli.mSubtypeId == currentSubtypeId) {
3138 if (!onlyCurrentIme) {
3139 return imList.get((i + 1) % N);
3140 }
3141 for (int j = 0; j < N - 1; ++j) {
3142 final ImeSubtypeListItem candidate = imList.get((i + j + 1) % N);
3143 if (candidate.mImi.equals(imi)) {
3144 return candidate;
3145 }
3146 }
3147 return null;
3148 }
3149 }
3150 return null;
3151 }
3152
3153 public List<ImeSubtypeListItem> getSortedInputMethodAndSubtypeList() {
3154 return getSortedInputMethodAndSubtypeList(true, false, false);
3155 }
3156
3157 public List<ImeSubtypeListItem> getSortedInputMethodAndSubtypeList(boolean showSubtypes,
3158 boolean inputShown, boolean isScreenLocked) {
3159 final ArrayList<ImeSubtypeListItem> imList = new ArrayList<ImeSubtypeListItem>();
3160 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
3161 mImms.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
3162 if (immis == null || immis.size() == 0) {
3163 return Collections.emptyList();
3164 }
3165 mSortedImmis.clear();
3166 mSortedImmis.putAll(immis);
3167 for (InputMethodInfo imi : mSortedImmis.keySet()) {
3168 if (imi == null) continue;
3169 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypeList = immis.get(imi);
3170 HashSet<String> enabledSubtypeSet = new HashSet<String>();
3171 for (InputMethodSubtype subtype: explicitlyOrImplicitlyEnabledSubtypeList) {
3172 enabledSubtypeSet.add(String.valueOf(subtype.hashCode()));
3173 }
satok688bd472012-02-09 20:09:17 +09003174 final CharSequence imeLabel = imi.loadLabel(mPm);
3175 if (showSubtypes && enabledSubtypeSet.size() > 0) {
3176 final int subtypeCount = imi.getSubtypeCount();
3177 if (DEBUG) {
3178 Slog.v(TAG, "Add subtypes: " + subtypeCount + ", " + imi.getId());
3179 }
3180 for (int j = 0; j < subtypeCount; ++j) {
3181 final InputMethodSubtype subtype = imi.getSubtypeAt(j);
3182 final String subtypeHashCode = String.valueOf(subtype.hashCode());
3183 // We show all enabled IMEs and subtypes when an IME is shown.
3184 if (enabledSubtypeSet.contains(subtypeHashCode)
3185 && ((inputShown && !isScreenLocked) || !subtype.isAuxiliary())) {
3186 final CharSequence subtypeLabel =
3187 subtype.overridesImplicitlyEnabledSubtype() ? null
3188 : subtype.getDisplayName(mContext, imi.getPackageName(),
3189 imi.getServiceInfo().applicationInfo);
satok93d744d2012-05-09 17:14:08 +09003190 imList.add(new ImeSubtypeListItem(imeLabel, subtypeLabel, imi, j,
3191 subtype.getLocale(), mSystemLocaleStr));
satok688bd472012-02-09 20:09:17 +09003192
3193 // Removing this subtype from enabledSubtypeSet because we no longer
3194 // need to add an entry of this subtype to imList to avoid duplicated
3195 // entries.
3196 enabledSubtypeSet.remove(subtypeHashCode);
3197 }
3198 }
3199 } else {
satok93d744d2012-05-09 17:14:08 +09003200 imList.add(new ImeSubtypeListItem(imeLabel, null, imi, NOT_A_SUBTYPE_ID,
3201 null, mSystemLocaleStr));
satok688bd472012-02-09 20:09:17 +09003202 }
3203 }
satok93d744d2012-05-09 17:14:08 +09003204 Collections.sort(imList);
satok688bd472012-02-09 20:09:17 +09003205 return imList;
3206 }
3207 }
3208
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003209 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003210 private static class InputMethodFileManager {
3211 private static final String SYSTEM_PATH = "system";
3212 private static final String INPUT_METHOD_PATH = "inputmethod";
3213 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3214 private static final String NODE_SUBTYPES = "subtypes";
3215 private static final String NODE_SUBTYPE = "subtype";
3216 private static final String NODE_IMI = "imi";
3217 private static final String ATTR_ID = "id";
3218 private static final String ATTR_LABEL = "label";
3219 private static final String ATTR_ICON = "icon";
3220 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3221 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3222 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3223 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3224 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3225 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003226 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
satoke7c6998e2011-06-03 17:57:59 +09003227 new HashMap<String, List<InputMethodSubtype>>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003228 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003229 if (methodMap == null) {
3230 throw new NullPointerException("methodMap is null");
3231 }
3232 mMethodMap = methodMap;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003233 final File systemDir = userId == UserHandle.USER_OWNER
3234 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3235 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003236 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3237 if (!inputMethodDir.mkdirs()) {
3238 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3239 }
3240 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3241 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3242 if (!subtypeFile.exists()) {
3243 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003244 writeAdditionalInputMethodSubtypes(
3245 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003246 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003247 readAdditionalInputMethodSubtypes(
3248 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003249 }
3250 }
3251
3252 private void deleteAllInputMethodSubtypes(String imiId) {
3253 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003254 mAdditionalSubtypesMap.remove(imiId);
3255 writeAdditionalInputMethodSubtypes(
3256 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003257 }
3258 }
3259
3260 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003261 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003262 synchronized (mMethodMap) {
3263 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3264 final int N = additionalSubtypes.length;
3265 for (int i = 0; i < N; ++i) {
3266 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003267 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003268 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003269 } else {
3270 Slog.w(TAG, "Duplicated subtype definition found: "
3271 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003272 }
3273 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003274 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3275 writeAdditionalInputMethodSubtypes(
3276 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003277 }
3278 }
3279
3280 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3281 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003282 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003283 }
3284 }
3285
3286 private static void writeAdditionalInputMethodSubtypes(
3287 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3288 HashMap<String, InputMethodInfo> methodMap) {
3289 // Safety net for the case that this function is called before methodMap is set.
3290 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3291 FileOutputStream fos = null;
3292 try {
3293 fos = subtypesFile.startWrite();
3294 final XmlSerializer out = new FastXmlSerializer();
3295 out.setOutput(fos, "utf-8");
3296 out.startDocument(null, true);
3297 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3298 out.startTag(null, NODE_SUBTYPES);
3299 for (String imiId : allSubtypes.keySet()) {
3300 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3301 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3302 continue;
3303 }
3304 out.startTag(null, NODE_IMI);
3305 out.attribute(null, ATTR_ID, imiId);
3306 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3307 final int N = subtypesList.size();
3308 for (int i = 0; i < N; ++i) {
3309 final InputMethodSubtype subtype = subtypesList.get(i);
3310 out.startTag(null, NODE_SUBTYPE);
3311 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3312 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3313 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3314 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3315 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3316 out.attribute(null, ATTR_IS_AUXILIARY,
3317 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3318 out.endTag(null, NODE_SUBTYPE);
3319 }
3320 out.endTag(null, NODE_IMI);
3321 }
3322 out.endTag(null, NODE_SUBTYPES);
3323 out.endDocument();
3324 subtypesFile.finishWrite(fos);
3325 } catch (java.io.IOException e) {
3326 Slog.w(TAG, "Error writing subtypes", e);
3327 if (fos != null) {
3328 subtypesFile.failWrite(fos);
3329 }
3330 }
3331 }
3332
3333 private static void readAdditionalInputMethodSubtypes(
3334 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3335 if (allSubtypes == null || subtypesFile == null) return;
3336 allSubtypes.clear();
3337 FileInputStream fis = null;
3338 try {
3339 fis = subtypesFile.openRead();
3340 final XmlPullParser parser = Xml.newPullParser();
3341 parser.setInput(fis, null);
3342 int type = parser.getEventType();
3343 // Skip parsing until START_TAG
3344 while ((type = parser.next()) != XmlPullParser.START_TAG
3345 && type != XmlPullParser.END_DOCUMENT) {}
3346 String firstNodeName = parser.getName();
3347 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3348 throw new XmlPullParserException("Xml doesn't start with subtypes");
3349 }
3350 final int depth =parser.getDepth();
3351 String currentImiId = null;
3352 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3353 while (((type = parser.next()) != XmlPullParser.END_TAG
3354 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3355 if (type != XmlPullParser.START_TAG)
3356 continue;
3357 final String nodeName = parser.getName();
3358 if (NODE_IMI.equals(nodeName)) {
3359 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3360 if (TextUtils.isEmpty(currentImiId)) {
3361 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3362 continue;
3363 }
3364 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
3365 allSubtypes.put(currentImiId, tempSubtypesArray);
3366 } else if (NODE_SUBTYPE.equals(nodeName)) {
3367 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3368 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3369 continue;
3370 }
3371 final int icon = Integer.valueOf(
3372 parser.getAttributeValue(null, ATTR_ICON));
3373 final int label = Integer.valueOf(
3374 parser.getAttributeValue(null, ATTR_LABEL));
3375 final String imeSubtypeLocale =
3376 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3377 final String imeSubtypeMode =
3378 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3379 final String imeSubtypeExtraValue =
3380 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003381 final boolean isAuxiliary = "1".equals(String.valueOf(
3382 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
satoke7c6998e2011-06-03 17:57:59 +09003383 final InputMethodSubtype subtype =
3384 new InputMethodSubtype(label, icon, imeSubtypeLocale,
3385 imeSubtypeMode, imeSubtypeExtraValue, isAuxiliary);
3386 tempSubtypesArray.add(subtype);
3387 }
3388 }
3389 } catch (XmlPullParserException e) {
3390 Slog.w(TAG, "Error reading subtypes: " + e);
3391 return;
3392 } catch (java.io.IOException e) {
3393 Slog.w(TAG, "Error reading subtypes: " + e);
3394 return;
3395 } catch (NumberFormatException e) {
3396 Slog.w(TAG, "Error reading subtypes: " + e);
3397 return;
3398 } finally {
3399 if (fis != null) {
3400 try {
3401 fis.close();
3402 } catch (java.io.IOException e1) {
3403 Slog.w(TAG, "Failed to close.");
3404 }
3405 }
3406 }
3407 }
3408 }
3409
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003410 // ----------------------------------------------------------------------
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003411 // Utilities for debug
3412 private static String getStackTrace() {
3413 final StringBuilder sb = new StringBuilder();
3414 try {
3415 throw new RuntimeException();
3416 } catch (RuntimeException e) {
3417 final StackTraceElement[] frames = e.getStackTrace();
3418 // Start at 1 because the first frame is here and we don't care about it
3419 for (int j = 1; j < frames.length; ++j) {
3420 sb.append(frames[j].toString() + "\n");
3421 }
3422 }
3423 return sb.toString();
3424 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003425
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003426 @Override
3427 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3428 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3429 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003431 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3432 + Binder.getCallingPid()
3433 + ", uid=" + Binder.getCallingUid());
3434 return;
3435 }
3436
3437 IInputMethod method;
3438 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003440 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003442 synchronized (mMethodMap) {
3443 p.println("Current Input Method Manager state:");
3444 int N = mMethodList.size();
3445 p.println(" Input Methods:");
3446 for (int i=0; i<N; i++) {
3447 InputMethodInfo info = mMethodList.get(i);
3448 p.println(" InputMethod #" + i + ":");
3449 info.dump(p, " ");
3450 }
3451 p.println(" Clients:");
3452 for (ClientState ci : mClients.values()) {
3453 p.println(" Client " + ci + ":");
3454 p.println(" client=" + ci.client);
3455 p.println(" inputContext=" + ci.inputContext);
3456 p.println(" sessionRequested=" + ci.sessionRequested);
3457 p.println(" curSession=" + ci.curSession);
3458 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003459 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003460 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003461 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3462 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003463 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3464 + " mBoundToMethod=" + mBoundToMethod);
3465 p.println(" mCurToken=" + mCurToken);
3466 p.println(" mCurIntent=" + mCurIntent);
3467 method = mCurMethod;
3468 p.println(" mCurMethod=" + mCurMethod);
3469 p.println(" mEnabledSession=" + mEnabledSession);
3470 p.println(" mShowRequested=" + mShowRequested
3471 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3472 + " mShowForced=" + mShowForced
3473 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07003474 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003475 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003476
Jeff Brownb88102f2010-09-08 11:49:43 -07003477 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003478 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003479 pw.flush();
3480 try {
3481 client.client.asBinder().dump(fd, args);
3482 } catch (RemoteException e) {
3483 p.println("Input method client dead: " + e);
3484 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003485 } else {
3486 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003487 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003488
Jeff Brownb88102f2010-09-08 11:49:43 -07003489 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003490 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003491 pw.flush();
3492 try {
3493 method.asBinder().dump(fd, args);
3494 } catch (RemoteException e) {
3495 p.println("Input method service dead: " + e);
3496 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003497 } else {
3498 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003499 }
3500 }
3501}