blob: 102bb088299eebb7690aade13d11ad6f613c6040 [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
Yohei Yukawa0f3ad12015-04-06 16:48:24 -070018import android.annotation.NonNull;
Svet Ganovadc1cf42015-06-15 16:36:24 -070019import android.content.pm.PackageManagerInternal;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080020import com.android.internal.content.PackageMonitor;
Satoshi Kataokad7443c82013-10-15 17:45:43 +090021import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController;
Satoshi Kataokad787f692013-10-26 04:44:21 +090022import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController.ImeSubtypeListItem;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +090023import com.android.internal.inputmethod.InputMethodUtils;
24import com.android.internal.inputmethod.InputMethodUtils.InputMethodSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import com.android.internal.os.HandlerCaller;
Svetoslav Ganov758143e2012-08-06 16:40:27 -070026import com.android.internal.os.SomeArgs;
satoke7c6998e2011-06-03 17:57:59 +090027import com.android.internal.util.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import com.android.internal.view.IInputContext;
29import com.android.internal.view.IInputMethod;
Michael Wright52a53522013-03-14 10:59:38 -070030import com.android.internal.view.IInputSessionCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import com.android.internal.view.IInputMethodClient;
32import com.android.internal.view.IInputMethodManager;
33import com.android.internal.view.IInputMethodSession;
34import com.android.internal.view.InputBindResult;
Svet Ganovadc1cf42015-06-15 16:36:24 -070035import com.android.server.pm.UserManagerService;
Adam Lesinskief2ea1f2013-12-05 16:48:06 -080036import com.android.server.statusbar.StatusBarManagerService;
satok01038492012-04-09 21:08:27 +090037import com.android.server.wm.WindowManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038
satoke7c6998e2011-06-03 17:57:59 +090039import org.xmlpull.v1.XmlPullParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import org.xmlpull.v1.XmlPullParserException;
satoke7c6998e2011-06-03 17:57:59 +090041import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042
43import android.app.ActivityManagerNative;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090044import android.app.AppGlobals;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.app.AlertDialog;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +090046import android.app.AppOpsManager;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090047import android.app.IUserSwitchObserver;
satokf90a33e2011-07-19 11:55:52 +090048import android.app.KeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +090049import android.app.Notification;
50import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070051import android.app.PendingIntent;
satok5b927c432012-05-01 20:09:34 +090052import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.content.ComponentName;
54import android.content.ContentResolver;
55import android.content.Context;
56import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057import android.content.DialogInterface.OnCancelListener;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +090058import android.content.DialogInterface.OnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090060import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070062import android.content.pm.ApplicationInfo;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090063import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.content.pm.PackageManager;
65import android.content.pm.ResolveInfo;
66import android.content.pm.ServiceInfo;
Amith Yamasani734983f2014-03-04 16:48:05 -080067import android.content.pm.UserInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070068import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.content.res.Resources;
70import android.content.res.TypedArray;
71import android.database.ContentObserver;
Alan Viverette505e3ab2014-11-24 15:22:11 -080072import android.graphics.drawable.Drawable;
Joe Onorato857fd9b2011-01-27 15:08:35 -080073import android.inputmethodservice.InputMethodService;
Michael Wright7b5a96b2014-08-09 19:28:42 -070074import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.os.Binder;
Chris Wren1ce4b6d2015-06-11 10:19:43 -040076import android.os.Bundle;
satoke7c6998e2011-06-03 17:57:59 +090077import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078import android.os.Handler;
79import android.os.IBinder;
80import android.os.IInterface;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090081import android.os.IRemoteCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082import android.os.Message;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090083import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084import android.os.Parcel;
85import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080086import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087import android.os.ServiceManager;
88import android.os.SystemClock;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090089import android.os.UserHandle;
Amith Yamasani734983f2014-03-04 16:48:05 -080090import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091import android.provider.Settings;
92import android.text.TextUtils;
Christopher Tate7b9a28c2015-03-18 13:06:16 -070093import android.text.TextUtils.SimpleStringSplitter;
satokf9f01002011-05-19 21:31:50 +090094import android.text.style.SuggestionSpan;
Christopher Tate7b9a28c2015-03-18 13:06:16 -070095import android.util.ArrayMap;
96import android.util.ArraySet;
Dianne Hackborn39606a02012-07-31 17:54:35 -070097import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +090099import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +0900100import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101import android.util.PrintWriterPrinter;
102import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +0900103import android.util.Slog;
104import android.util.Xml;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +0900105import android.view.ContextThemeWrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106import android.view.IWindowManager;
Jeff Brownc28867a2013-03-26 15:42:39 -0700107import android.view.InputChannel;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900108import android.view.LayoutInflater;
109import android.view.View;
110import android.view.ViewGroup;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111import android.view.WindowManager;
satokab751aa2010-09-14 19:17:36 +0900112import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113import android.view.inputmethod.InputBinding;
114import android.view.inputmethod.InputMethod;
115import android.view.inputmethod.InputMethodInfo;
116import android.view.inputmethod.InputMethodManager;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700117import android.view.inputmethod.InputMethodManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900118import android.view.inputmethod.InputMethodSubtype;
Yohei Yukawa443c2ba2014-09-10 14:10:30 +0900119import android.view.inputmethod.InputMethodSubtype.InputMethodSubtypeBuilder;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900120import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +0900121import android.widget.CompoundButton;
122import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900123import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +0900124import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900125import android.widget.TextView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126
satoke7c6998e2011-06-03 17:57:59 +0900127import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900129import java.io.FileInputStream;
130import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131import java.io.IOException;
132import java.io.PrintWriter;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100133import java.nio.charset.StandardCharsets;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900135import java.util.Collections;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136import java.util.HashMap;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700137import java.util.HashSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138import java.util.List;
satok5b927c432012-05-01 20:09:34 +0900139import java.util.Locale;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140
141/**
142 * This class provides a system service that manages input methods.
143 */
144public class InputMethodManagerService extends IInputMethodManager.Stub
145 implements ServiceConnection, Handler.Callback {
146 static final boolean DEBUG = false;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700147 static final boolean DEBUG_RESTORE = DEBUG || false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700148 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700150 private static final char INPUT_METHOD_SEPARATOR = ':';
151 private static final char INPUT_METHOD_SUBTYPE_SEPARATOR = ';';
152
Seigo Nonakad4474cb2015-05-04 16:53:24 -0700153 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 1;
154 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 2;
155 static final int MSG_SHOW_IM_CONFIG = 3;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 static final int MSG_UNBIND_INPUT = 1000;
158 static final int MSG_BIND_INPUT = 1010;
159 static final int MSG_SHOW_SOFT_INPUT = 1020;
160 static final int MSG_HIDE_SOFT_INPUT = 1030;
161 static final int MSG_ATTACH_TOKEN = 1040;
162 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 static final int MSG_START_INPUT = 2000;
165 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 static final int MSG_UNBIND_METHOD = 3000;
168 static final int MSG_BIND_METHOD = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700169 static final int MSG_SET_ACTIVE = 3020;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700170 static final int MSG_SET_INTERACTIVE = 3030;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900171 static final int MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER = 3040;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800172
satok01038492012-04-09 21:08:27 +0900173 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
174
Satoshi Kataokabcacc322013-10-21 17:57:27 -0700175 static final long TIME_TO_RECONNECT = 3 * 1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800176
satokf9f01002011-05-19 21:31:50 +0900177 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
178
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900179 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
satokb6359412011-06-28 17:47:41 +0900180 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900181
satok4e4569d2010-11-19 18:45:53 +0900182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800184 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900186 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 final IWindowManager mIWindowManager;
189 final HandlerCaller mCaller;
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700190 final boolean mHasFeature;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900191 private InputMethodFileManager mFileManager;
satok01038492012-04-09 21:08:27 +0900192 private final HardKeyboardListener mHardKeyboardListener;
193 private final WindowManagerService mWindowManagerService;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900194 private final AppOpsManager mAppOpsManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800195
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900196 final InputBindResult mNoBinding = new InputBindResult(null, null, null, -1, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 // All known input methods. mMethodMap also serves as the global
199 // lock for this class.
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700200 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<>();
201 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<>();
satokf9f01002011-05-19 21:31:50 +0900202 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700203 new LruCache<>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Satoshi Kataokad787f692013-10-26 04:44:21 +0900204 private final InputMethodSubtypeSwitchingController mSwitchingController;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800205
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700206 // Used to bring IME service up to visible adjustment while it is being shown.
207 final ServiceConnection mVisibleConnection = new ServiceConnection() {
208 @Override public void onServiceConnected(ComponentName name, IBinder service) {
209 }
210
211 @Override public void onServiceDisconnected(ComponentName name) {
212 }
213 };
214 boolean mVisibleBound = false;
215
satok7cfc0ed2011-06-20 21:29:36 +0900216 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700217 private NotificationManager mNotificationManager;
218 private KeyguardManager mKeyguardManager;
219 private StatusBarManagerService mStatusBar;
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400220 private Notification.Builder mImeSwitcherNotification;
Dianne Hackborn661cd522011-08-22 00:26:20 -0700221 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900222 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900223 private boolean mNotificationShown;
satok0a1bcf42012-05-16 19:26:31 +0900224 private final boolean mImeSelectedOnBoot;
satok7cfc0ed2011-06-20 21:29:36 +0900225
Tadashi G. Takaoka8c6d4772014-08-05 15:29:17 +0900226 static class SessionState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 final ClientState client;
228 final IInputMethod method;
Jeff Brownc28867a2013-03-26 15:42:39 -0700229
230 IInputMethodSession session;
231 InputChannel channel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 @Override
234 public String toString() {
235 return "SessionState{uid " + client.uid + " pid " + client.pid
236 + " method " + Integer.toHexString(
237 System.identityHashCode(method))
238 + " session " + Integer.toHexString(
239 System.identityHashCode(session))
Jeff Brownc28867a2013-03-26 15:42:39 -0700240 + " channel " + channel
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 + "}";
242 }
243
244 SessionState(ClientState _client, IInputMethod _method,
Jeff Brownc28867a2013-03-26 15:42:39 -0700245 IInputMethodSession _session, InputChannel _channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 client = _client;
247 method = _method;
248 session = _session;
Jeff Brownc28867a2013-03-26 15:42:39 -0700249 channel = _channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 }
251 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800252
Jeff Brownc28867a2013-03-26 15:42:39 -0700253 static final class ClientState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 final IInputMethodClient client;
255 final IInputContext inputContext;
256 final int uid;
257 final int pid;
258 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 boolean sessionRequested;
261 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 @Override
264 public String toString() {
265 return "ClientState{" + Integer.toHexString(
266 System.identityHashCode(this)) + " uid " + uid
267 + " pid " + pid + "}";
268 }
269
270 ClientState(IInputMethodClient _client, IInputContext _inputContext,
271 int _uid, int _pid) {
272 client = _client;
273 inputContext = _inputContext;
274 uid = _uid;
275 pid = _pid;
276 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
277 }
278 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800279
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700280 final HashMap<IBinder, ClientState> mClients = new HashMap<>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700283 * Set once the system is ready to run third party code.
284 */
285 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800286
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700287 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 * Id of the currently selected input method.
289 */
290 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 /**
293 * The current binding sequence number, incremented every time there is
294 * a new bind performed.
295 */
296 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800297
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298 /**
299 * The client that is currently bound to an input method.
300 */
301 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700304 * The last window token that gained focus.
305 */
306 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800307
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700308 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309 * The input context last provided by the current client.
310 */
311 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 /**
314 * The attributes last provided by the current client.
315 */
316 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800317
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 /**
319 * The input method ID of the input method service that we are currently
320 * connected to or in the process of connecting to.
321 */
322 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 /**
satokab751aa2010-09-14 19:17:36 +0900325 * The current subtype of the current input method.
326 */
327 private InputMethodSubtype mCurrentSubtype;
328
satok4e4569d2010-11-19 18:45:53 +0900329 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900330 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700331 mShortcutInputMethodsAndSubtypes = new HashMap<>();
satokab751aa2010-09-14 19:17:36 +0900332
John Spurlocke0980502013-10-25 11:59:29 -0400333 // Was the keyguard locked when this client became current?
334 private boolean mCurClientInKeyguard;
335
satokab751aa2010-09-14 19:17:36 +0900336 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 * Set to true if our ServiceConnection is currently actively bound to
338 * a service (whether or not we have gotten its IBinder back yet).
339 */
340 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800341
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 /**
343 * Set if the client has asked for the input method to be shown.
344 */
345 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 /**
348 * Set if we were explicitly told to show the input method.
349 */
350 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800351
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 /**
353 * Set if we were forced to be shown.
354 */
355 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800356
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 /**
358 * Set if we last told the input method to show itself.
359 */
360 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 /**
363 * The Intent used to connect to the current input method.
364 */
365 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800366
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367 /**
368 * The token we have made for the currently active input method, to
369 * identify it in the future.
370 */
371 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 /**
374 * If non-null, this is the input method service we are currently connected
375 * to.
376 */
377 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 /**
380 * Time that we last initiated a bind to the input method, to determine
381 * if we should try to disconnect and reconnect to it.
382 */
383 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800384
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385 /**
386 * Have we called mCurMethod.bindInput()?
387 */
388 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 /**
391 * Currently enabled session. Only touched by service thread, not
392 * protected by a lock.
393 */
394 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 /**
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700397 * True if the device is currently interactive with user. The value is true initially.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398 */
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700399 boolean mIsInteractive = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800400
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900401 int mCurUserActionNotificationSequenceNumber = 0;
402
Joe Onorato857fd9b2011-01-27 15:08:35 -0800403 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900404
405 /**
406 * A set of status bits regarding the active IME.
407 *
408 * <p>This value is a combination of following two bits:</p>
409 * <dl>
410 * <dt>{@link InputMethodService#IME_ACTIVE}</dt>
411 * <dd>
412 * If this bit is ON, connected IME is ready to accept touch/key events.
413 * </dd>
414 * <dt>{@link InputMethodService#IME_VISIBLE}</dt>
415 * <dd>
416 * If this bit is ON, some of IME view, e.g. software input, candidate view, is visible.
417 * </dd>
418 * </dl>
419 * <em>Do not update this value outside of setImeWindowStatus.</em>
420 */
Joe Onorato857fd9b2011-01-27 15:08:35 -0800421 int mImeWindowVis;
422
Ken Wakasa05dbb652011-08-22 15:22:43 +0900423 private AlertDialog.Builder mDialogBuilder;
424 private AlertDialog mSwitchingDialog;
satok01038492012-04-09 21:08:27 +0900425 private View mSwitchingDialogTitleView;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900426 private InputMethodInfo[] mIms;
427 private int[] mSubtypeIds;
satok5b927c432012-05-01 20:09:34 +0900428 private Locale mLastSystemLocale;
Michael Wright7b5a96b2014-08-09 19:28:42 -0700429 private boolean mShowImeWithHardKeyboard;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900430 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
431 private final IPackageManager mIPackageManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 class SettingsObserver extends ContentObserver {
Yohei Yukawa81482972015-06-04 00:58:59 -0700434 int mUserId;
435 boolean mRegistered = false;
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800436 String mLastEnabled = "";
437
Yohei Yukawa81482972015-06-04 00:58:59 -0700438 /**
439 * <em>This constructor must be called within the lock.</em>
440 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441 SettingsObserver(Handler handler) {
442 super(handler);
Yohei Yukawa81482972015-06-04 00:58:59 -0700443 }
444
445 public void registerContentObserverLocked(int userId) {
446 if (mRegistered && mUserId == userId) {
447 return;
448 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 ContentResolver resolver = mContext.getContentResolver();
Yohei Yukawa81482972015-06-04 00:58:59 -0700450 if (mRegistered) {
451 mContext.getContentResolver().unregisterContentObserver(this);
452 mRegistered = false;
453 }
454 if (mUserId != userId) {
455 mLastEnabled = "";
456 mUserId = userId;
457 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700459 Settings.Secure.DEFAULT_INPUT_METHOD), false, this, userId);
satokab751aa2010-09-14 19:17:36 +0900460 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700461 Settings.Secure.ENABLED_INPUT_METHODS), false, this, userId);
satokb6109bb2011-02-03 22:24:54 +0900462 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700463 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this, userId);
Michael Wright7b5a96b2014-08-09 19:28:42 -0700464 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700465 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD), false, this, userId);
466 mRegistered = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800468
Michael Wright7b5a96b2014-08-09 19:28:42 -0700469 @Override public void onChange(boolean selfChange, Uri uri) {
470 final Uri showImeUri =
471 Settings.Secure.getUriFor(Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 synchronized (mMethodMap) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700473 if (showImeUri.equals(uri)) {
474 updateKeyboardFromSettingsLocked();
475 } else {
476 boolean enabledChanged = false;
477 String newEnabled = mSettings.getEnabledInputMethodsStr();
478 if (!mLastEnabled.equals(newEnabled)) {
479 mLastEnabled = newEnabled;
480 enabledChanged = true;
481 }
482 updateInputMethodsFromSettingsLocked(enabledChanged);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800483 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800484 }
485 }
Yohei Yukawa81482972015-06-04 00:58:59 -0700486
487 @Override
488 public String toString() {
489 return "SettingsObserver{mUserId=" + mUserId + " mRegistered=" + mRegistered
490 + " mLastEnabled=" + mLastEnabled + "}";
491 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800493
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900494 class ImmsBroadcastReceiver extends android.content.BroadcastReceiver {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900495 @Override
496 public void onReceive(Context context, Intent intent) {
497 final String action = intent.getAction();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700498 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900499 hideInputMethodMenu();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700500 // No need to update mIsInteractive
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900501 return;
Amith Yamasani734983f2014-03-04 16:48:05 -0800502 } else if (Intent.ACTION_USER_ADDED.equals(action)
503 || Intent.ACTION_USER_REMOVED.equals(action)) {
Kenny Guy2a764942014-04-02 13:29:20 +0100504 updateCurrentProfileIds();
Amith Yamasani734983f2014-03-04 16:48:05 -0800505 return;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700506 } else if (Intent.ACTION_SETTING_RESTORED.equals(action)) {
507 final String name = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
508 if (Settings.Secure.ENABLED_INPUT_METHODS.equals(name)) {
509 final String prevValue = intent.getStringExtra(
510 Intent.EXTRA_SETTING_PREVIOUS_VALUE);
511 final String newValue = intent.getStringExtra(
512 Intent.EXTRA_SETTING_NEW_VALUE);
513 restoreEnabledInputMethods(mContext, prevValue, newValue);
514 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900515 } else {
516 Slog.w(TAG, "Unexpected intent " + intent);
517 }
518 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800520
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700521 // Apply the results of a restore operation to the set of enabled IMEs. Note that this
522 // does not attempt to validate on the fly with any installed device policy, so must only
523 // be run in the context of initial device setup.
524 //
525 // TODO: Move this method to InputMethodUtils with adding unit tests.
526 static void restoreEnabledInputMethods(Context context, String prevValue, String newValue) {
527 if (DEBUG_RESTORE) {
528 Slog.i(TAG, "Restoring enabled input methods:");
529 Slog.i(TAG, "prev=" + prevValue);
530 Slog.i(TAG, " new=" + newValue);
531 }
532 // 'new' is the just-restored state, 'prev' is what was in settings prior to the restore
533 ArrayMap<String, ArraySet<String>> prevMap = parseInputMethodsAndSubtypesString(prevValue);
534 ArrayMap<String, ArraySet<String>> newMap = parseInputMethodsAndSubtypesString(newValue);
535
536 // Merge the restored ime+subtype enabled states into the live state
537 for (ArrayMap.Entry<String, ArraySet<String>> entry : newMap.entrySet()) {
538 final String imeId = entry.getKey();
539 ArraySet<String> prevSubtypes = prevMap.get(imeId);
540 if (prevSubtypes == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700541 prevSubtypes = new ArraySet<>(2);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700542 prevMap.put(imeId, prevSubtypes);
543 }
544 prevSubtypes.addAll(entry.getValue());
545 }
546
547 final String mergedImesAndSubtypesString = buildInputMethodsAndSubtypesString(prevMap);
548 if (DEBUG_RESTORE) {
549 Slog.i(TAG, "Merged IME string:");
550 Slog.i(TAG, " " + mergedImesAndSubtypesString);
551 }
552 Settings.Secure.putString(context.getContentResolver(),
553 Settings.Secure.ENABLED_INPUT_METHODS, mergedImesAndSubtypesString);
554 }
555
556 // TODO: Move this method to InputMethodUtils with adding unit tests.
557 static String buildInputMethodsAndSubtypesString(ArrayMap<String, ArraySet<String>> map) {
558 // we want to use the canonical InputMethodSettings implementation,
559 // so we convert data structures first.
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700560 List<Pair<String, ArrayList<String>>> imeMap = new ArrayList<>(4);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700561 for (ArrayMap.Entry<String, ArraySet<String>> entry : map.entrySet()) {
562 final String imeName = entry.getKey();
563 final ArraySet<String> subtypeSet = entry.getValue();
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700564 final ArrayList<String> subtypes = new ArrayList<>(2);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700565 if (subtypeSet != null) {
566 subtypes.addAll(subtypeSet);
567 }
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700568 imeMap.add(new Pair<>(imeName, subtypes));
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700569 }
570 return InputMethodSettings.buildInputMethodsSettingString(imeMap);
571 }
572
573 // TODO: Move this method to InputMethodUtils with adding unit tests.
574 static ArrayMap<String, ArraySet<String>> parseInputMethodsAndSubtypesString(
575 final String inputMethodsAndSubtypesString) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700576 final ArrayMap<String, ArraySet<String>> imeMap = new ArrayMap<>();
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700577 if (TextUtils.isEmpty(inputMethodsAndSubtypesString)) {
578 return imeMap;
579 }
580
581 final SimpleStringSplitter typeSplitter =
582 new SimpleStringSplitter(INPUT_METHOD_SEPARATOR);
583 final SimpleStringSplitter subtypeSplitter =
584 new SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATOR);
585 List<Pair<String, ArrayList<String>>> allImeSettings =
586 InputMethodSettings.buildInputMethodsAndSubtypeList(inputMethodsAndSubtypesString,
587 typeSplitter,
588 subtypeSplitter);
589 for (Pair<String, ArrayList<String>> ime : allImeSettings) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700590 ArraySet<String> subtypes = new ArraySet<>();
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700591 if (ime.second != null) {
592 subtypes.addAll(ime.second);
593 }
594 imeMap.put(ime.first, subtypes);
595 }
596 return imeMap;
597 }
598
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800599 class MyPackageMonitor extends PackageMonitor {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900600 private boolean isChangingPackagesOfCurrentUser() {
601 final int userId = getChangingUserId();
602 final boolean retval = userId == mSettings.getCurrentUserId();
603 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900604 if (!retval) {
605 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
606 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900607 }
608 return retval;
609 }
610
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800612 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900613 if (!isChangingPackagesOfCurrentUser()) {
614 return false;
615 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800616 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900617 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 final int N = mMethodList.size();
619 if (curInputMethodId != null) {
620 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800621 InputMethodInfo imi = mMethodList.get(i);
622 if (imi.getId().equals(curInputMethodId)) {
623 for (String pkg : packages) {
624 if (imi.getPackageName().equals(pkg)) {
625 if (!doit) {
626 return true;
627 }
satok723a27e2010-11-11 14:58:11 +0900628 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800629 chooseNewDefaultIMELocked();
630 return true;
631 }
632 }
633 }
634 }
635 }
636 }
637 return false;
638 }
639
640 @Override
641 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900642 if (!isChangingPackagesOfCurrentUser()) {
643 return;
644 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800645 synchronized (mMethodMap) {
646 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900647 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800648 final int N = mMethodList.size();
649 if (curInputMethodId != null) {
650 for (int i=0; i<N; i++) {
651 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900652 final String imiId = imi.getId();
653 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800654 curIm = imi;
655 }
satoke7c6998e2011-06-03 17:57:59 +0900656
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800657 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900658 if (isPackageModified(imi.getPackageName())) {
659 mFileManager.deleteAllInputMethodSubtypes(imiId);
660 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800661 if (change == PACKAGE_TEMPORARY_CHANGE
662 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800663 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800664 + imi.getComponent());
665 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800666 }
667 }
668 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800669
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900670 buildInputMethodListLocked(
671 mMethodList, mMethodMap, false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800672
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800674
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800675 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800676 int change = isPackageDisappearing(curIm.getPackageName());
677 if (change == PACKAGE_TEMPORARY_CHANGE
678 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800679 ServiceInfo si = null;
680 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900681 si = mIPackageManager.getServiceInfo(
682 curIm.getComponent(), 0, mSettings.getCurrentUserId());
683 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800684 }
685 if (si == null) {
686 // Uh oh, current input method is no longer around!
687 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800688 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900689 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800690 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800691 changed = true;
692 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800693 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900694 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800695 }
696 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800697 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800698 }
satokab751aa2010-09-14 19:17:36 +0900699
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800700 if (curIm == null) {
701 // We currently don't have a default input method... is
702 // one now available?
703 changed = chooseNewDefaultIMELocked();
Yohei Yukawa54d512c2015-05-19 22:15:02 -0700704 } else if (!changed && isPackageModified(curIm.getPackageName())) {
705 // Even if the current input method is still available, mCurrentSubtype could
706 // be obsolete when the package is modified in practice.
707 changed = true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800708 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800709
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800710 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800711 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712 }
713 }
714 }
715 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800716
Jeff Brownc28867a2013-03-26 15:42:39 -0700717 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900718 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -0700719 private final IInputMethod mMethod;
720 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800721
Jeff Brownc28867a2013-03-26 15:42:39 -0700722 MethodCallback(InputMethodManagerService imms, IInputMethod method,
723 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900724 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -0700725 mMethod = method;
726 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800727 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800728
satoke7c6998e2011-06-03 17:57:59 +0900729 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -0700730 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -0700731 long ident = Binder.clearCallingIdentity();
732 try {
733 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
734 } finally {
735 Binder.restoreCallingIdentity(ident);
736 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800737 }
738 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800739
satok01038492012-04-09 21:08:27 +0900740 private class HardKeyboardListener
741 implements WindowManagerService.OnHardKeyboardStatusChangeListener {
742 @Override
Michael Wright7b5a96b2014-08-09 19:28:42 -0700743 public void onHardKeyboardStatusChange(boolean available) {
744 mHandler.sendMessage(mHandler.obtainMessage(MSG_HARD_KEYBOARD_SWITCH_CHANGED,
745 available ? 1 : 0));
satok01038492012-04-09 21:08:27 +0900746 }
747
Michael Wright7b5a96b2014-08-09 19:28:42 -0700748 public void handleHardKeyboardStatusChange(boolean available) {
satok01038492012-04-09 21:08:27 +0900749 if (DEBUG) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700750 Slog.w(TAG, "HardKeyboardStatusChanged: available=" + available);
satok01038492012-04-09 21:08:27 +0900751 }
752 synchronized(mMethodMap) {
753 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
754 && mSwitchingDialog.isShowing()) {
755 mSwitchingDialogTitleView.findViewById(
756 com.android.internal.R.id.hard_keyboard_section).setVisibility(
757 available ? View.VISIBLE : View.GONE);
758 }
759 }
760 }
761 }
762
763 public InputMethodManagerService(Context context, WindowManagerService windowManager) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900764 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800765 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800766 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800767 mHandler = new Handler(this);
Yohei Yukawa81482972015-06-04 00:58:59 -0700768 // Note: SettingsObserver doesn't register observers in its constructor.
769 mSettingsObserver = new SettingsObserver(mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770 mIWindowManager = IWindowManager.Stub.asInterface(
771 ServiceManager.getService(Context.WINDOW_SERVICE));
Mita Yuned218c72012-12-06 17:18:25 -0800772 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900773 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800774 public void executeMessage(Message msg) {
775 handleMessage(msg);
776 }
Mita Yuned218c72012-12-06 17:18:25 -0800777 }, true /*asyncHandler*/);
satok01038492012-04-09 21:08:27 +0900778 mWindowManagerService = windowManager;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900779 mAppOpsManager = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE);
satok01038492012-04-09 21:08:27 +0900780 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700781 mHasFeature = context.getPackageManager().hasSystemFeature(
782 PackageManager.FEATURE_INPUT_METHODS);
satok7cfc0ed2011-06-20 21:29:36 +0900783
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400784 Bundle extras = new Bundle();
785 extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
786 mImeSwitcherNotification = new Notification.Builder(mContext)
787 .setSmallIcon(com.android.internal.R.drawable.ic_notification_ime_default)
788 .setWhen(0)
789 .setOngoing(true)
790 .addExtras(extras)
791 .setCategory(Notification.CATEGORY_SYSTEM)
792 .setColor(com.android.internal.R.color.system_notification_accent_color);
Daniel Sandler590d5152012-06-14 16:10:13 -0400793
satok7cfc0ed2011-06-20 21:29:36 +0900794 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900795 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900796
797 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900798
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900799 final IntentFilter broadcastFilter = new IntentFilter();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900800 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Amith Yamasani734983f2014-03-04 16:48:05 -0800801 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
802 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700803 broadcastFilter.addAction(Intent.ACTION_SETTING_RESTORED);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900804 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800805
satok7cfc0ed2011-06-20 21:29:36 +0900806 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900807 int userId = 0;
808 try {
809 ActivityManagerNative.getDefault().registerUserSwitchObserver(
810 new IUserSwitchObserver.Stub() {
811 @Override
812 public void onUserSwitching(int newUserId, IRemoteCallback reply) {
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900813 synchronized(mMethodMap) {
814 switchUserLocked(newUserId);
815 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900816 if (reply != null) {
817 try {
818 reply.sendResult(null);
819 } catch (RemoteException e) {
820 }
821 }
822 }
823
824 @Override
825 public void onUserSwitchComplete(int newUserId) throws RemoteException {
826 }
Kenny Guy42979622015-04-13 18:03:05 +0000827
828 @Override
829 public void onForegroundProfileSwitch(int newProfileId) {
830 // Ignore.
831 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900832 });
833 userId = ActivityManagerNative.getDefault().getCurrentUser().id;
834 } catch (RemoteException e) {
835 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
836 }
satok81f8b7c2012-12-04 20:42:56 +0900837 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
satok913a8922010-08-26 21:53:41 +0900838
satokd87c2592010-09-29 11:52:06 +0900839 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900840 mSettings = new InputMethodSettings(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900841 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId);
Svet Ganovadc1cf42015-06-15 16:36:24 -0700842
843 // Let the package manager query which are the default imes
844 // as they get certain permissions granted by default.
845 PackageManagerInternal packageManagerInternal = LocalServices.getService(
846 PackageManagerInternal.class);
847 packageManagerInternal.setImePackagesProvider(
848 new PackageManagerInternal.PackagesProvider() {
849 @Override
850 public String[] getPackages(int userId) {
851 synchronized (mMethodMap) {
852 final int currentUserId = mSettings.getCurrentUserId();
853 // TODO: We are switching the current user id in the settings
854 // object to query it and then revert the user id. Ideally, we
855 // should call a API in settings with the user id as an argument.
856 mSettings.setCurrentUserId(userId);
857 List<InputMethodInfo> imes = mSettings
858 .getEnabledInputMethodListLocked();
859 String[] packageNames = null;
860 if (imes != null) {
861 final int imeCount = imes.size();
862 packageNames = new String[imeCount];
863 for (int i = 0; i < imeCount; i++) {
864 InputMethodInfo ime = imes.get(i);
865 packageNames[i] = ime.getPackageName();
866 }
867 }
868 mSettings.setCurrentUserId(currentUserId);
869 return packageNames;
870 }
871 }
872 });
873
Kenny Guy2a764942014-04-02 13:29:20 +0100874 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900875 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900876 synchronized (mMethodMap) {
877 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
878 mSettings, context);
879 }
satok0a1bcf42012-05-16 19:26:31 +0900880
881 // Just checking if defaultImiId is empty or not
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900882 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900883 if (DEBUG) {
884 Slog.d(TAG, "Initial default ime = " + defaultImiId);
885 }
satok0a1bcf42012-05-16 19:26:31 +0900886 mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
887
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900888 synchronized (mMethodMap) {
889 buildInputMethodListLocked(mMethodList, mMethodMap,
890 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
891 }
satokd87c2592010-09-29 11:52:06 +0900892 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800893
satok0a1bcf42012-05-16 19:26:31 +0900894 if (!mImeSelectedOnBoot) {
895 Slog.w(TAG, "No IME selected. Choose the most applicable IME.");
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900896 synchronized (mMethodMap) {
897 resetDefaultImeLocked(context);
898 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800899 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800900
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900901 synchronized (mMethodMap) {
Yohei Yukawa81482972015-06-04 00:58:59 -0700902 mSettingsObserver.registerContentObserverLocked(userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900903 updateFromSettingsLocked(true);
904 }
satok5b927c432012-05-01 20:09:34 +0900905
906 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME
907 // according to the new system locale.
908 final IntentFilter filter = new IntentFilter();
909 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
910 mContext.registerReceiver(
911 new BroadcastReceiver() {
912 @Override
913 public void onReceive(Context context, Intent intent) {
914 synchronized(mMethodMap) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900915 resetStateIfCurrentLocaleChangedLocked();
satok5b927c432012-05-01 20:09:34 +0900916 }
917 }
918 }, filter);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700919 LocalServices.addService(InputMethodManagerInternal.class, new LocalServiceImpl(mHandler));
satok5b927c432012-05-01 20:09:34 +0900920 }
921
satok5b927c432012-05-01 20:09:34 +0900922 private void resetDefaultImeLocked(Context context) {
923 // Do not reset the default (current) IME when it is a 3rd-party IME
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900924 if (mCurMethodId != null
925 && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +0900926 return;
927 }
928
929 InputMethodInfo defIm = null;
930 for (InputMethodInfo imi : mMethodList) {
Yohei Yukawa6aa03782015-02-21 03:00:22 +0900931 if (defIm == null && mSystemReady) {
932 final Locale systemLocale = context.getResources().getConfiguration().locale;
933 if (InputMethodUtils.isSystemImeThatHasSubtypeOf(imi, context,
934 true /* checkDefaultAttribute */, systemLocale, false /* checkCountry */,
935 InputMethodUtils.SUBTYPE_MODE_ANY)) {
satok5b927c432012-05-01 20:09:34 +0900936 defIm = imi;
937 Slog.i(TAG, "Selected default: " + imi.getId());
938 }
939 }
940 }
941 if (defIm == null && mMethodList.size() > 0) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900942 defIm = InputMethodUtils.getMostApplicableDefaultIME(
943 mSettings.getEnabledInputMethodListLocked());
Hyejin Kim5baaaac2014-10-27 17:17:06 +0900944 if (defIm != null) {
945 Slog.i(TAG, "Default found, using " + defIm.getId());
946 } else {
947 Slog.i(TAG, "No default found");
948 }
satok5b927c432012-05-01 20:09:34 +0900949 }
950 if (defIm != null) {
951 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
952 }
953 }
954
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900955 private void resetAllInternalStateLocked(final boolean updateOnlyWhenLocaleChanged,
956 final boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900957 if (!mSystemReady) {
958 // not system ready
959 return;
960 }
961 final Locale newLocale = mRes.getConfiguration().locale;
962 if (!updateOnlyWhenLocaleChanged
963 || (newLocale != null && !newLocale.equals(mLastSystemLocale))) {
964 if (!updateOnlyWhenLocaleChanged) {
965 hideCurrentInputLocked(0, null);
966 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -0700967 unbindCurrentMethodLocked(true, false);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900968 }
969 if (DEBUG) {
970 Slog.i(TAG, "Locale has been changed to " + newLocale);
971 }
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900972 buildInputMethodListLocked(mMethodList, mMethodMap, resetDefaultEnabledIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900973 if (!updateOnlyWhenLocaleChanged) {
974 final String selectedImiId = mSettings.getSelectedInputMethod();
975 if (TextUtils.isEmpty(selectedImiId)) {
976 // This is the first time of the user switch and
977 // set the current ime to the proper one.
978 resetDefaultImeLocked(mContext);
979 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +0900980 } else {
981 // If the locale is changed, needs to reset the default ime
982 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900983 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800984 updateFromSettingsLocked(true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900985 mLastSystemLocale = newLocale;
986 if (!updateOnlyWhenLocaleChanged) {
987 try {
988 startInputInnerLocked();
989 } catch (RuntimeException e) {
990 Slog.w(TAG, "Unexpected exception", e);
991 }
992 }
993 }
994 }
995
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900996 private void resetStateIfCurrentLocaleChangedLocked() {
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900997 resetAllInternalStateLocked(true /* updateOnlyWhenLocaleChanged */,
998 true /* resetDefaultImeLocked */);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900999 }
1000
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001001 private void switchUserLocked(int newUserId) {
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001002 if (DEBUG) Slog.d(TAG, "Switching user stage 1/3. newUserId=" + newUserId
1003 + " currentUserId=" + mSettings.getCurrentUserId());
1004
Yohei Yukawa81482972015-06-04 00:58:59 -07001005 // ContentObserver should be registered again when the user is changed
1006 mSettingsObserver.registerContentObserverLocked(newUserId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001007 mSettings.setCurrentUserId(newUserId);
Kenny Guy2a764942014-04-02 13:29:20 +01001008 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001009 // InputMethodFileManager should be reset when the user is changed
1010 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001011 final String defaultImiId = mSettings.getSelectedInputMethod();
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001012
1013 if (DEBUG) Slog.d(TAG, "Switching user stage 2/3. newUserId=" + newUserId
1014 + " defaultImiId=" + defaultImiId);
1015
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +09001016 // For secondary users, the list of enabled IMEs may not have been updated since the
1017 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
1018 // not be empty even if the IME has been uninstalled by the primary user.
1019 // Even in such cases, IMMS works fine because it will find the most applicable
1020 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001021 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001022 resetAllInternalStateLocked(false /* updateOnlyWhenLocaleChanged */,
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001023 initialUserSwitch /* needsToResetDefaultIme */);
1024 if (initialUserSwitch) {
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001025 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1026 mSettings.getEnabledInputMethodListLocked(), newUserId,
1027 mContext.getBasePackageName());
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001028 }
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001029
1030 if (DEBUG) Slog.d(TAG, "Switching user stage 3/3. newUserId=" + newUserId
1031 + " selectedIme=" + mSettings.getSelectedInputMethod());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001032 }
1033
Kenny Guy2a764942014-04-02 13:29:20 +01001034 void updateCurrentProfileIds() {
1035 List<UserInfo> profiles =
1036 UserManager.get(mContext).getProfiles(mSettings.getCurrentUserId());
1037 int[] currentProfileIds = new int[profiles.size()]; // profiles will not be null
1038 for (int i = 0; i < currentProfileIds.length; i++) {
1039 currentProfileIds[i] = profiles.get(i).id;
Amith Yamasani734983f2014-03-04 16:48:05 -08001040 }
Kenny Guy2a764942014-04-02 13:29:20 +01001041 mSettings.setCurrentProfileIds(currentProfileIds);
Amith Yamasani734983f2014-03-04 16:48:05 -08001042 }
1043
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001044 @Override
1045 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
1046 throws RemoteException {
1047 try {
1048 return super.onTransact(code, data, reply, flags);
1049 } catch (RuntimeException e) {
1050 // The input method manager only throws security exceptions, so let's
1051 // log all others.
1052 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -07001053 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001054 }
1055 throw e;
1056 }
1057 }
1058
Svetoslav Ganova0027152013-06-25 14:59:53 -07001059 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001060 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001061 if (DEBUG) {
1062 Slog.d(TAG, "--- systemReady");
1063 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001064 if (!mSystemReady) {
1065 mSystemReady = true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001066 mKeyguardManager =
1067 (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001068 mNotificationManager = (NotificationManager)
1069 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
1070 mStatusBar = statusBar;
1071 statusBar.setIconVisibility("ime", false);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001072 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokb858c732011-07-22 19:54:34 +09001073 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
1074 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +09001075 if (mShowOngoingImeSwitcherForPhones) {
1076 mWindowManagerService.setOnHardKeyboardStatusChangeListener(
1077 mHardKeyboardListener);
1078 }
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09001079 buildInputMethodListLocked(mMethodList, mMethodMap,
1080 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satok0a1bcf42012-05-16 19:26:31 +09001081 if (!mImeSelectedOnBoot) {
1082 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001083 resetStateIfCurrentLocaleChangedLocked();
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001084 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1085 mSettings.getEnabledInputMethodListLocked(),
1086 mSettings.getCurrentUserId(), mContext.getBasePackageName());
satok0a1bcf42012-05-16 19:26:31 +09001087 }
1088 mLastSystemLocale = mRes.getConfiguration().locale;
Dianne Hackborncc278702009-09-02 23:07:23 -07001089 try {
1090 startInputInnerLocked();
1091 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001092 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -07001093 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001094 }
1095 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001096 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001097
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001098 // ---------------------------------------------------------------------------------------
1099 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
1100 // 1) it comes from the system process
1101 // 2) the calling process' user id is identical to the current user id IMMS thinks.
1102 private boolean calledFromValidUser() {
1103 final int uid = Binder.getCallingUid();
1104 final int userId = UserHandle.getUserId(uid);
1105 if (DEBUG) {
1106 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
1107 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
1108 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +09001109 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
1110 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001111 }
Kenny Guy2a764942014-04-02 13:29:20 +01001112 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001113 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001114 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001115
1116 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
1117 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
1118 // must not manage background users' states in any functions.
1119 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
1120 // by a token.
1121 if (mContext.checkCallingOrSelfPermission(
1122 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1123 == PackageManager.PERMISSION_GRANTED) {
1124 if (DEBUG) {
1125 Slog.d(TAG, "--- Access granted because the calling process has "
1126 + "the INTERACT_ACROSS_USERS_FULL permission");
1127 }
1128 return true;
1129 }
Satoshi Kataoka0766eb02013-07-31 18:30:13 +09001130 Slog.w(TAG, "--- IPC called from background users. Ignore. \n"
1131 + InputMethodUtils.getStackTrace());
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001132 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001133 }
1134
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001135
1136 /**
1137 * Returns true iff the caller is identified to be the current input method with the token.
1138 * @param token The window token given to the input method when it was started.
1139 * @return true if and only if non-null valid token is specified.
1140 */
1141 private boolean calledWithValidToken(IBinder token) {
1142 if (token == null || mCurToken != token) {
1143 return false;
1144 }
1145 return true;
1146 }
1147
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001148 private boolean bindCurrentInputMethodService(
1149 Intent service, ServiceConnection conn, int flags) {
1150 if (service == null || conn == null) {
1151 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
1152 return false;
1153 }
Amith Yamasani27b89e62013-01-16 12:30:11 -08001154 return mContext.bindServiceAsUser(service, conn, flags,
1155 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001156 }
1157
satoke7c6998e2011-06-03 17:57:59 +09001158 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001160 // TODO: Make this work even for non-current users?
1161 if (!calledFromValidUser()) {
1162 return Collections.emptyList();
1163 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001164 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001165 return new ArrayList<>(mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001166 }
1167 }
1168
satoke7c6998e2011-06-03 17:57:59 +09001169 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001170 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001171 // TODO: Make this work even for non-current users?
1172 if (!calledFromValidUser()) {
1173 return Collections.emptyList();
1174 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001175 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001176 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001177 }
1178 }
1179
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001180 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001181 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001182 * @return enabled subtypes of the specified imi
1183 */
satoke7c6998e2011-06-03 17:57:59 +09001184 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001185 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001186 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001187 // TODO: Make this work even for non-current users?
1188 if (!calledFromValidUser()) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001189 return Collections.emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001190 }
satok67ddf9c2010-11-17 09:45:54 +09001191 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001192 final InputMethodInfo imi;
1193 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001194 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001195 } else {
1196 imi = mMethodMap.get(imiId);
1197 }
1198 if (imi == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001199 return Collections.emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001200 }
1201 return mSettings.getEnabledInputMethodSubtypeListLocked(
1202 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001203 }
1204 }
1205
satoke7c6998e2011-06-03 17:57:59 +09001206 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001207 public void addClient(IInputMethodClient client,
1208 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001209 if (!calledFromValidUser()) {
1210 return;
1211 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 synchronized (mMethodMap) {
1213 mClients.put(client.asBinder(), new ClientState(client,
1214 inputContext, uid, pid));
1215 }
1216 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001217
satoke7c6998e2011-06-03 17:57:59 +09001218 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001219 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001220 if (!calledFromValidUser()) {
1221 return;
1222 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001224 ClientState cs = mClients.remove(client.asBinder());
1225 if (cs != null) {
1226 clearClientSessionLocked(cs);
1227 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 }
1229 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001231 void executeOrSendMessage(IInterface target, Message msg) {
1232 if (target.asBinder() instanceof Binder) {
1233 mCaller.sendMessage(msg);
1234 } else {
1235 handleMessage(msg);
1236 msg.recycle();
1237 }
1238 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001239
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001240 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001241 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001242 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 + mCurClient.client.asBinder());
1244 if (mBoundToMethod) {
1245 mBoundToMethod = false;
1246 if (mCurMethod != null) {
1247 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1248 MSG_UNBIND_INPUT, mCurMethod));
1249 }
1250 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001251
1252 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1253 MSG_SET_ACTIVE, 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001254 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1255 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1256 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001257 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001258
The Android Open Source Project10592532009-03-18 17:39:46 -07001259 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001260 }
1261 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 private int getImeShowFlags() {
1264 int flags = 0;
1265 if (mShowForced) {
1266 flags |= InputMethod.SHOW_FORCED
1267 | InputMethod.SHOW_EXPLICIT;
1268 } else if (mShowExplicitlyRequested) {
1269 flags |= InputMethod.SHOW_EXPLICIT;
1270 }
1271 return flags;
1272 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001274 private int getAppShowFlags() {
1275 int flags = 0;
1276 if (mShowForced) {
1277 flags |= InputMethodManager.SHOW_FORCED;
1278 } else if (!mShowExplicitlyRequested) {
1279 flags |= InputMethodManager.SHOW_IMPLICIT;
1280 }
1281 return flags;
1282 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001283
Dianne Hackborn7663d802012-02-24 13:08:49 -08001284 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001285 if (!mBoundToMethod) {
1286 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1287 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1288 mBoundToMethod = true;
1289 }
1290 final SessionState session = mCurClient.curSession;
1291 if (initial) {
1292 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1293 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
1294 } else {
1295 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1296 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
1297 }
1298 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001299 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001300 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001301 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001302 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001303 (session.channel != null ? session.channel.dup() : null),
1304 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001305 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001306
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001307 InputBindResult startInputLocked(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001308 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 // If no method is currently selected, do nothing.
1310 if (mCurMethodId == null) {
1311 return mNoBinding;
1312 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001313
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001314 ClientState cs = mClients.get(client.asBinder());
1315 if (cs == null) {
1316 throw new IllegalArgumentException("unknown client "
1317 + client.asBinder());
1318 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320 try {
1321 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1322 // Check with the window manager to make sure this client actually
1323 // has a window with focus. If not, reject. This is thread safe
1324 // because if the focus changes some time before or after, the
1325 // next client receiving focus that has any interest in input will
1326 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001327 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001328 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1329 return null;
1330 }
1331 } catch (RemoteException e) {
1332 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001333
Dianne Hackborn7663d802012-02-24 13:08:49 -08001334 return startInputUncheckedLocked(cs, inputContext, attribute, controlFlags);
1335 }
1336
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001337 InputBindResult startInputUncheckedLocked(@NonNull ClientState cs,
Yohei Yukawad57ba672015-06-08 16:39:46 -07001338 IInputContext inputContext, @NonNull EditorInfo attribute, int controlFlags) {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001339 // If no method is currently selected, do nothing.
1340 if (mCurMethodId == null) {
1341 return mNoBinding;
1342 }
1343
Yohei Yukawad57ba672015-06-08 16:39:46 -07001344 if (!InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.uid,
1345 attribute.packageName)) {
1346 Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
1347 + " uid=" + cs.uid + " package=" + attribute.packageName);
1348 return mNoBinding;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001349 }
1350
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001351 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001352 // Was the keyguard locked when switching over to the new client?
1353 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001354 // If the client is changing, we need to switch over to the new
1355 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001356 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001357 if (DEBUG) Slog.v(TAG, "switching to client: client = "
John Spurlocke0980502013-10-25 11:59:29 -04001358 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001359
1360 // If the screen is on, inform the new client it is active
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001361 if (mIsInteractive) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001362 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001363 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001364 }
1365 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001366
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001367 // Bump up the sequence for this client and attach it.
1368 mCurSeq++;
1369 if (mCurSeq <= 0) mCurSeq = 1;
1370 mCurClient = cs;
1371 mCurInputContext = inputContext;
1372 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001373
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001374 // Check if the input method is changing.
1375 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1376 if (cs.curSession != null) {
1377 // Fast case: if we are already connected to the input method,
1378 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001379 return attachNewInputLocked(
1380 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001381 }
1382 if (mHaveConnection) {
1383 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001384 // Return to client, and we will get back with it when
1385 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001386 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001387 return new InputBindResult(null, null, mCurId, mCurSeq,
1388 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001389 } else if (SystemClock.uptimeMillis()
1390 < (mLastBindTime+TIME_TO_RECONNECT)) {
1391 // In this case we have connected to the service, but
1392 // don't yet have its interface. If it hasn't been too
1393 // long since we did the connection, we'll return to
1394 // the client and wait to get the service interface so
1395 // we can report back. If it has been too long, we want
1396 // to fall through so we can try a disconnect/reconnect
1397 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001398 return new InputBindResult(null, null, mCurId, mCurSeq,
1399 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001400 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001401 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1402 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001403 }
1404 }
1405 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001406
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001407 return startInputInnerLocked();
1408 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001409
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001410 InputBindResult startInputInnerLocked() {
1411 if (mCurMethodId == null) {
1412 return mNoBinding;
1413 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001414
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001415 if (!mSystemReady) {
1416 // If the system is not yet ready, we shouldn't be running third
1417 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001418 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1419 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001420 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001421
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001422 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1423 if (info == null) {
1424 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1425 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001426
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001427 unbindCurrentMethodLocked(false, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001429 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1430 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001431 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1432 com.android.internal.R.string.input_method_binding_label);
1433 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1434 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001435 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001436 | Context.BIND_NOT_VISIBLE | Context.BIND_NOT_FOREGROUND
1437 | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001438 mLastBindTime = SystemClock.uptimeMillis();
1439 mHaveConnection = true;
1440 mCurId = info.getId();
1441 mCurToken = new Binder();
1442 try {
Craig Mautnerca0ac712013-03-14 09:43:02 -07001443 if (true || DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001444 mIWindowManager.addWindowToken(mCurToken,
1445 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1446 } catch (RemoteException e) {
1447 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001448 return new InputBindResult(null, null, mCurId, mCurSeq,
1449 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001450 } else {
1451 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001452 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001453 + mCurIntent);
1454 }
1455 return null;
1456 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001457
satoke7c6998e2011-06-03 17:57:59 +09001458 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001459 public InputBindResult startInput(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001460 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001461 if (!calledFromValidUser()) {
1462 return null;
1463 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001464 synchronized (mMethodMap) {
1465 final long ident = Binder.clearCallingIdentity();
1466 try {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001467 return startInputLocked(client, inputContext, attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001468 } finally {
1469 Binder.restoreCallingIdentity(ident);
1470 }
1471 }
1472 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001473
satoke7c6998e2011-06-03 17:57:59 +09001474 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001475 public void finishInput(IInputMethodClient client) {
1476 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001477
satoke7c6998e2011-06-03 17:57:59 +09001478 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001479 public void onServiceConnected(ComponentName name, IBinder service) {
1480 synchronized (mMethodMap) {
1481 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1482 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001483 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001484 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001485 unbindCurrentMethodLocked(false, false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001486 return;
1487 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001488 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001489 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1490 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001491 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001492 clearClientSessionLocked(mCurClient);
1493 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001494 }
1495 }
1496 }
1497 }
1498
Jeff Brownc28867a2013-03-26 15:42:39 -07001499 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1500 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001501 synchronized (mMethodMap) {
1502 if (mCurMethod != null && method != null
1503 && mCurMethod.asBinder() == method.asBinder()) {
1504 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001505 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001506 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001507 method, session, channel);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001508 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001509 if (res.method != null) {
1510 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
1511 MSG_BIND_METHOD, mCurClient.client, res));
1512 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001513 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001514 }
1515 }
1516 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001517
1518 // Session abandoned. Close its associated input channel.
1519 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001520 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001521
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001522 void unbindCurrentMethodLocked(boolean reportToClient, boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001523 if (mVisibleBound) {
1524 mContext.unbindService(mVisibleConnection);
1525 mVisibleBound = false;
1526 }
1527
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001528 if (mHaveConnection) {
1529 mContext.unbindService(this);
1530 mHaveConnection = false;
1531 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001532
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001533 if (mCurToken != null) {
1534 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001535 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001536 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001537 // The current IME is shown. Hence an IME switch (transition) is happening.
1538 mWindowManagerService.saveLastInputMethodWindowForTransition();
1539 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001540 mIWindowManager.removeWindowToken(mCurToken);
1541 } catch (RemoteException e) {
1542 }
1543 mCurToken = null;
1544 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001545
The Android Open Source Project10592532009-03-18 17:39:46 -07001546 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001547 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001548
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001549 if (reportToClient && mCurClient != null) {
1550 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1551 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1552 }
1553 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001554
1555 void requestClientSessionLocked(ClientState cs) {
1556 if (!cs.sessionRequested) {
1557 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1558 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1559 cs.sessionRequested = true;
1560 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1561 MSG_CREATE_SESSION, mCurMethod, channels[1],
1562 new MethodCallback(this, mCurMethod, channels[0])));
1563 }
1564 }
1565
1566 void clearClientSessionLocked(ClientState cs) {
1567 finishSessionLocked(cs.curSession);
1568 cs.curSession = null;
1569 cs.sessionRequested = false;
1570 }
1571
1572 private void finishSessionLocked(SessionState sessionState) {
1573 if (sessionState != null) {
1574 if (sessionState.session != null) {
1575 try {
1576 sessionState.session.finishSession();
1577 } catch (RemoteException e) {
1578 Slog.w(TAG, "Session failed to close due to remote exception", e);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001579 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Jeff Brownc28867a2013-03-26 15:42:39 -07001580 }
1581 sessionState.session = null;
1582 }
1583 if (sessionState.channel != null) {
1584 sessionState.channel.dispose();
1585 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001586 }
1587 }
1588 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001589
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001590 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001591 if (mCurMethod != null) {
1592 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001593 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001594 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001595
Jeff Brownc28867a2013-03-26 15:42:39 -07001596 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001597 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001598 mCurMethod = null;
1599 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001600 if (mStatusBar != null) {
1601 mStatusBar.setIconVisibility("ime", false);
1602 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001603 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001604
satoke7c6998e2011-06-03 17:57:59 +09001605 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001606 public void onServiceDisconnected(ComponentName name) {
1607 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001608 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001609 + " mCurIntent=" + mCurIntent);
1610 if (mCurMethod != null && mCurIntent != null
1611 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001612 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001613 // We consider this to be a new bind attempt, since the system
1614 // should now try to restart the service for us.
1615 mLastBindTime = SystemClock.uptimeMillis();
1616 mShowRequested = mInputShown;
1617 mInputShown = false;
1618 if (mCurClient != null) {
1619 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1620 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1621 }
1622 }
1623 }
1624 }
1625
satokf9f01002011-05-19 21:31:50 +09001626 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001627 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
1628 long ident = Binder.clearCallingIdentity();
1629 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001630 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09001631 if (!calledWithValidToken(token)) {
1632 final int uid = Binder.getCallingUid();
1633 Slog.e(TAG, "Ignoring updateStatusIcon due to an invalid token. uid:" + uid
1634 + " token:" + token);
1635 return;
1636 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001637 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001638 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001639 if (mStatusBar != null) {
1640 mStatusBar.setIconVisibility("ime", false);
1641 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001642 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001643 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001644 CharSequence contentDescription = null;
1645 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001646 // Use PackageManager to load label
1647 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001648 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001649 mIPackageManager.getApplicationInfo(packageName, 0,
1650 mSettings.getCurrentUserId()));
1651 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001652 /* ignore */
1653 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001654 if (mStatusBar != null) {
1655 mStatusBar.setIcon("ime", packageName, iconId, 0,
1656 contentDescription != null
1657 ? contentDescription.toString() : null);
1658 mStatusBar.setIconVisibility("ime", true);
1659 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001660 }
1661 }
1662 } finally {
1663 Binder.restoreCallingIdentity(ident);
1664 }
1665 }
1666
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001667 private boolean shouldShowImeSwitcherLocked(int visibility) {
satok7cfc0ed2011-06-20 21:29:36 +09001668 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04001669 if (mSwitchingDialog != null) return false;
satok2c93efc2012-04-02 19:33:47 +09001670 if (isScreenLocked()) return false;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001671 if ((visibility & InputMethodService.IME_ACTIVE) == 0) return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001672 if (mWindowManagerService.isHardKeyboardAvailable()) {
1673 // When physical keyboard is attached, we show the ime switcher (or notification if
1674 // NavBar is not available) because SHOW_IME_WITH_HARD_KEYBOARD settings currently
1675 // exists in the IME switcher dialog. Might be OK to remove this condition once
1676 // SHOW_IME_WITH_HARD_KEYBOARD settings finds a good place to live.
1677 return true;
1678 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001679 if ((visibility & InputMethodService.IME_VISIBLE) == 0) return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001680
1681 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1682 final int N = imis.size();
1683 if (N > 2) return true;
1684 if (N < 1) return false;
1685 int nonAuxCount = 0;
1686 int auxCount = 0;
1687 InputMethodSubtype nonAuxSubtype = null;
1688 InputMethodSubtype auxSubtype = null;
1689 for(int i = 0; i < N; ++i) {
1690 final InputMethodInfo imi = imis.get(i);
1691 final List<InputMethodSubtype> subtypes =
1692 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
1693 final int subtypeCount = subtypes.size();
1694 if (subtypeCount == 0) {
1695 ++nonAuxCount;
1696 } else {
1697 for (int j = 0; j < subtypeCount; ++j) {
1698 final InputMethodSubtype subtype = subtypes.get(j);
1699 if (!subtype.isAuxiliary()) {
1700 ++nonAuxCount;
1701 nonAuxSubtype = subtype;
1702 } else {
1703 ++auxCount;
1704 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001705 }
1706 }
satok7cfc0ed2011-06-20 21:29:36 +09001707 }
1708 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001709 if (nonAuxCount > 1 || auxCount > 1) {
1710 return true;
1711 } else if (nonAuxCount == 1 && auxCount == 1) {
1712 if (nonAuxSubtype != null && auxSubtype != null
1713 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1714 || auxSubtype.overridesImplicitlyEnabledSubtype()
1715 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
1716 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1717 return false;
1718 }
1719 return true;
1720 }
1721 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001722 }
1723
John Spurlocke0980502013-10-25 11:59:29 -04001724 private boolean isKeyguardLocked() {
1725 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1726 }
1727
satokdbf29502011-08-25 15:28:23 +09001728 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001729 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001730 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001731 if (!calledWithValidToken(token)) {
1732 final int uid = Binder.getCallingUid();
1733 Slog.e(TAG, "Ignoring setImeWindowStatus due to an invalid token. uid:" + uid
1734 + " token:" + token);
1735 return;
1736 }
1737
1738 synchronized (mMethodMap) {
1739 mImeWindowVis = vis;
1740 mBackDisposition = backDisposition;
1741 updateSystemUiLocked(token, vis, backDisposition);
1742 }
1743 }
1744
1745 private void updateSystemUi(IBinder token, int vis, int backDisposition) {
1746 synchronized (mMethodMap) {
1747 updateSystemUiLocked(token, vis, backDisposition);
1748 }
1749 }
1750
1751 // Caution! This method is called in this class. Handle multi-user carefully
1752 private void updateSystemUiLocked(IBinder token, int vis, int backDisposition) {
1753 if (!calledWithValidToken(token)) {
1754 final int uid = Binder.getCallingUid();
1755 Slog.e(TAG, "Ignoring updateSystemUiLocked due to an invalid token. uid:" + uid
1756 + " token:" + token);
1757 return;
1758 }
1759
1760 // TODO: Move this clearing calling identity block to setImeWindowStatus after making sure
1761 // all updateSystemUi happens on system previlege.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001762 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001763 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001764 // apply policy for binder calls
1765 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
1766 vis = 0;
satok06487a52010-10-29 11:37:18 +09001767 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001768 // mImeWindowVis should be updated before calling shouldShowImeSwitcherLocked().
1769 final boolean needsToShowImeSwitcher = shouldShowImeSwitcherLocked(vis);
1770 if (mStatusBar != null) {
1771 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
1772 needsToShowImeSwitcher);
1773 }
1774 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1775 if (imi != null && needsToShowImeSwitcher) {
1776 // Used to load label
1777 final CharSequence title = mRes.getText(
1778 com.android.internal.R.string.select_input_method);
1779 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
1780 mContext, imi, mCurrentSubtype);
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001781 mImeSwitcherNotification.setContentTitle(title)
1782 .setContentText(summary)
1783 .setContentIntent(mImeSwitchPendingIntent);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001784 if ((mNotificationManager != null)
1785 && !mWindowManagerService.hasNavigationBar()) {
1786 if (DEBUG) {
1787 Slog.d(TAG, "--- show notification: label = " + summary);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001788 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001789 mNotificationManager.notifyAsUser(null,
1790 com.android.internal.R.string.select_input_method,
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001791 mImeSwitcherNotification.build(), UserHandle.ALL);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001792 mNotificationShown = true;
1793 }
1794 } else {
1795 if (mNotificationShown && mNotificationManager != null) {
1796 if (DEBUG) {
1797 Slog.d(TAG, "--- hide notification");
satok7cfc0ed2011-06-20 21:29:36 +09001798 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001799 mNotificationManager.cancelAsUser(null,
1800 com.android.internal.R.string.select_input_method, UserHandle.ALL);
1801 mNotificationShown = false;
satok7cfc0ed2011-06-20 21:29:36 +09001802 }
satok06487a52010-10-29 11:37:18 +09001803 }
1804 } finally {
1805 Binder.restoreCallingIdentity(ident);
1806 }
1807 }
1808
satoke7c6998e2011-06-03 17:57:59 +09001809 @Override
satokf9f01002011-05-19 21:31:50 +09001810 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001811 if (!calledFromValidUser()) {
1812 return;
1813 }
satokf9f01002011-05-19 21:31:50 +09001814 synchronized (mMethodMap) {
1815 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1816 for (int i = 0; i < spans.length; ++i) {
1817 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001818 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001819 mSecureSuggestionSpans.put(ss, currentImi);
1820 }
1821 }
1822 }
1823 }
1824
satoke7c6998e2011-06-03 17:57:59 +09001825 @Override
satokf9f01002011-05-19 21:31:50 +09001826 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001827 if (!calledFromValidUser()) {
1828 return false;
1829 }
satokf9f01002011-05-19 21:31:50 +09001830 synchronized (mMethodMap) {
1831 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1832 // TODO: Do not send the intent if the process of the targetImi is already dead.
1833 if (targetImi != null) {
1834 final String[] suggestions = span.getSuggestions();
1835 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001836 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001837 final Intent intent = new Intent();
1838 // Ensures that only a class in the original IME package will receive the
1839 // notification.
satok42c5a162011-05-26 16:46:14 +09001840 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001841 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1842 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1843 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1844 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001845 final long ident = Binder.clearCallingIdentity();
1846 try {
1847 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1848 } finally {
1849 Binder.restoreCallingIdentity(ident);
1850 }
satokf9f01002011-05-19 21:31:50 +09001851 return true;
1852 }
1853 }
1854 return false;
1855 }
1856
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001857 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001858 updateInputMethodsFromSettingsLocked(enabledMayChange);
1859 updateKeyboardFromSettingsLocked();
1860 }
1861
1862 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001863 if (enabledMayChange) {
1864 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1865 for (int i=0; i<enabled.size(); i++) {
1866 // We allow the user to select "disabled until used" apps, so if they
1867 // are enabling one of those here we now need to make it enabled.
1868 InputMethodInfo imm = enabled.get(i);
1869 try {
1870 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1871 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1872 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09001873 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001874 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001875 if (DEBUG) {
1876 Slog.d(TAG, "Update state(" + imm.getId()
1877 + "): DISABLED_UNTIL_USED -> DEFAULT");
1878 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001879 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1880 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001881 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
1882 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001883 }
1884 } catch (RemoteException e) {
1885 }
1886 }
1887 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001888 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1889 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1890 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1891 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001892 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001893 // There is no input method selected, try to choose new applicable input method.
1894 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001895 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001896 }
1897 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001898 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001899 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001900 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001901 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001902 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001903 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001904 }
satokf3db1af2010-11-23 13:34:33 +09001905 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001906 } else {
1907 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001908 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001909 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001910 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09001911 // Here is not the perfect place to reset the switching controller. Ideally
1912 // mSwitchingController and mSettings should be able to share the same state.
1913 // TODO: Make sure that mSwitchingController and mSettings are sharing the
1914 // the same enabled IMEs list.
1915 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07001916
1917 }
1918
1919 public void updateKeyboardFromSettingsLocked() {
1920 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
1921 if (mSwitchingDialog != null
1922 && mSwitchingDialogTitleView != null
1923 && mSwitchingDialog.isShowing()) {
1924 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
1925 com.android.internal.R.id.hard_keyboard_switch);
1926 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
1927 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001928 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001929
satokab751aa2010-09-14 19:17:36 +09001930 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001931 InputMethodInfo info = mMethodMap.get(id);
1932 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001933 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001934 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001935
Yohei Yukawae63b5fa2014-09-19 13:14:55 +09001936 if (mCurClient != null && mCurAttribute != null) {
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001937 // We have already made sure that the package name belongs to the application's UID.
1938 // No further UID check is required.
1939 if (SystemConfig.getInstance().getFixedImeApps().contains(mCurAttribute.packageName)) {
1940 return;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +09001941 }
1942 }
1943
satokd81e9502012-05-21 12:58:45 +09001944 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001945 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09001946 final int subtypeCount = info.getSubtypeCount();
1947 if (subtypeCount <= 0) {
1948 return;
satokcd7cd292010-11-20 15:46:23 +09001949 }
satokd81e9502012-05-21 12:58:45 +09001950 final InputMethodSubtype oldSubtype = mCurrentSubtype;
1951 final InputMethodSubtype newSubtype;
1952 if (subtypeId >= 0 && subtypeId < subtypeCount) {
1953 newSubtype = info.getSubtypeAt(subtypeId);
1954 } else {
1955 // If subtype is null, try to find the most applicable one from
1956 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001957 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09001958 }
1959 if (newSubtype == null || oldSubtype == null) {
1960 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
1961 + ", new subtype = " + newSubtype);
1962 return;
1963 }
1964 if (newSubtype != oldSubtype) {
1965 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1966 if (mCurMethod != null) {
1967 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001968 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokd81e9502012-05-21 12:58:45 +09001969 mCurMethod.changeInputMethodSubtype(newSubtype);
1970 } catch (RemoteException e) {
1971 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
satokab751aa2010-09-14 19:17:36 +09001972 }
1973 }
1974 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001975 return;
1976 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001977
satokd81e9502012-05-21 12:58:45 +09001978 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001979 final long ident = Binder.clearCallingIdentity();
1980 try {
satokab751aa2010-09-14 19:17:36 +09001981 // Set a subtype to this input method.
1982 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001983 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1984 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1985 // because mCurMethodId is stored as a history in
1986 // setSelectedInputMethodAndSubtypeLocked().
1987 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001988
1989 if (ActivityManagerNative.isSystemReady()) {
1990 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001991 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001992 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07001993 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001994 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001995 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001996 } finally {
1997 Binder.restoreCallingIdentity(ident);
1998 }
1999 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002000
satok42c5a162011-05-26 16:46:14 +09002001 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002002 public boolean showSoftInput(IInputMethodClient client, int flags,
2003 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002004 if (!calledFromValidUser()) {
2005 return false;
2006 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002007 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002008 long ident = Binder.clearCallingIdentity();
2009 try {
2010 synchronized (mMethodMap) {
2011 if (mCurClient == null || client == null
2012 || mCurClient.client.asBinder() != client.asBinder()) {
2013 try {
2014 // We need to check if this is the current client with
2015 // focus in the window manager, to allow this call to
2016 // be made before input is started in it.
2017 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002018 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002019 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002020 }
2021 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002022 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002023 }
2024 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002025
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002026 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002027 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002028 }
2029 } finally {
2030 Binder.restoreCallingIdentity(ident);
2031 }
2032 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002033
The Android Open Source Project4df24232009-03-05 14:34:35 -08002034 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002035 mShowRequested = true;
2036 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
2037 mShowExplicitlyRequested = true;
2038 }
2039 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
2040 mShowExplicitlyRequested = true;
2041 mShowForced = true;
2042 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002043
Dianne Hackborncc278702009-09-02 23:07:23 -07002044 if (!mSystemReady) {
2045 return false;
2046 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002047
The Android Open Source Project4df24232009-03-05 14:34:35 -08002048 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002049 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002050 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002051 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
2052 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
2053 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002054 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002055 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002056 bindCurrentInputMethodService(
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07002057 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE
Dianne Hackbornd69e4c12015-04-24 09:54:54 -07002058 | Context.BIND_TREAT_LIKE_ACTIVITY
2059 | Context.BIND_FOREGROUND_SERVICE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002060 mVisibleBound = true;
2061 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002062 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002063 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09002064 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002065 // The client has asked to have the input method shown, but
2066 // we have been sitting here too long with a connection to the
2067 // service and no interface received, so let's disconnect/connect
2068 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002069 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002070 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09002071 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002072 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002073 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002074 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002075 } else {
2076 if (DEBUG) {
2077 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
2078 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
2079 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002080 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002081
The Android Open Source Project4df24232009-03-05 14:34:35 -08002082 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002083 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002084
satok42c5a162011-05-26 16:46:14 +09002085 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002086 public boolean hideSoftInput(IInputMethodClient client, int flags,
2087 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002088 if (!calledFromValidUser()) {
2089 return false;
2090 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002091 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002092 long ident = Binder.clearCallingIdentity();
2093 try {
2094 synchronized (mMethodMap) {
2095 if (mCurClient == null || client == null
2096 || mCurClient.client.asBinder() != client.asBinder()) {
2097 try {
2098 // We need to check if this is the current client with
2099 // focus in the window manager, to allow this call to
2100 // be made before input is started in it.
2101 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002102 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
2103 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002104 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002105 }
2106 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002107 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002108 }
2109 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002110
Joe Onorato8a9b2202010-02-26 18:56:32 -08002111 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002112 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002113 }
2114 } finally {
2115 Binder.restoreCallingIdentity(ident);
2116 }
2117 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002118
The Android Open Source Project4df24232009-03-05 14:34:35 -08002119 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002120 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
2121 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002122 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 -08002123 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002124 }
2125 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002126 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 -08002127 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002128 }
Seigo Nonakaec928652015-06-10 15:31:20 +09002129
2130 // There is a chance that IMM#hideSoftInput() is called in a transient state where
2131 // IMMS#InputShown is already updated to be true whereas IMMS#mImeWindowVis is still waiting
2132 // to be updated with the new value sent from IME process. Even in such a transient state
2133 // historically we have accepted an incoming call of IMM#hideSoftInput() from the
2134 // application process as a valid request, and have even promised such a behavior with CTS
2135 // since Android Eclair. That's why we need to accept IMM#hideSoftInput() even when only
2136 // IMMS#InputShown indicates that the software keyboard is shown.
2137 // TODO: Clean up, IMMS#mInputShown, IMMS#mImeWindowVis and mShowRequested.
2138 final boolean shouldHideSoftInput = (mCurMethod != null) && (mInputShown ||
2139 (mImeWindowVis & InputMethodService.IME_ACTIVE) != 0);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002140 boolean res;
Seigo Nonakaec928652015-06-10 15:31:20 +09002141 if (shouldHideSoftInput) {
2142 // The IME will report its visible state again after the following message finally
2143 // delivered to the IME process as an IPC. Hence the inconsistency between
2144 // IMMS#mInputShown and IMMS#mImeWindowVis should be resolved spontaneously in
2145 // the final state.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002146 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2147 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
2148 res = true;
2149 } else {
2150 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002151 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002152 if (mHaveConnection && mVisibleBound) {
2153 mContext.unbindService(mVisibleConnection);
2154 mVisibleBound = false;
2155 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002156 mInputShown = false;
2157 mShowRequested = false;
2158 mShowExplicitlyRequested = false;
2159 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002160 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002161 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002162
satok42c5a162011-05-26 16:46:14 +09002163 @Override
Dianne Hackborn7663d802012-02-24 13:08:49 -08002164 public InputBindResult windowGainedFocus(IInputMethodClient client, IBinder windowToken,
2165 int controlFlags, int softInputMode, int windowFlags,
2166 EditorInfo attribute, IInputContext inputContext) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002167 // Needs to check the validity before clearing calling identity
2168 final boolean calledFromValidUser = calledFromValidUser();
2169
Dianne Hackborn7663d802012-02-24 13:08:49 -08002170 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002171 long ident = Binder.clearCallingIdentity();
2172 try {
2173 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002174 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
Dianne Hackborn7663d802012-02-24 13:08:49 -08002175 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002176 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08002177 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002178
Dianne Hackborn7663d802012-02-24 13:08:49 -08002179 ClientState cs = mClients.get(client.asBinder());
2180 if (cs == null) {
2181 throw new IllegalArgumentException("unknown client "
2182 + client.asBinder());
2183 }
2184
2185 try {
2186 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2187 // Check with the window manager to make sure this client actually
2188 // has a window with focus. If not, reject. This is thread safe
2189 // because if the focus changes some time before or after, the
2190 // next client receiving focus that has any interest in input will
2191 // be calling through here after that change happens.
2192 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2193 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2194 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002195 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002196 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002197 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002198
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002199 if (!calledFromValidUser) {
2200 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2201 Slog.w(TAG, "If you want to interect with IME, you need "
2202 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2203 hideCurrentInputLocked(0, null);
2204 return null;
2205 }
2206
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002207 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07002208 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09002209 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002210 if (attribute != null) {
2211 return startInputUncheckedLocked(cs, inputContext, attribute,
2212 controlFlags);
2213 }
2214 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002215 }
2216 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002217
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002218 // Should we auto-show the IME even if the caller has not
2219 // specified what should be done with it?
2220 // We only do this automatically if the window can resize
2221 // to accommodate the IME (so what the user sees will give
2222 // them good context without input information being obscured
2223 // by the IME) or if running on a large screen where there
2224 // is more room for the target window + IME.
2225 final boolean doAutoShow =
2226 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2227 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2228 || mRes.getConfiguration().isLayoutSizeAtLeast(
2229 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002230 final boolean isTextEditor =
2231 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2232
2233 // We want to start input before showing the IME, but after closing
2234 // it. We want to do this after closing it to help the IME disappear
2235 // more quickly (not get stuck behind it initializing itself for the
2236 // new focused input, even if its window wants to hide the IME).
2237 boolean didStart = false;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002238
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002239 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2240 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002241 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002242 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2243 // There is no focus view, and this window will
2244 // be behind any soft input window, so hide the
2245 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002246 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002247 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002248 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002249 } else if (isTextEditor && doAutoShow && (softInputMode &
2250 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002251 // There is a focus view, and we are navigating forward
2252 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002253 // We only do this automatically if the window can resize
2254 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002255 // them good context without input information being obscured
2256 // by the IME) or if running on a large screen where there
2257 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002258 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002259 if (attribute != null) {
2260 res = startInputUncheckedLocked(cs, inputContext, attribute,
2261 controlFlags);
2262 didStart = true;
2263 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002264 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002265 }
2266 break;
2267 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2268 // Do nothing.
2269 break;
2270 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2271 if ((softInputMode &
2272 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002273 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002274 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002275 }
2276 break;
2277 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002278 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002279 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002280 break;
2281 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2282 if ((softInputMode &
2283 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002284 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002285 if (attribute != null) {
2286 res = startInputUncheckedLocked(cs, inputContext, attribute,
2287 controlFlags);
2288 didStart = true;
2289 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002290 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002291 }
2292 break;
2293 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002294 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002295 if (attribute != null) {
2296 res = startInputUncheckedLocked(cs, inputContext, attribute,
2297 controlFlags);
2298 didStart = true;
2299 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002300 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002301 break;
2302 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002303
2304 if (!didStart && attribute != null) {
2305 res = startInputUncheckedLocked(cs, inputContext, attribute,
2306 controlFlags);
2307 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002308 }
2309 } finally {
2310 Binder.restoreCallingIdentity(ident);
2311 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002312
2313 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002314 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002315
satok42c5a162011-05-26 16:46:14 +09002316 @Override
Seigo Nonaka14e13912015-05-06 21:04:13 -07002317 public void showInputMethodPickerFromClient(
2318 IInputMethodClient client, int auxiliarySubtypeMode) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002319 if (!calledFromValidUser()) {
2320 return;
2321 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002322 synchronized (mMethodMap) {
2323 if (mCurClient == null || client == null
2324 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002325 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002326 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002327 }
2328
satok440aab52010-11-25 09:43:11 +09002329 // Always call subtype picker, because subtype picker is a superset of input method
2330 // picker.
Seigo Nonaka14e13912015-05-06 21:04:13 -07002331 mHandler.sendMessage(mCaller.obtainMessageI(
2332 MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode));
satokab751aa2010-09-14 19:17:36 +09002333 }
2334 }
2335
satok42c5a162011-05-26 16:46:14 +09002336 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002337 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002338 if (!calledFromValidUser()) {
2339 return;
2340 }
satok28203512010-11-24 11:06:49 +09002341 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2342 }
2343
satok42c5a162011-05-26 16:46:14 +09002344 @Override
satok28203512010-11-24 11:06:49 +09002345 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002346 if (!calledFromValidUser()) {
2347 return;
2348 }
satok28203512010-11-24 11:06:49 +09002349 synchronized (mMethodMap) {
2350 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002351 setInputMethodWithSubtypeIdLocked(token, id,
2352 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2353 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002354 } else {
2355 setInputMethod(token, id);
2356 }
2357 }
satokab751aa2010-09-14 19:17:36 +09002358 }
2359
satok42c5a162011-05-26 16:46:14 +09002360 @Override
satokb416a712010-11-25 20:42:14 +09002361 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002362 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002363 if (!calledFromValidUser()) {
2364 return;
2365 }
satokb416a712010-11-25 20:42:14 +09002366 synchronized (mMethodMap) {
2367 if (mCurClient == null || client == null
2368 || mCurClient.client.asBinder() != client.asBinder()) {
2369 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
2370 }
satok7fee71f2010-12-17 18:54:26 +09002371 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2372 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002373 }
2374 }
2375
satok4fc87d62011-05-20 16:13:43 +09002376 @Override
satok735cf382010-11-11 20:40:09 +09002377 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002378 if (!calledFromValidUser()) {
2379 return false;
2380 }
satok735cf382010-11-11 20:40:09 +09002381 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002382 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002383 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002384 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002385 lastImi = mMethodMap.get(lastIme.first);
2386 } else {
2387 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002388 }
satok4fc87d62011-05-20 16:13:43 +09002389 String targetLastImiId = null;
2390 int subtypeId = NOT_A_SUBTYPE_ID;
2391 if (lastIme != null && lastImi != null) {
2392 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
2393 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
2394 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2395 : mCurrentSubtype.hashCode();
2396 // If the last IME is the same as the current IME and the last subtype is not
2397 // defined, there is no need to switch to the last IME.
2398 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2399 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002400 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002401 }
2402 }
2403
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002404 if (TextUtils.isEmpty(targetLastImiId)
2405 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002406 // This is a safety net. If the currentSubtype can't be added to the history
2407 // and the framework couldn't find the last ime, we will make the last ime be
2408 // the most applicable enabled keyboard subtype of the system imes.
2409 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2410 if (enabled != null) {
2411 final int N = enabled.size();
2412 final String locale = mCurrentSubtype == null
2413 ? mRes.getConfiguration().locale.toString()
2414 : mCurrentSubtype.getLocale();
2415 for (int i = 0; i < N; ++i) {
2416 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002417 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002418 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002419 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2420 InputMethodUtils.getSubtypes(imi),
2421 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002422 if (keyboardSubtype != null) {
2423 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002424 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002425 imi, keyboardSubtype.hashCode());
2426 if(keyboardSubtype.getLocale().equals(locale)) {
2427 break;
2428 }
2429 }
2430 }
2431 }
2432 }
2433 }
2434
2435 if (!TextUtils.isEmpty(targetLastImiId)) {
2436 if (DEBUG) {
2437 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2438 + ", from: " + mCurMethodId + ", " + subtypeId);
2439 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002440 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002441 return true;
2442 } else {
2443 return false;
2444 }
satok735cf382010-11-11 20:40:09 +09002445 }
2446 }
2447
satoke7c6998e2011-06-03 17:57:59 +09002448 @Override
satok688bd472012-02-09 20:09:17 +09002449 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002450 if (!calledFromValidUser()) {
2451 return false;
2452 }
satok688bd472012-02-09 20:09:17 +09002453 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002454 if (!calledWithValidToken(token)) {
2455 final int uid = Binder.getCallingUid();
2456 Slog.e(TAG, "Ignoring switchToNextInputMethod due to an invalid token. uid:" + uid
2457 + " token:" + token);
2458 return false;
2459 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002460 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
satok688bd472012-02-09 20:09:17 +09002461 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2462 if (nextSubtype == null) {
2463 return false;
2464 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002465 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2466 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002467 return true;
2468 }
2469 }
2470
2471 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002472 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2473 if (!calledFromValidUser()) {
2474 return false;
2475 }
2476 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002477 if (!calledWithValidToken(token)) {
2478 final int uid = Binder.getCallingUid();
2479 Slog.e(TAG, "Ignoring shouldOfferSwitchingToNextInputMethod due to an invalid "
2480 + "token. uid:" + uid + " token:" + token);
2481 return false;
2482 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002483 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002484 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2485 if (nextSubtype == null) {
2486 return false;
2487 }
2488 return true;
2489 }
2490 }
2491
2492 @Override
satok68f1b782011-04-11 14:26:04 +09002493 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002494 if (!calledFromValidUser()) {
2495 return null;
2496 }
satok68f1b782011-04-11 14:26:04 +09002497 synchronized (mMethodMap) {
2498 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2499 // TODO: Handle the case of the last IME with no subtypes
2500 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2501 || TextUtils.isEmpty(lastIme.second)) return null;
2502 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2503 if (lastImi == null) return null;
2504 try {
2505 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002506 final int lastSubtypeId =
2507 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002508 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2509 return null;
2510 }
2511 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002512 } catch (NumberFormatException e) {
2513 return null;
2514 }
2515 }
2516 }
2517
satoke7c6998e2011-06-03 17:57:59 +09002518 @Override
satokee5e77c2011-09-02 18:50:15 +09002519 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002520 if (!calledFromValidUser()) {
2521 return;
2522 }
satok91e88122011-07-18 11:11:42 +09002523 // By this IPC call, only a process which shares the same uid with the IME can add
2524 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09002525 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09002526 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002527 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002528 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002529 final String[] packageInfos;
2530 try {
2531 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2532 } catch (RemoteException e) {
2533 Slog.e(TAG, "Failed to get package infos");
2534 return;
2535 }
satok91e88122011-07-18 11:11:42 +09002536 if (packageInfos != null) {
2537 final int packageNum = packageInfos.length;
2538 for (int i = 0; i < packageNum; ++i) {
2539 if (packageInfos[i].equals(imi.getPackageName())) {
2540 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002541 final long ident = Binder.clearCallingIdentity();
2542 try {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002543 buildInputMethodListLocked(mMethodList, mMethodMap,
2544 false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002545 } finally {
2546 Binder.restoreCallingIdentity(ident);
2547 }
satokee5e77c2011-09-02 18:50:15 +09002548 return;
satok91e88122011-07-18 11:11:42 +09002549 }
2550 }
2551 }
satoke7c6998e2011-06-03 17:57:59 +09002552 }
satokee5e77c2011-09-02 18:50:15 +09002553 return;
satoke7c6998e2011-06-03 17:57:59 +09002554 }
2555
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002556 @Override
2557 public int getInputMethodWindowVisibleHeight() {
2558 return mWindowManagerService.getInputMethodWindowVisibleHeight();
2559 }
2560
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002561 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002562 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002563 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002564 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002565 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002566 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002567 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
2568 if (DEBUG) {
2569 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
2570 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
2571 + " actual: " + sequenceNumber);
2572 }
2573 return;
2574 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002575 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2576 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09002577 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002578 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002579 }
2580 }
2581
satok28203512010-11-24 11:06:49 +09002582 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002583 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002584 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
2585 }
2586 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002587
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002588 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
2589 if (token == null) {
2590 if (mContext.checkCallingOrSelfPermission(
2591 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2592 != PackageManager.PERMISSION_GRANTED) {
2593 throw new SecurityException(
2594 "Using null token requires permission "
2595 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002596 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002597 } else if (mCurToken != token) {
2598 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2599 + " token: " + token);
2600 return;
2601 }
2602
2603 final long ident = Binder.clearCallingIdentity();
2604 try {
2605 setInputMethodLocked(id, subtypeId);
2606 } finally {
2607 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002608 }
2609 }
2610
satok42c5a162011-05-26 16:46:14 +09002611 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002612 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002613 if (!calledFromValidUser()) {
2614 return;
2615 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002616 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002617 if (!calledWithValidToken(token)) {
2618 final int uid = Binder.getCallingUid();
2619 Slog.e(TAG, "Ignoring hideInputMethod due to an invalid token. uid:"
2620 + uid + " token:" + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002621 return;
2622 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002623 long ident = Binder.clearCallingIdentity();
2624 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002625 hideCurrentInputLocked(flags, null);
2626 } finally {
2627 Binder.restoreCallingIdentity(ident);
2628 }
2629 }
2630 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002631
satok42c5a162011-05-26 16:46:14 +09002632 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002633 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002634 if (!calledFromValidUser()) {
2635 return;
2636 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002637 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002638 if (!calledWithValidToken(token)) {
2639 final int uid = Binder.getCallingUid();
2640 Slog.e(TAG, "Ignoring showMySoftInput due to an invalid token. uid:"
2641 + uid + " token:" + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002642 return;
2643 }
2644 long ident = Binder.clearCallingIdentity();
2645 try {
2646 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002647 } finally {
2648 Binder.restoreCallingIdentity(ident);
2649 }
2650 }
2651 }
2652
2653 void setEnabledSessionInMainThread(SessionState session) {
2654 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002655 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002656 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002657 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002658 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002659 } catch (RemoteException e) {
2660 }
2661 }
2662 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002663 if (mEnabledSession != null && mEnabledSession.session != null) {
2664 try {
2665 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
2666 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
2667 } catch (RemoteException e) {
2668 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002669 }
2670 }
2671 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002672
satok42c5a162011-05-26 16:46:14 +09002673 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002674 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002675 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002676 switch (msg.what) {
satokab751aa2010-09-14 19:17:36 +09002677 case MSG_SHOW_IM_SUBTYPE_PICKER:
Seigo Nonaka14e13912015-05-06 21:04:13 -07002678 final boolean showAuxSubtypes;
2679 switch (msg.arg1) {
2680 case InputMethodManager.SHOW_IM_PICKER_MODE_AUTO:
2681 // This is undocumented so far, but IMM#showInputMethodPicker() has been
2682 // implemented so that auxiliary subtypes will be excluded when the soft
2683 // keyboard is invisible.
2684 showAuxSubtypes = mInputShown;
2685 break;
2686 case InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES:
2687 showAuxSubtypes = true;
2688 break;
2689 case InputMethodManager.SHOW_IM_PICKER_MODE_EXCLUDE_AUXILIARY_SUBTYPES:
2690 showAuxSubtypes = false;
2691 break;
2692 default:
2693 Slog.e(TAG, "Unknown subtype picker mode = " + msg.arg1);
2694 return false;
2695 }
2696 showInputMethodMenu(showAuxSubtypes);
satokab751aa2010-09-14 19:17:36 +09002697 return true;
2698
satok47a44912010-10-06 16:03:58 +09002699 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002700 args = (SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09002701 showInputMethodAndSubtypeEnabler((String)args.arg1);
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002702 args.recycle();
satok217f5482010-12-15 05:19:19 +09002703 return true;
2704
2705 case MSG_SHOW_IM_CONFIG:
2706 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002707 return true;
2708
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002709 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002710
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002711 case MSG_UNBIND_INPUT:
2712 try {
2713 ((IInputMethod)msg.obj).unbindInput();
2714 } catch (RemoteException e) {
2715 // There is nothing interesting about the method dying.
2716 }
2717 return true;
2718 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002719 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002720 try {
2721 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2722 } catch (RemoteException e) {
2723 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002724 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002725 return true;
2726 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002727 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002728 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002729 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002730 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002731 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002732 } catch (RemoteException e) {
2733 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002734 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002735 return true;
2736 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002737 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002738 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002739 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002740 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002741 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002742 } catch (RemoteException e) {
2743 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002744 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002745 return true;
2746 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002747 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002748 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002749 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002750 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2751 } catch (RemoteException e) {
2752 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002753 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002754 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002755 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002756 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002757 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002758 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002759 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002760 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002761 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002762 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002763 // Dispose the channel if the input method is not local to this process
2764 // because the remote proxy will get its own copy when unparceled.
2765 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002766 channel.dispose();
2767 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002768 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002769 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002770 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002771 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002772 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002773
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002774 case MSG_START_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002775 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002776 try {
2777 SessionState session = (SessionState)args.arg1;
2778 setEnabledSessionInMainThread(session);
2779 session.method.startInput((IInputContext)args.arg2,
2780 (EditorInfo)args.arg3);
2781 } catch (RemoteException e) {
2782 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002783 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002784 return true;
2785 case MSG_RESTART_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002786 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002787 try {
2788 SessionState session = (SessionState)args.arg1;
2789 setEnabledSessionInMainThread(session);
2790 session.method.restartInput((IInputContext)args.arg2,
2791 (EditorInfo)args.arg3);
2792 } catch (RemoteException e) {
2793 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002794 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002795 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002796
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002797 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002798
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002799 case MSG_UNBIND_METHOD:
2800 try {
2801 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
2802 } catch (RemoteException e) {
2803 // There is nothing interesting about the last client dying.
2804 }
2805 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002806 case MSG_BIND_METHOD: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002807 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002808 IInputMethodClient client = (IInputMethodClient)args.arg1;
2809 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002810 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002811 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002812 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002813 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002814 } finally {
2815 // Dispose the channel if the input method is not local to this process
2816 // because the remote proxy will get its own copy when unparceled.
2817 if (res.channel != null && Binder.isProxy(client)) {
2818 res.channel.dispose();
2819 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002820 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002821 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002822 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002823 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002824 case MSG_SET_ACTIVE:
2825 try {
2826 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2827 } catch (RemoteException e) {
2828 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2829 + ((ClientState)msg.obj).pid + " uid "
2830 + ((ClientState)msg.obj).uid);
2831 }
2832 return true;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002833 case MSG_SET_INTERACTIVE:
2834 handleSetInteractive(msg.arg1 != 0);
2835 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002836 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
2837 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07002838 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002839 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07002840 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002841 } catch (RemoteException e) {
2842 Slog.w(TAG, "Got RemoteException sending "
2843 + "setUserActionNotificationSequenceNumber("
2844 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07002845 + clientState.pid + " uid "
2846 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002847 }
2848 return true;
2849 }
satok01038492012-04-09 21:08:27 +09002850
2851 // --------------------------------------------------------------
2852 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07002853 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09002854 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002855 }
2856 return false;
2857 }
2858
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002859 private void handleSetInteractive(final boolean interactive) {
2860 synchronized (mMethodMap) {
2861 mIsInteractive = interactive;
2862 updateSystemUiLocked(mCurToken, interactive ? mImeWindowVis : 0, mBackDisposition);
2863
2864 // Inform the current client of the change in active status
2865 if (mCurClient != null && mCurClient.client != null) {
2866 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
2867 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, mCurClient));
2868 }
2869 }
2870 }
2871
satokdc9ddae2011-10-06 12:22:36 +09002872 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002873 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
2874 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09002875 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002876 if (DEBUG) {
2877 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2878 }
satok723a27e2010-11-11 14:58:11 +09002879 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002880 return true;
2881 }
2882
2883 return false;
2884 }
2885
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002886 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002887 HashMap<String, InputMethodInfo> map, boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002888 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002889 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Satoshi Kataoka0766eb02013-07-31 18:30:13 +09002890 + " \n ------ \n" + InputMethodUtils.getStackTrace());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002891 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002892 list.clear();
2893 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002894
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002895 // Use for queryIntentServicesAsUser
2896 final PackageManager pm = mContext.getPackageManager();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002897 String disabledSysImes = mSettings.getDisabledSystemInputMethods();
Amith Yamasanie861ec12010-03-24 21:39:27 -07002898 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002899
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002900 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002901 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002902 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2903 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002904
satoke7c6998e2011-06-03 17:57:59 +09002905 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2906 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002907 for (int i = 0; i < services.size(); ++i) {
2908 ResolveInfo ri = services.get(i);
2909 ServiceInfo si = ri.serviceInfo;
2910 ComponentName compName = new ComponentName(si.packageName, si.name);
2911 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2912 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002913 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002914 + ": it does not require the permission "
2915 + android.Manifest.permission.BIND_INPUT_METHOD);
2916 continue;
2917 }
2918
Joe Onorato8a9b2202010-02-26 18:56:32 -08002919 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002920
2921 try {
satoke7c6998e2011-06-03 17:57:59 +09002922 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002923 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002924 final String id = p.getId();
2925 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002926
2927 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002928 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002929 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002930
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002931 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002932 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002933 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002934 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002935 }
2936 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002937
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002938 if (resetDefaultEnabledIme) {
2939 final ArrayList<InputMethodInfo> defaultEnabledIme =
2940 InputMethodUtils.getDefaultEnabledImes(mContext, mSystemReady, list);
2941 for (int i = 0; i < defaultEnabledIme.size(); ++i) {
2942 final InputMethodInfo imi = defaultEnabledIme.get(i);
2943 if (DEBUG) {
2944 Slog.d(TAG, "--- enable ime = " + imi);
2945 }
2946 setInputMethodEnabledLocked(imi.getId(), true);
2947 }
2948 }
2949
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002950 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09002951 if (!TextUtils.isEmpty(defaultImiId)) {
2952 if (!map.containsKey(defaultImiId)) {
2953 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
2954 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07002955 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09002956 }
2957 } else {
2958 // Double check that the default IME is certainly enabled.
2959 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002960 }
2961 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09002962 // Here is not the perfect place to reset the switching controller. Ideally
2963 // mSwitchingController and mSettings should be able to share the same state.
2964 // TODO: Make sure that mSwitchingController and mSettings are sharing the
2965 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09002966 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002967 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002968
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002969 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002970
satok217f5482010-12-15 05:19:19 +09002971 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002972 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002973 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002974 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2975 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002976 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002977 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002978 }
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002979 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok217f5482010-12-15 05:19:19 +09002980 }
2981
2982 private void showConfigureInputMethods() {
2983 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2984 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2985 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2986 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002987 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09002988 }
2989
satok2c93efc2012-04-02 19:33:47 +09002990 private boolean isScreenLocked() {
2991 return mKeyguardManager != null
2992 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
2993 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09002994
Seigo Nonaka14e13912015-05-06 21:04:13 -07002995 private void showInputMethodMenu(boolean showAuxSubtypes) {
2996 if (DEBUG) Slog.v(TAG, "Show switching menu. showAuxSubtypes=" + showAuxSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002997
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002998 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09002999 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003000
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003001 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003002 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003003 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003004
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003005 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09003006 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09003007 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
3008 mContext);
satok7f35c8c2010-10-07 21:13:11 +09003009 if (immis == null || immis.size() == 0) {
3010 return;
3011 }
3012
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003013 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003014
satok688bd472012-02-09 20:09:17 +09003015 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003016 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
Seigo Nonaka14e13912015-05-06 21:04:13 -07003017 true /* showSubtypes */, showAuxSubtypes, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09003018
satokc3690562012-01-10 20:14:43 +09003019 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003020 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09003021 if (currentSubtype != null) {
3022 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003023 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
3024 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09003025 }
3026 }
3027
Ken Wakasa761eb372011-03-04 19:06:18 +09003028 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09003029 mIms = new InputMethodInfo[N];
3030 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003031 int checkedItem = 0;
3032 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09003033 final ImeSubtypeListItem item = imList.get(i);
3034 mIms[i] = item.mImi;
3035 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003036 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09003037 int subtypeId = mSubtypeIds[i];
3038 if ((subtypeId == NOT_A_SUBTYPE_ID)
3039 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
3040 || (subtypeId == lastInputMethodSubtypeId)) {
3041 checkedItem = i;
3042 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003043 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003044 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08003045
3046 final Context settingsContext = new ContextThemeWrapper(context,
3047 com.android.internal.R.style.Theme_DeviceDefault_Settings);
3048
3049 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003050 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
3051 @Override
3052 public void onCancel(DialogInterface dialog) {
3053 hideInputMethodMenu();
3054 }
3055 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08003056
3057 final Context dialogContext = mDialogBuilder.getContext();
3058 final TypedArray a = dialogContext.obtainStyledAttributes(null,
3059 com.android.internal.R.styleable.DialogPreference,
3060 com.android.internal.R.attr.alertDialogStyle, 0);
3061 final Drawable dialogIcon = a.getDrawable(
3062 com.android.internal.R.styleable.DialogPreference_dialogIcon);
3063 a.recycle();
3064
3065 mDialogBuilder.setIcon(dialogIcon);
3066
3067 final LayoutInflater inflater = (LayoutInflater) dialogContext.getSystemService(
3068 Context.LAYOUT_INFLATER_SERVICE);
satok01038492012-04-09 21:08:27 +09003069 final View tv = inflater.inflate(
3070 com.android.internal.R.layout.input_method_switch_dialog_title, null);
3071 mDialogBuilder.setCustomTitle(tv);
3072
3073 // Setup layout for a toggle switch of the hardware keyboard
3074 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003075 mSwitchingDialogTitleView
3076 .findViewById(com.android.internal.R.id.hard_keyboard_section)
3077 .setVisibility(mWindowManagerService.isHardKeyboardAvailable()
3078 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003079 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003080 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07003081 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003082 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
3083 @Override
3084 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003085 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003086 // Ensure that the input method dialog is dismissed when changing
3087 // the hardware keyboard state.
3088 hideInputMethodMenu();
3089 }
3090 });
3091
Alan Viverette505e3ab2014-11-24 15:22:11 -08003092 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003093 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
3094 final OnClickListener choiceListener = new OnClickListener() {
3095 @Override
3096 public void onClick(final DialogInterface dialog, final int which) {
3097 synchronized (mMethodMap) {
3098 if (mIms == null || mIms.length <= which || mSubtypeIds == null
3099 || mSubtypeIds.length <= which) {
3100 return;
satok01038492012-04-09 21:08:27 +09003101 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003102 final InputMethodInfo im = mIms[which];
3103 int subtypeId = mSubtypeIds[which];
3104 adapter.mCheckedItem = which;
3105 adapter.notifyDataSetChanged();
3106 hideInputMethodMenu();
3107 if (im != null) {
3108 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
3109 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003110 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003111 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003112 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003113 }
3114 }
3115 };
3116 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003117
Seigo Nonakad4474cb2015-05-04 16:53:24 -07003118 if (!isScreenLocked) {
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003119 final OnClickListener positiveListener = new OnClickListener() {
3120 @Override
3121 public void onClick(DialogInterface dialog, int whichButton) {
3122 showConfigureInputMethods();
3123 }
3124 };
satok82beadf2010-12-27 19:03:06 +09003125 mDialogBuilder.setPositiveButton(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003126 com.android.internal.R.string.configure_input_methods, positiveListener);
satok7f35c8c2010-10-07 21:13:11 +09003127 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003128 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003129 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003130 mSwitchingDialog.getWindow().setType(
3131 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09003132 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
3133 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003134 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003135 updateSystemUi(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003136 mSwitchingDialog.show();
3137 }
3138 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003139
Ken Wakasa05dbb652011-08-22 15:22:43 +09003140 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
3141 private final LayoutInflater mInflater;
3142 private final int mTextViewResourceId;
3143 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09003144 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09003145 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
3146 List<ImeSubtypeListItem> itemsList, int checkedItem) {
3147 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003148
Ken Wakasa05dbb652011-08-22 15:22:43 +09003149 mTextViewResourceId = textViewResourceId;
3150 mItemsList = itemsList;
3151 mCheckedItem = checkedItem;
Alan Viverette505e3ab2014-11-24 15:22:11 -08003152 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Ken Wakasa05dbb652011-08-22 15:22:43 +09003153 }
3154
3155 @Override
3156 public View getView(int position, View convertView, ViewGroup parent) {
3157 final View view = convertView != null ? convertView
3158 : mInflater.inflate(mTextViewResourceId, null);
3159 if (position < 0 || position >= mItemsList.size()) return view;
3160 final ImeSubtypeListItem item = mItemsList.get(position);
3161 final CharSequence imeName = item.mImeName;
3162 final CharSequence subtypeName = item.mSubtypeName;
3163 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
3164 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
3165 if (TextUtils.isEmpty(subtypeName)) {
3166 firstTextView.setText(imeName);
3167 secondTextView.setVisibility(View.GONE);
3168 } else {
3169 firstTextView.setText(subtypeName);
3170 secondTextView.setText(imeName);
3171 secondTextView.setVisibility(View.VISIBLE);
3172 }
3173 final RadioButton radioButton =
3174 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
3175 radioButton.setChecked(position == mCheckedItem);
3176 return view;
3177 }
3178 }
3179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003180 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003181 synchronized (mMethodMap) {
3182 hideInputMethodMenuLocked();
3183 }
3184 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003185
The Android Open Source Project10592532009-03-18 17:39:46 -07003186 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003187 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003188
The Android Open Source Project10592532009-03-18 17:39:46 -07003189 if (mSwitchingDialog != null) {
3190 mSwitchingDialog.dismiss();
3191 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003192 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003193
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003194 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project10592532009-03-18 17:39:46 -07003195 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003196 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003197 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003198
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003199 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003200
satok42c5a162011-05-26 16:46:14 +09003201 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003202 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003203 // TODO: Make this work even for non-current users?
3204 if (!calledFromValidUser()) {
3205 return false;
3206 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003207 synchronized (mMethodMap) {
3208 if (mContext.checkCallingOrSelfPermission(
3209 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3210 != PackageManager.PERMISSION_GRANTED) {
3211 throw new SecurityException(
3212 "Requires permission "
3213 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3214 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003215
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003216 long ident = Binder.clearCallingIdentity();
3217 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003218 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003219 } finally {
3220 Binder.restoreCallingIdentity(ident);
3221 }
3222 }
3223 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003224
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003225 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3226 // Make sure this is a valid input method.
3227 InputMethodInfo imm = mMethodMap.get(id);
3228 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003229 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003230 }
3231
satokd87c2592010-09-29 11:52:06 +09003232 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3233 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003234
satokd87c2592010-09-29 11:52:06 +09003235 if (enabled) {
3236 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3237 if (pair.first.equals(id)) {
3238 // We are enabling this input method, but it is already enabled.
3239 // Nothing to do. The previous state was enabled.
3240 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003241 }
3242 }
satokd87c2592010-09-29 11:52:06 +09003243 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3244 // Previous state was disabled.
3245 return false;
3246 } else {
3247 StringBuilder builder = new StringBuilder();
3248 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3249 builder, enabledInputMethodsList, id)) {
3250 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003251 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003252 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3253 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3254 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003255 }
3256 // Previous state was enabled.
3257 return true;
3258 } else {
3259 // We are disabling the input method but it is already disabled.
3260 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003261 return false;
3262 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003263 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003264 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003265
satok723a27e2010-11-11 14:58:11 +09003266 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3267 boolean setSubtypeOnly) {
3268 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003269 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003270
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003271 mCurUserActionNotificationSequenceNumber =
3272 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3273 if (DEBUG) {
3274 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3275 + mCurUserActionNotificationSequenceNumber);
3276 }
3277
3278 if (mCurClient != null && mCurClient.client != null) {
3279 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3280 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003281 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003282 }
3283
satok723a27e2010-11-11 14:58:11 +09003284 // Set Subtype here
3285 if (imi == null || subtypeId < 0) {
3286 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003287 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003288 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003289 if (subtypeId < imi.getSubtypeCount()) {
3290 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3291 mSettings.putSelectedSubtype(subtype.hashCode());
3292 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003293 } else {
3294 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003295 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003296 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003297 }
satokab751aa2010-09-14 19:17:36 +09003298 }
satok723a27e2010-11-11 14:58:11 +09003299
satok4c0e7152012-06-20 20:08:44 +09003300 // Workaround.
3301 // ASEC is not ready in the IMMS constructor. Accordingly, forward-locked
3302 // IMEs are not recognized and considered uninstalled.
3303 // Actually, we can't move everything after SystemReady because
3304 // IMMS needs to run in the encryption lock screen. So, we just skip changing
3305 // the default IME here and try cheking the default IME again in systemReady().
3306 // TODO: Do nothing before system ready and implement a separated logic for
3307 // the encryption lock screen.
3308 // TODO: ASEC should be ready before IMMS is instantiated.
3309 if (mSystemReady && !setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003310 // Set InputMethod here
3311 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3312 }
3313 }
3314
3315 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3316 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3317 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3318 // newDefaultIme is empty when there is no candidate for the selected IME.
3319 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3320 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3321 if (subtypeHashCode != null) {
3322 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003323 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok723a27e2010-11-11 14:58:11 +09003324 imi, Integer.valueOf(subtypeHashCode));
3325 } catch (NumberFormatException e) {
3326 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3327 }
3328 }
3329 }
3330 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003331 }
3332
satok4e4569d2010-11-19 18:45:53 +09003333 // If there are no selected shortcuts, tries finding the most applicable ones.
3334 private Pair<InputMethodInfo, InputMethodSubtype>
3335 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3336 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3337 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003338 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003339 boolean foundInSystemIME = false;
3340
3341 // Search applicable subtype for each InputMethodInfo
3342 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003343 final String imiId = imi.getId();
3344 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3345 continue;
3346 }
satokcd7cd292010-11-20 15:46:23 +09003347 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003348 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003349 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003350 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003351 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003352 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003353 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003354 }
satokdf31ae62011-01-15 06:19:44 +09003355 // 2. Search by the system locale from enabledSubtypes.
3356 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003357 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003358 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003359 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003360 }
satoka86f5e42011-09-02 17:12:42 +09003361 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003362 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003363 final ArrayList<InputMethodSubtype> subtypesForSearch =
3364 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003365 ? InputMethodUtils.getSubtypes(imi)
3366 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003367 // 4. Search by the current subtype's locale from all subtypes.
3368 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003369 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003370 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003371 }
3372 // 5. Search by the system locale from all subtypes.
3373 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003374 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003375 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003376 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003377 }
satokcd7cd292010-11-20 15:46:23 +09003378 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003379 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003380 // The current input method is the most applicable IME.
3381 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003382 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003383 break;
satok7599a7f2010-12-22 13:45:23 +09003384 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003385 // The system input method is 2nd applicable IME.
3386 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003387 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003388 if ((imi.getServiceInfo().applicationInfo.flags
3389 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3390 foundInSystemIME = true;
3391 }
satok4e4569d2010-11-19 18:45:53 +09003392 }
3393 }
3394 }
3395 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003396 if (mostApplicableIMI != null) {
3397 Slog.w(TAG, "Most applicable shortcut input method was:"
3398 + mostApplicableIMI.getId());
3399 if (mostApplicableSubtype != null) {
3400 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3401 + "," + mostApplicableSubtype.getMode() + ","
3402 + mostApplicableSubtype.getLocale());
3403 }
3404 }
satok4e4569d2010-11-19 18:45:53 +09003405 }
satokcd7cd292010-11-20 15:46:23 +09003406 if (mostApplicableIMI != null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003407 return new Pair<> (mostApplicableIMI, mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003408 } else {
3409 return null;
3410 }
3411 }
3412
satokab751aa2010-09-14 19:17:36 +09003413 /**
3414 * @return Return the current subtype of this input method.
3415 */
satok42c5a162011-05-26 16:46:14 +09003416 @Override
satokab751aa2010-09-14 19:17:36 +09003417 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003418 // TODO: Make this work even for non-current users?
3419 if (!calledFromValidUser()) {
3420 return null;
3421 }
3422 synchronized (mMethodMap) {
3423 return getCurrentInputMethodSubtypeLocked();
3424 }
3425 }
3426
3427 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003428 if (mCurMethodId == null) {
3429 return null;
3430 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003431 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003432 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3433 if (imi == null || imi.getSubtypeCount() == 0) {
3434 return null;
satok4e4569d2010-11-19 18:45:53 +09003435 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003436 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003437 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3438 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003439 if (subtypeId == NOT_A_SUBTYPE_ID) {
3440 // If there are no selected subtypes, the framework will try to find
3441 // the most applicable subtype from explicitly or implicitly enabled
3442 // subtypes.
3443 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003444 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003445 // If there is only one explicitly or implicitly enabled subtype,
3446 // just returns it.
3447 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3448 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3449 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003450 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003451 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003452 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003453 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003454 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003455 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3456 true);
satok4e4569d2010-11-19 18:45:53 +09003457 }
satok3ef8b292010-11-23 06:06:29 +09003458 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003459 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003460 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003461 }
3462 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003463 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003464 }
3465
satokf3db1af2010-11-23 13:34:33 +09003466 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
3467 InputMethodSubtype subtype) {
3468 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
3469 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
3470 } else {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003471 ArrayList<InputMethodSubtype> subtypes = new ArrayList<>();
satokf3db1af2010-11-23 13:34:33 +09003472 subtypes.add(subtype);
3473 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
3474 }
3475 }
3476
satok4e4569d2010-11-19 18:45:53 +09003477 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003478 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003479 @Override
satok4e4569d2010-11-19 18:45:53 +09003480 public List getShortcutInputMethodsAndSubtypes() {
3481 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003482 ArrayList<Object> ret = new ArrayList<>();
satokf3db1af2010-11-23 13:34:33 +09003483 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003484 // If there are no selected shortcut subtypes, the framework will try to find
3485 // the most applicable subtype from all subtypes whose mode is
3486 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003487 Pair<InputMethodInfo, InputMethodSubtype> info =
3488 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003489 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003490 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003491 ret.add(info.first);
3492 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003493 }
satok3da92232011-01-11 22:46:30 +09003494 return ret;
satokf3db1af2010-11-23 13:34:33 +09003495 }
satokf3db1af2010-11-23 13:34:33 +09003496 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3497 ret.add(imi);
3498 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3499 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003500 }
3501 }
satokf3db1af2010-11-23 13:34:33 +09003502 return ret;
satok4e4569d2010-11-19 18:45:53 +09003503 }
3504 }
3505
satok42c5a162011-05-26 16:46:14 +09003506 @Override
satokb66d2872010-11-10 01:04:04 +09003507 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003508 // TODO: Make this work even for non-current users?
3509 if (!calledFromValidUser()) {
3510 return false;
3511 }
satokb66d2872010-11-10 01:04:04 +09003512 synchronized (mMethodMap) {
3513 if (subtype != null && mCurMethodId != null) {
3514 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003515 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003516 if (subtypeId != NOT_A_SUBTYPE_ID) {
3517 setInputMethodLocked(mCurMethodId, subtypeId);
3518 return true;
3519 }
3520 }
3521 return false;
3522 }
3523 }
3524
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003525 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003526 private static class InputMethodFileManager {
3527 private static final String SYSTEM_PATH = "system";
3528 private static final String INPUT_METHOD_PATH = "inputmethod";
3529 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3530 private static final String NODE_SUBTYPES = "subtypes";
3531 private static final String NODE_SUBTYPE = "subtype";
3532 private static final String NODE_IMI = "imi";
3533 private static final String ATTR_ID = "id";
3534 private static final String ATTR_LABEL = "label";
3535 private static final String ATTR_ICON = "icon";
3536 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3537 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3538 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3539 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3540 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3541 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003542 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003543 new HashMap<>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003544 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003545 if (methodMap == null) {
3546 throw new NullPointerException("methodMap is null");
3547 }
3548 mMethodMap = methodMap;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003549 final File systemDir = userId == UserHandle.USER_OWNER
3550 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3551 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003552 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
Yohei Yukawadf5af482015-08-04 22:11:11 -07003553 if (!inputMethodDir.exists() && !inputMethodDir.mkdirs()) {
satoke7c6998e2011-06-03 17:57:59 +09003554 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3555 }
3556 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3557 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3558 if (!subtypeFile.exists()) {
3559 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003560 writeAdditionalInputMethodSubtypes(
3561 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003562 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003563 readAdditionalInputMethodSubtypes(
3564 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003565 }
3566 }
3567
3568 private void deleteAllInputMethodSubtypes(String imiId) {
3569 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003570 mAdditionalSubtypesMap.remove(imiId);
3571 writeAdditionalInputMethodSubtypes(
3572 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003573 }
3574 }
3575
3576 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003577 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003578 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003579 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003580 final int N = additionalSubtypes.length;
3581 for (int i = 0; i < N; ++i) {
3582 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003583 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003584 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003585 } else {
3586 Slog.w(TAG, "Duplicated subtype definition found: "
3587 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003588 }
3589 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003590 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3591 writeAdditionalInputMethodSubtypes(
3592 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003593 }
3594 }
3595
3596 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3597 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003598 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003599 }
3600 }
3601
3602 private static void writeAdditionalInputMethodSubtypes(
3603 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3604 HashMap<String, InputMethodInfo> methodMap) {
3605 // Safety net for the case that this function is called before methodMap is set.
3606 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3607 FileOutputStream fos = null;
3608 try {
3609 fos = subtypesFile.startWrite();
3610 final XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003611 out.setOutput(fos, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003612 out.startDocument(null, true);
3613 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3614 out.startTag(null, NODE_SUBTYPES);
3615 for (String imiId : allSubtypes.keySet()) {
3616 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3617 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3618 continue;
3619 }
3620 out.startTag(null, NODE_IMI);
3621 out.attribute(null, ATTR_ID, imiId);
3622 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3623 final int N = subtypesList.size();
3624 for (int i = 0; i < N; ++i) {
3625 final InputMethodSubtype subtype = subtypesList.get(i);
3626 out.startTag(null, NODE_SUBTYPE);
3627 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3628 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3629 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3630 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3631 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3632 out.attribute(null, ATTR_IS_AUXILIARY,
3633 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3634 out.endTag(null, NODE_SUBTYPE);
3635 }
3636 out.endTag(null, NODE_IMI);
3637 }
3638 out.endTag(null, NODE_SUBTYPES);
3639 out.endDocument();
3640 subtypesFile.finishWrite(fos);
3641 } catch (java.io.IOException e) {
3642 Slog.w(TAG, "Error writing subtypes", e);
3643 if (fos != null) {
3644 subtypesFile.failWrite(fos);
3645 }
3646 }
3647 }
3648
3649 private static void readAdditionalInputMethodSubtypes(
3650 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3651 if (allSubtypes == null || subtypesFile == null) return;
3652 allSubtypes.clear();
3653 FileInputStream fis = null;
3654 try {
3655 fis = subtypesFile.openRead();
3656 final XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003657 parser.setInput(fis, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003658 int type = parser.getEventType();
3659 // Skip parsing until START_TAG
3660 while ((type = parser.next()) != XmlPullParser.START_TAG
3661 && type != XmlPullParser.END_DOCUMENT) {}
3662 String firstNodeName = parser.getName();
3663 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3664 throw new XmlPullParserException("Xml doesn't start with subtypes");
3665 }
3666 final int depth =parser.getDepth();
3667 String currentImiId = null;
3668 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3669 while (((type = parser.next()) != XmlPullParser.END_TAG
3670 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3671 if (type != XmlPullParser.START_TAG)
3672 continue;
3673 final String nodeName = parser.getName();
3674 if (NODE_IMI.equals(nodeName)) {
3675 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3676 if (TextUtils.isEmpty(currentImiId)) {
3677 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3678 continue;
3679 }
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003680 tempSubtypesArray = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003681 allSubtypes.put(currentImiId, tempSubtypesArray);
3682 } else if (NODE_SUBTYPE.equals(nodeName)) {
3683 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3684 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3685 continue;
3686 }
3687 final int icon = Integer.valueOf(
3688 parser.getAttributeValue(null, ATTR_ICON));
3689 final int label = Integer.valueOf(
3690 parser.getAttributeValue(null, ATTR_LABEL));
3691 final String imeSubtypeLocale =
3692 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3693 final String imeSubtypeMode =
3694 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3695 final String imeSubtypeExtraValue =
3696 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003697 final boolean isAuxiliary = "1".equals(String.valueOf(
3698 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003699 final InputMethodSubtype subtype = new InputMethodSubtypeBuilder()
3700 .setSubtypeNameResId(label)
3701 .setSubtypeIconResId(icon)
3702 .setSubtypeLocale(imeSubtypeLocale)
3703 .setSubtypeMode(imeSubtypeMode)
3704 .setSubtypeExtraValue(imeSubtypeExtraValue)
3705 .setIsAuxiliary(isAuxiliary)
3706 .build();
satoke7c6998e2011-06-03 17:57:59 +09003707 tempSubtypesArray.add(subtype);
3708 }
3709 }
3710 } catch (XmlPullParserException e) {
3711 Slog.w(TAG, "Error reading subtypes: " + e);
3712 return;
3713 } catch (java.io.IOException e) {
3714 Slog.w(TAG, "Error reading subtypes: " + e);
3715 return;
3716 } catch (NumberFormatException e) {
3717 Slog.w(TAG, "Error reading subtypes: " + e);
3718 return;
3719 } finally {
3720 if (fis != null) {
3721 try {
3722 fis.close();
3723 } catch (java.io.IOException e1) {
3724 Slog.w(TAG, "Failed to close.");
3725 }
3726 }
3727 }
3728 }
3729 }
3730
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003731 private static final class LocalServiceImpl implements InputMethodManagerInternal {
3732 @NonNull
3733 private final Handler mHandler;
3734
3735 LocalServiceImpl(@NonNull final Handler handler) {
3736 mHandler = handler;
3737 }
3738
3739 @Override
3740 public void setInteractive(boolean interactive) {
3741 // Do everything in handler so as not to block the caller.
3742 mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_INTERACTIVE,
3743 interactive ? 1 : 0, 0));
3744 }
3745 }
3746
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003747 @Override
3748 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3749 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3750 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003751
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003752 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3753 + Binder.getCallingPid()
3754 + ", uid=" + Binder.getCallingUid());
3755 return;
3756 }
3757
3758 IInputMethod method;
3759 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003760
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003761 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003762
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003763 synchronized (mMethodMap) {
3764 p.println("Current Input Method Manager state:");
3765 int N = mMethodList.size();
3766 p.println(" Input Methods:");
3767 for (int i=0; i<N; i++) {
3768 InputMethodInfo info = mMethodList.get(i);
3769 p.println(" InputMethod #" + i + ":");
3770 info.dump(p, " ");
3771 }
3772 p.println(" Clients:");
3773 for (ClientState ci : mClients.values()) {
3774 p.println(" Client " + ci + ":");
3775 p.println(" client=" + ci.client);
3776 p.println(" inputContext=" + ci.inputContext);
3777 p.println(" sessionRequested=" + ci.sessionRequested);
3778 p.println(" curSession=" + ci.curSession);
3779 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003780 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003781 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003782 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3783 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003784 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3785 + " mBoundToMethod=" + mBoundToMethod);
3786 p.println(" mCurToken=" + mCurToken);
3787 p.println(" mCurIntent=" + mCurIntent);
3788 method = mCurMethod;
3789 p.println(" mCurMethod=" + mCurMethod);
3790 p.println(" mEnabledSession=" + mEnabledSession);
3791 p.println(" mShowRequested=" + mShowRequested
3792 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3793 + " mShowForced=" + mShowForced
3794 + " mInputShown=" + mInputShown);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003795 p.println(" mCurUserActionNotificationSequenceNumber="
3796 + mCurUserActionNotificationSequenceNumber);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003797 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mIsInteractive);
Yohei Yukawa81482972015-06-04 00:58:59 -07003798 p.println(" mSettingsObserver=" + mSettingsObserver);
Yohei Yukawad7248862015-06-03 23:56:12 -07003799 p.println(" mSwitchingController:");
3800 mSwitchingController.dump(p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003801 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003802
Jeff Brownb88102f2010-09-08 11:49:43 -07003803 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003804 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003805 pw.flush();
3806 try {
3807 client.client.asBinder().dump(fd, args);
3808 } catch (RemoteException e) {
3809 p.println("Input method client dead: " + e);
3810 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003811 } else {
3812 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003813 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003814
Jeff Brownb88102f2010-09-08 11:49:43 -07003815 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003816 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003817 pw.flush();
3818 try {
3819 method.asBinder().dump(fd, args);
3820 } catch (RemoteException e) {
3821 p.println("Input method service dead: " + e);
3822 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003823 } else {
3824 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003825 }
3826 }
3827}