The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006-2008 The Android Open Source Project |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | * use this file except in compliance with the License. You may obtain a copy of |
| 6 | * the License at |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 7 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 9 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | * License for the specific language governing permissions and limitations under |
| 14 | * the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server; |
| 18 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 19 | import com.android.internal.content.PackageMonitor; |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 20 | import com.android.internal.os.AtomicFile; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | import com.android.internal.os.HandlerCaller; |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 22 | import com.android.internal.util.FastXmlSerializer; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | import com.android.internal.view.IInputContext; |
| 24 | import com.android.internal.view.IInputMethod; |
| 25 | import com.android.internal.view.IInputMethodCallback; |
| 26 | import com.android.internal.view.IInputMethodClient; |
| 27 | import com.android.internal.view.IInputMethodManager; |
| 28 | import com.android.internal.view.IInputMethodSession; |
| 29 | import com.android.internal.view.InputBindResult; |
Dianne Hackborn | a924dc0d | 2011-02-17 14:22:17 -0800 | [diff] [blame] | 30 | import com.android.server.EventLogTags; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 32 | import org.xmlpull.v1.XmlPullParser; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | import org.xmlpull.v1.XmlPullParserException; |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 34 | import org.xmlpull.v1.XmlSerializer; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | |
| 36 | import android.app.ActivityManagerNative; |
| 37 | import android.app.AlertDialog; |
satok | 7cfc0ed | 2011-06-20 21:29:36 +0900 | [diff] [blame] | 38 | import android.app.Notification; |
| 39 | import android.app.NotificationManager; |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 40 | import android.app.PendingIntent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | import android.content.ComponentName; |
| 42 | import android.content.ContentResolver; |
| 43 | import android.content.Context; |
| 44 | import android.content.DialogInterface; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | import android.content.DialogInterface.OnCancelListener; |
| 46 | import android.content.Intent; |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 47 | import android.content.IntentFilter; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | import android.content.ServiceConnection; |
Brandon Ballinger | 6da35a0 | 2009-10-21 00:38:13 -0700 | [diff] [blame] | 49 | import android.content.pm.ApplicationInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | import android.content.pm.PackageManager; |
| 51 | import android.content.pm.ResolveInfo; |
| 52 | import android.content.pm.ServiceInfo; |
Amith Yamasani | e861ec1 | 2010-03-24 21:39:27 -0700 | [diff] [blame] | 53 | import android.content.res.Configuration; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | import android.content.res.Resources; |
| 55 | import android.content.res.TypedArray; |
| 56 | import android.database.ContentObserver; |
satok | 7cfc0ed | 2011-06-20 21:29:36 +0900 | [diff] [blame] | 57 | import android.graphics.BitmapFactory; |
Joe Onorato | 857fd9b | 2011-01-27 15:08:35 -0800 | [diff] [blame] | 58 | import android.inputmethodservice.InputMethodService; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | import android.os.Binder; |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 60 | import android.os.Environment; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | import android.os.Handler; |
| 62 | import android.os.IBinder; |
| 63 | import android.os.IInterface; |
| 64 | import android.os.Message; |
| 65 | import android.os.Parcel; |
| 66 | import android.os.RemoteException; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 67 | import android.os.ResultReceiver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | import android.os.ServiceManager; |
| 69 | import android.os.SystemClock; |
| 70 | import android.provider.Settings; |
Amith Yamasani | e861ec1 | 2010-03-24 21:39:27 -0700 | [diff] [blame] | 71 | import android.provider.Settings.Secure; |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 72 | import android.provider.Settings.SettingNotFoundException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | import android.text.TextUtils; |
satok | f9f0100 | 2011-05-19 21:31:50 +0900 | [diff] [blame] | 74 | import android.text.style.SuggestionSpan; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | import android.util.EventLog; |
satok | f9f0100 | 2011-05-19 21:31:50 +0900 | [diff] [blame] | 76 | import android.util.LruCache; |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 77 | import android.util.Pair; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | import android.util.PrintWriterPrinter; |
| 79 | import android.util.Printer; |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 80 | import android.util.Slog; |
| 81 | import android.util.Xml; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | import android.view.IWindowManager; |
| 83 | import android.view.WindowManager; |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 84 | import android.view.inputmethod.EditorInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | import android.view.inputmethod.InputBinding; |
| 86 | import android.view.inputmethod.InputMethod; |
| 87 | import android.view.inputmethod.InputMethodInfo; |
| 88 | import android.view.inputmethod.InputMethodManager; |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 89 | import android.view.inputmethod.InputMethodSubtype; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 91 | import java.io.File; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | import java.io.FileDescriptor; |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 93 | import java.io.FileInputStream; |
| 94 | import java.io.FileOutputStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | import java.io.IOException; |
| 96 | import java.io.PrintWriter; |
| 97 | import java.util.ArrayList; |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 98 | import java.util.Collections; |
Ken Wakasa | 761eb37 | 2011-03-04 19:06:18 +0900 | [diff] [blame] | 99 | import java.util.Comparator; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | import java.util.HashMap; |
satok | 7f35c8c | 2010-10-07 21:13:11 +0900 | [diff] [blame] | 101 | import java.util.HashSet; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | import java.util.List; |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 103 | import java.util.Set; |
satok | 913a892 | 2010-08-26 21:53:41 +0900 | [diff] [blame] | 104 | import java.util.TreeMap; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | |
| 106 | /** |
| 107 | * This class provides a system service that manages input methods. |
| 108 | */ |
| 109 | public class InputMethodManagerService extends IInputMethodManager.Stub |
| 110 | implements ServiceConnection, Handler.Callback { |
| 111 | static final boolean DEBUG = false; |
| 112 | static final String TAG = "InputManagerService"; |
| 113 | |
| 114 | static final int MSG_SHOW_IM_PICKER = 1; |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 115 | static final int MSG_SHOW_IM_SUBTYPE_PICKER = 2; |
satok | 47a4491 | 2010-10-06 16:03:58 +0900 | [diff] [blame] | 116 | static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 3; |
satok | 217f548 | 2010-12-15 05:19:19 +0900 | [diff] [blame] | 117 | static final int MSG_SHOW_IM_CONFIG = 4; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 118 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 119 | static final int MSG_UNBIND_INPUT = 1000; |
| 120 | static final int MSG_BIND_INPUT = 1010; |
| 121 | static final int MSG_SHOW_SOFT_INPUT = 1020; |
| 122 | static final int MSG_HIDE_SOFT_INPUT = 1030; |
| 123 | static final int MSG_ATTACH_TOKEN = 1040; |
| 124 | static final int MSG_CREATE_SESSION = 1050; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 125 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 126 | static final int MSG_START_INPUT = 2000; |
| 127 | static final int MSG_RESTART_INPUT = 2010; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 128 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 129 | static final int MSG_UNBIND_METHOD = 3000; |
| 130 | static final int MSG_BIND_METHOD = 3010; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 131 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 132 | static final long TIME_TO_RECONNECT = 10*1000; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 133 | |
satok | f9f0100 | 2011-05-19 21:31:50 +0900 | [diff] [blame] | 134 | static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20; |
| 135 | |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 136 | private static final int NOT_A_SUBTYPE_ID = -1; |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 137 | private static final String NOT_A_SUBTYPE_ID_STR = String.valueOf(NOT_A_SUBTYPE_ID); |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 138 | private static final String SUBTYPE_MODE_KEYBOARD = "keyboard"; |
| 139 | private static final String SUBTYPE_MODE_VOICE = "voice"; |
satok | b635941 | 2011-06-28 17:47:41 +0900 | [diff] [blame] | 140 | private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher"; |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 141 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 142 | final Context mContext; |
Dianne Hackborn | 7d3a5bc | 2010-11-29 22:52:12 -0800 | [diff] [blame] | 143 | final Resources mRes; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | final Handler mHandler; |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 145 | final InputMethodSettings mSettings; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 146 | final SettingsObserver mSettingsObserver; |
Joe Onorato | 089de88 | 2010-04-12 08:18:45 -0700 | [diff] [blame] | 147 | final StatusBarManagerService mStatusBar; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | final IWindowManager mIWindowManager; |
| 149 | final HandlerCaller mCaller; |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 150 | private final InputMethodFileManager mFileManager; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 151 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | final InputBindResult mNoBinding = new InputBindResult(null, null, -1); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 153 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 154 | // All known input methods. mMethodMap also serves as the global |
| 155 | // lock for this class. |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 156 | final ArrayList<InputMethodInfo> mMethodList = new ArrayList<InputMethodInfo>(); |
| 157 | final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<String, InputMethodInfo>(); |
satok | f9f0100 | 2011-05-19 21:31:50 +0900 | [diff] [blame] | 158 | private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans = |
| 159 | new LruCache<SuggestionSpan, InputMethodInfo>(SECURE_SUGGESTION_SPANS_MAX_SIZE); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 160 | |
satok | 7cfc0ed | 2011-06-20 21:29:36 +0900 | [diff] [blame] | 161 | // Ongoing notification |
| 162 | private final NotificationManager mNotificationManager; |
| 163 | private final Notification mImeSwitcherNotification; |
| 164 | private final PendingIntent mImeSwitchPendingIntent; |
| 165 | private final boolean mShowOngoingImeSwitcherForPhones; |
| 166 | private boolean mNotificationShown; |
| 167 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 168 | class SessionState { |
| 169 | final ClientState client; |
| 170 | final IInputMethod method; |
| 171 | final IInputMethodSession session; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 172 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 173 | @Override |
| 174 | public String toString() { |
| 175 | return "SessionState{uid " + client.uid + " pid " + client.pid |
| 176 | + " method " + Integer.toHexString( |
| 177 | System.identityHashCode(method)) |
| 178 | + " session " + Integer.toHexString( |
| 179 | System.identityHashCode(session)) |
| 180 | + "}"; |
| 181 | } |
| 182 | |
| 183 | SessionState(ClientState _client, IInputMethod _method, |
| 184 | IInputMethodSession _session) { |
| 185 | client = _client; |
| 186 | method = _method; |
| 187 | session = _session; |
| 188 | } |
| 189 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 190 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 191 | class ClientState { |
| 192 | final IInputMethodClient client; |
| 193 | final IInputContext inputContext; |
| 194 | final int uid; |
| 195 | final int pid; |
| 196 | final InputBinding binding; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 197 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 198 | boolean sessionRequested; |
| 199 | SessionState curSession; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 200 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 201 | @Override |
| 202 | public String toString() { |
| 203 | return "ClientState{" + Integer.toHexString( |
| 204 | System.identityHashCode(this)) + " uid " + uid |
| 205 | + " pid " + pid + "}"; |
| 206 | } |
| 207 | |
| 208 | ClientState(IInputMethodClient _client, IInputContext _inputContext, |
| 209 | int _uid, int _pid) { |
| 210 | client = _client; |
| 211 | inputContext = _inputContext; |
| 212 | uid = _uid; |
| 213 | pid = _pid; |
| 214 | binding = new InputBinding(null, inputContext.asBinder(), uid, pid); |
| 215 | } |
| 216 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 217 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 218 | final HashMap<IBinder, ClientState> mClients |
| 219 | = new HashMap<IBinder, ClientState>(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 220 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 221 | /** |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 222 | * Set once the system is ready to run third party code. |
| 223 | */ |
| 224 | boolean mSystemReady; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 225 | |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 226 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 227 | * Id of the currently selected input method. |
| 228 | */ |
| 229 | String mCurMethodId; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 230 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 231 | /** |
| 232 | * The current binding sequence number, incremented every time there is |
| 233 | * a new bind performed. |
| 234 | */ |
| 235 | int mCurSeq; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 236 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 237 | /** |
| 238 | * The client that is currently bound to an input method. |
| 239 | */ |
| 240 | ClientState mCurClient; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 241 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 242 | /** |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 243 | * The last window token that gained focus. |
| 244 | */ |
| 245 | IBinder mCurFocusedWindow; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 246 | |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 247 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 248 | * The input context last provided by the current client. |
| 249 | */ |
| 250 | IInputContext mCurInputContext; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 251 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 252 | /** |
| 253 | * The attributes last provided by the current client. |
| 254 | */ |
| 255 | EditorInfo mCurAttribute; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 256 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 257 | /** |
| 258 | * The input method ID of the input method service that we are currently |
| 259 | * connected to or in the process of connecting to. |
| 260 | */ |
| 261 | String mCurId; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 262 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 263 | /** |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 264 | * The current subtype of the current input method. |
| 265 | */ |
| 266 | private InputMethodSubtype mCurrentSubtype; |
| 267 | |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 268 | // This list contains the pairs of InputMethodInfo and InputMethodSubtype. |
satok | f3db1af | 2010-11-23 13:34:33 +0900 | [diff] [blame] | 269 | private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>> |
| 270 | mShortcutInputMethodsAndSubtypes = |
| 271 | new HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>(); |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 272 | |
| 273 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 274 | * Set to true if our ServiceConnection is currently actively bound to |
| 275 | * a service (whether or not we have gotten its IBinder back yet). |
| 276 | */ |
| 277 | boolean mHaveConnection; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 278 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 279 | /** |
| 280 | * Set if the client has asked for the input method to be shown. |
| 281 | */ |
| 282 | boolean mShowRequested; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 283 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 284 | /** |
| 285 | * Set if we were explicitly told to show the input method. |
| 286 | */ |
| 287 | boolean mShowExplicitlyRequested; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 288 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 289 | /** |
| 290 | * Set if we were forced to be shown. |
| 291 | */ |
| 292 | boolean mShowForced; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 293 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 294 | /** |
| 295 | * Set if we last told the input method to show itself. |
| 296 | */ |
| 297 | boolean mInputShown; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 298 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 299 | /** |
| 300 | * The Intent used to connect to the current input method. |
| 301 | */ |
| 302 | Intent mCurIntent; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 303 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 304 | /** |
| 305 | * The token we have made for the currently active input method, to |
| 306 | * identify it in the future. |
| 307 | */ |
| 308 | IBinder mCurToken; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 309 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 310 | /** |
| 311 | * If non-null, this is the input method service we are currently connected |
| 312 | * to. |
| 313 | */ |
| 314 | IInputMethod mCurMethod; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 315 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 316 | /** |
| 317 | * Time that we last initiated a bind to the input method, to determine |
| 318 | * if we should try to disconnect and reconnect to it. |
| 319 | */ |
| 320 | long mLastBindTime; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 321 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 322 | /** |
| 323 | * Have we called mCurMethod.bindInput()? |
| 324 | */ |
| 325 | boolean mBoundToMethod; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 326 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 327 | /** |
| 328 | * Currently enabled session. Only touched by service thread, not |
| 329 | * protected by a lock. |
| 330 | */ |
| 331 | SessionState mEnabledSession; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 332 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 333 | /** |
| 334 | * True if the screen is on. The value is true initially. |
| 335 | */ |
| 336 | boolean mScreenOn = true; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 337 | |
Joe Onorato | 857fd9b | 2011-01-27 15:08:35 -0800 | [diff] [blame] | 338 | int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT; |
| 339 | int mImeWindowVis; |
| 340 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 341 | AlertDialog.Builder mDialogBuilder; |
| 342 | AlertDialog mSwitchingDialog; |
| 343 | InputMethodInfo[] mIms; |
| 344 | CharSequence[] mItems; |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 345 | int[] mSubtypeIds; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 346 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 347 | class SettingsObserver extends ContentObserver { |
| 348 | SettingsObserver(Handler handler) { |
| 349 | super(handler); |
| 350 | ContentResolver resolver = mContext.getContentResolver(); |
| 351 | resolver.registerContentObserver(Settings.Secure.getUriFor( |
| 352 | Settings.Secure.DEFAULT_INPUT_METHOD), false, this); |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 353 | resolver.registerContentObserver(Settings.Secure.getUriFor( |
satok | b6109bb | 2011-02-03 22:24:54 +0900 | [diff] [blame] | 354 | Settings.Secure.ENABLED_INPUT_METHODS), false, this); |
| 355 | resolver.registerContentObserver(Settings.Secure.getUriFor( |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 356 | Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 357 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 358 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 359 | @Override public void onChange(boolean selfChange) { |
| 360 | synchronized (mMethodMap) { |
| 361 | updateFromSettingsLocked(); |
| 362 | } |
| 363 | } |
| 364 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 365 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 366 | class ScreenOnOffReceiver extends android.content.BroadcastReceiver { |
| 367 | @Override |
| 368 | public void onReceive(Context context, Intent intent) { |
| 369 | if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) { |
| 370 | mScreenOn = true; |
| 371 | } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { |
| 372 | mScreenOn = false; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 373 | } else if (intent.getAction().equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) { |
| 374 | hideInputMethodMenu(); |
| 375 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 376 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 377 | Slog.w(TAG, "Unexpected intent " + intent); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | // Inform the current client of the change in active status |
| 381 | try { |
| 382 | if (mCurClient != null && mCurClient.client != null) { |
| 383 | mCurClient.client.setActive(mScreenOn); |
| 384 | } |
| 385 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 386 | Slog.w(TAG, "Got RemoteException sending 'screen on/off' notification to pid " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 387 | + mCurClient.pid + " uid " + mCurClient.uid); |
| 388 | } |
| 389 | } |
| 390 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 391 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 392 | class MyPackageMonitor extends PackageMonitor { |
| 393 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 394 | @Override |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 395 | public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 396 | synchronized (mMethodMap) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 397 | String curInputMethodId = Settings.Secure.getString(mContext |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 398 | .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD); |
| 399 | final int N = mMethodList.size(); |
| 400 | if (curInputMethodId != null) { |
| 401 | for (int i=0; i<N; i++) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 402 | InputMethodInfo imi = mMethodList.get(i); |
| 403 | if (imi.getId().equals(curInputMethodId)) { |
| 404 | for (String pkg : packages) { |
| 405 | if (imi.getPackageName().equals(pkg)) { |
| 406 | if (!doit) { |
| 407 | return true; |
| 408 | } |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 409 | resetSelectedInputMethodAndSubtypeLocked(""); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 410 | chooseNewDefaultIMELocked(); |
| 411 | return true; |
| 412 | } |
| 413 | } |
| 414 | } |
| 415 | } |
| 416 | } |
| 417 | } |
| 418 | return false; |
| 419 | } |
| 420 | |
| 421 | @Override |
| 422 | public void onSomePackagesChanged() { |
| 423 | synchronized (mMethodMap) { |
| 424 | InputMethodInfo curIm = null; |
| 425 | String curInputMethodId = Settings.Secure.getString(mContext |
| 426 | .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD); |
| 427 | final int N = mMethodList.size(); |
| 428 | if (curInputMethodId != null) { |
| 429 | for (int i=0; i<N; i++) { |
| 430 | InputMethodInfo imi = mMethodList.get(i); |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 431 | final String imiId = imi.getId(); |
| 432 | if (imiId.equals(curInputMethodId)) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 433 | curIm = imi; |
| 434 | } |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 435 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 436 | int change = isPackageDisappearing(imi.getPackageName()); |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 437 | if (isPackageModified(imi.getPackageName())) { |
| 438 | mFileManager.deleteAllInputMethodSubtypes(imiId); |
| 439 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 440 | if (change == PACKAGE_TEMPORARY_CHANGE |
| 441 | || change == PACKAGE_PERMANENT_CHANGE) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 442 | Slog.i(TAG, "Input method uninstalled, disabling: " |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 443 | + imi.getComponent()); |
| 444 | setInputMethodEnabledLocked(imi.getId(), false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 445 | } |
| 446 | } |
| 447 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 448 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 449 | buildInputMethodListLocked(mMethodList, mMethodMap); |
| 450 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 451 | boolean changed = false; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 452 | |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 453 | if (curIm != null) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 454 | int change = isPackageDisappearing(curIm.getPackageName()); |
| 455 | if (change == PACKAGE_TEMPORARY_CHANGE |
| 456 | || change == PACKAGE_PERMANENT_CHANGE) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 457 | ServiceInfo si = null; |
| 458 | try { |
| 459 | si = mContext.getPackageManager().getServiceInfo( |
| 460 | curIm.getComponent(), 0); |
| 461 | } catch (PackageManager.NameNotFoundException ex) { |
| 462 | } |
| 463 | if (si == null) { |
| 464 | // Uh oh, current input method is no longer around! |
| 465 | // Pick another one... |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 466 | Slog.i(TAG, "Current input method removed: " + curInputMethodId); |
Joe Onorato | 857fd9b | 2011-01-27 15:08:35 -0800 | [diff] [blame] | 467 | mImeWindowVis = 0; |
| 468 | mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis, |
| 469 | mBackDisposition); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 470 | if (!chooseNewDefaultIMELocked()) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 471 | changed = true; |
| 472 | curIm = null; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 473 | Slog.i(TAG, "Unsetting current input method"); |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 474 | resetSelectedInputMethodAndSubtypeLocked(""); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 475 | } |
| 476 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 477 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 478 | } |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 479 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 480 | if (curIm == null) { |
| 481 | // We currently don't have a default input method... is |
| 482 | // one now available? |
| 483 | changed = chooseNewDefaultIMELocked(); |
| 484 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 485 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 486 | if (changed) { |
| 487 | updateFromSettingsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 488 | } |
| 489 | } |
| 490 | } |
| 491 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 492 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 493 | class MethodCallback extends IInputMethodCallback.Stub { |
| 494 | final IInputMethod mMethod; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 495 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 496 | MethodCallback(IInputMethod method) { |
| 497 | mMethod = method; |
| 498 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 499 | |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 500 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 501 | public void finishedEvent(int seq, boolean handled) throws RemoteException { |
| 502 | } |
| 503 | |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 504 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 505 | public void sessionCreated(IInputMethodSession session) throws RemoteException { |
| 506 | onSessionCreated(mMethod, session); |
| 507 | } |
| 508 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 509 | |
Joe Onorato | 089de88 | 2010-04-12 08:18:45 -0700 | [diff] [blame] | 510 | public InputMethodManagerService(Context context, StatusBarManagerService statusBar) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 511 | mContext = context; |
Dianne Hackborn | 7d3a5bc | 2010-11-29 22:52:12 -0800 | [diff] [blame] | 512 | mRes = context.getResources(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 513 | mHandler = new Handler(this); |
| 514 | mIWindowManager = IWindowManager.Stub.asInterface( |
| 515 | ServiceManager.getService(Context.WINDOW_SERVICE)); |
| 516 | mCaller = new HandlerCaller(context, new HandlerCaller.Callback() { |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 517 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 518 | public void executeMessage(Message msg) { |
| 519 | handleMessage(msg); |
| 520 | } |
| 521 | }); |
satok | 7cfc0ed | 2011-06-20 21:29:36 +0900 | [diff] [blame] | 522 | |
| 523 | mNotificationManager = (NotificationManager) |
| 524 | mContext.getSystemService(Context.NOTIFICATION_SERVICE); |
| 525 | mImeSwitcherNotification = new Notification(); |
| 526 | mImeSwitcherNotification.icon = com.android.internal.R.drawable.ic_notification_ime_default; |
| 527 | mImeSwitcherNotification.when = 0; |
| 528 | mImeSwitcherNotification.flags = Notification.FLAG_ONGOING_EVENT; |
| 529 | mImeSwitcherNotification.tickerText = null; |
| 530 | mImeSwitcherNotification.defaults = 0; // please be quiet |
| 531 | mImeSwitcherNotification.sound = null; |
| 532 | mImeSwitcherNotification.vibrate = null; |
| 533 | Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER); |
| 534 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
| 535 | | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED |
| 536 | | Intent.FLAG_ACTIVITY_CLEAR_TOP); |
| 537 | mImeSwitchPendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0); |
| 538 | mShowOngoingImeSwitcherForPhones = mRes.getBoolean( |
| 539 | com.android.internal.R.bool.show_ongoing_ime_switcher); |
| 540 | |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 541 | synchronized (mMethodMap) { |
| 542 | mFileManager = new InputMethodFileManager(mMethodMap); |
| 543 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 544 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 545 | (new MyPackageMonitor()).register(mContext, true); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 546 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 547 | IntentFilter screenOnOffFilt = new IntentFilter(); |
| 548 | screenOnOffFilt.addAction(Intent.ACTION_SCREEN_ON); |
| 549 | screenOnOffFilt.addAction(Intent.ACTION_SCREEN_OFF); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 550 | screenOnOffFilt.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 551 | mContext.registerReceiver(new ScreenOnOffReceiver(), screenOnOffFilt); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 552 | |
satok | 913a892 | 2010-08-26 21:53:41 +0900 | [diff] [blame] | 553 | mStatusBar = statusBar; |
| 554 | statusBar.setIconVisibility("ime", false); |
satok | 7cfc0ed | 2011-06-20 21:29:36 +0900 | [diff] [blame] | 555 | mNotificationShown = false; |
satok | 913a892 | 2010-08-26 21:53:41 +0900 | [diff] [blame] | 556 | |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 557 | // mSettings should be created before buildInputMethodListLocked |
satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 558 | mSettings = new InputMethodSettings( |
| 559 | mRes, context.getContentResolver(), mMethodMap, mMethodList); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 560 | buildInputMethodListLocked(mMethodList, mMethodMap); |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 561 | mSettings.enableAllIMEsIfThereIsNoEnabledIME(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 562 | |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 563 | if (TextUtils.isEmpty(Settings.Secure.getString( |
| 564 | mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD))) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 565 | InputMethodInfo defIm = null; |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 566 | for (InputMethodInfo imi: mMethodList) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 567 | if (defIm == null && imi.getIsDefaultResourceId() != 0) { |
| 568 | try { |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 569 | Resources res = context.createPackageContext( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 570 | imi.getPackageName(), 0).getResources(); |
| 571 | if (res.getBoolean(imi.getIsDefaultResourceId())) { |
| 572 | defIm = imi; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 573 | Slog.i(TAG, "Selected default: " + imi.getId()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 574 | } |
| 575 | } catch (PackageManager.NameNotFoundException ex) { |
| 576 | } catch (Resources.NotFoundException ex) { |
| 577 | } |
| 578 | } |
| 579 | } |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 580 | if (defIm == null && mMethodList.size() > 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 581 | defIm = mMethodList.get(0); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 582 | Slog.i(TAG, "No default found, using " + defIm.getId()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 583 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 584 | if (defIm != null) { |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 585 | setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 586 | } |
| 587 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 588 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 589 | mSettingsObserver = new SettingsObserver(mHandler); |
| 590 | updateFromSettingsLocked(); |
| 591 | } |
| 592 | |
| 593 | @Override |
| 594 | public boolean onTransact(int code, Parcel data, Parcel reply, int flags) |
| 595 | throws RemoteException { |
| 596 | try { |
| 597 | return super.onTransact(code, data, reply, flags); |
| 598 | } catch (RuntimeException e) { |
| 599 | // The input method manager only throws security exceptions, so let's |
| 600 | // log all others. |
| 601 | if (!(e instanceof SecurityException)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 602 | Slog.e(TAG, "Input Method Manager Crash", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 603 | } |
| 604 | throw e; |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | public void systemReady() { |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 609 | synchronized (mMethodMap) { |
| 610 | if (!mSystemReady) { |
| 611 | mSystemReady = true; |
Dianne Hackborn | cc27870 | 2009-09-02 23:07:23 -0700 | [diff] [blame] | 612 | try { |
| 613 | startInputInnerLocked(); |
| 614 | } catch (RuntimeException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 615 | Slog.w(TAG, "Unexpected exception", e); |
Dianne Hackborn | cc27870 | 2009-09-02 23:07:23 -0700 | [diff] [blame] | 616 | } |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 617 | } |
| 618 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 619 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 620 | |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 621 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 622 | public List<InputMethodInfo> getInputMethodList() { |
| 623 | synchronized (mMethodMap) { |
| 624 | return new ArrayList<InputMethodInfo>(mMethodList); |
| 625 | } |
| 626 | } |
| 627 | |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 628 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 629 | public List<InputMethodInfo> getEnabledInputMethodList() { |
| 630 | synchronized (mMethodMap) { |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 631 | return mSettings.getEnabledInputMethodListLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 632 | } |
| 633 | } |
| 634 | |
satok | bb4aa06 | 2011-01-19 21:40:27 +0900 | [diff] [blame] | 635 | private HashMap<InputMethodInfo, List<InputMethodSubtype>> |
| 636 | getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked() { |
| 637 | HashMap<InputMethodInfo, List<InputMethodSubtype>> enabledInputMethodAndSubtypes = |
| 638 | new HashMap<InputMethodInfo, List<InputMethodSubtype>>(); |
| 639 | for (InputMethodInfo imi: getEnabledInputMethodList()) { |
| 640 | enabledInputMethodAndSubtypes.put( |
| 641 | imi, getEnabledInputMethodSubtypeListLocked(imi, true)); |
| 642 | } |
| 643 | return enabledInputMethodAndSubtypes; |
| 644 | } |
| 645 | |
| 646 | public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(InputMethodInfo imi, |
| 647 | boolean allowsImplicitlySelectedSubtypes) { |
| 648 | if (imi == null && mCurMethodId != null) { |
| 649 | imi = mMethodMap.get(mCurMethodId); |
| 650 | } |
satok | 7265d9b | 2011-02-14 15:47:30 +0900 | [diff] [blame] | 651 | List<InputMethodSubtype> enabledSubtypes = |
satok | bb4aa06 | 2011-01-19 21:40:27 +0900 | [diff] [blame] | 652 | mSettings.getEnabledInputMethodSubtypeListLocked(imi); |
satok | 7265d9b | 2011-02-14 15:47:30 +0900 | [diff] [blame] | 653 | if (allowsImplicitlySelectedSubtypes && enabledSubtypes.isEmpty()) { |
| 654 | enabledSubtypes = getApplicableSubtypesLocked(mRes, getSubtypes(imi)); |
satok | bb4aa06 | 2011-01-19 21:40:27 +0900 | [diff] [blame] | 655 | } |
satok | 7265d9b | 2011-02-14 15:47:30 +0900 | [diff] [blame] | 656 | return InputMethodSubtype.sort(mContext, 0, imi, enabledSubtypes); |
satok | bb4aa06 | 2011-01-19 21:40:27 +0900 | [diff] [blame] | 657 | } |
| 658 | |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 659 | @Override |
satok | 16331c8 | 2010-12-20 23:48:46 +0900 | [diff] [blame] | 660 | public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi, |
| 661 | boolean allowsImplicitlySelectedSubtypes) { |
satok | 67ddf9c | 2010-11-17 09:45:54 +0900 | [diff] [blame] | 662 | synchronized (mMethodMap) { |
satok | bb4aa06 | 2011-01-19 21:40:27 +0900 | [diff] [blame] | 663 | return getEnabledInputMethodSubtypeListLocked(imi, allowsImplicitlySelectedSubtypes); |
satok | 67ddf9c | 2010-11-17 09:45:54 +0900 | [diff] [blame] | 664 | } |
| 665 | } |
| 666 | |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 667 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 668 | public void addClient(IInputMethodClient client, |
| 669 | IInputContext inputContext, int uid, int pid) { |
| 670 | synchronized (mMethodMap) { |
| 671 | mClients.put(client.asBinder(), new ClientState(client, |
| 672 | inputContext, uid, pid)); |
| 673 | } |
| 674 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 675 | |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 676 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 677 | public void removeClient(IInputMethodClient client) { |
| 678 | synchronized (mMethodMap) { |
| 679 | mClients.remove(client.asBinder()); |
| 680 | } |
| 681 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 682 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 683 | void executeOrSendMessage(IInterface target, Message msg) { |
| 684 | if (target.asBinder() instanceof Binder) { |
| 685 | mCaller.sendMessage(msg); |
| 686 | } else { |
| 687 | handleMessage(msg); |
| 688 | msg.recycle(); |
| 689 | } |
| 690 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 691 | |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 692 | void unbindCurrentClientLocked() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 693 | if (mCurClient != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 694 | if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 695 | + mCurClient.client.asBinder()); |
| 696 | if (mBoundToMethod) { |
| 697 | mBoundToMethod = false; |
| 698 | if (mCurMethod != null) { |
| 699 | executeOrSendMessage(mCurMethod, mCaller.obtainMessageO( |
| 700 | MSG_UNBIND_INPUT, mCurMethod)); |
| 701 | } |
| 702 | } |
| 703 | executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO( |
| 704 | MSG_UNBIND_METHOD, mCurSeq, mCurClient.client)); |
| 705 | mCurClient.sessionRequested = false; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 706 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 707 | // Call setActive(false) on the old client |
| 708 | try { |
| 709 | mCurClient.client.setActive(false); |
| 710 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 711 | Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 712 | + mCurClient.pid + " uid " + mCurClient.uid); |
| 713 | } |
| 714 | mCurClient = null; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 715 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 716 | hideInputMethodMenuLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 717 | } |
| 718 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 719 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 720 | private int getImeShowFlags() { |
| 721 | int flags = 0; |
| 722 | if (mShowForced) { |
| 723 | flags |= InputMethod.SHOW_FORCED |
| 724 | | InputMethod.SHOW_EXPLICIT; |
| 725 | } else if (mShowExplicitlyRequested) { |
| 726 | flags |= InputMethod.SHOW_EXPLICIT; |
| 727 | } |
| 728 | return flags; |
| 729 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 730 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 731 | private int getAppShowFlags() { |
| 732 | int flags = 0; |
| 733 | if (mShowForced) { |
| 734 | flags |= InputMethodManager.SHOW_FORCED; |
| 735 | } else if (!mShowExplicitlyRequested) { |
| 736 | flags |= InputMethodManager.SHOW_IMPLICIT; |
| 737 | } |
| 738 | return flags; |
| 739 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 740 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 741 | InputBindResult attachNewInputLocked(boolean initial, boolean needResult) { |
| 742 | if (!mBoundToMethod) { |
| 743 | executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO( |
| 744 | MSG_BIND_INPUT, mCurMethod, mCurClient.binding)); |
| 745 | mBoundToMethod = true; |
| 746 | } |
| 747 | final SessionState session = mCurClient.curSession; |
| 748 | if (initial) { |
| 749 | executeOrSendMessage(session.method, mCaller.obtainMessageOOO( |
| 750 | MSG_START_INPUT, session, mCurInputContext, mCurAttribute)); |
| 751 | } else { |
| 752 | executeOrSendMessage(session.method, mCaller.obtainMessageOOO( |
| 753 | MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute)); |
| 754 | } |
| 755 | if (mShowRequested) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 756 | if (DEBUG) Slog.v(TAG, "Attach new input asks to show input"); |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 757 | showCurrentInputLocked(getAppShowFlags(), null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 758 | } |
| 759 | return needResult |
| 760 | ? new InputBindResult(session.session, mCurId, mCurSeq) |
| 761 | : null; |
| 762 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 763 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 764 | InputBindResult startInputLocked(IInputMethodClient client, |
| 765 | IInputContext inputContext, EditorInfo attribute, |
| 766 | boolean initial, boolean needResult) { |
| 767 | // If no method is currently selected, do nothing. |
| 768 | if (mCurMethodId == null) { |
| 769 | return mNoBinding; |
| 770 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 771 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 772 | ClientState cs = mClients.get(client.asBinder()); |
| 773 | if (cs == null) { |
| 774 | throw new IllegalArgumentException("unknown client " |
| 775 | + client.asBinder()); |
| 776 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 777 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 778 | try { |
| 779 | if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) { |
| 780 | // Check with the window manager to make sure this client actually |
| 781 | // has a window with focus. If not, reject. This is thread safe |
| 782 | // because if the focus changes some time before or after, the |
| 783 | // next client receiving focus that has any interest in input will |
| 784 | // be calling through here after that change happens. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 785 | Slog.w(TAG, "Starting input on non-focused client " + cs.client |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 786 | + " (uid=" + cs.uid + " pid=" + cs.pid + ")"); |
| 787 | return null; |
| 788 | } |
| 789 | } catch (RemoteException e) { |
| 790 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 791 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 792 | if (mCurClient != cs) { |
| 793 | // If the client is changing, we need to switch over to the new |
| 794 | // one. |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 795 | unbindCurrentClientLocked(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 796 | if (DEBUG) Slog.v(TAG, "switching to client: client = " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 797 | + cs.client.asBinder()); |
| 798 | |
| 799 | // If the screen is on, inform the new client it is active |
| 800 | if (mScreenOn) { |
| 801 | try { |
| 802 | cs.client.setActive(mScreenOn); |
| 803 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 804 | Slog.w(TAG, "Got RemoteException sending setActive notification to pid " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 805 | + cs.pid + " uid " + cs.uid); |
| 806 | } |
| 807 | } |
| 808 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 809 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 810 | // Bump up the sequence for this client and attach it. |
| 811 | mCurSeq++; |
| 812 | if (mCurSeq <= 0) mCurSeq = 1; |
| 813 | mCurClient = cs; |
| 814 | mCurInputContext = inputContext; |
| 815 | mCurAttribute = attribute; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 816 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 817 | // Check if the input method is changing. |
| 818 | if (mCurId != null && mCurId.equals(mCurMethodId)) { |
| 819 | if (cs.curSession != null) { |
| 820 | // Fast case: if we are already connected to the input method, |
| 821 | // then just return it. |
| 822 | return attachNewInputLocked(initial, needResult); |
| 823 | } |
| 824 | if (mHaveConnection) { |
| 825 | if (mCurMethod != null) { |
| 826 | if (!cs.sessionRequested) { |
| 827 | cs.sessionRequested = true; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 828 | if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 829 | executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO( |
| 830 | MSG_CREATE_SESSION, mCurMethod, |
| 831 | new MethodCallback(mCurMethod))); |
| 832 | } |
| 833 | // Return to client, and we will get back with it when |
| 834 | // we have had a session made for it. |
| 835 | return new InputBindResult(null, mCurId, mCurSeq); |
| 836 | } else if (SystemClock.uptimeMillis() |
| 837 | < (mLastBindTime+TIME_TO_RECONNECT)) { |
| 838 | // In this case we have connected to the service, but |
| 839 | // don't yet have its interface. If it hasn't been too |
| 840 | // long since we did the connection, we'll return to |
| 841 | // the client and wait to get the service interface so |
| 842 | // we can report back. If it has been too long, we want |
| 843 | // to fall through so we can try a disconnect/reconnect |
| 844 | // to see if we can get back in touch with the service. |
| 845 | return new InputBindResult(null, mCurId, mCurSeq); |
| 846 | } else { |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 847 | EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, |
| 848 | mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 849 | } |
| 850 | } |
| 851 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 852 | |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 853 | return startInputInnerLocked(); |
| 854 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 855 | |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 856 | InputBindResult startInputInnerLocked() { |
| 857 | if (mCurMethodId == null) { |
| 858 | return mNoBinding; |
| 859 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 860 | |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 861 | if (!mSystemReady) { |
| 862 | // If the system is not yet ready, we shouldn't be running third |
| 863 | // party code. |
Dianne Hackborn | cc27870 | 2009-09-02 23:07:23 -0700 | [diff] [blame] | 864 | return new InputBindResult(null, mCurMethodId, mCurSeq); |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 865 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 866 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 867 | InputMethodInfo info = mMethodMap.get(mCurMethodId); |
| 868 | if (info == null) { |
| 869 | throw new IllegalArgumentException("Unknown id: " + mCurMethodId); |
| 870 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 871 | |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 872 | unbindCurrentMethodLocked(false); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 873 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 874 | mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE); |
| 875 | mCurIntent.setComponent(info.getComponent()); |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 876 | mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL, |
| 877 | com.android.internal.R.string.input_method_binding_label); |
| 878 | mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity( |
| 879 | mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 880 | if (mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE)) { |
| 881 | mLastBindTime = SystemClock.uptimeMillis(); |
| 882 | mHaveConnection = true; |
| 883 | mCurId = info.getId(); |
| 884 | mCurToken = new Binder(); |
| 885 | try { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 886 | if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 887 | mIWindowManager.addWindowToken(mCurToken, |
| 888 | WindowManager.LayoutParams.TYPE_INPUT_METHOD); |
| 889 | } catch (RemoteException e) { |
| 890 | } |
| 891 | return new InputBindResult(null, mCurId, mCurSeq); |
| 892 | } else { |
| 893 | mCurIntent = null; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 894 | Slog.w(TAG, "Failure connecting to input method service: " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 895 | + mCurIntent); |
| 896 | } |
| 897 | return null; |
| 898 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 899 | |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 900 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 901 | public InputBindResult startInput(IInputMethodClient client, |
| 902 | IInputContext inputContext, EditorInfo attribute, |
| 903 | boolean initial, boolean needResult) { |
| 904 | synchronized (mMethodMap) { |
| 905 | final long ident = Binder.clearCallingIdentity(); |
| 906 | try { |
| 907 | return startInputLocked(client, inputContext, attribute, |
| 908 | initial, needResult); |
| 909 | } finally { |
| 910 | Binder.restoreCallingIdentity(ident); |
| 911 | } |
| 912 | } |
| 913 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 914 | |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 915 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 916 | public void finishInput(IInputMethodClient client) { |
| 917 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 918 | |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 919 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 920 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 921 | synchronized (mMethodMap) { |
| 922 | if (mCurIntent != null && name.equals(mCurIntent.getComponent())) { |
| 923 | mCurMethod = IInputMethod.Stub.asInterface(service); |
Dianne Hackborn | cc27870 | 2009-09-02 23:07:23 -0700 | [diff] [blame] | 924 | if (mCurToken == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 925 | Slog.w(TAG, "Service connected without a token!"); |
Dianne Hackborn | cc27870 | 2009-09-02 23:07:23 -0700 | [diff] [blame] | 926 | unbindCurrentMethodLocked(false); |
| 927 | return; |
| 928 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 929 | if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken); |
Dianne Hackborn | cc27870 | 2009-09-02 23:07:23 -0700 | [diff] [blame] | 930 | executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO( |
| 931 | MSG_ATTACH_TOKEN, mCurMethod, mCurToken)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 932 | if (mCurClient != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 933 | if (DEBUG) Slog.v(TAG, "Creating first session while with client " |
Dianne Hackborn | cc27870 | 2009-09-02 23:07:23 -0700 | [diff] [blame] | 934 | + mCurClient); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 935 | executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO( |
Dianne Hackborn | cc27870 | 2009-09-02 23:07:23 -0700 | [diff] [blame] | 936 | MSG_CREATE_SESSION, mCurMethod, |
| 937 | new MethodCallback(mCurMethod))); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 938 | } |
| 939 | } |
| 940 | } |
| 941 | } |
| 942 | |
| 943 | void onSessionCreated(IInputMethod method, IInputMethodSession session) { |
| 944 | synchronized (mMethodMap) { |
| 945 | if (mCurMethod != null && method != null |
| 946 | && mCurMethod.asBinder() == method.asBinder()) { |
| 947 | if (mCurClient != null) { |
| 948 | mCurClient.curSession = new SessionState(mCurClient, |
| 949 | method, session); |
| 950 | mCurClient.sessionRequested = false; |
| 951 | InputBindResult res = attachNewInputLocked(true, true); |
| 952 | if (res.method != null) { |
| 953 | executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO( |
| 954 | MSG_BIND_METHOD, mCurClient.client, res)); |
| 955 | } |
| 956 | } |
| 957 | } |
| 958 | } |
| 959 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 960 | |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 961 | void unbindCurrentMethodLocked(boolean reportToClient) { |
| 962 | if (mHaveConnection) { |
| 963 | mContext.unbindService(this); |
| 964 | mHaveConnection = false; |
| 965 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 966 | |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 967 | if (mCurToken != null) { |
| 968 | try { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 969 | if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken); |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 970 | mIWindowManager.removeWindowToken(mCurToken); |
| 971 | } catch (RemoteException e) { |
| 972 | } |
| 973 | mCurToken = null; |
| 974 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 975 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 976 | mCurId = null; |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 977 | clearCurMethodLocked(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 978 | |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 979 | if (reportToClient && mCurClient != null) { |
| 980 | executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO( |
| 981 | MSG_UNBIND_METHOD, mCurSeq, mCurClient.client)); |
| 982 | } |
| 983 | } |
| 984 | |
Devin Taylor | 0c33ed2 | 2010-02-23 13:26:46 -0600 | [diff] [blame] | 985 | private void finishSession(SessionState sessionState) { |
| 986 | if (sessionState != null && sessionState.session != null) { |
| 987 | try { |
| 988 | sessionState.session.finishSession(); |
| 989 | } catch (RemoteException e) { |
Jean-Baptiste Queru | 9d0f6df | 2010-03-29 12:55:09 -0700 | [diff] [blame] | 990 | Slog.w(TAG, "Session failed to close due to remote exception", e); |
Devin Taylor | 0c33ed2 | 2010-02-23 13:26:46 -0600 | [diff] [blame] | 991 | } |
| 992 | } |
| 993 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 994 | |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 995 | void clearCurMethodLocked() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 996 | if (mCurMethod != null) { |
| 997 | for (ClientState cs : mClients.values()) { |
| 998 | cs.sessionRequested = false; |
Devin Taylor | 0c33ed2 | 2010-02-23 13:26:46 -0600 | [diff] [blame] | 999 | finishSession(cs.curSession); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1000 | cs.curSession = null; |
| 1001 | } |
Devin Taylor | 0c33ed2 | 2010-02-23 13:26:46 -0600 | [diff] [blame] | 1002 | |
| 1003 | finishSession(mEnabledSession); |
| 1004 | mEnabledSession = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1005 | mCurMethod = null; |
| 1006 | } |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 1007 | mStatusBar.setIconVisibility("ime", false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1008 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1009 | |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 1010 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1011 | public void onServiceDisconnected(ComponentName name) { |
| 1012 | synchronized (mMethodMap) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1013 | if (DEBUG) Slog.v(TAG, "Service disconnected: " + name |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1014 | + " mCurIntent=" + mCurIntent); |
| 1015 | if (mCurMethod != null && mCurIntent != null |
| 1016 | && name.equals(mCurIntent.getComponent())) { |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1017 | clearCurMethodLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1018 | // We consider this to be a new bind attempt, since the system |
| 1019 | // should now try to restart the service for us. |
| 1020 | mLastBindTime = SystemClock.uptimeMillis(); |
| 1021 | mShowRequested = mInputShown; |
| 1022 | mInputShown = false; |
| 1023 | if (mCurClient != null) { |
| 1024 | executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO( |
| 1025 | MSG_UNBIND_METHOD, mCurSeq, mCurClient.client)); |
| 1026 | } |
| 1027 | } |
| 1028 | } |
| 1029 | } |
| 1030 | |
satok | f9f0100 | 2011-05-19 21:31:50 +0900 | [diff] [blame] | 1031 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1032 | public void updateStatusIcon(IBinder token, String packageName, int iconId) { |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 1033 | int uid = Binder.getCallingUid(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1034 | long ident = Binder.clearCallingIdentity(); |
| 1035 | try { |
| 1036 | if (token == null || mCurToken != token) { |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 1037 | Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1038 | return; |
| 1039 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1040 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1041 | synchronized (mMethodMap) { |
| 1042 | if (iconId == 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1043 | if (DEBUG) Slog.d(TAG, "hide the small icon for the input method"); |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 1044 | mStatusBar.setIconVisibility("ime", false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1045 | } else if (packageName != null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1046 | if (DEBUG) Slog.d(TAG, "show a small icon for the input method"); |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 1047 | mStatusBar.setIcon("ime", packageName, iconId, 0); |
| 1048 | mStatusBar.setIconVisibility("ime", true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1049 | } |
| 1050 | } |
| 1051 | } finally { |
| 1052 | Binder.restoreCallingIdentity(ident); |
| 1053 | } |
| 1054 | } |
| 1055 | |
satok | 7cfc0ed | 2011-06-20 21:29:36 +0900 | [diff] [blame] | 1056 | private boolean needsToShowImeSwitchOngoingNotification() { |
| 1057 | if (!mShowOngoingImeSwitcherForPhones) return false; |
| 1058 | synchronized (mMethodMap) { |
| 1059 | List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked(); |
| 1060 | final int N = imis.size(); |
satok | b635941 | 2011-06-28 17:47:41 +0900 | [diff] [blame] | 1061 | if (N > 2) return true; |
| 1062 | if (N < 1) return false; |
| 1063 | int nonAuxCount = 0; |
| 1064 | int auxCount = 0; |
| 1065 | InputMethodSubtype nonAuxSubtype = null; |
| 1066 | InputMethodSubtype auxSubtype = null; |
satok | 7cfc0ed | 2011-06-20 21:29:36 +0900 | [diff] [blame] | 1067 | for(int i = 0; i < N; ++i) { |
| 1068 | final InputMethodInfo imi = imis.get(i); |
| 1069 | final List<InputMethodSubtype> subtypes = getEnabledInputMethodSubtypeListLocked( |
| 1070 | imi, true); |
| 1071 | final int subtypeCount = subtypes.size(); |
| 1072 | if (subtypeCount == 0) { |
satok | b635941 | 2011-06-28 17:47:41 +0900 | [diff] [blame] | 1073 | ++nonAuxCount; |
satok | 7cfc0ed | 2011-06-20 21:29:36 +0900 | [diff] [blame] | 1074 | } else { |
| 1075 | for (int j = 0; j < subtypeCount; ++j) { |
satok | b635941 | 2011-06-28 17:47:41 +0900 | [diff] [blame] | 1076 | final InputMethodSubtype subtype = subtypes.get(j); |
| 1077 | if (!subtype.isAuxiliary()) { |
| 1078 | ++nonAuxCount; |
| 1079 | nonAuxSubtype = subtype; |
| 1080 | } else { |
| 1081 | ++auxCount; |
| 1082 | auxSubtype = subtype; |
satok | 7cfc0ed | 2011-06-20 21:29:36 +0900 | [diff] [blame] | 1083 | } |
| 1084 | } |
| 1085 | } |
satok | 7cfc0ed | 2011-06-20 21:29:36 +0900 | [diff] [blame] | 1086 | } |
satok | b635941 | 2011-06-28 17:47:41 +0900 | [diff] [blame] | 1087 | if (nonAuxCount > 1 || auxCount > 1) { |
| 1088 | return true; |
| 1089 | } else if (nonAuxCount == 1 && auxCount == 1) { |
| 1090 | if (nonAuxSubtype != null && auxSubtype != null |
| 1091 | && nonAuxSubtype.getLocale().equals(auxSubtype.getLocale()) |
| 1092 | && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) { |
| 1093 | return false; |
| 1094 | } |
| 1095 | return true; |
| 1096 | } |
| 1097 | return false; |
satok | 7cfc0ed | 2011-06-20 21:29:36 +0900 | [diff] [blame] | 1098 | } |
satok | 7cfc0ed | 2011-06-20 21:29:36 +0900 | [diff] [blame] | 1099 | } |
| 1100 | |
satok | f9f0100 | 2011-05-19 21:31:50 +0900 | [diff] [blame] | 1101 | @Override |
Joe Onorato | 857fd9b | 2011-01-27 15:08:35 -0800 | [diff] [blame] | 1102 | public void setImeWindowStatus(IBinder token, int vis, int backDisposition) { |
satok | 06487a5 | 2010-10-29 11:37:18 +0900 | [diff] [blame] | 1103 | int uid = Binder.getCallingUid(); |
| 1104 | long ident = Binder.clearCallingIdentity(); |
| 1105 | try { |
| 1106 | if (token == null || mCurToken != token) { |
Joe Onorato | 857fd9b | 2011-01-27 15:08:35 -0800 | [diff] [blame] | 1107 | Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token); |
satok | 06487a5 | 2010-10-29 11:37:18 +0900 | [diff] [blame] | 1108 | return; |
| 1109 | } |
| 1110 | |
| 1111 | synchronized (mMethodMap) { |
Joe Onorato | 857fd9b | 2011-01-27 15:08:35 -0800 | [diff] [blame] | 1112 | mImeWindowVis = vis; |
| 1113 | mBackDisposition = backDisposition; |
| 1114 | mStatusBar.setImeWindowStatus(token, vis, backDisposition); |
satok | 7cfc0ed | 2011-06-20 21:29:36 +0900 | [diff] [blame] | 1115 | final boolean iconVisibility = (vis & InputMethodService.IME_ACTIVE) != 0; |
| 1116 | if (iconVisibility && needsToShowImeSwitchOngoingNotification()) { |
| 1117 | final PackageManager pm = mContext.getPackageManager(); |
| 1118 | final CharSequence label = mMethodMap.get(mCurMethodId).loadLabel(pm); |
| 1119 | final CharSequence title = mRes.getText( |
| 1120 | com.android.internal.R.string.select_input_method); |
| 1121 | mImeSwitcherNotification.setLatestEventInfo( |
| 1122 | mContext, title, label, mImeSwitchPendingIntent); |
| 1123 | mNotificationManager.notify( |
| 1124 | com.android.internal.R.string.select_input_method, |
| 1125 | mImeSwitcherNotification); |
| 1126 | mNotificationShown = true; |
| 1127 | } else { |
| 1128 | if (mNotificationShown) { |
| 1129 | mNotificationManager.cancel( |
| 1130 | com.android.internal.R.string.select_input_method); |
| 1131 | mNotificationShown = false; |
| 1132 | } |
| 1133 | } |
satok | 06487a5 | 2010-10-29 11:37:18 +0900 | [diff] [blame] | 1134 | } |
| 1135 | } finally { |
| 1136 | Binder.restoreCallingIdentity(ident); |
| 1137 | } |
| 1138 | } |
| 1139 | |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 1140 | @Override |
satok | f9f0100 | 2011-05-19 21:31:50 +0900 | [diff] [blame] | 1141 | public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) { |
| 1142 | synchronized (mMethodMap) { |
| 1143 | final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId); |
| 1144 | for (int i = 0; i < spans.length; ++i) { |
| 1145 | SuggestionSpan ss = spans[i]; |
satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 1146 | if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) { |
satok | f9f0100 | 2011-05-19 21:31:50 +0900 | [diff] [blame] | 1147 | mSecureSuggestionSpans.put(ss, currentImi); |
satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 1148 | final InputMethodInfo targetImi = mSecureSuggestionSpans.get(ss); |
satok | f9f0100 | 2011-05-19 21:31:50 +0900 | [diff] [blame] | 1149 | } |
| 1150 | } |
| 1151 | } |
| 1152 | } |
| 1153 | |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 1154 | @Override |
satok | f9f0100 | 2011-05-19 21:31:50 +0900 | [diff] [blame] | 1155 | public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) { |
| 1156 | synchronized (mMethodMap) { |
| 1157 | final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span); |
| 1158 | // TODO: Do not send the intent if the process of the targetImi is already dead. |
| 1159 | if (targetImi != null) { |
| 1160 | final String[] suggestions = span.getSuggestions(); |
| 1161 | if (index < 0 || index >= suggestions.length) return false; |
satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 1162 | final String className = span.getNotificationTargetClassName(); |
satok | f9f0100 | 2011-05-19 21:31:50 +0900 | [diff] [blame] | 1163 | final Intent intent = new Intent(); |
| 1164 | // Ensures that only a class in the original IME package will receive the |
| 1165 | // notification. |
satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 1166 | intent.setClassName(targetImi.getPackageName(), className); |
satok | f9f0100 | 2011-05-19 21:31:50 +0900 | [diff] [blame] | 1167 | intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED); |
| 1168 | intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString); |
| 1169 | intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]); |
| 1170 | intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode()); |
| 1171 | mContext.sendBroadcast(intent); |
| 1172 | return true; |
| 1173 | } |
| 1174 | } |
| 1175 | return false; |
| 1176 | } |
| 1177 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1178 | void updateFromSettingsLocked() { |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1179 | // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and |
| 1180 | // ENABLED_INPUT_METHODS is taking care of keeping them correctly in |
| 1181 | // sync, so we will never have a DEFAULT_INPUT_METHOD that is not |
| 1182 | // enabled. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1183 | String id = Settings.Secure.getString(mContext.getContentResolver(), |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1184 | Settings.Secure.DEFAULT_INPUT_METHOD); |
satok | 03eb319a | 2010-11-11 18:17:42 +0900 | [diff] [blame] | 1185 | // There is no input method selected, try to choose new applicable input method. |
| 1186 | if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) { |
| 1187 | id = Settings.Secure.getString(mContext.getContentResolver(), |
| 1188 | Settings.Secure.DEFAULT_INPUT_METHOD); |
| 1189 | } |
| 1190 | if (!TextUtils.isEmpty(id)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1191 | try { |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1192 | setInputMethodLocked(id, getSelectedInputMethodSubtypeId(id)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1193 | } catch (IllegalArgumentException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1194 | Slog.w(TAG, "Unknown input method from prefs: " + id, e); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1195 | mCurMethodId = null; |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1196 | unbindCurrentMethodLocked(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1197 | } |
satok | f3db1af | 2010-11-23 13:34:33 +0900 | [diff] [blame] | 1198 | mShortcutInputMethodsAndSubtypes.clear(); |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1199 | } else { |
| 1200 | // There is no longer an input method set, so stop any current one. |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1201 | mCurMethodId = null; |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1202 | unbindCurrentMethodLocked(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1203 | } |
| 1204 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1205 | |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1206 | /* package */ void setInputMethodLocked(String id, int subtypeId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1207 | InputMethodInfo info = mMethodMap.get(id); |
| 1208 | if (info == null) { |
satok | 913a892 | 2010-08-26 21:53:41 +0900 | [diff] [blame] | 1209 | throw new IllegalArgumentException("Unknown id: " + id); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1210 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1211 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1212 | if (id.equals(mCurMethodId)) { |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 1213 | InputMethodSubtype subtype = null; |
Ken Wakasa | 586f051 | 2011-01-20 22:31:01 +0900 | [diff] [blame] | 1214 | if (subtypeId >= 0 && subtypeId < info.getSubtypeCount()) { |
| 1215 | subtype = info.getSubtypeAt(subtypeId); |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 1216 | } |
| 1217 | if (subtype != mCurrentSubtype) { |
| 1218 | synchronized (mMethodMap) { |
satok | ca83021 | 2011-01-13 21:15:04 +0900 | [diff] [blame] | 1219 | if (subtype != null) { |
| 1220 | setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true); |
| 1221 | } |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 1222 | if (mCurMethod != null) { |
| 1223 | try { |
satok | e40dea0 | 2011-01-30 01:14:02 +0900 | [diff] [blame] | 1224 | final Configuration conf = mRes.getConfiguration(); |
| 1225 | final boolean haveHardKeyboard = conf.keyboard |
| 1226 | != Configuration.KEYBOARD_NOKEYS; |
| 1227 | final boolean hardKeyShown = haveHardKeyboard |
Ken Wakasa | 8710e76 | 2011-01-30 11:02:09 +0900 | [diff] [blame] | 1228 | && conf.hardKeyboardHidden |
| 1229 | != Configuration.HARDKEYBOARDHIDDEN_YES; |
satok | e40dea0 | 2011-01-30 01:14:02 +0900 | [diff] [blame] | 1230 | mImeWindowVis = (mInputShown || hardKeyShown) ? ( |
| 1231 | InputMethodService.IME_ACTIVE | InputMethodService.IME_VISIBLE) |
| 1232 | : 0; |
Joe Onorato | 857fd9b | 2011-01-27 15:08:35 -0800 | [diff] [blame] | 1233 | mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis, |
| 1234 | mBackDisposition); |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 1235 | // If subtype is null, try to find the most applicable one from |
| 1236 | // getCurrentInputMethodSubtype. |
| 1237 | if (subtype == null) { |
| 1238 | subtype = getCurrentInputMethodSubtype(); |
| 1239 | } |
| 1240 | mCurMethod.changeInputMethodSubtype(subtype); |
| 1241 | } catch (RemoteException e) { |
| 1242 | return; |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1243 | } |
| 1244 | } |
| 1245 | } |
| 1246 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1247 | return; |
| 1248 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1249 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1250 | final long ident = Binder.clearCallingIdentity(); |
| 1251 | try { |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1252 | // Set a subtype to this input method. |
| 1253 | // subtypeId the name of a subtype which will be set. |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 1254 | setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false); |
| 1255 | // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked() |
| 1256 | // because mCurMethodId is stored as a history in |
| 1257 | // setSelectedInputMethodAndSubtypeLocked(). |
| 1258 | mCurMethodId = id; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1259 | |
| 1260 | if (ActivityManagerNative.isSystemReady()) { |
| 1261 | Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 1262 | intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1263 | intent.putExtra("input_method_id", id); |
| 1264 | mContext.sendBroadcast(intent); |
| 1265 | } |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1266 | unbindCurrentClientLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1267 | } finally { |
| 1268 | Binder.restoreCallingIdentity(ident); |
| 1269 | } |
| 1270 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1271 | |
satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 1272 | @Override |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1273 | public boolean showSoftInput(IInputMethodClient client, int flags, |
| 1274 | ResultReceiver resultReceiver) { |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 1275 | int uid = Binder.getCallingUid(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1276 | long ident = Binder.clearCallingIdentity(); |
| 1277 | try { |
| 1278 | synchronized (mMethodMap) { |
| 1279 | if (mCurClient == null || client == null |
| 1280 | || mCurClient.client.asBinder() != client.asBinder()) { |
| 1281 | try { |
| 1282 | // We need to check if this is the current client with |
| 1283 | // focus in the window manager, to allow this call to |
| 1284 | // be made before input is started in it. |
| 1285 | if (!mIWindowManager.inputMethodClientHasFocus(client)) { |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 1286 | Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client); |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1287 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1288 | } |
| 1289 | } catch (RemoteException e) { |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1290 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1291 | } |
| 1292 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1293 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1294 | if (DEBUG) Slog.v(TAG, "Client requesting input be shown"); |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1295 | return showCurrentInputLocked(flags, resultReceiver); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1296 | } |
| 1297 | } finally { |
| 1298 | Binder.restoreCallingIdentity(ident); |
| 1299 | } |
| 1300 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1301 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1302 | boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1303 | mShowRequested = true; |
| 1304 | if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) { |
| 1305 | mShowExplicitlyRequested = true; |
| 1306 | } |
| 1307 | if ((flags&InputMethodManager.SHOW_FORCED) != 0) { |
| 1308 | mShowExplicitlyRequested = true; |
| 1309 | mShowForced = true; |
| 1310 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1311 | |
Dianne Hackborn | cc27870 | 2009-09-02 23:07:23 -0700 | [diff] [blame] | 1312 | if (!mSystemReady) { |
| 1313 | return false; |
| 1314 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1315 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1316 | boolean res = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1317 | if (mCurMethod != null) { |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1318 | executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO( |
| 1319 | MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod, |
| 1320 | resultReceiver)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1321 | mInputShown = true; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1322 | res = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1323 | } else if (mHaveConnection && SystemClock.uptimeMillis() |
| 1324 | < (mLastBindTime+TIME_TO_RECONNECT)) { |
| 1325 | // The client has asked to have the input method shown, but |
| 1326 | // we have been sitting here too long with a connection to the |
| 1327 | // service and no interface received, so let's disconnect/connect |
| 1328 | // to try to prod things along. |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1329 | EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1330 | SystemClock.uptimeMillis()-mLastBindTime,1); |
| 1331 | mContext.unbindService(this); |
| 1332 | mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE); |
| 1333 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1334 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1335 | return res; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1336 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1337 | |
satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 1338 | @Override |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1339 | public boolean hideSoftInput(IInputMethodClient client, int flags, |
| 1340 | ResultReceiver resultReceiver) { |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 1341 | int uid = Binder.getCallingUid(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1342 | long ident = Binder.clearCallingIdentity(); |
| 1343 | try { |
| 1344 | synchronized (mMethodMap) { |
| 1345 | if (mCurClient == null || client == null |
| 1346 | || mCurClient.client.asBinder() != client.asBinder()) { |
| 1347 | try { |
| 1348 | // We need to check if this is the current client with |
| 1349 | // focus in the window manager, to allow this call to |
| 1350 | // be made before input is started in it. |
| 1351 | if (!mIWindowManager.inputMethodClientHasFocus(client)) { |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 1352 | if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid " |
| 1353 | + uid + ": " + client); |
Joe Onorato | 857fd9b | 2011-01-27 15:08:35 -0800 | [diff] [blame] | 1354 | mImeWindowVis = 0; |
| 1355 | mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis, |
| 1356 | mBackDisposition); |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1357 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1358 | } |
| 1359 | } catch (RemoteException e) { |
Joe Onorato | 857fd9b | 2011-01-27 15:08:35 -0800 | [diff] [blame] | 1360 | mImeWindowVis = 0; |
| 1361 | mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition); |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1362 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1363 | } |
| 1364 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1365 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1366 | if (DEBUG) Slog.v(TAG, "Client requesting input be hidden"); |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1367 | return hideCurrentInputLocked(flags, resultReceiver); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1368 | } |
| 1369 | } finally { |
| 1370 | Binder.restoreCallingIdentity(ident); |
| 1371 | } |
| 1372 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1373 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1374 | boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1375 | if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0 |
| 1376 | && (mShowExplicitlyRequested || mShowForced)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1377 | if (DEBUG) Slog.v(TAG, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1378 | "Not hiding: explicit show not cancelled by non-explicit hide"); |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1379 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1380 | } |
| 1381 | if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1382 | if (DEBUG) Slog.v(TAG, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1383 | "Not hiding: forced show not cancelled by not-always hide"); |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1384 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1385 | } |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1386 | boolean res; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1387 | if (mInputShown && mCurMethod != null) { |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1388 | executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO( |
| 1389 | MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver)); |
| 1390 | res = true; |
| 1391 | } else { |
| 1392 | res = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1393 | } |
| 1394 | mInputShown = false; |
| 1395 | mShowRequested = false; |
| 1396 | mShowExplicitlyRequested = false; |
| 1397 | mShowForced = false; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1398 | return res; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1399 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1400 | |
satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 1401 | @Override |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1402 | public void windowGainedFocus(IInputMethodClient client, IBinder windowToken, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1403 | boolean viewHasFocus, boolean isTextEditor, int softInputMode, |
| 1404 | boolean first, int windowFlags) { |
| 1405 | long ident = Binder.clearCallingIdentity(); |
| 1406 | try { |
| 1407 | synchronized (mMethodMap) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1408 | if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1409 | + " viewHasFocus=" + viewHasFocus |
| 1410 | + " isTextEditor=" + isTextEditor |
| 1411 | + " softInputMode=#" + Integer.toHexString(softInputMode) |
| 1412 | + " first=" + first + " flags=#" |
| 1413 | + Integer.toHexString(windowFlags)); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1414 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1415 | if (mCurClient == null || client == null |
| 1416 | || mCurClient.client.asBinder() != client.asBinder()) { |
| 1417 | try { |
| 1418 | // We need to check if this is the current client with |
| 1419 | // focus in the window manager, to allow this call to |
| 1420 | // be made before input is started in it. |
| 1421 | if (!mIWindowManager.inputMethodClientHasFocus(client)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1422 | Slog.w(TAG, "Client not active, ignoring focus gain of: " + client); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1423 | return; |
| 1424 | } |
| 1425 | } catch (RemoteException e) { |
| 1426 | } |
| 1427 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1428 | |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1429 | if (mCurFocusedWindow == windowToken) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1430 | Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client); |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1431 | return; |
| 1432 | } |
| 1433 | mCurFocusedWindow = windowToken; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1434 | |
Dianne Hackborn | 7d3a5bc | 2010-11-29 22:52:12 -0800 | [diff] [blame] | 1435 | // Should we auto-show the IME even if the caller has not |
| 1436 | // specified what should be done with it? |
| 1437 | // We only do this automatically if the window can resize |
| 1438 | // to accommodate the IME (so what the user sees will give |
| 1439 | // them good context without input information being obscured |
| 1440 | // by the IME) or if running on a large screen where there |
| 1441 | // is more room for the target window + IME. |
| 1442 | final boolean doAutoShow = |
| 1443 | (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST) |
| 1444 | == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE |
| 1445 | || mRes.getConfiguration().isLayoutSizeAtLeast( |
| 1446 | Configuration.SCREENLAYOUT_SIZE_LARGE); |
| 1447 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1448 | switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) { |
| 1449 | case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED: |
Dianne Hackborn | 7d3a5bc | 2010-11-29 22:52:12 -0800 | [diff] [blame] | 1450 | if (!isTextEditor || !doAutoShow) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1451 | if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) { |
| 1452 | // There is no focus view, and this window will |
| 1453 | // be behind any soft input window, so hide the |
| 1454 | // soft input window if it is shown. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1455 | if (DEBUG) Slog.v(TAG, "Unspecified window will hide input"); |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1456 | hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1457 | } |
Dianne Hackborn | 7d3a5bc | 2010-11-29 22:52:12 -0800 | [diff] [blame] | 1458 | } else if (isTextEditor && doAutoShow && (softInputMode & |
| 1459 | WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1460 | // There is a focus view, and we are navigating forward |
| 1461 | // into the window, so show the input window for the user. |
Dianne Hackborn | 7d3a5bc | 2010-11-29 22:52:12 -0800 | [diff] [blame] | 1462 | // We only do this automatically if the window an resize |
| 1463 | // to accomodate the IME (so what the user sees will give |
| 1464 | // them good context without input information being obscured |
| 1465 | // by the IME) or if running on a large screen where there |
| 1466 | // is more room for the target window + IME. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1467 | if (DEBUG) Slog.v(TAG, "Unspecified window will show input"); |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1468 | showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1469 | } |
| 1470 | break; |
| 1471 | case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED: |
| 1472 | // Do nothing. |
| 1473 | break; |
| 1474 | case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN: |
| 1475 | if ((softInputMode & |
| 1476 | WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1477 | if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward"); |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1478 | hideCurrentInputLocked(0, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1479 | } |
| 1480 | break; |
| 1481 | case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN: |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1482 | if (DEBUG) Slog.v(TAG, "Window asks to hide input"); |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1483 | hideCurrentInputLocked(0, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1484 | break; |
| 1485 | case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE: |
| 1486 | if ((softInputMode & |
| 1487 | WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1488 | if (DEBUG) Slog.v(TAG, "Window asks to show input going forward"); |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1489 | showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1490 | } |
| 1491 | break; |
| 1492 | case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE: |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1493 | if (DEBUG) Slog.v(TAG, "Window asks to always show input"); |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1494 | showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1495 | break; |
| 1496 | } |
| 1497 | } |
| 1498 | } finally { |
| 1499 | Binder.restoreCallingIdentity(ident); |
| 1500 | } |
| 1501 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1502 | |
satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 1503 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1504 | public void showInputMethodPickerFromClient(IInputMethodClient client) { |
| 1505 | synchronized (mMethodMap) { |
| 1506 | if (mCurClient == null || client == null |
| 1507 | || mCurClient.client.asBinder() != client.asBinder()) { |
satok | 47a4491 | 2010-10-06 16:03:58 +0900 | [diff] [blame] | 1508 | Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid " |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 1509 | + Binder.getCallingUid() + ": " + client); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1510 | } |
| 1511 | |
satok | 440aab5 | 2010-11-25 09:43:11 +0900 | [diff] [blame] | 1512 | // Always call subtype picker, because subtype picker is a superset of input method |
| 1513 | // picker. |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1514 | mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER); |
| 1515 | } |
| 1516 | } |
| 1517 | |
satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 1518 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1519 | public void setInputMethod(IBinder token, String id) { |
satok | 2820351 | 2010-11-24 11:06:49 +0900 | [diff] [blame] | 1520 | setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID); |
| 1521 | } |
| 1522 | |
satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 1523 | @Override |
satok | 2820351 | 2010-11-24 11:06:49 +0900 | [diff] [blame] | 1524 | public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) { |
| 1525 | synchronized (mMethodMap) { |
| 1526 | if (subtype != null) { |
| 1527 | setInputMethodWithSubtypeId(token, id, getSubtypeIdFromHashCode( |
| 1528 | mMethodMap.get(id), subtype.hashCode())); |
| 1529 | } else { |
| 1530 | setInputMethod(token, id); |
| 1531 | } |
| 1532 | } |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1533 | } |
| 1534 | |
satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 1535 | @Override |
satok | b416a71 | 2010-11-25 20:42:14 +0900 | [diff] [blame] | 1536 | public void showInputMethodAndSubtypeEnablerFromClient( |
satok | 217f548 | 2010-12-15 05:19:19 +0900 | [diff] [blame] | 1537 | IInputMethodClient client, String inputMethodId) { |
satok | b416a71 | 2010-11-25 20:42:14 +0900 | [diff] [blame] | 1538 | synchronized (mMethodMap) { |
| 1539 | if (mCurClient == null || client == null |
| 1540 | || mCurClient.client.asBinder() != client.asBinder()) { |
| 1541 | Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client); |
| 1542 | } |
satok | 7fee71f | 2010-12-17 18:54:26 +0900 | [diff] [blame] | 1543 | executeOrSendMessage(mCurMethod, mCaller.obtainMessageO( |
| 1544 | MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId)); |
satok | b416a71 | 2010-11-25 20:42:14 +0900 | [diff] [blame] | 1545 | } |
| 1546 | } |
| 1547 | |
satok | 4fc87d6 | 2011-05-20 16:13:43 +0900 | [diff] [blame] | 1548 | @Override |
satok | 735cf38 | 2010-11-11 20:40:09 +0900 | [diff] [blame] | 1549 | public boolean switchToLastInputMethod(IBinder token) { |
| 1550 | synchronized (mMethodMap) { |
satok | c445bcd | 2011-01-25 18:57:24 +0900 | [diff] [blame] | 1551 | final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked(); |
satok | 4fc87d6 | 2011-05-20 16:13:43 +0900 | [diff] [blame] | 1552 | final InputMethodInfo lastImi; |
satok | 208d563 | 2011-05-20 22:13:38 +0900 | [diff] [blame] | 1553 | if (lastIme != null) { |
satok | 4fc87d6 | 2011-05-20 16:13:43 +0900 | [diff] [blame] | 1554 | lastImi = mMethodMap.get(lastIme.first); |
| 1555 | } else { |
| 1556 | lastImi = null; |
satok | 735cf38 | 2010-11-11 20:40:09 +0900 | [diff] [blame] | 1557 | } |
satok | 4fc87d6 | 2011-05-20 16:13:43 +0900 | [diff] [blame] | 1558 | String targetLastImiId = null; |
| 1559 | int subtypeId = NOT_A_SUBTYPE_ID; |
| 1560 | if (lastIme != null && lastImi != null) { |
| 1561 | final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId); |
| 1562 | final int lastSubtypeHash = Integer.valueOf(lastIme.second); |
| 1563 | final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID |
| 1564 | : mCurrentSubtype.hashCode(); |
| 1565 | // If the last IME is the same as the current IME and the last subtype is not |
| 1566 | // defined, there is no need to switch to the last IME. |
| 1567 | if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) { |
| 1568 | targetLastImiId = lastIme.first; |
| 1569 | subtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash); |
| 1570 | } |
| 1571 | } |
| 1572 | |
| 1573 | if (TextUtils.isEmpty(targetLastImiId) && !canAddToLastInputMethod(mCurrentSubtype)) { |
| 1574 | // This is a safety net. If the currentSubtype can't be added to the history |
| 1575 | // and the framework couldn't find the last ime, we will make the last ime be |
| 1576 | // the most applicable enabled keyboard subtype of the system imes. |
| 1577 | final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked(); |
| 1578 | if (enabled != null) { |
| 1579 | final int N = enabled.size(); |
| 1580 | final String locale = mCurrentSubtype == null |
| 1581 | ? mRes.getConfiguration().locale.toString() |
| 1582 | : mCurrentSubtype.getLocale(); |
| 1583 | for (int i = 0; i < N; ++i) { |
| 1584 | final InputMethodInfo imi = enabled.get(i); |
| 1585 | if (imi.getSubtypeCount() > 0 && isSystemIme(imi)) { |
| 1586 | InputMethodSubtype keyboardSubtype = |
| 1587 | findLastResortApplicableSubtypeLocked(mRes, getSubtypes(imi), |
| 1588 | SUBTYPE_MODE_KEYBOARD, locale, true); |
| 1589 | if (keyboardSubtype != null) { |
| 1590 | targetLastImiId = imi.getId(); |
| 1591 | subtypeId = getSubtypeIdFromHashCode( |
| 1592 | imi, keyboardSubtype.hashCode()); |
| 1593 | if(keyboardSubtype.getLocale().equals(locale)) { |
| 1594 | break; |
| 1595 | } |
| 1596 | } |
| 1597 | } |
| 1598 | } |
| 1599 | } |
| 1600 | } |
| 1601 | |
| 1602 | if (!TextUtils.isEmpty(targetLastImiId)) { |
| 1603 | if (DEBUG) { |
| 1604 | Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second |
| 1605 | + ", from: " + mCurMethodId + ", " + subtypeId); |
| 1606 | } |
| 1607 | setInputMethodWithSubtypeId(token, targetLastImiId, subtypeId); |
| 1608 | return true; |
| 1609 | } else { |
| 1610 | return false; |
| 1611 | } |
satok | 735cf38 | 2010-11-11 20:40:09 +0900 | [diff] [blame] | 1612 | } |
| 1613 | } |
| 1614 | |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 1615 | @Override |
satok | 68f1b78 | 2011-04-11 14:26:04 +0900 | [diff] [blame] | 1616 | public InputMethodSubtype getLastInputMethodSubtype() { |
| 1617 | synchronized (mMethodMap) { |
| 1618 | final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked(); |
| 1619 | // TODO: Handle the case of the last IME with no subtypes |
| 1620 | if (lastIme == null || TextUtils.isEmpty(lastIme.first) |
| 1621 | || TextUtils.isEmpty(lastIme.second)) return null; |
| 1622 | final InputMethodInfo lastImi = mMethodMap.get(lastIme.first); |
| 1623 | if (lastImi == null) return null; |
| 1624 | try { |
| 1625 | final int lastSubtypeHash = Integer.valueOf(lastIme.second); |
| 1626 | return lastImi.getSubtypeAt(getSubtypeIdFromHashCode( |
| 1627 | lastImi, lastSubtypeHash)); |
| 1628 | } catch (NumberFormatException e) { |
| 1629 | return null; |
| 1630 | } |
| 1631 | } |
| 1632 | } |
| 1633 | |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 1634 | @Override |
| 1635 | public boolean setAdditionalInputMethodSubtypes(IBinder token, InputMethodSubtype[] subtypes) { |
| 1636 | if (token == null || mCurToken != token) { |
| 1637 | return false; |
| 1638 | } |
| 1639 | if (subtypes == null || subtypes.length == 0) return false; |
| 1640 | synchronized (mMethodMap) { |
| 1641 | final InputMethodInfo imi = mMethodMap.get(mCurMethodId); |
| 1642 | if (imi == null) return false; |
| 1643 | final int N = subtypes.length; |
satok | 4a28bde | 2011-06-29 21:03:40 +0900 | [diff] [blame^] | 1644 | mFileManager.addInputMethodSubtypes(imi, subtypes); |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 1645 | buildInputMethodListLocked(mMethodList, mMethodMap); |
| 1646 | return true; |
| 1647 | } |
| 1648 | } |
| 1649 | |
satok | 2820351 | 2010-11-24 11:06:49 +0900 | [diff] [blame] | 1650 | private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1651 | synchronized (mMethodMap) { |
| 1652 | if (token == null) { |
| 1653 | if (mContext.checkCallingOrSelfPermission( |
| 1654 | android.Manifest.permission.WRITE_SECURE_SETTINGS) |
| 1655 | != PackageManager.PERMISSION_GRANTED) { |
| 1656 | throw new SecurityException( |
| 1657 | "Using null token requires permission " |
| 1658 | + android.Manifest.permission.WRITE_SECURE_SETTINGS); |
| 1659 | } |
| 1660 | } else if (mCurToken != token) { |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 1661 | Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid() |
| 1662 | + " token: " + token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1663 | return; |
| 1664 | } |
| 1665 | |
| 1666 | long ident = Binder.clearCallingIdentity(); |
| 1667 | try { |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1668 | setInputMethodLocked(id, subtypeId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1669 | } finally { |
| 1670 | Binder.restoreCallingIdentity(ident); |
| 1671 | } |
| 1672 | } |
| 1673 | } |
| 1674 | |
satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 1675 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1676 | public void hideMySoftInput(IBinder token, int flags) { |
| 1677 | synchronized (mMethodMap) { |
| 1678 | if (token == null || mCurToken != token) { |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 1679 | if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid " |
| 1680 | + Binder.getCallingUid() + " token: " + token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1681 | return; |
| 1682 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1683 | long ident = Binder.clearCallingIdentity(); |
| 1684 | try { |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1685 | hideCurrentInputLocked(flags, null); |
| 1686 | } finally { |
| 1687 | Binder.restoreCallingIdentity(ident); |
| 1688 | } |
| 1689 | } |
| 1690 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1691 | |
satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 1692 | @Override |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1693 | public void showMySoftInput(IBinder token, int flags) { |
| 1694 | synchronized (mMethodMap) { |
| 1695 | if (token == null || mCurToken != token) { |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 1696 | Slog.w(TAG, "Ignoring showMySoftInput of uid " |
| 1697 | + Binder.getCallingUid() + " token: " + token); |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1698 | return; |
| 1699 | } |
| 1700 | long ident = Binder.clearCallingIdentity(); |
| 1701 | try { |
| 1702 | showCurrentInputLocked(flags, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1703 | } finally { |
| 1704 | Binder.restoreCallingIdentity(ident); |
| 1705 | } |
| 1706 | } |
| 1707 | } |
| 1708 | |
| 1709 | void setEnabledSessionInMainThread(SessionState session) { |
| 1710 | if (mEnabledSession != session) { |
| 1711 | if (mEnabledSession != null) { |
| 1712 | try { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1713 | if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1714 | mEnabledSession.method.setSessionEnabled( |
| 1715 | mEnabledSession.session, false); |
| 1716 | } catch (RemoteException e) { |
| 1717 | } |
| 1718 | } |
| 1719 | mEnabledSession = session; |
| 1720 | try { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1721 | if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1722 | session.method.setSessionEnabled( |
| 1723 | session.session, true); |
| 1724 | } catch (RemoteException e) { |
| 1725 | } |
| 1726 | } |
| 1727 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1728 | |
satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 1729 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1730 | public boolean handleMessage(Message msg) { |
| 1731 | HandlerCaller.SomeArgs args; |
| 1732 | switch (msg.what) { |
| 1733 | case MSG_SHOW_IM_PICKER: |
| 1734 | showInputMethodMenu(); |
| 1735 | return true; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1736 | |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1737 | case MSG_SHOW_IM_SUBTYPE_PICKER: |
| 1738 | showInputMethodSubtypeMenu(); |
| 1739 | return true; |
| 1740 | |
satok | 47a4491 | 2010-10-06 16:03:58 +0900 | [diff] [blame] | 1741 | case MSG_SHOW_IM_SUBTYPE_ENABLER: |
satok | 217f548 | 2010-12-15 05:19:19 +0900 | [diff] [blame] | 1742 | args = (HandlerCaller.SomeArgs)msg.obj; |
satok | 7fee71f | 2010-12-17 18:54:26 +0900 | [diff] [blame] | 1743 | showInputMethodAndSubtypeEnabler((String)args.arg1); |
satok | 217f548 | 2010-12-15 05:19:19 +0900 | [diff] [blame] | 1744 | return true; |
| 1745 | |
| 1746 | case MSG_SHOW_IM_CONFIG: |
| 1747 | showConfigureInputMethods(); |
satok | 47a4491 | 2010-10-06 16:03:58 +0900 | [diff] [blame] | 1748 | return true; |
| 1749 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1750 | // --------------------------------------------------------- |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1751 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1752 | case MSG_UNBIND_INPUT: |
| 1753 | try { |
| 1754 | ((IInputMethod)msg.obj).unbindInput(); |
| 1755 | } catch (RemoteException e) { |
| 1756 | // There is nothing interesting about the method dying. |
| 1757 | } |
| 1758 | return true; |
| 1759 | case MSG_BIND_INPUT: |
| 1760 | args = (HandlerCaller.SomeArgs)msg.obj; |
| 1761 | try { |
| 1762 | ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2); |
| 1763 | } catch (RemoteException e) { |
| 1764 | } |
| 1765 | return true; |
| 1766 | case MSG_SHOW_SOFT_INPUT: |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1767 | args = (HandlerCaller.SomeArgs)msg.obj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1768 | try { |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1769 | ((IInputMethod)args.arg1).showSoftInput(msg.arg1, |
| 1770 | (ResultReceiver)args.arg2); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1771 | } catch (RemoteException e) { |
| 1772 | } |
| 1773 | return true; |
| 1774 | case MSG_HIDE_SOFT_INPUT: |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1775 | args = (HandlerCaller.SomeArgs)msg.obj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1776 | try { |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1777 | ((IInputMethod)args.arg1).hideSoftInput(0, |
| 1778 | (ResultReceiver)args.arg2); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1779 | } catch (RemoteException e) { |
| 1780 | } |
| 1781 | return true; |
| 1782 | case MSG_ATTACH_TOKEN: |
| 1783 | args = (HandlerCaller.SomeArgs)msg.obj; |
| 1784 | try { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1785 | if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1786 | ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2); |
| 1787 | } catch (RemoteException e) { |
| 1788 | } |
| 1789 | return true; |
| 1790 | case MSG_CREATE_SESSION: |
| 1791 | args = (HandlerCaller.SomeArgs)msg.obj; |
| 1792 | try { |
| 1793 | ((IInputMethod)args.arg1).createSession( |
| 1794 | (IInputMethodCallback)args.arg2); |
| 1795 | } catch (RemoteException e) { |
| 1796 | } |
| 1797 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1798 | // --------------------------------------------------------- |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1799 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1800 | case MSG_START_INPUT: |
| 1801 | args = (HandlerCaller.SomeArgs)msg.obj; |
| 1802 | try { |
| 1803 | SessionState session = (SessionState)args.arg1; |
| 1804 | setEnabledSessionInMainThread(session); |
| 1805 | session.method.startInput((IInputContext)args.arg2, |
| 1806 | (EditorInfo)args.arg3); |
| 1807 | } catch (RemoteException e) { |
| 1808 | } |
| 1809 | return true; |
| 1810 | case MSG_RESTART_INPUT: |
| 1811 | args = (HandlerCaller.SomeArgs)msg.obj; |
| 1812 | try { |
| 1813 | SessionState session = (SessionState)args.arg1; |
| 1814 | setEnabledSessionInMainThread(session); |
| 1815 | session.method.restartInput((IInputContext)args.arg2, |
| 1816 | (EditorInfo)args.arg3); |
| 1817 | } catch (RemoteException e) { |
| 1818 | } |
| 1819 | return true; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1820 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1821 | // --------------------------------------------------------- |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1822 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1823 | case MSG_UNBIND_METHOD: |
| 1824 | try { |
| 1825 | ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1); |
| 1826 | } catch (RemoteException e) { |
| 1827 | // There is nothing interesting about the last client dying. |
| 1828 | } |
| 1829 | return true; |
| 1830 | case MSG_BIND_METHOD: |
| 1831 | args = (HandlerCaller.SomeArgs)msg.obj; |
| 1832 | try { |
| 1833 | ((IInputMethodClient)args.arg1).onBindMethod( |
| 1834 | (InputBindResult)args.arg2); |
| 1835 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1836 | Slog.w(TAG, "Client died receiving input method " + args.arg2); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1837 | } |
| 1838 | return true; |
| 1839 | } |
| 1840 | return false; |
| 1841 | } |
| 1842 | |
Brandon Ballinger | 6da35a0 | 2009-10-21 00:38:13 -0700 | [diff] [blame] | 1843 | private boolean isSystemIme(InputMethodInfo inputMethod) { |
| 1844 | return (inputMethod.getServiceInfo().applicationInfo.flags |
| 1845 | & ApplicationInfo.FLAG_SYSTEM) != 0; |
| 1846 | } |
| 1847 | |
Ken Wakasa | 586f051 | 2011-01-20 22:31:01 +0900 | [diff] [blame] | 1848 | private static ArrayList<InputMethodSubtype> getSubtypes(InputMethodInfo imi) { |
| 1849 | ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>(); |
| 1850 | final int subtypeCount = imi.getSubtypeCount(); |
| 1851 | for (int i = 0; i < subtypeCount; ++i) { |
| 1852 | subtypes.add(imi.getSubtypeAt(i)); |
| 1853 | } |
| 1854 | return subtypes; |
| 1855 | } |
| 1856 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1857 | private boolean chooseNewDefaultIMELocked() { |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 1858 | List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked(); |
Brandon Ballinger | 6da35a0 | 2009-10-21 00:38:13 -0700 | [diff] [blame] | 1859 | if (enabled != null && enabled.size() > 0) { |
Dianne Hackborn | 83e48f5 | 2010-03-23 23:03:25 -0700 | [diff] [blame] | 1860 | // We'd prefer to fall back on a system IME, since that is safer. |
| 1861 | int i=enabled.size(); |
| 1862 | while (i > 0) { |
| 1863 | i--; |
| 1864 | if ((enabled.get(i).getServiceInfo().applicationInfo.flags |
| 1865 | & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 1866 | break; |
| 1867 | } |
| 1868 | } |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1869 | InputMethodInfo imi = enabled.get(i); |
satok | 03eb319a | 2010-11-11 18:17:42 +0900 | [diff] [blame] | 1870 | if (DEBUG) { |
| 1871 | Slog.d(TAG, "New default IME was selected: " + imi.getId()); |
| 1872 | } |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 1873 | resetSelectedInputMethodAndSubtypeLocked(imi.getId()); |
Brandon Ballinger | 6da35a0 | 2009-10-21 00:38:13 -0700 | [diff] [blame] | 1874 | return true; |
| 1875 | } |
| 1876 | |
| 1877 | return false; |
| 1878 | } |
| 1879 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1880 | void buildInputMethodListLocked(ArrayList<InputMethodInfo> list, |
| 1881 | HashMap<String, InputMethodInfo> map) { |
| 1882 | list.clear(); |
| 1883 | map.clear(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1884 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1885 | PackageManager pm = mContext.getPackageManager(); |
Dianne Hackborn | 7d3a5bc | 2010-11-29 22:52:12 -0800 | [diff] [blame] | 1886 | final Configuration config = mRes.getConfiguration(); |
Amith Yamasani | e861ec1 | 2010-03-24 21:39:27 -0700 | [diff] [blame] | 1887 | final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY; |
| 1888 | String disabledSysImes = Settings.Secure.getString(mContext.getContentResolver(), |
| 1889 | Secure.DISABLED_SYSTEM_INPUT_METHODS); |
| 1890 | if (disabledSysImes == null) disabledSysImes = ""; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1891 | |
| 1892 | List<ResolveInfo> services = pm.queryIntentServices( |
| 1893 | new Intent(InputMethod.SERVICE_INTERFACE), |
| 1894 | PackageManager.GET_META_DATA); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1895 | |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 1896 | final HashMap<String, List<InputMethodSubtype>> additionalSubtypes = |
| 1897 | mFileManager.getAllAdditionalInputMethodSubtypes(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1898 | for (int i = 0; i < services.size(); ++i) { |
| 1899 | ResolveInfo ri = services.get(i); |
| 1900 | ServiceInfo si = ri.serviceInfo; |
| 1901 | ComponentName compName = new ComponentName(si.packageName, si.name); |
| 1902 | if (!android.Manifest.permission.BIND_INPUT_METHOD.equals( |
| 1903 | si.permission)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1904 | Slog.w(TAG, "Skipping input method " + compName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1905 | + ": it does not require the permission " |
| 1906 | + android.Manifest.permission.BIND_INPUT_METHOD); |
| 1907 | continue; |
| 1908 | } |
| 1909 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1910 | if (DEBUG) Slog.d(TAG, "Checking " + compName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1911 | |
| 1912 | try { |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 1913 | InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1914 | list.add(p); |
Amith Yamasani | e861ec1 | 2010-03-24 21:39:27 -0700 | [diff] [blame] | 1915 | final String id = p.getId(); |
| 1916 | map.put(id, p); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1917 | |
Amith Yamasani | e861ec1 | 2010-03-24 21:39:27 -0700 | [diff] [blame] | 1918 | // System IMEs are enabled by default, unless there's a hard keyboard |
| 1919 | // and the system IME was explicitly disabled |
| 1920 | if (isSystemIme(p) && (!haveHardKeyboard || disabledSysImes.indexOf(id) < 0)) { |
| 1921 | setInputMethodEnabledLocked(id, true); |
Brandon Ballinger | 6da35a0 | 2009-10-21 00:38:13 -0700 | [diff] [blame] | 1922 | } |
| 1923 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1924 | if (DEBUG) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1925 | Slog.d(TAG, "Found a third-party input method " + p); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1926 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1927 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1928 | } catch (XmlPullParserException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1929 | Slog.w(TAG, "Unable to load input method " + compName, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1930 | } catch (IOException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1931 | Slog.w(TAG, "Unable to load input method " + compName, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1932 | } |
| 1933 | } |
Brandon Ballinger | 6da35a0 | 2009-10-21 00:38:13 -0700 | [diff] [blame] | 1934 | |
| 1935 | String defaultIme = Settings.Secure.getString(mContext |
| 1936 | .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD); |
satok | 913a892 | 2010-08-26 21:53:41 +0900 | [diff] [blame] | 1937 | if (!TextUtils.isEmpty(defaultIme) && !map.containsKey(defaultIme)) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1938 | if (chooseNewDefaultIMELocked()) { |
Brandon Ballinger | 6da35a0 | 2009-10-21 00:38:13 -0700 | [diff] [blame] | 1939 | updateFromSettingsLocked(); |
| 1940 | } |
| 1941 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1942 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1943 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1944 | // ---------------------------------------------------------------------- |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1945 | |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1946 | private void showInputMethodMenu() { |
| 1947 | showInputMethodMenuInternal(false); |
| 1948 | } |
| 1949 | |
| 1950 | private void showInputMethodSubtypeMenu() { |
| 1951 | showInputMethodMenuInternal(true); |
| 1952 | } |
| 1953 | |
satok | 217f548 | 2010-12-15 05:19:19 +0900 | [diff] [blame] | 1954 | private void showInputMethodAndSubtypeEnabler(String inputMethodId) { |
Tadashi G. Takaoka | f49688f | 2011-01-20 17:56:13 +0900 | [diff] [blame] | 1955 | Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS); |
satok | 47a4491 | 2010-10-06 16:03:58 +0900 | [diff] [blame] | 1956 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
satok | 86417ea | 2010-10-27 14:11:03 +0900 | [diff] [blame] | 1957 | | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED |
| 1958 | | Intent.FLAG_ACTIVITY_CLEAR_TOP); |
satok | 7fee71f | 2010-12-17 18:54:26 +0900 | [diff] [blame] | 1959 | if (!TextUtils.isEmpty(inputMethodId)) { |
Tadashi G. Takaoka | 2548020 | 2011-01-20 23:13:02 +0900 | [diff] [blame] | 1960 | intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId); |
satok | 7fee71f | 2010-12-17 18:54:26 +0900 | [diff] [blame] | 1961 | } |
satok | 217f548 | 2010-12-15 05:19:19 +0900 | [diff] [blame] | 1962 | mContext.startActivity(intent); |
| 1963 | } |
| 1964 | |
| 1965 | private void showConfigureInputMethods() { |
| 1966 | Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS); |
| 1967 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
| 1968 | | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED |
| 1969 | | Intent.FLAG_ACTIVITY_CLEAR_TOP); |
satok | 47a4491 | 2010-10-06 16:03:58 +0900 | [diff] [blame] | 1970 | mContext.startActivity(intent); |
| 1971 | } |
| 1972 | |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1973 | private void showInputMethodMenuInternal(boolean showSubtypes) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1974 | if (DEBUG) Slog.v(TAG, "Show switching menu"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1975 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1976 | final Context context = mContext; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1977 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1978 | final PackageManager pm = context.getPackageManager(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1979 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1980 | String lastInputMethodId = Settings.Secure.getString(context |
| 1981 | .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD); |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1982 | int lastInputMethodSubtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1983 | if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1984 | |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 1985 | synchronized (mMethodMap) { |
satok | bb4aa06 | 2011-01-19 21:40:27 +0900 | [diff] [blame] | 1986 | final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis = |
| 1987 | getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(); |
satok | 7f35c8c | 2010-10-07 21:13:11 +0900 | [diff] [blame] | 1988 | if (immis == null || immis.size() == 0) { |
| 1989 | return; |
| 1990 | } |
| 1991 | |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 1992 | hideInputMethodMenuLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1993 | |
Ken Wakasa | 761eb37 | 2011-03-04 19:06:18 +0900 | [diff] [blame] | 1994 | final TreeMap<InputMethodInfo, List<InputMethodSubtype>> sortedImmis = |
| 1995 | new TreeMap<InputMethodInfo, List<InputMethodSubtype>>( |
| 1996 | new Comparator<InputMethodInfo>() { |
| 1997 | @Override |
| 1998 | public int compare(InputMethodInfo imi1, InputMethodInfo imi2) { |
| 1999 | if (imi2 == null) return 0; |
| 2000 | if (imi1 == null) return 1; |
| 2001 | if (pm == null) { |
| 2002 | return imi1.getId().compareTo(imi2.getId()); |
| 2003 | } |
| 2004 | CharSequence imiId1 = imi1.loadLabel(pm) + "/" + imi1.getId(); |
| 2005 | CharSequence imiId2 = imi2.loadLabel(pm) + "/" + imi2.getId(); |
| 2006 | return imiId1.toString().compareTo(imiId2.toString()); |
| 2007 | } |
| 2008 | }); |
satok | 913a892 | 2010-08-26 21:53:41 +0900 | [diff] [blame] | 2009 | |
Ken Wakasa | 761eb37 | 2011-03-04 19:06:18 +0900 | [diff] [blame] | 2010 | sortedImmis.putAll(immis); |
| 2011 | |
| 2012 | final ArrayList<Pair<CharSequence, Pair<InputMethodInfo, Integer>>> imList = |
| 2013 | new ArrayList<Pair<CharSequence, Pair<InputMethodInfo, Integer>>>(); |
| 2014 | |
| 2015 | for (InputMethodInfo imi : sortedImmis.keySet()) { |
satok | bb4aa06 | 2011-01-19 21:40:27 +0900 | [diff] [blame] | 2016 | if (imi == null) continue; |
| 2017 | List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypeList = immis.get(imi); |
satok | 7f35c8c | 2010-10-07 21:13:11 +0900 | [diff] [blame] | 2018 | HashSet<String> enabledSubtypeSet = new HashSet<String>(); |
satok | bb4aa06 | 2011-01-19 21:40:27 +0900 | [diff] [blame] | 2019 | for (InputMethodSubtype subtype: explicitlyOrImplicitlyEnabledSubtypeList) { |
| 2020 | enabledSubtypeSet.add(String.valueOf(subtype.hashCode())); |
satok | 7f35c8c | 2010-10-07 21:13:11 +0900 | [diff] [blame] | 2021 | } |
satok | bb4aa06 | 2011-01-19 21:40:27 +0900 | [diff] [blame] | 2022 | ArrayList<InputMethodSubtype> subtypes = getSubtypes(imi); |
Ken Wakasa | 81f6e3d | 2011-03-04 09:59:52 +0900 | [diff] [blame] | 2023 | final CharSequence label = imi.loadLabel(pm); |
satok | 7f35c8c | 2010-10-07 21:13:11 +0900 | [diff] [blame] | 2024 | if (showSubtypes && enabledSubtypeSet.size() > 0) { |
satok | bb4aa06 | 2011-01-19 21:40:27 +0900 | [diff] [blame] | 2025 | final int subtypeCount = imi.getSubtypeCount(); |
satok | 4a28bde | 2011-06-29 21:03:40 +0900 | [diff] [blame^] | 2026 | if (DEBUG) { |
| 2027 | Slog.v(TAG, "Add subtypes: " + subtypeCount + ", " + imi.getId()); |
| 2028 | } |
Ken Wakasa | 586f051 | 2011-01-20 22:31:01 +0900 | [diff] [blame] | 2029 | for (int j = 0; j < subtypeCount; ++j) { |
satok | 4a28bde | 2011-06-29 21:03:40 +0900 | [diff] [blame^] | 2030 | final InputMethodSubtype subtype = imi.getSubtypeAt(j); |
| 2031 | final String subtypeHashCode = String.valueOf(subtype.hashCode()); |
| 2032 | // We show all enabled IMEs and subtypes when an IME is shown. |
| 2033 | if (enabledSubtypeSet.contains(subtypeHashCode) |
satok | 7dca6cd | 2011-06-29 18:06:25 +0900 | [diff] [blame] | 2034 | && (mInputShown || !subtype.isAuxiliary())) { |
Ken Wakasa | 761eb37 | 2011-03-04 19:06:18 +0900 | [diff] [blame] | 2035 | final CharSequence title; |
satok | 4a28bde | 2011-06-29 21:03:40 +0900 | [diff] [blame^] | 2036 | final String mode = subtype.getMode(); |
| 2037 | title = TextUtils.concat(subtype.getDisplayName(context, |
| 2038 | imi.getPackageName(), imi.getServiceInfo().applicationInfo), |
| 2039 | (TextUtils.isEmpty(label) ? "" : " (" + label + ")")); |
Ken Wakasa | 761eb37 | 2011-03-04 19:06:18 +0900 | [diff] [blame] | 2040 | imList.add(new Pair<CharSequence, Pair<InputMethodInfo, Integer>>( |
| 2041 | title, new Pair<InputMethodInfo, Integer>(imi, j))); |
satok | 4a28bde | 2011-06-29 21:03:40 +0900 | [diff] [blame^] | 2042 | // Removing this subtype from enabledSubtypeSet because we no longer |
| 2043 | // need to add an entry of this subtype to imList to avoid duplicated |
| 2044 | // entries. |
| 2045 | enabledSubtypeSet.remove(subtypeHashCode); |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 2046 | } |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 2047 | } |
| 2048 | } else { |
Ken Wakasa | 761eb37 | 2011-03-04 19:06:18 +0900 | [diff] [blame] | 2049 | imList.add(new Pair<CharSequence, Pair<InputMethodInfo, Integer>>( |
| 2050 | label, new Pair<InputMethodInfo, Integer>(imi, NOT_A_SUBTYPE_ID))); |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 2051 | } |
Dianne Hackborn | 97106ab | 2010-03-03 00:08:31 -0800 | [diff] [blame] | 2052 | } |
satok | 913a892 | 2010-08-26 21:53:41 +0900 | [diff] [blame] | 2053 | |
Ken Wakasa | 761eb37 | 2011-03-04 19:06:18 +0900 | [diff] [blame] | 2054 | final int N = imList.size(); |
| 2055 | mItems = new CharSequence[N]; |
| 2056 | for (int i = 0; i < N; ++i) { |
| 2057 | mItems[i] = imList.get(i).first; |
| 2058 | } |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 2059 | mIms = new InputMethodInfo[N]; |
| 2060 | mSubtypeIds = new int[N]; |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 2061 | int checkedItem = 0; |
| 2062 | for (int i = 0; i < N; ++i) { |
Ken Wakasa | 761eb37 | 2011-03-04 19:06:18 +0900 | [diff] [blame] | 2063 | Pair<InputMethodInfo, Integer> value = imList.get(i).second; |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 2064 | mIms[i] = value.first; |
| 2065 | mSubtypeIds[i] = value.second; |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 2066 | if (mIms[i].getId().equals(lastInputMethodId)) { |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 2067 | int subtypeId = mSubtypeIds[i]; |
| 2068 | if ((subtypeId == NOT_A_SUBTYPE_ID) |
| 2069 | || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0) |
| 2070 | || (subtypeId == lastInputMethodSubtypeId)) { |
| 2071 | checkedItem = i; |
| 2072 | } |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 2073 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2074 | } |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 2075 | |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 2076 | AlertDialog.OnClickListener adocl = new AlertDialog.OnClickListener() { |
satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 2077 | @Override |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 2078 | public void onClick(DialogInterface dialog, int which) { |
| 2079 | hideInputMethodMenu(); |
| 2080 | } |
| 2081 | }; |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2082 | |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 2083 | TypedArray a = context.obtainStyledAttributes(null, |
| 2084 | com.android.internal.R.styleable.DialogPreference, |
| 2085 | com.android.internal.R.attr.alertDialogStyle, 0); |
| 2086 | mDialogBuilder = new AlertDialog.Builder(context) |
| 2087 | .setTitle(com.android.internal.R.string.select_input_method) |
| 2088 | .setOnCancelListener(new OnCancelListener() { |
satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 2089 | @Override |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 2090 | public void onCancel(DialogInterface dialog) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2091 | hideInputMethodMenu(); |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 2092 | } |
| 2093 | }) |
| 2094 | .setIcon(a.getDrawable( |
| 2095 | com.android.internal.R.styleable.DialogPreference_dialogTitle)); |
| 2096 | a.recycle(); |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2097 | |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 2098 | mDialogBuilder.setSingleChoiceItems(mItems, checkedItem, |
| 2099 | new AlertDialog.OnClickListener() { |
satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 2100 | @Override |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 2101 | public void onClick(DialogInterface dialog, int which) { |
| 2102 | synchronized (mMethodMap) { |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 2103 | if (mIms == null || mIms.length <= which |
| 2104 | || mSubtypeIds == null || mSubtypeIds.length <= which) { |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 2105 | return; |
| 2106 | } |
| 2107 | InputMethodInfo im = mIms[which]; |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 2108 | int subtypeId = mSubtypeIds[which]; |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 2109 | hideInputMethodMenu(); |
| 2110 | if (im != null) { |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 2111 | if ((subtypeId < 0) |
Ken Wakasa | 586f051 | 2011-01-20 22:31:01 +0900 | [diff] [blame] | 2112 | || (subtypeId >= im.getSubtypeCount())) { |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 2113 | subtypeId = NOT_A_SUBTYPE_ID; |
| 2114 | } |
| 2115 | setInputMethodLocked(im.getId(), subtypeId); |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 2116 | } |
Dianne Hackborn | 20cb56e | 2010-03-04 00:58:29 -0800 | [diff] [blame] | 2117 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2118 | } |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 2119 | }); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2120 | |
satok | 7f35c8c | 2010-10-07 21:13:11 +0900 | [diff] [blame] | 2121 | if (showSubtypes) { |
satok | 82beadf | 2010-12-27 19:03:06 +0900 | [diff] [blame] | 2122 | mDialogBuilder.setPositiveButton( |
| 2123 | com.android.internal.R.string.configure_input_methods, |
satok | 7f35c8c | 2010-10-07 21:13:11 +0900 | [diff] [blame] | 2124 | new DialogInterface.OnClickListener() { |
satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 2125 | @Override |
satok | 7f35c8c | 2010-10-07 21:13:11 +0900 | [diff] [blame] | 2126 | public void onClick(DialogInterface dialog, int whichButton) { |
satok | 217f548 | 2010-12-15 05:19:19 +0900 | [diff] [blame] | 2127 | showConfigureInputMethods(); |
satok | 7f35c8c | 2010-10-07 21:13:11 +0900 | [diff] [blame] | 2128 | } |
| 2129 | }); |
| 2130 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2131 | mSwitchingDialog = mDialogBuilder.create(); |
Dianne Hackborn | e3a7f62 | 2011-03-03 21:48:24 -0800 | [diff] [blame] | 2132 | mSwitchingDialog.setCanceledOnTouchOutside(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2133 | mSwitchingDialog.getWindow().setType( |
| 2134 | WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG); |
Dianne Hackborn | e3a7f62 | 2011-03-03 21:48:24 -0800 | [diff] [blame] | 2135 | mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2136 | mSwitchingDialog.show(); |
| 2137 | } |
| 2138 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2139 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2140 | void hideInputMethodMenu() { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2141 | synchronized (mMethodMap) { |
| 2142 | hideInputMethodMenuLocked(); |
| 2143 | } |
| 2144 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2145 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2146 | void hideInputMethodMenuLocked() { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2147 | if (DEBUG) Slog.v(TAG, "Hide switching menu"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2148 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2149 | if (mSwitchingDialog != null) { |
| 2150 | mSwitchingDialog.dismiss(); |
| 2151 | mSwitchingDialog = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2152 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2153 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2154 | mDialogBuilder = null; |
| 2155 | mItems = null; |
| 2156 | mIms = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2157 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2158 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2159 | // ---------------------------------------------------------------------- |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2160 | |
satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 2161 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2162 | public boolean setInputMethodEnabled(String id, boolean enabled) { |
| 2163 | synchronized (mMethodMap) { |
| 2164 | if (mContext.checkCallingOrSelfPermission( |
| 2165 | android.Manifest.permission.WRITE_SECURE_SETTINGS) |
| 2166 | != PackageManager.PERMISSION_GRANTED) { |
| 2167 | throw new SecurityException( |
| 2168 | "Requires permission " |
| 2169 | + android.Manifest.permission.WRITE_SECURE_SETTINGS); |
| 2170 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 2171 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2172 | long ident = Binder.clearCallingIdentity(); |
| 2173 | try { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 2174 | return setInputMethodEnabledLocked(id, enabled); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2175 | } finally { |
| 2176 | Binder.restoreCallingIdentity(ident); |
| 2177 | } |
| 2178 | } |
| 2179 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 2180 | |
| 2181 | boolean setInputMethodEnabledLocked(String id, boolean enabled) { |
| 2182 | // Make sure this is a valid input method. |
| 2183 | InputMethodInfo imm = mMethodMap.get(id); |
| 2184 | if (imm == null) { |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2185 | throw new IllegalArgumentException("Unknown id: " + mCurMethodId); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 2186 | } |
| 2187 | |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2188 | List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings |
| 2189 | .getEnabledInputMethodsAndSubtypeListLocked(); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 2190 | |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2191 | if (enabled) { |
| 2192 | for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) { |
| 2193 | if (pair.first.equals(id)) { |
| 2194 | // We are enabling this input method, but it is already enabled. |
| 2195 | // Nothing to do. The previous state was enabled. |
| 2196 | return true; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 2197 | } |
| 2198 | } |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2199 | mSettings.appendAndPutEnabledInputMethodLocked(id, false); |
| 2200 | // Previous state was disabled. |
| 2201 | return false; |
| 2202 | } else { |
| 2203 | StringBuilder builder = new StringBuilder(); |
| 2204 | if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked( |
| 2205 | builder, enabledInputMethodsList, id)) { |
| 2206 | // Disabled input method is currently selected, switch to another one. |
| 2207 | String selId = Settings.Secure.getString(mContext.getContentResolver(), |
| 2208 | Settings.Secure.DEFAULT_INPUT_METHOD); |
satok | 03eb319a | 2010-11-11 18:17:42 +0900 | [diff] [blame] | 2209 | if (id.equals(selId) && !chooseNewDefaultIMELocked()) { |
| 2210 | Slog.i(TAG, "Can't find new IME, unsetting the current input method."); |
| 2211 | resetSelectedInputMethodAndSubtypeLocked(""); |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2212 | } |
| 2213 | // Previous state was enabled. |
| 2214 | return true; |
| 2215 | } else { |
| 2216 | // We are disabling the input method but it is already disabled. |
| 2217 | // Nothing to do. The previous state was disabled. |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 2218 | return false; |
| 2219 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 2220 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 2221 | } |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 2222 | |
satok | 57ffc00 | 2011-01-25 00:11:47 +0900 | [diff] [blame] | 2223 | private boolean canAddToLastInputMethod(InputMethodSubtype subtype) { |
| 2224 | if (subtype == null) return true; |
satok | 9b41579 | 2011-05-30 12:37:52 +0900 | [diff] [blame] | 2225 | return !subtype.isAuxiliary(); |
satok | 57ffc00 | 2011-01-25 00:11:47 +0900 | [diff] [blame] | 2226 | } |
| 2227 | |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 2228 | private void saveCurrentInputMethodAndSubtypeToHistory() { |
| 2229 | String subtypeId = NOT_A_SUBTYPE_ID_STR; |
| 2230 | if (mCurrentSubtype != null) { |
| 2231 | subtypeId = String.valueOf(mCurrentSubtype.hashCode()); |
| 2232 | } |
satok | 57ffc00 | 2011-01-25 00:11:47 +0900 | [diff] [blame] | 2233 | if (canAddToLastInputMethod(mCurrentSubtype)) { |
| 2234 | mSettings.addSubtypeToHistory(mCurMethodId, subtypeId); |
| 2235 | } |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 2236 | } |
| 2237 | |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 2238 | private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId, |
| 2239 | boolean setSubtypeOnly) { |
| 2240 | // Update the history of InputMethod and Subtype |
| 2241 | saveCurrentInputMethodAndSubtypeToHistory(); |
| 2242 | |
| 2243 | // Set Subtype here |
| 2244 | if (imi == null || subtypeId < 0) { |
| 2245 | mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID); |
Tadashi G. Takaoka | 0ba75bb | 2010-11-09 12:19:32 -0800 | [diff] [blame] | 2246 | mCurrentSubtype = null; |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 2247 | } else { |
Ken Wakasa | 586f051 | 2011-01-20 22:31:01 +0900 | [diff] [blame] | 2248 | if (subtypeId < imi.getSubtypeCount()) { |
| 2249 | InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId); |
| 2250 | mSettings.putSelectedSubtype(subtype.hashCode()); |
| 2251 | mCurrentSubtype = subtype; |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 2252 | } else { |
| 2253 | mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID); |
| 2254 | mCurrentSubtype = null; |
| 2255 | } |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 2256 | } |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 2257 | |
| 2258 | if (!setSubtypeOnly) { |
| 2259 | // Set InputMethod here |
| 2260 | mSettings.putSelectedInputMethod(imi != null ? imi.getId() : ""); |
| 2261 | } |
| 2262 | } |
| 2263 | |
| 2264 | private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) { |
| 2265 | InputMethodInfo imi = mMethodMap.get(newDefaultIme); |
| 2266 | int lastSubtypeId = NOT_A_SUBTYPE_ID; |
| 2267 | // newDefaultIme is empty when there is no candidate for the selected IME. |
| 2268 | if (imi != null && !TextUtils.isEmpty(newDefaultIme)) { |
| 2269 | String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme); |
| 2270 | if (subtypeHashCode != null) { |
| 2271 | try { |
| 2272 | lastSubtypeId = getSubtypeIdFromHashCode( |
| 2273 | imi, Integer.valueOf(subtypeHashCode)); |
| 2274 | } catch (NumberFormatException e) { |
| 2275 | Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e); |
| 2276 | } |
| 2277 | } |
| 2278 | } |
| 2279 | setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false); |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 2280 | } |
| 2281 | |
| 2282 | private int getSelectedInputMethodSubtypeId(String id) { |
| 2283 | InputMethodInfo imi = mMethodMap.get(id); |
| 2284 | if (imi == null) { |
| 2285 | return NOT_A_SUBTYPE_ID; |
| 2286 | } |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 2287 | int subtypeId; |
| 2288 | try { |
| 2289 | subtypeId = Settings.Secure.getInt(mContext.getContentResolver(), |
| 2290 | Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE); |
| 2291 | } catch (SettingNotFoundException e) { |
| 2292 | return NOT_A_SUBTYPE_ID; |
| 2293 | } |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 2294 | return getSubtypeIdFromHashCode(imi, subtypeId); |
| 2295 | } |
| 2296 | |
| 2297 | private int getSubtypeIdFromHashCode(InputMethodInfo imi, int subtypeHashCode) { |
satok | 2820351 | 2010-11-24 11:06:49 +0900 | [diff] [blame] | 2298 | if (imi != null) { |
Ken Wakasa | 586f051 | 2011-01-20 22:31:01 +0900 | [diff] [blame] | 2299 | final int subtypeCount = imi.getSubtypeCount(); |
| 2300 | for (int i = 0; i < subtypeCount; ++i) { |
| 2301 | InputMethodSubtype ims = imi.getSubtypeAt(i); |
satok | 2820351 | 2010-11-24 11:06:49 +0900 | [diff] [blame] | 2302 | if (subtypeHashCode == ims.hashCode()) { |
| 2303 | return i; |
| 2304 | } |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 2305 | } |
| 2306 | } |
| 2307 | return NOT_A_SUBTYPE_ID; |
| 2308 | } |
| 2309 | |
satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 2310 | private static ArrayList<InputMethodSubtype> getApplicableSubtypesLocked( |
| 2311 | Resources res, List<InputMethodSubtype> subtypes) { |
| 2312 | final String systemLocale = res.getConfiguration().locale.toString(); |
satok | 3da9223 | 2011-01-11 22:46:30 +0900 | [diff] [blame] | 2313 | if (TextUtils.isEmpty(systemLocale)) return new ArrayList<InputMethodSubtype>(); |
| 2314 | HashMap<String, InputMethodSubtype> applicableModeAndSubtypesMap = |
| 2315 | new HashMap<String, InputMethodSubtype>(); |
satok | 16331c8 | 2010-12-20 23:48:46 +0900 | [diff] [blame] | 2316 | final int N = subtypes.size(); |
| 2317 | boolean containsKeyboardSubtype = false; |
| 2318 | for (int i = 0; i < N; ++i) { |
| 2319 | InputMethodSubtype subtype = subtypes.get(i); |
satok | 3da9223 | 2011-01-11 22:46:30 +0900 | [diff] [blame] | 2320 | final String locale = subtype.getLocale(); |
| 2321 | final String mode = subtype.getMode(); |
| 2322 | // When system locale starts with subtype's locale, that subtype will be applicable |
| 2323 | // for system locale |
| 2324 | // For instance, it's clearly applicable for cases like system locale = en_US and |
| 2325 | // subtype = en, but it is not necessarily considered applicable for cases like system |
| 2326 | // locale = en and subtype = en_US. |
| 2327 | // We just call systemLocale.startsWith(locale) in this function because there is no |
| 2328 | // need to find applicable subtypes aggressively unlike |
| 2329 | // findLastResortApplicableSubtypeLocked. |
| 2330 | if (systemLocale.startsWith(locale)) { |
| 2331 | InputMethodSubtype applicableSubtype = applicableModeAndSubtypesMap.get(mode); |
| 2332 | // If more applicable subtypes are contained, skip. |
| 2333 | if (applicableSubtype != null |
| 2334 | && systemLocale.equals(applicableSubtype.getLocale())) continue; |
| 2335 | applicableModeAndSubtypesMap.put(mode, subtype); |
satok | 16331c8 | 2010-12-20 23:48:46 +0900 | [diff] [blame] | 2336 | if (!containsKeyboardSubtype |
| 2337 | && SUBTYPE_MODE_KEYBOARD.equalsIgnoreCase(subtype.getMode())) { |
| 2338 | containsKeyboardSubtype = true; |
| 2339 | } |
| 2340 | } |
| 2341 | } |
satok | 4a28bde | 2011-06-29 21:03:40 +0900 | [diff] [blame^] | 2342 | final ArrayList<InputMethodSubtype> applicableSubtypes = new ArrayList<InputMethodSubtype>( |
satok | 3da9223 | 2011-01-11 22:46:30 +0900 | [diff] [blame] | 2343 | applicableModeAndSubtypesMap.values()); |
satok | 16331c8 | 2010-12-20 23:48:46 +0900 | [diff] [blame] | 2344 | if (!containsKeyboardSubtype) { |
| 2345 | InputMethodSubtype lastResortKeyboardSubtype = findLastResortApplicableSubtypeLocked( |
satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 2346 | res, subtypes, SUBTYPE_MODE_KEYBOARD, systemLocale, true); |
satok | 16331c8 | 2010-12-20 23:48:46 +0900 | [diff] [blame] | 2347 | if (lastResortKeyboardSubtype != null) { |
| 2348 | applicableSubtypes.add(lastResortKeyboardSubtype); |
| 2349 | } |
| 2350 | } |
| 2351 | return applicableSubtypes; |
| 2352 | } |
| 2353 | |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2354 | /** |
| 2355 | * If there are no selected subtypes, tries finding the most applicable one according to the |
| 2356 | * given locale. |
| 2357 | * @param subtypes this function will search the most applicable subtype in subtypes |
| 2358 | * @param mode subtypes will be filtered by mode |
| 2359 | * @param locale subtypes will be filtered by locale |
satok | 7599a7f | 2010-12-22 13:45:23 +0900 | [diff] [blame] | 2360 | * @param canIgnoreLocaleAsLastResort if this function can't find the most applicable subtype, |
| 2361 | * it will return the first subtype matched with mode |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2362 | * @return the most applicable subtypeId |
| 2363 | */ |
satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 2364 | private static InputMethodSubtype findLastResortApplicableSubtypeLocked( |
| 2365 | Resources res, List<InputMethodSubtype> subtypes, String mode, String locale, |
satok | 7599a7f | 2010-12-22 13:45:23 +0900 | [diff] [blame] | 2366 | boolean canIgnoreLocaleAsLastResort) { |
satok | 8fbb1e8 | 2010-11-02 23:15:58 +0900 | [diff] [blame] | 2367 | if (subtypes == null || subtypes.size() == 0) { |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 2368 | return null; |
satok | 8fbb1e8 | 2010-11-02 23:15:58 +0900 | [diff] [blame] | 2369 | } |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2370 | if (TextUtils.isEmpty(locale)) { |
satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 2371 | locale = res.getConfiguration().locale.toString(); |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2372 | } |
satok | 8fbb1e8 | 2010-11-02 23:15:58 +0900 | [diff] [blame] | 2373 | final String language = locale.substring(0, 2); |
| 2374 | boolean partialMatchFound = false; |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 2375 | InputMethodSubtype applicableSubtype = null; |
satok | 7599a7f | 2010-12-22 13:45:23 +0900 | [diff] [blame] | 2376 | InputMethodSubtype firstMatchedModeSubtype = null; |
satok | 16331c8 | 2010-12-20 23:48:46 +0900 | [diff] [blame] | 2377 | final int N = subtypes.size(); |
| 2378 | for (int i = 0; i < N; ++i) { |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 2379 | InputMethodSubtype subtype = subtypes.get(i); |
| 2380 | final String subtypeLocale = subtype.getLocale(); |
satok | d871343 | 2011-01-18 00:55:13 +0900 | [diff] [blame] | 2381 | // An applicable subtype should match "mode". If mode is null, mode will be ignored, |
| 2382 | // and all subtypes with all modes can be candidates. |
| 2383 | if (mode == null || subtypes.get(i).getMode().equalsIgnoreCase(mode)) { |
satok | 7599a7f | 2010-12-22 13:45:23 +0900 | [diff] [blame] | 2384 | if (firstMatchedModeSubtype == null) { |
| 2385 | firstMatchedModeSubtype = subtype; |
| 2386 | } |
satok | 9ef0283 | 2010-11-04 21:17:48 +0900 | [diff] [blame] | 2387 | if (locale.equals(subtypeLocale)) { |
| 2388 | // Exact match (e.g. system locale is "en_US" and subtype locale is "en_US") |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 2389 | applicableSubtype = subtype; |
satok | 9ef0283 | 2010-11-04 21:17:48 +0900 | [diff] [blame] | 2390 | break; |
| 2391 | } else if (!partialMatchFound && subtypeLocale.startsWith(language)) { |
| 2392 | // Partial match (e.g. system locale is "en_US" and subtype locale is "en") |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 2393 | applicableSubtype = subtype; |
satok | 9ef0283 | 2010-11-04 21:17:48 +0900 | [diff] [blame] | 2394 | partialMatchFound = true; |
| 2395 | } |
satok | 8fbb1e8 | 2010-11-02 23:15:58 +0900 | [diff] [blame] | 2396 | } |
| 2397 | } |
| 2398 | |
satok | 7599a7f | 2010-12-22 13:45:23 +0900 | [diff] [blame] | 2399 | if (applicableSubtype == null && canIgnoreLocaleAsLastResort) { |
| 2400 | return firstMatchedModeSubtype; |
satok | 16331c8 | 2010-12-20 23:48:46 +0900 | [diff] [blame] | 2401 | } |
| 2402 | |
satok | 8fbb1e8 | 2010-11-02 23:15:58 +0900 | [diff] [blame] | 2403 | // The first subtype applicable to the system locale will be defined as the most applicable |
| 2404 | // subtype. |
| 2405 | if (DEBUG) { |
satok | 16331c8 | 2010-12-20 23:48:46 +0900 | [diff] [blame] | 2406 | if (applicableSubtype != null) { |
| 2407 | Slog.d(TAG, "Applicable InputMethodSubtype was found: " |
| 2408 | + applicableSubtype.getMode() + "," + applicableSubtype.getLocale()); |
| 2409 | } |
satok | 8fbb1e8 | 2010-11-02 23:15:58 +0900 | [diff] [blame] | 2410 | } |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 2411 | return applicableSubtype; |
satok | 8fbb1e8 | 2010-11-02 23:15:58 +0900 | [diff] [blame] | 2412 | } |
| 2413 | |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2414 | // If there are no selected shortcuts, tries finding the most applicable ones. |
| 2415 | private Pair<InputMethodInfo, InputMethodSubtype> |
| 2416 | findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) { |
| 2417 | List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked(); |
| 2418 | InputMethodInfo mostApplicableIMI = null; |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 2419 | InputMethodSubtype mostApplicableSubtype = null; |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2420 | boolean foundInSystemIME = false; |
| 2421 | |
| 2422 | // Search applicable subtype for each InputMethodInfo |
| 2423 | for (InputMethodInfo imi: imis) { |
satok | 7599a7f | 2010-12-22 13:45:23 +0900 | [diff] [blame] | 2424 | final String imiId = imi.getId(); |
| 2425 | if (foundInSystemIME && !imiId.equals(mCurMethodId)) { |
| 2426 | continue; |
| 2427 | } |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 2428 | InputMethodSubtype subtype = null; |
satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 2429 | final List<InputMethodSubtype> enabledSubtypes = |
| 2430 | getEnabledInputMethodSubtypeList(imi, true); |
| 2431 | // 1. Search by the current subtype's locale from enabledSubtypes. |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2432 | if (mCurrentSubtype != null) { |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 2433 | subtype = findLastResortApplicableSubtypeLocked( |
satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 2434 | mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false); |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2435 | } |
satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 2436 | // 2. Search by the system locale from enabledSubtypes. |
| 2437 | // 3. Search the first enabled subtype matched with mode from enabledSubtypes. |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 2438 | if (subtype == null) { |
| 2439 | subtype = findLastResortApplicableSubtypeLocked( |
satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 2440 | mRes, enabledSubtypes, mode, null, true); |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2441 | } |
satok | 7599a7f | 2010-12-22 13:45:23 +0900 | [diff] [blame] | 2442 | // 4. Search by the current subtype's locale from all subtypes. |
| 2443 | if (subtype == null && mCurrentSubtype != null) { |
| 2444 | subtype = findLastResortApplicableSubtypeLocked( |
Ken Wakasa | 586f051 | 2011-01-20 22:31:01 +0900 | [diff] [blame] | 2445 | mRes, getSubtypes(imi), mode, mCurrentSubtype.getLocale(), false); |
satok | 7599a7f | 2010-12-22 13:45:23 +0900 | [diff] [blame] | 2446 | } |
| 2447 | // 5. Search by the system locale from all subtypes. |
| 2448 | // 6. Search the first enabled subtype matched with mode from all subtypes. |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 2449 | if (subtype == null) { |
satok | 7599a7f | 2010-12-22 13:45:23 +0900 | [diff] [blame] | 2450 | subtype = findLastResortApplicableSubtypeLocked( |
Ken Wakasa | 586f051 | 2011-01-20 22:31:01 +0900 | [diff] [blame] | 2451 | mRes, getSubtypes(imi), mode, null, true); |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2452 | } |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 2453 | if (subtype != null) { |
satok | 7599a7f | 2010-12-22 13:45:23 +0900 | [diff] [blame] | 2454 | if (imiId.equals(mCurMethodId)) { |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2455 | // The current input method is the most applicable IME. |
| 2456 | mostApplicableIMI = imi; |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 2457 | mostApplicableSubtype = subtype; |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2458 | break; |
satok | 7599a7f | 2010-12-22 13:45:23 +0900 | [diff] [blame] | 2459 | } else if (!foundInSystemIME) { |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2460 | // The system input method is 2nd applicable IME. |
| 2461 | mostApplicableIMI = imi; |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 2462 | mostApplicableSubtype = subtype; |
satok | 7599a7f | 2010-12-22 13:45:23 +0900 | [diff] [blame] | 2463 | if ((imi.getServiceInfo().applicationInfo.flags |
| 2464 | & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 2465 | foundInSystemIME = true; |
| 2466 | } |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2467 | } |
| 2468 | } |
| 2469 | } |
| 2470 | if (DEBUG) { |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 2471 | if (mostApplicableIMI != null) { |
| 2472 | Slog.w(TAG, "Most applicable shortcut input method was:" |
| 2473 | + mostApplicableIMI.getId()); |
| 2474 | if (mostApplicableSubtype != null) { |
| 2475 | Slog.w(TAG, "Most applicable shortcut input method subtype was:" |
| 2476 | + "," + mostApplicableSubtype.getMode() + "," |
| 2477 | + mostApplicableSubtype.getLocale()); |
| 2478 | } |
| 2479 | } |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2480 | } |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 2481 | if (mostApplicableIMI != null) { |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2482 | return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI, |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 2483 | mostApplicableSubtype); |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2484 | } else { |
| 2485 | return null; |
| 2486 | } |
| 2487 | } |
| 2488 | |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 2489 | /** |
| 2490 | * @return Return the current subtype of this input method. |
| 2491 | */ |
satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 2492 | @Override |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 2493 | public InputMethodSubtype getCurrentInputMethodSubtype() { |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2494 | boolean subtypeIsSelected = false; |
| 2495 | try { |
| 2496 | subtypeIsSelected = Settings.Secure.getInt(mContext.getContentResolver(), |
| 2497 | Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE) != NOT_A_SUBTYPE_ID; |
| 2498 | } catch (SettingNotFoundException e) { |
| 2499 | } |
satok | 3ef8b29 | 2010-11-23 06:06:29 +0900 | [diff] [blame] | 2500 | synchronized (mMethodMap) { |
satok | 3ef8b29 | 2010-11-23 06:06:29 +0900 | [diff] [blame] | 2501 | if (!subtypeIsSelected || mCurrentSubtype == null) { |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2502 | String lastInputMethodId = Settings.Secure.getString( |
| 2503 | mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD); |
satok | 3ef8b29 | 2010-11-23 06:06:29 +0900 | [diff] [blame] | 2504 | int subtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId); |
| 2505 | if (subtypeId == NOT_A_SUBTYPE_ID) { |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2506 | InputMethodInfo imi = mMethodMap.get(lastInputMethodId); |
| 2507 | if (imi != null) { |
| 2508 | // If there are no selected subtypes, the framework will try to find |
satok | d871343 | 2011-01-18 00:55:13 +0900 | [diff] [blame] | 2509 | // the most applicable subtype from explicitly or implicitly enabled |
| 2510 | // subtypes. |
| 2511 | List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes = |
| 2512 | getEnabledInputMethodSubtypeList(imi, true); |
| 2513 | // If there is only one explicitly or implicitly enabled subtype, |
| 2514 | // just returns it. |
| 2515 | if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) { |
| 2516 | mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0); |
| 2517 | } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) { |
| 2518 | mCurrentSubtype = findLastResortApplicableSubtypeLocked( |
| 2519 | mRes, explicitlyOrImplicitlyEnabledSubtypes, |
| 2520 | SUBTYPE_MODE_KEYBOARD, null, true); |
| 2521 | if (mCurrentSubtype == null) { |
| 2522 | mCurrentSubtype = findLastResortApplicableSubtypeLocked( |
| 2523 | mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null, |
| 2524 | true); |
| 2525 | } |
| 2526 | } |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2527 | } |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 2528 | } else { |
satok | 3ef8b29 | 2010-11-23 06:06:29 +0900 | [diff] [blame] | 2529 | mCurrentSubtype = |
Ken Wakasa | 586f051 | 2011-01-20 22:31:01 +0900 | [diff] [blame] | 2530 | getSubtypes(mMethodMap.get(lastInputMethodId)).get(subtypeId); |
satok | 3ef8b29 | 2010-11-23 06:06:29 +0900 | [diff] [blame] | 2531 | } |
satok | 8fbb1e8 | 2010-11-02 23:15:58 +0900 | [diff] [blame] | 2532 | } |
satok | 3ef8b29 | 2010-11-23 06:06:29 +0900 | [diff] [blame] | 2533 | return mCurrentSubtype; |
satok | 8fbb1e8 | 2010-11-02 23:15:58 +0900 | [diff] [blame] | 2534 | } |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 2535 | } |
| 2536 | |
satok | f3db1af | 2010-11-23 13:34:33 +0900 | [diff] [blame] | 2537 | private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi, |
| 2538 | InputMethodSubtype subtype) { |
| 2539 | if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) { |
| 2540 | mShortcutInputMethodsAndSubtypes.get(imi).add(subtype); |
| 2541 | } else { |
| 2542 | ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>(); |
| 2543 | subtypes.add(subtype); |
| 2544 | mShortcutInputMethodsAndSubtypes.put(imi, subtypes); |
| 2545 | } |
| 2546 | } |
| 2547 | |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2548 | // TODO: We should change the return type from List to List<Parcelable> |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 2549 | @Override |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2550 | public List getShortcutInputMethodsAndSubtypes() { |
| 2551 | synchronized (mMethodMap) { |
satok | 3da9223 | 2011-01-11 22:46:30 +0900 | [diff] [blame] | 2552 | ArrayList<Object> ret = new ArrayList<Object>(); |
satok | f3db1af | 2010-11-23 13:34:33 +0900 | [diff] [blame] | 2553 | if (mShortcutInputMethodsAndSubtypes.size() == 0) { |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2554 | // If there are no selected shortcut subtypes, the framework will try to find |
| 2555 | // the most applicable subtype from all subtypes whose mode is |
| 2556 | // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode. |
satok | f3db1af | 2010-11-23 13:34:33 +0900 | [diff] [blame] | 2557 | Pair<InputMethodInfo, InputMethodSubtype> info = |
| 2558 | findLastResortApplicableShortcutInputMethodAndSubtypeLocked( |
| 2559 | SUBTYPE_MODE_VOICE); |
satok | 7599a7f | 2010-12-22 13:45:23 +0900 | [diff] [blame] | 2560 | if (info != null) { |
satok | 3da9223 | 2011-01-11 22:46:30 +0900 | [diff] [blame] | 2561 | ret.add(info.first); |
| 2562 | ret.add(info.second); |
satok | 7599a7f | 2010-12-22 13:45:23 +0900 | [diff] [blame] | 2563 | } |
satok | 3da9223 | 2011-01-11 22:46:30 +0900 | [diff] [blame] | 2564 | return ret; |
satok | f3db1af | 2010-11-23 13:34:33 +0900 | [diff] [blame] | 2565 | } |
satok | f3db1af | 2010-11-23 13:34:33 +0900 | [diff] [blame] | 2566 | for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) { |
| 2567 | ret.add(imi); |
| 2568 | for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) { |
| 2569 | ret.add(subtype); |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2570 | } |
| 2571 | } |
satok | f3db1af | 2010-11-23 13:34:33 +0900 | [diff] [blame] | 2572 | return ret; |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2573 | } |
| 2574 | } |
| 2575 | |
satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 2576 | @Override |
satok | b66d287 | 2010-11-10 01:04:04 +0900 | [diff] [blame] | 2577 | public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) { |
| 2578 | synchronized (mMethodMap) { |
| 2579 | if (subtype != null && mCurMethodId != null) { |
| 2580 | InputMethodInfo imi = mMethodMap.get(mCurMethodId); |
| 2581 | int subtypeId = getSubtypeIdFromHashCode(imi, subtype.hashCode()); |
| 2582 | if (subtypeId != NOT_A_SUBTYPE_ID) { |
| 2583 | setInputMethodLocked(mCurMethodId, subtypeId); |
| 2584 | return true; |
| 2585 | } |
| 2586 | } |
| 2587 | return false; |
| 2588 | } |
| 2589 | } |
| 2590 | |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2591 | /** |
| 2592 | * Utility class for putting and getting settings for InputMethod |
| 2593 | * TODO: Move all putters and getters of settings to this class. |
| 2594 | */ |
| 2595 | private static class InputMethodSettings { |
| 2596 | // The string for enabled input method is saved as follows: |
| 2597 | // example: ("ime0;subtype0;subtype1;subtype2:ime1:ime2;subtype0") |
| 2598 | private static final char INPUT_METHOD_SEPARATER = ':'; |
| 2599 | private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';'; |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 2600 | private final TextUtils.SimpleStringSplitter mInputMethodSplitter = |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2601 | new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER); |
| 2602 | |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 2603 | private final TextUtils.SimpleStringSplitter mSubtypeSplitter = |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2604 | new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATER); |
| 2605 | |
satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 2606 | private final Resources mRes; |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2607 | private final ContentResolver mResolver; |
| 2608 | private final HashMap<String, InputMethodInfo> mMethodMap; |
| 2609 | private final ArrayList<InputMethodInfo> mMethodList; |
| 2610 | |
| 2611 | private String mEnabledInputMethodsStrCache; |
| 2612 | |
| 2613 | private static void buildEnabledInputMethodsSettingString( |
| 2614 | StringBuilder builder, Pair<String, ArrayList<String>> pair) { |
| 2615 | String id = pair.first; |
| 2616 | ArrayList<String> subtypes = pair.second; |
| 2617 | builder.append(id); |
satok | 57c767c | 2010-11-01 22:34:08 +0900 | [diff] [blame] | 2618 | // Inputmethod and subtypes are saved in the settings as follows: |
| 2619 | // ime0;subtype0;subtype1:ime1;subtype0:ime2:ime3;subtype0;subtype1 |
| 2620 | for (String subtypeId: subtypes) { |
| 2621 | builder.append(INPUT_METHOD_SUBTYPE_SEPARATER).append(subtypeId); |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2622 | } |
| 2623 | } |
| 2624 | |
| 2625 | public InputMethodSettings( |
satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 2626 | Resources res, ContentResolver resolver, |
| 2627 | HashMap<String, InputMethodInfo> methodMap, ArrayList<InputMethodInfo> methodList) { |
| 2628 | mRes = res; |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2629 | mResolver = resolver; |
| 2630 | mMethodMap = methodMap; |
| 2631 | mMethodList = methodList; |
| 2632 | } |
| 2633 | |
| 2634 | public List<InputMethodInfo> getEnabledInputMethodListLocked() { |
| 2635 | return createEnabledInputMethodListLocked( |
| 2636 | getEnabledInputMethodsAndSubtypeListLocked()); |
| 2637 | } |
| 2638 | |
satok | 7f35c8c | 2010-10-07 21:13:11 +0900 | [diff] [blame] | 2639 | public List<Pair<InputMethodInfo, ArrayList<String>>> |
satok | 67ddf9c | 2010-11-17 09:45:54 +0900 | [diff] [blame] | 2640 | getEnabledInputMethodAndSubtypeHashCodeListLocked() { |
| 2641 | return createEnabledInputMethodAndSubtypeHashCodeListLocked( |
satok | 7f35c8c | 2010-10-07 21:13:11 +0900 | [diff] [blame] | 2642 | getEnabledInputMethodsAndSubtypeListLocked()); |
| 2643 | } |
| 2644 | |
satok | 67ddf9c | 2010-11-17 09:45:54 +0900 | [diff] [blame] | 2645 | public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked( |
| 2646 | InputMethodInfo imi) { |
| 2647 | List<Pair<String, ArrayList<String>>> imsList = |
| 2648 | getEnabledInputMethodsAndSubtypeListLocked(); |
| 2649 | ArrayList<InputMethodSubtype> enabledSubtypes = |
| 2650 | new ArrayList<InputMethodSubtype>(); |
satok | 884ef9a | 2010-11-18 10:39:46 +0900 | [diff] [blame] | 2651 | if (imi != null) { |
| 2652 | for (Pair<String, ArrayList<String>> imsPair : imsList) { |
| 2653 | InputMethodInfo info = mMethodMap.get(imsPair.first); |
| 2654 | if (info != null && info.getId().equals(imi.getId())) { |
Ken Wakasa | 586f051 | 2011-01-20 22:31:01 +0900 | [diff] [blame] | 2655 | final int subtypeCount = info.getSubtypeCount(); |
| 2656 | for (int i = 0; i < subtypeCount; ++i) { |
| 2657 | InputMethodSubtype ims = info.getSubtypeAt(i); |
satok | 884ef9a | 2010-11-18 10:39:46 +0900 | [diff] [blame] | 2658 | for (String s: imsPair.second) { |
| 2659 | if (String.valueOf(ims.hashCode()).equals(s)) { |
| 2660 | enabledSubtypes.add(ims); |
| 2661 | } |
satok | 67ddf9c | 2010-11-17 09:45:54 +0900 | [diff] [blame] | 2662 | } |
| 2663 | } |
satok | 884ef9a | 2010-11-18 10:39:46 +0900 | [diff] [blame] | 2664 | break; |
satok | 67ddf9c | 2010-11-17 09:45:54 +0900 | [diff] [blame] | 2665 | } |
satok | 67ddf9c | 2010-11-17 09:45:54 +0900 | [diff] [blame] | 2666 | } |
| 2667 | } |
| 2668 | return enabledSubtypes; |
| 2669 | } |
| 2670 | |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2671 | // At the initial boot, the settings for input methods are not set, |
| 2672 | // so we need to enable IME in that case. |
| 2673 | public void enableAllIMEsIfThereIsNoEnabledIME() { |
| 2674 | if (TextUtils.isEmpty(getEnabledInputMethodsStr())) { |
| 2675 | StringBuilder sb = new StringBuilder(); |
| 2676 | final int N = mMethodList.size(); |
| 2677 | for (int i = 0; i < N; i++) { |
| 2678 | InputMethodInfo imi = mMethodList.get(i); |
| 2679 | Slog.i(TAG, "Adding: " + imi.getId()); |
| 2680 | if (i > 0) sb.append(':'); |
| 2681 | sb.append(imi.getId()); |
| 2682 | } |
| 2683 | putEnabledInputMethodsStr(sb.toString()); |
| 2684 | } |
| 2685 | } |
| 2686 | |
satok | bb4aa06 | 2011-01-19 21:40:27 +0900 | [diff] [blame] | 2687 | private List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() { |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2688 | ArrayList<Pair<String, ArrayList<String>>> imsList |
| 2689 | = new ArrayList<Pair<String, ArrayList<String>>>(); |
| 2690 | final String enabledInputMethodsStr = getEnabledInputMethodsStr(); |
| 2691 | if (TextUtils.isEmpty(enabledInputMethodsStr)) { |
| 2692 | return imsList; |
| 2693 | } |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 2694 | mInputMethodSplitter.setString(enabledInputMethodsStr); |
| 2695 | while (mInputMethodSplitter.hasNext()) { |
| 2696 | String nextImsStr = mInputMethodSplitter.next(); |
| 2697 | mSubtypeSplitter.setString(nextImsStr); |
| 2698 | if (mSubtypeSplitter.hasNext()) { |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2699 | ArrayList<String> subtypeHashes = new ArrayList<String>(); |
| 2700 | // The first element is ime id. |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 2701 | String imeId = mSubtypeSplitter.next(); |
| 2702 | while (mSubtypeSplitter.hasNext()) { |
| 2703 | subtypeHashes.add(mSubtypeSplitter.next()); |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2704 | } |
| 2705 | imsList.add(new Pair<String, ArrayList<String>>(imeId, subtypeHashes)); |
| 2706 | } |
| 2707 | } |
| 2708 | return imsList; |
| 2709 | } |
| 2710 | |
| 2711 | public void appendAndPutEnabledInputMethodLocked(String id, boolean reloadInputMethodStr) { |
| 2712 | if (reloadInputMethodStr) { |
| 2713 | getEnabledInputMethodsStr(); |
| 2714 | } |
| 2715 | if (TextUtils.isEmpty(mEnabledInputMethodsStrCache)) { |
| 2716 | // Add in the newly enabled input method. |
| 2717 | putEnabledInputMethodsStr(id); |
| 2718 | } else { |
| 2719 | putEnabledInputMethodsStr( |
| 2720 | mEnabledInputMethodsStrCache + INPUT_METHOD_SEPARATER + id); |
| 2721 | } |
| 2722 | } |
| 2723 | |
| 2724 | /** |
| 2725 | * Build and put a string of EnabledInputMethods with removing specified Id. |
| 2726 | * @return the specified id was removed or not. |
| 2727 | */ |
| 2728 | public boolean buildAndPutEnabledInputMethodsStrRemovingIdLocked( |
| 2729 | StringBuilder builder, List<Pair<String, ArrayList<String>>> imsList, String id) { |
| 2730 | boolean isRemoved = false; |
| 2731 | boolean needsAppendSeparator = false; |
| 2732 | for (Pair<String, ArrayList<String>> ims: imsList) { |
| 2733 | String curId = ims.first; |
| 2734 | if (curId.equals(id)) { |
| 2735 | // We are disabling this input method, and it is |
| 2736 | // currently enabled. Skip it to remove from the |
| 2737 | // new list. |
| 2738 | isRemoved = true; |
| 2739 | } else { |
| 2740 | if (needsAppendSeparator) { |
| 2741 | builder.append(INPUT_METHOD_SEPARATER); |
| 2742 | } else { |
| 2743 | needsAppendSeparator = true; |
| 2744 | } |
| 2745 | buildEnabledInputMethodsSettingString(builder, ims); |
| 2746 | } |
| 2747 | } |
| 2748 | if (isRemoved) { |
| 2749 | // Update the setting with the new list of input methods. |
| 2750 | putEnabledInputMethodsStr(builder.toString()); |
| 2751 | } |
| 2752 | return isRemoved; |
| 2753 | } |
| 2754 | |
| 2755 | private List<InputMethodInfo> createEnabledInputMethodListLocked( |
| 2756 | List<Pair<String, ArrayList<String>>> imsList) { |
| 2757 | final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>(); |
| 2758 | for (Pair<String, ArrayList<String>> ims: imsList) { |
| 2759 | InputMethodInfo info = mMethodMap.get(ims.first); |
| 2760 | if (info != null) { |
| 2761 | res.add(info); |
| 2762 | } |
| 2763 | } |
| 2764 | return res; |
| 2765 | } |
| 2766 | |
satok | 7f35c8c | 2010-10-07 21:13:11 +0900 | [diff] [blame] | 2767 | private List<Pair<InputMethodInfo, ArrayList<String>>> |
satok | 67ddf9c | 2010-11-17 09:45:54 +0900 | [diff] [blame] | 2768 | createEnabledInputMethodAndSubtypeHashCodeListLocked( |
satok | 7f35c8c | 2010-10-07 21:13:11 +0900 | [diff] [blame] | 2769 | List<Pair<String, ArrayList<String>>> imsList) { |
| 2770 | final ArrayList<Pair<InputMethodInfo, ArrayList<String>>> res |
| 2771 | = new ArrayList<Pair<InputMethodInfo, ArrayList<String>>>(); |
| 2772 | for (Pair<String, ArrayList<String>> ims : imsList) { |
| 2773 | InputMethodInfo info = mMethodMap.get(ims.first); |
| 2774 | if (info != null) { |
| 2775 | res.add(new Pair<InputMethodInfo, ArrayList<String>>(info, ims.second)); |
| 2776 | } |
| 2777 | } |
| 2778 | return res; |
| 2779 | } |
| 2780 | |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2781 | private void putEnabledInputMethodsStr(String str) { |
| 2782 | Settings.Secure.putString(mResolver, Settings.Secure.ENABLED_INPUT_METHODS, str); |
| 2783 | mEnabledInputMethodsStrCache = str; |
| 2784 | } |
| 2785 | |
| 2786 | private String getEnabledInputMethodsStr() { |
| 2787 | mEnabledInputMethodsStrCache = Settings.Secure.getString( |
| 2788 | mResolver, Settings.Secure.ENABLED_INPUT_METHODS); |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 2789 | if (DEBUG) { |
| 2790 | Slog.d(TAG, "getEnabledInputMethodsStr: " + mEnabledInputMethodsStrCache); |
| 2791 | } |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2792 | return mEnabledInputMethodsStrCache; |
| 2793 | } |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 2794 | |
| 2795 | private void saveSubtypeHistory( |
| 2796 | List<Pair<String, String>> savedImes, String newImeId, String newSubtypeId) { |
| 2797 | StringBuilder builder = new StringBuilder(); |
| 2798 | boolean isImeAdded = false; |
| 2799 | if (!TextUtils.isEmpty(newImeId) && !TextUtils.isEmpty(newSubtypeId)) { |
| 2800 | builder.append(newImeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append( |
| 2801 | newSubtypeId); |
| 2802 | isImeAdded = true; |
| 2803 | } |
| 2804 | for (Pair<String, String> ime: savedImes) { |
| 2805 | String imeId = ime.first; |
| 2806 | String subtypeId = ime.second; |
| 2807 | if (TextUtils.isEmpty(subtypeId)) { |
| 2808 | subtypeId = NOT_A_SUBTYPE_ID_STR; |
| 2809 | } |
| 2810 | if (isImeAdded) { |
| 2811 | builder.append(INPUT_METHOD_SEPARATER); |
| 2812 | } else { |
| 2813 | isImeAdded = true; |
| 2814 | } |
| 2815 | builder.append(imeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append( |
| 2816 | subtypeId); |
| 2817 | } |
| 2818 | // Remove the last INPUT_METHOD_SEPARATER |
| 2819 | putSubtypeHistoryStr(builder.toString()); |
| 2820 | } |
| 2821 | |
| 2822 | public void addSubtypeToHistory(String imeId, String subtypeId) { |
| 2823 | List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked(); |
| 2824 | for (Pair<String, String> ime: subtypeHistory) { |
| 2825 | if (ime.first.equals(imeId)) { |
| 2826 | if (DEBUG) { |
satok | bb4aa06 | 2011-01-19 21:40:27 +0900 | [diff] [blame] | 2827 | Slog.v(TAG, "Subtype found in the history: " + imeId + ", " |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 2828 | + ime.second); |
| 2829 | } |
| 2830 | // We should break here |
| 2831 | subtypeHistory.remove(ime); |
| 2832 | break; |
| 2833 | } |
| 2834 | } |
satok | bb4aa06 | 2011-01-19 21:40:27 +0900 | [diff] [blame] | 2835 | if (DEBUG) { |
| 2836 | Slog.v(TAG, "Add subtype to the history: " + imeId + ", " + subtypeId); |
| 2837 | } |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 2838 | saveSubtypeHistory(subtypeHistory, imeId, subtypeId); |
| 2839 | } |
| 2840 | |
| 2841 | private void putSubtypeHistoryStr(String str) { |
| 2842 | if (DEBUG) { |
| 2843 | Slog.d(TAG, "putSubtypeHistoryStr: " + str); |
| 2844 | } |
| 2845 | Settings.Secure.putString( |
| 2846 | mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, str); |
| 2847 | } |
| 2848 | |
| 2849 | public Pair<String, String> getLastInputMethodAndSubtypeLocked() { |
| 2850 | // Gets the first one from the history |
| 2851 | return getLastSubtypeForInputMethodLockedInternal(null); |
| 2852 | } |
| 2853 | |
| 2854 | public String getLastSubtypeForInputMethodLocked(String imeId) { |
| 2855 | Pair<String, String> ime = getLastSubtypeForInputMethodLockedInternal(imeId); |
| 2856 | if (ime != null) { |
| 2857 | return ime.second; |
| 2858 | } else { |
| 2859 | return null; |
| 2860 | } |
| 2861 | } |
| 2862 | |
| 2863 | private Pair<String, String> getLastSubtypeForInputMethodLockedInternal(String imeId) { |
| 2864 | List<Pair<String, ArrayList<String>>> enabledImes = |
| 2865 | getEnabledInputMethodsAndSubtypeListLocked(); |
| 2866 | List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked(); |
satok | 4fc87d6 | 2011-05-20 16:13:43 +0900 | [diff] [blame] | 2867 | for (Pair<String, String> imeAndSubtype : subtypeHistory) { |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 2868 | final String imeInTheHistory = imeAndSubtype.first; |
| 2869 | // If imeId is empty, returns the first IME and subtype in the history |
| 2870 | if (TextUtils.isEmpty(imeId) || imeInTheHistory.equals(imeId)) { |
| 2871 | final String subtypeInTheHistory = imeAndSubtype.second; |
satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 2872 | final String subtypeHashCode = |
| 2873 | getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked( |
| 2874 | enabledImes, imeInTheHistory, subtypeInTheHistory); |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 2875 | if (!TextUtils.isEmpty(subtypeHashCode)) { |
| 2876 | if (DEBUG) { |
satok | bb4aa06 | 2011-01-19 21:40:27 +0900 | [diff] [blame] | 2877 | Slog.d(TAG, "Enabled subtype found in the history: " + subtypeHashCode); |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 2878 | } |
| 2879 | return new Pair<String, String>(imeInTheHistory, subtypeHashCode); |
| 2880 | } |
| 2881 | } |
| 2882 | } |
| 2883 | if (DEBUG) { |
| 2884 | Slog.d(TAG, "No enabled IME found in the history"); |
| 2885 | } |
| 2886 | return null; |
| 2887 | } |
| 2888 | |
satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 2889 | private String getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(List<Pair<String, |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 2890 | ArrayList<String>>> enabledImes, String imeId, String subtypeHashCode) { |
| 2891 | for (Pair<String, ArrayList<String>> enabledIme: enabledImes) { |
| 2892 | if (enabledIme.first.equals(imeId)) { |
satok | f6cafb6 | 2011-01-17 16:29:02 +0900 | [diff] [blame] | 2893 | final ArrayList<String> explicitlyEnabledSubtypes = enabledIme.second; |
| 2894 | if (explicitlyEnabledSubtypes.size() == 0) { |
| 2895 | // If there are no explicitly enabled subtypes, applicable subtypes are |
| 2896 | // enabled implicitly. |
satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 2897 | InputMethodInfo ime = mMethodMap.get(imeId); |
| 2898 | // If IME is enabled and no subtypes are enabled, applicable subtypes |
| 2899 | // are enabled implicitly, so needs to treat them to be enabled. |
Ken Wakasa | 586f051 | 2011-01-20 22:31:01 +0900 | [diff] [blame] | 2900 | if (ime != null && ime.getSubtypeCount() > 0) { |
satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 2901 | List<InputMethodSubtype> implicitlySelectedSubtypes = |
Ken Wakasa | 586f051 | 2011-01-20 22:31:01 +0900 | [diff] [blame] | 2902 | getApplicableSubtypesLocked(mRes, getSubtypes(ime)); |
satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 2903 | if (implicitlySelectedSubtypes != null) { |
| 2904 | final int N = implicitlySelectedSubtypes.size(); |
| 2905 | for (int i = 0; i < N; ++i) { |
| 2906 | final InputMethodSubtype st = implicitlySelectedSubtypes.get(i); |
| 2907 | if (String.valueOf(st.hashCode()).equals(subtypeHashCode)) { |
| 2908 | return subtypeHashCode; |
| 2909 | } |
| 2910 | } |
| 2911 | } |
| 2912 | } |
| 2913 | } else { |
satok | f6cafb6 | 2011-01-17 16:29:02 +0900 | [diff] [blame] | 2914 | for (String s: explicitlyEnabledSubtypes) { |
satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 2915 | if (s.equals(subtypeHashCode)) { |
| 2916 | // If both imeId and subtypeId are enabled, return subtypeId. |
| 2917 | return s; |
| 2918 | } |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 2919 | } |
| 2920 | } |
| 2921 | // If imeId was enabled but subtypeId was disabled. |
| 2922 | return NOT_A_SUBTYPE_ID_STR; |
| 2923 | } |
| 2924 | } |
| 2925 | // If both imeId and subtypeId are disabled, return null |
| 2926 | return null; |
| 2927 | } |
| 2928 | |
| 2929 | private List<Pair<String, String>> loadInputMethodAndSubtypeHistoryLocked() { |
| 2930 | ArrayList<Pair<String, String>> imsList = new ArrayList<Pair<String, String>>(); |
| 2931 | final String subtypeHistoryStr = getSubtypeHistoryStr(); |
| 2932 | if (TextUtils.isEmpty(subtypeHistoryStr)) { |
| 2933 | return imsList; |
| 2934 | } |
| 2935 | mInputMethodSplitter.setString(subtypeHistoryStr); |
| 2936 | while (mInputMethodSplitter.hasNext()) { |
| 2937 | String nextImsStr = mInputMethodSplitter.next(); |
| 2938 | mSubtypeSplitter.setString(nextImsStr); |
| 2939 | if (mSubtypeSplitter.hasNext()) { |
| 2940 | String subtypeId = NOT_A_SUBTYPE_ID_STR; |
| 2941 | // The first element is ime id. |
| 2942 | String imeId = mSubtypeSplitter.next(); |
| 2943 | while (mSubtypeSplitter.hasNext()) { |
| 2944 | subtypeId = mSubtypeSplitter.next(); |
| 2945 | break; |
| 2946 | } |
| 2947 | imsList.add(new Pair<String, String>(imeId, subtypeId)); |
| 2948 | } |
| 2949 | } |
| 2950 | return imsList; |
| 2951 | } |
| 2952 | |
| 2953 | private String getSubtypeHistoryStr() { |
| 2954 | if (DEBUG) { |
| 2955 | Slog.d(TAG, "getSubtypeHistoryStr: " + Settings.Secure.getString( |
| 2956 | mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY)); |
| 2957 | } |
| 2958 | return Settings.Secure.getString( |
| 2959 | mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY); |
| 2960 | } |
| 2961 | |
| 2962 | public void putSelectedInputMethod(String imeId) { |
| 2963 | Settings.Secure.putString(mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, imeId); |
| 2964 | } |
| 2965 | |
| 2966 | public void putSelectedSubtype(int subtypeId) { |
| 2967 | Settings.Secure.putInt( |
| 2968 | mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, subtypeId); |
| 2969 | } |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2970 | } |
| 2971 | |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 2972 | private static class InputMethodFileManager { |
| 2973 | private static final String SYSTEM_PATH = "system"; |
| 2974 | private static final String INPUT_METHOD_PATH = "inputmethod"; |
| 2975 | private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml"; |
| 2976 | private static final String NODE_SUBTYPES = "subtypes"; |
| 2977 | private static final String NODE_SUBTYPE = "subtype"; |
| 2978 | private static final String NODE_IMI = "imi"; |
| 2979 | private static final String ATTR_ID = "id"; |
| 2980 | private static final String ATTR_LABEL = "label"; |
| 2981 | private static final String ATTR_ICON = "icon"; |
| 2982 | private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale"; |
| 2983 | private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode"; |
| 2984 | private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue"; |
| 2985 | private static final String ATTR_IS_AUXILIARY = "isAuxiliary"; |
| 2986 | private final AtomicFile mAdditionalInputMethodSubtypeFile; |
| 2987 | private final HashMap<String, InputMethodInfo> mMethodMap; |
| 2988 | private final HashMap<String, List<InputMethodSubtype>> mSubtypesMap = |
| 2989 | new HashMap<String, List<InputMethodSubtype>>(); |
| 2990 | public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap) { |
| 2991 | if (methodMap == null) { |
| 2992 | throw new NullPointerException("methodMap is null"); |
| 2993 | } |
| 2994 | mMethodMap = methodMap; |
| 2995 | final File systemDir = new File(Environment.getDataDirectory(), SYSTEM_PATH); |
| 2996 | final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH); |
| 2997 | if (!inputMethodDir.mkdirs()) { |
| 2998 | Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath()); |
| 2999 | } |
| 3000 | final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME); |
| 3001 | mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile); |
| 3002 | if (!subtypeFile.exists()) { |
| 3003 | // If "subtypes.xml" doesn't exist, create a blank file. |
| 3004 | writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile, |
| 3005 | methodMap); |
| 3006 | } else { |
| 3007 | readAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile); |
| 3008 | } |
| 3009 | } |
| 3010 | |
| 3011 | private void deleteAllInputMethodSubtypes(String imiId) { |
| 3012 | synchronized (mMethodMap) { |
| 3013 | mSubtypesMap.remove(imiId); |
| 3014 | writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile, |
| 3015 | mMethodMap); |
| 3016 | } |
| 3017 | } |
| 3018 | |
| 3019 | public void addInputMethodSubtypes( |
satok | 4a28bde | 2011-06-29 21:03:40 +0900 | [diff] [blame^] | 3020 | InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) { |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 3021 | synchronized (mMethodMap) { |
satok | 4a28bde | 2011-06-29 21:03:40 +0900 | [diff] [blame^] | 3022 | final HashSet<InputMethodSubtype> existingSubtypes = |
| 3023 | new HashSet<InputMethodSubtype>(); |
| 3024 | for (int i = 0; i < imi.getSubtypeCount(); ++i) { |
| 3025 | existingSubtypes.add(imi.getSubtypeAt(i)); |
| 3026 | } |
| 3027 | |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 3028 | final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>(); |
| 3029 | final int N = additionalSubtypes.length; |
| 3030 | for (int i = 0; i < N; ++i) { |
| 3031 | final InputMethodSubtype subtype = additionalSubtypes[i]; |
satok | 4a28bde | 2011-06-29 21:03:40 +0900 | [diff] [blame^] | 3032 | if (!subtypes.contains(subtype) && !existingSubtypes.contains(subtype)) { |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 3033 | subtypes.add(subtype); |
| 3034 | } |
| 3035 | } |
satok | 4a28bde | 2011-06-29 21:03:40 +0900 | [diff] [blame^] | 3036 | mSubtypesMap.put(imi.getId(), subtypes); |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 3037 | writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile, |
| 3038 | mMethodMap); |
| 3039 | } |
| 3040 | } |
| 3041 | |
| 3042 | public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() { |
| 3043 | synchronized (mMethodMap) { |
| 3044 | return mSubtypesMap; |
| 3045 | } |
| 3046 | } |
| 3047 | |
| 3048 | private static void writeAdditionalInputMethodSubtypes( |
| 3049 | HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile, |
| 3050 | HashMap<String, InputMethodInfo> methodMap) { |
| 3051 | // Safety net for the case that this function is called before methodMap is set. |
| 3052 | final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0; |
| 3053 | FileOutputStream fos = null; |
| 3054 | try { |
| 3055 | fos = subtypesFile.startWrite(); |
| 3056 | final XmlSerializer out = new FastXmlSerializer(); |
| 3057 | out.setOutput(fos, "utf-8"); |
| 3058 | out.startDocument(null, true); |
| 3059 | out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); |
| 3060 | out.startTag(null, NODE_SUBTYPES); |
| 3061 | for (String imiId : allSubtypes.keySet()) { |
| 3062 | if (isSetMethodMap && !methodMap.containsKey(imiId)) { |
| 3063 | Slog.w(TAG, "IME uninstalled or not valid.: " + imiId); |
| 3064 | continue; |
| 3065 | } |
| 3066 | out.startTag(null, NODE_IMI); |
| 3067 | out.attribute(null, ATTR_ID, imiId); |
| 3068 | final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId); |
| 3069 | final int N = subtypesList.size(); |
| 3070 | for (int i = 0; i < N; ++i) { |
| 3071 | final InputMethodSubtype subtype = subtypesList.get(i); |
| 3072 | out.startTag(null, NODE_SUBTYPE); |
| 3073 | out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId())); |
| 3074 | out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId())); |
| 3075 | out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale()); |
| 3076 | out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode()); |
| 3077 | out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue()); |
| 3078 | out.attribute(null, ATTR_IS_AUXILIARY, |
| 3079 | String.valueOf(subtype.isAuxiliary() ? 1 : 0)); |
| 3080 | out.endTag(null, NODE_SUBTYPE); |
| 3081 | } |
| 3082 | out.endTag(null, NODE_IMI); |
| 3083 | } |
| 3084 | out.endTag(null, NODE_SUBTYPES); |
| 3085 | out.endDocument(); |
| 3086 | subtypesFile.finishWrite(fos); |
| 3087 | } catch (java.io.IOException e) { |
| 3088 | Slog.w(TAG, "Error writing subtypes", e); |
| 3089 | if (fos != null) { |
| 3090 | subtypesFile.failWrite(fos); |
| 3091 | } |
| 3092 | } |
| 3093 | } |
| 3094 | |
| 3095 | private static void readAdditionalInputMethodSubtypes( |
| 3096 | HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) { |
| 3097 | if (allSubtypes == null || subtypesFile == null) return; |
| 3098 | allSubtypes.clear(); |
| 3099 | FileInputStream fis = null; |
| 3100 | try { |
| 3101 | fis = subtypesFile.openRead(); |
| 3102 | final XmlPullParser parser = Xml.newPullParser(); |
| 3103 | parser.setInput(fis, null); |
| 3104 | int type = parser.getEventType(); |
| 3105 | // Skip parsing until START_TAG |
| 3106 | while ((type = parser.next()) != XmlPullParser.START_TAG |
| 3107 | && type != XmlPullParser.END_DOCUMENT) {} |
| 3108 | String firstNodeName = parser.getName(); |
| 3109 | if (!NODE_SUBTYPES.equals(firstNodeName)) { |
| 3110 | throw new XmlPullParserException("Xml doesn't start with subtypes"); |
| 3111 | } |
| 3112 | final int depth =parser.getDepth(); |
| 3113 | String currentImiId = null; |
| 3114 | ArrayList<InputMethodSubtype> tempSubtypesArray = null; |
| 3115 | while (((type = parser.next()) != XmlPullParser.END_TAG |
| 3116 | || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) { |
| 3117 | if (type != XmlPullParser.START_TAG) |
| 3118 | continue; |
| 3119 | final String nodeName = parser.getName(); |
| 3120 | if (NODE_IMI.equals(nodeName)) { |
| 3121 | currentImiId = parser.getAttributeValue(null, ATTR_ID); |
| 3122 | if (TextUtils.isEmpty(currentImiId)) { |
| 3123 | Slog.w(TAG, "Invalid imi id found in subtypes.xml"); |
| 3124 | continue; |
| 3125 | } |
| 3126 | tempSubtypesArray = new ArrayList<InputMethodSubtype>(); |
| 3127 | allSubtypes.put(currentImiId, tempSubtypesArray); |
| 3128 | } else if (NODE_SUBTYPE.equals(nodeName)) { |
| 3129 | if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) { |
| 3130 | Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId); |
| 3131 | continue; |
| 3132 | } |
| 3133 | final int icon = Integer.valueOf( |
| 3134 | parser.getAttributeValue(null, ATTR_ICON)); |
| 3135 | final int label = Integer.valueOf( |
| 3136 | parser.getAttributeValue(null, ATTR_LABEL)); |
| 3137 | final String imeSubtypeLocale = |
| 3138 | parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE); |
| 3139 | final String imeSubtypeMode = |
| 3140 | parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE); |
| 3141 | final String imeSubtypeExtraValue = |
| 3142 | parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE); |
satok | 4a28bde | 2011-06-29 21:03:40 +0900 | [diff] [blame^] | 3143 | final boolean isAuxiliary = "1".equals(String.valueOf( |
| 3144 | parser.getAttributeValue(null, ATTR_IS_AUXILIARY))); |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 3145 | final InputMethodSubtype subtype = |
| 3146 | new InputMethodSubtype(label, icon, imeSubtypeLocale, |
| 3147 | imeSubtypeMode, imeSubtypeExtraValue, isAuxiliary); |
| 3148 | tempSubtypesArray.add(subtype); |
| 3149 | } |
| 3150 | } |
| 3151 | } catch (XmlPullParserException e) { |
| 3152 | Slog.w(TAG, "Error reading subtypes: " + e); |
| 3153 | return; |
| 3154 | } catch (java.io.IOException e) { |
| 3155 | Slog.w(TAG, "Error reading subtypes: " + e); |
| 3156 | return; |
| 3157 | } catch (NumberFormatException e) { |
| 3158 | Slog.w(TAG, "Error reading subtypes: " + e); |
| 3159 | return; |
| 3160 | } finally { |
| 3161 | if (fis != null) { |
| 3162 | try { |
| 3163 | fis.close(); |
| 3164 | } catch (java.io.IOException e1) { |
| 3165 | Slog.w(TAG, "Failed to close."); |
| 3166 | } |
| 3167 | } |
| 3168 | } |
| 3169 | } |
| 3170 | } |
| 3171 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3172 | // ---------------------------------------------------------------------- |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3173 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3174 | @Override |
| 3175 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 3176 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 3177 | != PackageManager.PERMISSION_GRANTED) { |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3178 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3179 | pw.println("Permission Denial: can't dump InputMethodManager from from pid=" |
| 3180 | + Binder.getCallingPid() |
| 3181 | + ", uid=" + Binder.getCallingUid()); |
| 3182 | return; |
| 3183 | } |
| 3184 | |
| 3185 | IInputMethod method; |
| 3186 | ClientState client; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3187 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3188 | final Printer p = new PrintWriterPrinter(pw); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3189 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3190 | synchronized (mMethodMap) { |
| 3191 | p.println("Current Input Method Manager state:"); |
| 3192 | int N = mMethodList.size(); |
| 3193 | p.println(" Input Methods:"); |
| 3194 | for (int i=0; i<N; i++) { |
| 3195 | InputMethodInfo info = mMethodList.get(i); |
| 3196 | p.println(" InputMethod #" + i + ":"); |
| 3197 | info.dump(p, " "); |
| 3198 | } |
| 3199 | p.println(" Clients:"); |
| 3200 | for (ClientState ci : mClients.values()) { |
| 3201 | p.println(" Client " + ci + ":"); |
| 3202 | p.println(" client=" + ci.client); |
| 3203 | p.println(" inputContext=" + ci.inputContext); |
| 3204 | p.println(" sessionRequested=" + ci.sessionRequested); |
| 3205 | p.println(" curSession=" + ci.curSession); |
| 3206 | } |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 3207 | p.println(" mCurMethodId=" + mCurMethodId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3208 | client = mCurClient; |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 3209 | p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq); |
| 3210 | p.println(" mCurFocusedWindow=" + mCurFocusedWindow); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3211 | p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection |
| 3212 | + " mBoundToMethod=" + mBoundToMethod); |
| 3213 | p.println(" mCurToken=" + mCurToken); |
| 3214 | p.println(" mCurIntent=" + mCurIntent); |
| 3215 | method = mCurMethod; |
| 3216 | p.println(" mCurMethod=" + mCurMethod); |
| 3217 | p.println(" mEnabledSession=" + mEnabledSession); |
| 3218 | p.println(" mShowRequested=" + mShowRequested |
| 3219 | + " mShowExplicitlyRequested=" + mShowExplicitlyRequested |
| 3220 | + " mShowForced=" + mShowForced |
| 3221 | + " mInputShown=" + mInputShown); |
Dianne Hackborn | cc27870 | 2009-09-02 23:07:23 -0700 | [diff] [blame] | 3222 | p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3223 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3224 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 3225 | p.println(" "); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3226 | if (client != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3227 | pw.flush(); |
| 3228 | try { |
| 3229 | client.client.asBinder().dump(fd, args); |
| 3230 | } catch (RemoteException e) { |
| 3231 | p.println("Input method client dead: " + e); |
| 3232 | } |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 3233 | } else { |
| 3234 | p.println("No input method client."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3235 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3236 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 3237 | p.println(" "); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3238 | if (method != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3239 | pw.flush(); |
| 3240 | try { |
| 3241 | method.asBinder().dump(fd, args); |
| 3242 | } catch (RemoteException e) { |
| 3243 | p.println("Input method service dead: " + e); |
| 3244 | } |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 3245 | } else { |
| 3246 | p.println("No input method service."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3247 | } |
| 3248 | } |
| 3249 | } |