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