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