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 |
satok | 9747f89 | 2011-09-12 15:56:40 +0900 | [diff] [blame] | 1118 | && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale()) |
| 1119 | || auxSubtype.overridesImplicitlyEnabledSubtype() |
| 1120 | || nonAuxSubtype.overridesImplicitlyEnabledSubtype()) |
satok | b635941 | 2011-06-28 17:47:41 +0900 | [diff] [blame] | 1121 | && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) { |
| 1122 | return false; |
| 1123 | } |
| 1124 | return true; |
| 1125 | } |
| 1126 | return false; |
satok | 7cfc0ed | 2011-06-20 21:29:36 +0900 | [diff] [blame] | 1127 | } |
satok | 7cfc0ed | 2011-06-20 21:29:36 +0900 | [diff] [blame] | 1128 | } |
| 1129 | |
satok | dbf2950 | 2011-08-25 15:28:23 +0900 | [diff] [blame] | 1130 | @SuppressWarnings("deprecation") |
satok | f9f0100 | 2011-05-19 21:31:50 +0900 | [diff] [blame] | 1131 | @Override |
Joe Onorato | 857fd9b | 2011-01-27 15:08:35 -0800 | [diff] [blame] | 1132 | public void setImeWindowStatus(IBinder token, int vis, int backDisposition) { |
satok | 06487a5 | 2010-10-29 11:37:18 +0900 | [diff] [blame] | 1133 | int uid = Binder.getCallingUid(); |
| 1134 | long ident = Binder.clearCallingIdentity(); |
| 1135 | try { |
| 1136 | if (token == null || mCurToken != token) { |
Joe Onorato | 857fd9b | 2011-01-27 15:08:35 -0800 | [diff] [blame] | 1137 | Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token); |
satok | 06487a5 | 2010-10-29 11:37:18 +0900 | [diff] [blame] | 1138 | return; |
| 1139 | } |
| 1140 | |
| 1141 | synchronized (mMethodMap) { |
Joe Onorato | 857fd9b | 2011-01-27 15:08:35 -0800 | [diff] [blame] | 1142 | mImeWindowVis = vis; |
| 1143 | mBackDisposition = backDisposition; |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 1144 | if (mStatusBar != null) { |
| 1145 | mStatusBar.setImeWindowStatus(token, vis, backDisposition); |
| 1146 | } |
satok | 7cfc0ed | 2011-06-20 21:29:36 +0900 | [diff] [blame] | 1147 | final boolean iconVisibility = (vis & InputMethodService.IME_ACTIVE) != 0; |
satok | 5bc8e73 | 2011-07-22 21:07:23 +0900 | [diff] [blame] | 1148 | final InputMethodInfo imi = mMethodMap.get(mCurMethodId); |
| 1149 | if (imi != null && iconVisibility && needsToShowImeSwitchOngoingNotification()) { |
satok | 7cfc0ed | 2011-06-20 21:29:36 +0900 | [diff] [blame] | 1150 | final PackageManager pm = mContext.getPackageManager(); |
satok | 7cfc0ed | 2011-06-20 21:29:36 +0900 | [diff] [blame] | 1151 | final CharSequence title = mRes.getText( |
| 1152 | com.android.internal.R.string.select_input_method); |
satok | 5bc8e73 | 2011-07-22 21:07:23 +0900 | [diff] [blame] | 1153 | final CharSequence imiLabel = imi.loadLabel(pm); |
| 1154 | final CharSequence summary = mCurrentSubtype != null |
| 1155 | ? TextUtils.concat(mCurrentSubtype.getDisplayName(mContext, |
| 1156 | imi.getPackageName(), imi.getServiceInfo().applicationInfo), |
| 1157 | (TextUtils.isEmpty(imiLabel) ? |
Ken Wakasa | 05dbb65 | 2011-08-22 15:22:43 +0900 | [diff] [blame] | 1158 | "" : " - " + imiLabel)) |
satok | 5bc8e73 | 2011-07-22 21:07:23 +0900 | [diff] [blame] | 1159 | : imiLabel; |
| 1160 | |
satok | 7cfc0ed | 2011-06-20 21:29:36 +0900 | [diff] [blame] | 1161 | mImeSwitcherNotification.setLatestEventInfo( |
satok | 5bc8e73 | 2011-07-22 21:07:23 +0900 | [diff] [blame] | 1162 | mContext, title, summary, mImeSwitchPendingIntent); |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 1163 | if (mNotificationManager != null) { |
| 1164 | mNotificationManager.notify( |
| 1165 | com.android.internal.R.string.select_input_method, |
| 1166 | mImeSwitcherNotification); |
| 1167 | mNotificationShown = true; |
| 1168 | } |
satok | 7cfc0ed | 2011-06-20 21:29:36 +0900 | [diff] [blame] | 1169 | } else { |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 1170 | if (mNotificationShown && mNotificationManager != null) { |
satok | 7cfc0ed | 2011-06-20 21:29:36 +0900 | [diff] [blame] | 1171 | mNotificationManager.cancel( |
| 1172 | com.android.internal.R.string.select_input_method); |
| 1173 | mNotificationShown = false; |
| 1174 | } |
| 1175 | } |
satok | 06487a5 | 2010-10-29 11:37:18 +0900 | [diff] [blame] | 1176 | } |
| 1177 | } finally { |
| 1178 | Binder.restoreCallingIdentity(ident); |
| 1179 | } |
| 1180 | } |
| 1181 | |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 1182 | @Override |
satok | f9f0100 | 2011-05-19 21:31:50 +0900 | [diff] [blame] | 1183 | public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) { |
| 1184 | synchronized (mMethodMap) { |
| 1185 | final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId); |
| 1186 | for (int i = 0; i < spans.length; ++i) { |
| 1187 | SuggestionSpan ss = spans[i]; |
satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 1188 | if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) { |
satok | f9f0100 | 2011-05-19 21:31:50 +0900 | [diff] [blame] | 1189 | mSecureSuggestionSpans.put(ss, currentImi); |
satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 1190 | final InputMethodInfo targetImi = mSecureSuggestionSpans.get(ss); |
satok | f9f0100 | 2011-05-19 21:31:50 +0900 | [diff] [blame] | 1191 | } |
| 1192 | } |
| 1193 | } |
| 1194 | } |
| 1195 | |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 1196 | @Override |
satok | f9f0100 | 2011-05-19 21:31:50 +0900 | [diff] [blame] | 1197 | public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) { |
| 1198 | synchronized (mMethodMap) { |
| 1199 | final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span); |
| 1200 | // TODO: Do not send the intent if the process of the targetImi is already dead. |
| 1201 | if (targetImi != null) { |
| 1202 | final String[] suggestions = span.getSuggestions(); |
| 1203 | if (index < 0 || index >= suggestions.length) return false; |
satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 1204 | final String className = span.getNotificationTargetClassName(); |
satok | f9f0100 | 2011-05-19 21:31:50 +0900 | [diff] [blame] | 1205 | final Intent intent = new Intent(); |
| 1206 | // Ensures that only a class in the original IME package will receive the |
| 1207 | // notification. |
satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 1208 | intent.setClassName(targetImi.getPackageName(), className); |
satok | f9f0100 | 2011-05-19 21:31:50 +0900 | [diff] [blame] | 1209 | intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED); |
| 1210 | intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString); |
| 1211 | intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]); |
| 1212 | intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode()); |
| 1213 | mContext.sendBroadcast(intent); |
| 1214 | return true; |
| 1215 | } |
| 1216 | } |
| 1217 | return false; |
| 1218 | } |
| 1219 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1220 | void updateFromSettingsLocked() { |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1221 | // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and |
| 1222 | // ENABLED_INPUT_METHODS is taking care of keeping them correctly in |
| 1223 | // sync, so we will never have a DEFAULT_INPUT_METHOD that is not |
| 1224 | // enabled. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1225 | String id = Settings.Secure.getString(mContext.getContentResolver(), |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1226 | Settings.Secure.DEFAULT_INPUT_METHOD); |
satok | 03eb319a | 2010-11-11 18:17:42 +0900 | [diff] [blame] | 1227 | // There is no input method selected, try to choose new applicable input method. |
| 1228 | if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) { |
| 1229 | id = Settings.Secure.getString(mContext.getContentResolver(), |
| 1230 | Settings.Secure.DEFAULT_INPUT_METHOD); |
| 1231 | } |
| 1232 | if (!TextUtils.isEmpty(id)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1233 | try { |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1234 | setInputMethodLocked(id, getSelectedInputMethodSubtypeId(id)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1235 | } catch (IllegalArgumentException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1236 | 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] | 1237 | mCurMethodId = null; |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1238 | unbindCurrentMethodLocked(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1239 | } |
satok | f3db1af | 2010-11-23 13:34:33 +0900 | [diff] [blame] | 1240 | mShortcutInputMethodsAndSubtypes.clear(); |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1241 | } else { |
| 1242 | // 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] | 1243 | mCurMethodId = null; |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1244 | unbindCurrentMethodLocked(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1245 | } |
| 1246 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1247 | |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1248 | /* package */ void setInputMethodLocked(String id, int subtypeId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1249 | InputMethodInfo info = mMethodMap.get(id); |
| 1250 | if (info == null) { |
satok | 913a892 | 2010-08-26 21:53:41 +0900 | [diff] [blame] | 1251 | throw new IllegalArgumentException("Unknown id: " + id); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1252 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1253 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1254 | if (id.equals(mCurMethodId)) { |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 1255 | InputMethodSubtype subtype = null; |
Ken Wakasa | 586f051 | 2011-01-20 22:31:01 +0900 | [diff] [blame] | 1256 | if (subtypeId >= 0 && subtypeId < info.getSubtypeCount()) { |
| 1257 | subtype = info.getSubtypeAt(subtypeId); |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 1258 | } |
| 1259 | if (subtype != mCurrentSubtype) { |
| 1260 | synchronized (mMethodMap) { |
satok | ca83021 | 2011-01-13 21:15:04 +0900 | [diff] [blame] | 1261 | if (subtype != null) { |
| 1262 | setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true); |
| 1263 | } |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 1264 | if (mCurMethod != null) { |
| 1265 | try { |
satok | e40dea0 | 2011-01-30 01:14:02 +0900 | [diff] [blame] | 1266 | final Configuration conf = mRes.getConfiguration(); |
| 1267 | final boolean haveHardKeyboard = conf.keyboard |
| 1268 | != Configuration.KEYBOARD_NOKEYS; |
| 1269 | final boolean hardKeyShown = haveHardKeyboard |
Ken Wakasa | 8710e76 | 2011-01-30 11:02:09 +0900 | [diff] [blame] | 1270 | && conf.hardKeyboardHidden |
| 1271 | != Configuration.HARDKEYBOARDHIDDEN_YES; |
satok | e40dea0 | 2011-01-30 01:14:02 +0900 | [diff] [blame] | 1272 | mImeWindowVis = (mInputShown || hardKeyShown) ? ( |
| 1273 | InputMethodService.IME_ACTIVE | InputMethodService.IME_VISIBLE) |
| 1274 | : 0; |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 1275 | updateImeWindowStatusLocked(); |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 1276 | // If subtype is null, try to find the most applicable one from |
| 1277 | // getCurrentInputMethodSubtype. |
| 1278 | if (subtype == null) { |
| 1279 | subtype = getCurrentInputMethodSubtype(); |
| 1280 | } |
| 1281 | mCurMethod.changeInputMethodSubtype(subtype); |
| 1282 | } catch (RemoteException e) { |
| 1283 | return; |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1284 | } |
| 1285 | } |
| 1286 | } |
| 1287 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1288 | return; |
| 1289 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1290 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1291 | final long ident = Binder.clearCallingIdentity(); |
| 1292 | try { |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1293 | // Set a subtype to this input method. |
| 1294 | // subtypeId the name of a subtype which will be set. |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 1295 | setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false); |
| 1296 | // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked() |
| 1297 | // because mCurMethodId is stored as a history in |
| 1298 | // setSelectedInputMethodAndSubtypeLocked(). |
| 1299 | mCurMethodId = id; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1300 | |
| 1301 | if (ActivityManagerNative.isSystemReady()) { |
| 1302 | Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 1303 | intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1304 | intent.putExtra("input_method_id", id); |
| 1305 | mContext.sendBroadcast(intent); |
| 1306 | } |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1307 | unbindCurrentClientLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1308 | } finally { |
| 1309 | Binder.restoreCallingIdentity(ident); |
| 1310 | } |
| 1311 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1312 | |
satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 1313 | @Override |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1314 | public boolean showSoftInput(IInputMethodClient client, int flags, |
| 1315 | ResultReceiver resultReceiver) { |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 1316 | int uid = Binder.getCallingUid(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1317 | long ident = Binder.clearCallingIdentity(); |
| 1318 | try { |
| 1319 | synchronized (mMethodMap) { |
| 1320 | if (mCurClient == null || client == null |
| 1321 | || mCurClient.client.asBinder() != client.asBinder()) { |
| 1322 | try { |
| 1323 | // We need to check if this is the current client with |
| 1324 | // focus in the window manager, to allow this call to |
| 1325 | // be made before input is started in it. |
| 1326 | if (!mIWindowManager.inputMethodClientHasFocus(client)) { |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 1327 | Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client); |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1328 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1329 | } |
| 1330 | } catch (RemoteException e) { |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1331 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1332 | } |
| 1333 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1334 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1335 | 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] | 1336 | return showCurrentInputLocked(flags, resultReceiver); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1337 | } |
| 1338 | } finally { |
| 1339 | Binder.restoreCallingIdentity(ident); |
| 1340 | } |
| 1341 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1342 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1343 | boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1344 | mShowRequested = true; |
| 1345 | if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) { |
| 1346 | mShowExplicitlyRequested = true; |
| 1347 | } |
| 1348 | if ((flags&InputMethodManager.SHOW_FORCED) != 0) { |
| 1349 | mShowExplicitlyRequested = true; |
| 1350 | mShowForced = true; |
| 1351 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1352 | |
Dianne Hackborn | cc27870 | 2009-09-02 23:07:23 -0700 | [diff] [blame] | 1353 | if (!mSystemReady) { |
| 1354 | return false; |
| 1355 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1356 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1357 | boolean res = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1358 | if (mCurMethod != null) { |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1359 | executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO( |
| 1360 | MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod, |
| 1361 | resultReceiver)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1362 | mInputShown = true; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1363 | res = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1364 | } else if (mHaveConnection && SystemClock.uptimeMillis() |
satok | 59b424c | 2011-09-30 17:21:46 +0900 | [diff] [blame^] | 1365 | >= (mLastBindTime+TIME_TO_RECONNECT)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1366 | // The client has asked to have the input method shown, but |
| 1367 | // we have been sitting here too long with a connection to the |
| 1368 | // service and no interface received, so let's disconnect/connect |
| 1369 | // to try to prod things along. |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1370 | EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1371 | SystemClock.uptimeMillis()-mLastBindTime,1); |
satok | 59b424c | 2011-09-30 17:21:46 +0900 | [diff] [blame^] | 1372 | Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1373 | mContext.unbindService(this); |
| 1374 | mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE); |
| 1375 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1376 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1377 | return res; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1378 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1379 | |
satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 1380 | @Override |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1381 | public boolean hideSoftInput(IInputMethodClient client, int flags, |
| 1382 | ResultReceiver resultReceiver) { |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 1383 | int uid = Binder.getCallingUid(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1384 | long ident = Binder.clearCallingIdentity(); |
| 1385 | try { |
| 1386 | synchronized (mMethodMap) { |
| 1387 | if (mCurClient == null || client == null |
| 1388 | || mCurClient.client.asBinder() != client.asBinder()) { |
| 1389 | try { |
| 1390 | // We need to check if this is the current client with |
| 1391 | // focus in the window manager, to allow this call to |
| 1392 | // be made before input is started in it. |
| 1393 | if (!mIWindowManager.inputMethodClientHasFocus(client)) { |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 1394 | if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid " |
| 1395 | + uid + ": " + client); |
Joe Onorato | 857fd9b | 2011-01-27 15:08:35 -0800 | [diff] [blame] | 1396 | mImeWindowVis = 0; |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 1397 | updateImeWindowStatusLocked(); |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1398 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1399 | } |
| 1400 | } catch (RemoteException e) { |
Joe Onorato | 857fd9b | 2011-01-27 15:08:35 -0800 | [diff] [blame] | 1401 | mImeWindowVis = 0; |
Dianne Hackborn | 661cd52 | 2011-08-22 00:26:20 -0700 | [diff] [blame] | 1402 | updateImeWindowStatusLocked(); |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1403 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1404 | } |
| 1405 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1406 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1407 | 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] | 1408 | return hideCurrentInputLocked(flags, resultReceiver); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1409 | } |
| 1410 | } finally { |
| 1411 | Binder.restoreCallingIdentity(ident); |
| 1412 | } |
| 1413 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1414 | |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1415 | boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1416 | if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0 |
| 1417 | && (mShowExplicitlyRequested || mShowForced)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1418 | if (DEBUG) Slog.v(TAG, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1419 | "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] | 1420 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1421 | } |
| 1422 | if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1423 | if (DEBUG) Slog.v(TAG, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1424 | "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] | 1425 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1426 | } |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1427 | boolean res; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1428 | if (mInputShown && mCurMethod != null) { |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1429 | executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO( |
| 1430 | MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver)); |
| 1431 | res = true; |
| 1432 | } else { |
| 1433 | res = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1434 | } |
| 1435 | mInputShown = false; |
| 1436 | mShowRequested = false; |
| 1437 | mShowExplicitlyRequested = false; |
| 1438 | mShowForced = false; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1439 | return res; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1440 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1441 | |
satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 1442 | @Override |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1443 | public void windowGainedFocus(IInputMethodClient client, IBinder windowToken, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1444 | boolean viewHasFocus, boolean isTextEditor, int softInputMode, |
| 1445 | boolean first, int windowFlags) { |
| 1446 | long ident = Binder.clearCallingIdentity(); |
| 1447 | try { |
| 1448 | synchronized (mMethodMap) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1449 | if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1450 | + " viewHasFocus=" + viewHasFocus |
| 1451 | + " isTextEditor=" + isTextEditor |
| 1452 | + " softInputMode=#" + Integer.toHexString(softInputMode) |
| 1453 | + " first=" + first + " flags=#" |
| 1454 | + Integer.toHexString(windowFlags)); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1455 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1456 | if (mCurClient == null || client == null |
| 1457 | || mCurClient.client.asBinder() != client.asBinder()) { |
| 1458 | try { |
| 1459 | // We need to check if this is the current client with |
| 1460 | // focus in the window manager, to allow this call to |
| 1461 | // be made before input is started in it. |
| 1462 | if (!mIWindowManager.inputMethodClientHasFocus(client)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1463 | 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] | 1464 | return; |
| 1465 | } |
| 1466 | } catch (RemoteException e) { |
| 1467 | } |
| 1468 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1469 | |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1470 | if (mCurFocusedWindow == windowToken) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1471 | 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] | 1472 | return; |
| 1473 | } |
| 1474 | mCurFocusedWindow = windowToken; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1475 | |
Dianne Hackborn | 7d3a5bc | 2010-11-29 22:52:12 -0800 | [diff] [blame] | 1476 | // Should we auto-show the IME even if the caller has not |
| 1477 | // specified what should be done with it? |
| 1478 | // We only do this automatically if the window can resize |
| 1479 | // to accommodate the IME (so what the user sees will give |
| 1480 | // them good context without input information being obscured |
| 1481 | // by the IME) or if running on a large screen where there |
| 1482 | // is more room for the target window + IME. |
| 1483 | final boolean doAutoShow = |
| 1484 | (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST) |
| 1485 | == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE |
| 1486 | || mRes.getConfiguration().isLayoutSizeAtLeast( |
| 1487 | Configuration.SCREENLAYOUT_SIZE_LARGE); |
| 1488 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1489 | switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) { |
| 1490 | case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED: |
Dianne Hackborn | 7d3a5bc | 2010-11-29 22:52:12 -0800 | [diff] [blame] | 1491 | if (!isTextEditor || !doAutoShow) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1492 | if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) { |
| 1493 | // There is no focus view, and this window will |
| 1494 | // be behind any soft input window, so hide the |
| 1495 | // soft input window if it is shown. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1496 | 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] | 1497 | hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1498 | } |
Dianne Hackborn | 7d3a5bc | 2010-11-29 22:52:12 -0800 | [diff] [blame] | 1499 | } else if (isTextEditor && doAutoShow && (softInputMode & |
| 1500 | WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1501 | // There is a focus view, and we are navigating forward |
| 1502 | // into the window, so show the input window for the user. |
Dianne Hackborn | 7d3a5bc | 2010-11-29 22:52:12 -0800 | [diff] [blame] | 1503 | // We only do this automatically if the window an resize |
| 1504 | // to accomodate the IME (so what the user sees will give |
| 1505 | // them good context without input information being obscured |
| 1506 | // by the IME) or if running on a large screen where there |
| 1507 | // is more room for the target window + IME. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1508 | 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] | 1509 | showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1510 | } |
| 1511 | break; |
| 1512 | case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED: |
| 1513 | // Do nothing. |
| 1514 | break; |
| 1515 | case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN: |
| 1516 | if ((softInputMode & |
| 1517 | WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1518 | 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] | 1519 | hideCurrentInputLocked(0, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1520 | } |
| 1521 | break; |
| 1522 | case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN: |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1523 | 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] | 1524 | hideCurrentInputLocked(0, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1525 | break; |
| 1526 | case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE: |
| 1527 | if ((softInputMode & |
| 1528 | WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1529 | 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] | 1530 | showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1531 | } |
| 1532 | break; |
| 1533 | case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE: |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1534 | 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] | 1535 | showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1536 | break; |
| 1537 | } |
| 1538 | } |
| 1539 | } finally { |
| 1540 | Binder.restoreCallingIdentity(ident); |
| 1541 | } |
| 1542 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1543 | |
satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 1544 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1545 | public void showInputMethodPickerFromClient(IInputMethodClient client) { |
| 1546 | synchronized (mMethodMap) { |
| 1547 | if (mCurClient == null || client == null |
| 1548 | || mCurClient.client.asBinder() != client.asBinder()) { |
satok | 47a4491 | 2010-10-06 16:03:58 +0900 | [diff] [blame] | 1549 | Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid " |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 1550 | + Binder.getCallingUid() + ": " + client); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1551 | } |
| 1552 | |
satok | 440aab5 | 2010-11-25 09:43:11 +0900 | [diff] [blame] | 1553 | // Always call subtype picker, because subtype picker is a superset of input method |
| 1554 | // picker. |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1555 | mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER); |
| 1556 | } |
| 1557 | } |
| 1558 | |
satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 1559 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1560 | public void setInputMethod(IBinder token, String id) { |
satok | 2820351 | 2010-11-24 11:06:49 +0900 | [diff] [blame] | 1561 | setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID); |
| 1562 | } |
| 1563 | |
satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 1564 | @Override |
satok | 2820351 | 2010-11-24 11:06:49 +0900 | [diff] [blame] | 1565 | public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) { |
| 1566 | synchronized (mMethodMap) { |
| 1567 | if (subtype != null) { |
| 1568 | setInputMethodWithSubtypeId(token, id, getSubtypeIdFromHashCode( |
| 1569 | mMethodMap.get(id), subtype.hashCode())); |
| 1570 | } else { |
| 1571 | setInputMethod(token, id); |
| 1572 | } |
| 1573 | } |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1574 | } |
| 1575 | |
satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 1576 | @Override |
satok | b416a71 | 2010-11-25 20:42:14 +0900 | [diff] [blame] | 1577 | public void showInputMethodAndSubtypeEnablerFromClient( |
satok | 217f548 | 2010-12-15 05:19:19 +0900 | [diff] [blame] | 1578 | IInputMethodClient client, String inputMethodId) { |
satok | b416a71 | 2010-11-25 20:42:14 +0900 | [diff] [blame] | 1579 | synchronized (mMethodMap) { |
| 1580 | if (mCurClient == null || client == null |
| 1581 | || mCurClient.client.asBinder() != client.asBinder()) { |
| 1582 | Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client); |
| 1583 | } |
satok | 7fee71f | 2010-12-17 18:54:26 +0900 | [diff] [blame] | 1584 | executeOrSendMessage(mCurMethod, mCaller.obtainMessageO( |
| 1585 | MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId)); |
satok | b416a71 | 2010-11-25 20:42:14 +0900 | [diff] [blame] | 1586 | } |
| 1587 | } |
| 1588 | |
satok | 4fc87d6 | 2011-05-20 16:13:43 +0900 | [diff] [blame] | 1589 | @Override |
satok | 735cf38 | 2010-11-11 20:40:09 +0900 | [diff] [blame] | 1590 | public boolean switchToLastInputMethod(IBinder token) { |
| 1591 | synchronized (mMethodMap) { |
satok | c445bcd | 2011-01-25 18:57:24 +0900 | [diff] [blame] | 1592 | final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked(); |
satok | 4fc87d6 | 2011-05-20 16:13:43 +0900 | [diff] [blame] | 1593 | final InputMethodInfo lastImi; |
satok | 208d563 | 2011-05-20 22:13:38 +0900 | [diff] [blame] | 1594 | if (lastIme != null) { |
satok | 4fc87d6 | 2011-05-20 16:13:43 +0900 | [diff] [blame] | 1595 | lastImi = mMethodMap.get(lastIme.first); |
| 1596 | } else { |
| 1597 | lastImi = null; |
satok | 735cf38 | 2010-11-11 20:40:09 +0900 | [diff] [blame] | 1598 | } |
satok | 4fc87d6 | 2011-05-20 16:13:43 +0900 | [diff] [blame] | 1599 | String targetLastImiId = null; |
| 1600 | int subtypeId = NOT_A_SUBTYPE_ID; |
| 1601 | if (lastIme != null && lastImi != null) { |
| 1602 | final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId); |
| 1603 | final int lastSubtypeHash = Integer.valueOf(lastIme.second); |
| 1604 | final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID |
| 1605 | : mCurrentSubtype.hashCode(); |
| 1606 | // If the last IME is the same as the current IME and the last subtype is not |
| 1607 | // defined, there is no need to switch to the last IME. |
| 1608 | if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) { |
| 1609 | targetLastImiId = lastIme.first; |
| 1610 | subtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash); |
| 1611 | } |
| 1612 | } |
| 1613 | |
| 1614 | if (TextUtils.isEmpty(targetLastImiId) && !canAddToLastInputMethod(mCurrentSubtype)) { |
| 1615 | // This is a safety net. If the currentSubtype can't be added to the history |
| 1616 | // and the framework couldn't find the last ime, we will make the last ime be |
| 1617 | // the most applicable enabled keyboard subtype of the system imes. |
| 1618 | final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked(); |
| 1619 | if (enabled != null) { |
| 1620 | final int N = enabled.size(); |
| 1621 | final String locale = mCurrentSubtype == null |
| 1622 | ? mRes.getConfiguration().locale.toString() |
| 1623 | : mCurrentSubtype.getLocale(); |
| 1624 | for (int i = 0; i < N; ++i) { |
| 1625 | final InputMethodInfo imi = enabled.get(i); |
| 1626 | if (imi.getSubtypeCount() > 0 && isSystemIme(imi)) { |
| 1627 | InputMethodSubtype keyboardSubtype = |
| 1628 | findLastResortApplicableSubtypeLocked(mRes, getSubtypes(imi), |
| 1629 | SUBTYPE_MODE_KEYBOARD, locale, true); |
| 1630 | if (keyboardSubtype != null) { |
| 1631 | targetLastImiId = imi.getId(); |
| 1632 | subtypeId = getSubtypeIdFromHashCode( |
| 1633 | imi, keyboardSubtype.hashCode()); |
| 1634 | if(keyboardSubtype.getLocale().equals(locale)) { |
| 1635 | break; |
| 1636 | } |
| 1637 | } |
| 1638 | } |
| 1639 | } |
| 1640 | } |
| 1641 | } |
| 1642 | |
| 1643 | if (!TextUtils.isEmpty(targetLastImiId)) { |
| 1644 | if (DEBUG) { |
| 1645 | Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second |
| 1646 | + ", from: " + mCurMethodId + ", " + subtypeId); |
| 1647 | } |
| 1648 | setInputMethodWithSubtypeId(token, targetLastImiId, subtypeId); |
| 1649 | return true; |
| 1650 | } else { |
| 1651 | return false; |
| 1652 | } |
satok | 735cf38 | 2010-11-11 20:40:09 +0900 | [diff] [blame] | 1653 | } |
| 1654 | } |
| 1655 | |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 1656 | @Override |
satok | 68f1b78 | 2011-04-11 14:26:04 +0900 | [diff] [blame] | 1657 | public InputMethodSubtype getLastInputMethodSubtype() { |
| 1658 | synchronized (mMethodMap) { |
| 1659 | final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked(); |
| 1660 | // TODO: Handle the case of the last IME with no subtypes |
| 1661 | if (lastIme == null || TextUtils.isEmpty(lastIme.first) |
| 1662 | || TextUtils.isEmpty(lastIme.second)) return null; |
| 1663 | final InputMethodInfo lastImi = mMethodMap.get(lastIme.first); |
| 1664 | if (lastImi == null) return null; |
| 1665 | try { |
| 1666 | final int lastSubtypeHash = Integer.valueOf(lastIme.second); |
satok | 0e7d7d6 | 2011-07-05 13:28:06 +0900 | [diff] [blame] | 1667 | final int lastSubtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash); |
| 1668 | if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) { |
| 1669 | return null; |
| 1670 | } |
| 1671 | return lastImi.getSubtypeAt(lastSubtypeId); |
satok | 68f1b78 | 2011-04-11 14:26:04 +0900 | [diff] [blame] | 1672 | } catch (NumberFormatException e) { |
| 1673 | return null; |
| 1674 | } |
| 1675 | } |
| 1676 | } |
| 1677 | |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 1678 | @Override |
satok | ee5e77c | 2011-09-02 18:50:15 +0900 | [diff] [blame] | 1679 | public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) { |
satok | 91e8812 | 2011-07-18 11:11:42 +0900 | [diff] [blame] | 1680 | // By this IPC call, only a process which shares the same uid with the IME can add |
| 1681 | // additional input method subtypes to the IME. |
satok | ee5e77c | 2011-09-02 18:50:15 +0900 | [diff] [blame] | 1682 | if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return; |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 1683 | synchronized (mMethodMap) { |
satok | 91e8812 | 2011-07-18 11:11:42 +0900 | [diff] [blame] | 1684 | final InputMethodInfo imi = mMethodMap.get(imiId); |
satok | ee5e77c | 2011-09-02 18:50:15 +0900 | [diff] [blame] | 1685 | if (imi == null) return; |
satok | 91e8812 | 2011-07-18 11:11:42 +0900 | [diff] [blame] | 1686 | final PackageManager pm = mContext.getPackageManager(); |
| 1687 | final String[] packageInfos = pm.getPackagesForUid(Binder.getCallingUid()); |
| 1688 | if (packageInfos != null) { |
| 1689 | final int packageNum = packageInfos.length; |
| 1690 | for (int i = 0; i < packageNum; ++i) { |
| 1691 | if (packageInfos[i].equals(imi.getPackageName())) { |
| 1692 | mFileManager.addInputMethodSubtypes(imi, subtypes); |
satok | c593380 | 2011-08-31 21:26:04 +0900 | [diff] [blame] | 1693 | final long ident = Binder.clearCallingIdentity(); |
| 1694 | try { |
| 1695 | buildInputMethodListLocked(mMethodList, mMethodMap); |
| 1696 | } finally { |
| 1697 | Binder.restoreCallingIdentity(ident); |
| 1698 | } |
satok | ee5e77c | 2011-09-02 18:50:15 +0900 | [diff] [blame] | 1699 | return; |
satok | 91e8812 | 2011-07-18 11:11:42 +0900 | [diff] [blame] | 1700 | } |
| 1701 | } |
| 1702 | } |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 1703 | } |
satok | ee5e77c | 2011-09-02 18:50:15 +0900 | [diff] [blame] | 1704 | return; |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 1705 | } |
| 1706 | |
satok | 2820351 | 2010-11-24 11:06:49 +0900 | [diff] [blame] | 1707 | private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1708 | synchronized (mMethodMap) { |
| 1709 | if (token == null) { |
| 1710 | if (mContext.checkCallingOrSelfPermission( |
| 1711 | android.Manifest.permission.WRITE_SECURE_SETTINGS) |
| 1712 | != PackageManager.PERMISSION_GRANTED) { |
| 1713 | throw new SecurityException( |
| 1714 | "Using null token requires permission " |
| 1715 | + android.Manifest.permission.WRITE_SECURE_SETTINGS); |
| 1716 | } |
| 1717 | } else if (mCurToken != token) { |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 1718 | Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid() |
| 1719 | + " token: " + token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1720 | return; |
| 1721 | } |
| 1722 | |
satok | c593380 | 2011-08-31 21:26:04 +0900 | [diff] [blame] | 1723 | final long ident = Binder.clearCallingIdentity(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1724 | try { |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1725 | setInputMethodLocked(id, subtypeId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1726 | } finally { |
| 1727 | Binder.restoreCallingIdentity(ident); |
| 1728 | } |
| 1729 | } |
| 1730 | } |
| 1731 | |
satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 1732 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1733 | public void hideMySoftInput(IBinder token, int flags) { |
| 1734 | synchronized (mMethodMap) { |
| 1735 | if (token == null || mCurToken != token) { |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 1736 | if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid " |
| 1737 | + Binder.getCallingUid() + " token: " + token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1738 | return; |
| 1739 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1740 | long ident = Binder.clearCallingIdentity(); |
| 1741 | try { |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1742 | hideCurrentInputLocked(flags, null); |
| 1743 | } finally { |
| 1744 | Binder.restoreCallingIdentity(ident); |
| 1745 | } |
| 1746 | } |
| 1747 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1748 | |
satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 1749 | @Override |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1750 | public void showMySoftInput(IBinder token, int flags) { |
| 1751 | synchronized (mMethodMap) { |
| 1752 | if (token == null || mCurToken != token) { |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 1753 | Slog.w(TAG, "Ignoring showMySoftInput of uid " |
| 1754 | + Binder.getCallingUid() + " token: " + token); |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1755 | return; |
| 1756 | } |
| 1757 | long ident = Binder.clearCallingIdentity(); |
| 1758 | try { |
| 1759 | showCurrentInputLocked(flags, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1760 | } finally { |
| 1761 | Binder.restoreCallingIdentity(ident); |
| 1762 | } |
| 1763 | } |
| 1764 | } |
| 1765 | |
| 1766 | void setEnabledSessionInMainThread(SessionState session) { |
| 1767 | if (mEnabledSession != session) { |
| 1768 | if (mEnabledSession != null) { |
| 1769 | try { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1770 | if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1771 | mEnabledSession.method.setSessionEnabled( |
| 1772 | mEnabledSession.session, false); |
| 1773 | } catch (RemoteException e) { |
| 1774 | } |
| 1775 | } |
| 1776 | mEnabledSession = session; |
| 1777 | try { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1778 | if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1779 | session.method.setSessionEnabled( |
| 1780 | session.session, true); |
| 1781 | } catch (RemoteException e) { |
| 1782 | } |
| 1783 | } |
| 1784 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1785 | |
satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 1786 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1787 | public boolean handleMessage(Message msg) { |
| 1788 | HandlerCaller.SomeArgs args; |
| 1789 | switch (msg.what) { |
| 1790 | case MSG_SHOW_IM_PICKER: |
| 1791 | showInputMethodMenu(); |
| 1792 | return true; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1793 | |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1794 | case MSG_SHOW_IM_SUBTYPE_PICKER: |
| 1795 | showInputMethodSubtypeMenu(); |
| 1796 | return true; |
| 1797 | |
satok | 47a4491 | 2010-10-06 16:03:58 +0900 | [diff] [blame] | 1798 | case MSG_SHOW_IM_SUBTYPE_ENABLER: |
satok | 217f548 | 2010-12-15 05:19:19 +0900 | [diff] [blame] | 1799 | args = (HandlerCaller.SomeArgs)msg.obj; |
satok | 7fee71f | 2010-12-17 18:54:26 +0900 | [diff] [blame] | 1800 | showInputMethodAndSubtypeEnabler((String)args.arg1); |
satok | 217f548 | 2010-12-15 05:19:19 +0900 | [diff] [blame] | 1801 | return true; |
| 1802 | |
| 1803 | case MSG_SHOW_IM_CONFIG: |
| 1804 | showConfigureInputMethods(); |
satok | 47a4491 | 2010-10-06 16:03:58 +0900 | [diff] [blame] | 1805 | return true; |
| 1806 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1807 | // --------------------------------------------------------- |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1808 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1809 | case MSG_UNBIND_INPUT: |
| 1810 | try { |
| 1811 | ((IInputMethod)msg.obj).unbindInput(); |
| 1812 | } catch (RemoteException e) { |
| 1813 | // There is nothing interesting about the method dying. |
| 1814 | } |
| 1815 | return true; |
| 1816 | case MSG_BIND_INPUT: |
| 1817 | args = (HandlerCaller.SomeArgs)msg.obj; |
| 1818 | try { |
| 1819 | ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2); |
| 1820 | } catch (RemoteException e) { |
| 1821 | } |
| 1822 | return true; |
| 1823 | case MSG_SHOW_SOFT_INPUT: |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1824 | args = (HandlerCaller.SomeArgs)msg.obj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1825 | try { |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1826 | ((IInputMethod)args.arg1).showSoftInput(msg.arg1, |
| 1827 | (ResultReceiver)args.arg2); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1828 | } catch (RemoteException e) { |
| 1829 | } |
| 1830 | return true; |
| 1831 | case MSG_HIDE_SOFT_INPUT: |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1832 | args = (HandlerCaller.SomeArgs)msg.obj; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1833 | try { |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1834 | ((IInputMethod)args.arg1).hideSoftInput(0, |
| 1835 | (ResultReceiver)args.arg2); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1836 | } catch (RemoteException e) { |
| 1837 | } |
| 1838 | return true; |
| 1839 | case MSG_ATTACH_TOKEN: |
| 1840 | args = (HandlerCaller.SomeArgs)msg.obj; |
| 1841 | try { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1842 | 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] | 1843 | ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2); |
| 1844 | } catch (RemoteException e) { |
| 1845 | } |
| 1846 | return true; |
| 1847 | case MSG_CREATE_SESSION: |
| 1848 | args = (HandlerCaller.SomeArgs)msg.obj; |
| 1849 | try { |
| 1850 | ((IInputMethod)args.arg1).createSession( |
| 1851 | (IInputMethodCallback)args.arg2); |
| 1852 | } catch (RemoteException e) { |
| 1853 | } |
| 1854 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1855 | // --------------------------------------------------------- |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1856 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1857 | case MSG_START_INPUT: |
| 1858 | args = (HandlerCaller.SomeArgs)msg.obj; |
| 1859 | try { |
| 1860 | SessionState session = (SessionState)args.arg1; |
| 1861 | setEnabledSessionInMainThread(session); |
| 1862 | session.method.startInput((IInputContext)args.arg2, |
| 1863 | (EditorInfo)args.arg3); |
| 1864 | } catch (RemoteException e) { |
| 1865 | } |
| 1866 | return true; |
| 1867 | case MSG_RESTART_INPUT: |
| 1868 | args = (HandlerCaller.SomeArgs)msg.obj; |
| 1869 | try { |
| 1870 | SessionState session = (SessionState)args.arg1; |
| 1871 | setEnabledSessionInMainThread(session); |
| 1872 | session.method.restartInput((IInputContext)args.arg2, |
| 1873 | (EditorInfo)args.arg3); |
| 1874 | } catch (RemoteException e) { |
| 1875 | } |
| 1876 | return true; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1877 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1878 | // --------------------------------------------------------- |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1879 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1880 | case MSG_UNBIND_METHOD: |
| 1881 | try { |
| 1882 | ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1); |
| 1883 | } catch (RemoteException e) { |
| 1884 | // There is nothing interesting about the last client dying. |
| 1885 | } |
| 1886 | return true; |
| 1887 | case MSG_BIND_METHOD: |
| 1888 | args = (HandlerCaller.SomeArgs)msg.obj; |
| 1889 | try { |
| 1890 | ((IInputMethodClient)args.arg1).onBindMethod( |
| 1891 | (InputBindResult)args.arg2); |
| 1892 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1893 | 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] | 1894 | } |
| 1895 | return true; |
| 1896 | } |
| 1897 | return false; |
| 1898 | } |
| 1899 | |
Brandon Ballinger | 6da35a0 | 2009-10-21 00:38:13 -0700 | [diff] [blame] | 1900 | private boolean isSystemIme(InputMethodInfo inputMethod) { |
| 1901 | return (inputMethod.getServiceInfo().applicationInfo.flags |
| 1902 | & ApplicationInfo.FLAG_SYSTEM) != 0; |
| 1903 | } |
| 1904 | |
Ken Wakasa | 586f051 | 2011-01-20 22:31:01 +0900 | [diff] [blame] | 1905 | private static ArrayList<InputMethodSubtype> getSubtypes(InputMethodInfo imi) { |
| 1906 | ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>(); |
| 1907 | final int subtypeCount = imi.getSubtypeCount(); |
| 1908 | for (int i = 0; i < subtypeCount; ++i) { |
| 1909 | subtypes.add(imi.getSubtypeAt(i)); |
| 1910 | } |
| 1911 | return subtypes; |
| 1912 | } |
| 1913 | |
satok | a86f5e4 | 2011-09-02 17:12:42 +0900 | [diff] [blame] | 1914 | |
| 1915 | private static ArrayList<InputMethodSubtype> getOverridingImplicitlyEnabledSubtypes( |
| 1916 | InputMethodInfo imi, String mode) { |
| 1917 | ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>(); |
| 1918 | final int subtypeCount = imi.getSubtypeCount(); |
| 1919 | for (int i = 0; i < subtypeCount; ++i) { |
| 1920 | final InputMethodSubtype subtype = imi.getSubtypeAt(i); |
| 1921 | if (subtype.overridesImplicitlyEnabledSubtype() && subtype.getMode().equals(mode)) { |
| 1922 | subtypes.add(subtype); |
| 1923 | } |
| 1924 | } |
| 1925 | return subtypes; |
| 1926 | } |
| 1927 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1928 | private boolean chooseNewDefaultIMELocked() { |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 1929 | List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked(); |
Brandon Ballinger | 6da35a0 | 2009-10-21 00:38:13 -0700 | [diff] [blame] | 1930 | if (enabled != null && enabled.size() > 0) { |
Dianne Hackborn | 83e48f5 | 2010-03-23 23:03:25 -0700 | [diff] [blame] | 1931 | // We'd prefer to fall back on a system IME, since that is safer. |
| 1932 | int i=enabled.size(); |
| 1933 | while (i > 0) { |
| 1934 | i--; |
| 1935 | if ((enabled.get(i).getServiceInfo().applicationInfo.flags |
| 1936 | & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 1937 | break; |
| 1938 | } |
| 1939 | } |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 1940 | InputMethodInfo imi = enabled.get(i); |
satok | 03eb319a | 2010-11-11 18:17:42 +0900 | [diff] [blame] | 1941 | if (DEBUG) { |
| 1942 | Slog.d(TAG, "New default IME was selected: " + imi.getId()); |
| 1943 | } |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 1944 | resetSelectedInputMethodAndSubtypeLocked(imi.getId()); |
Brandon Ballinger | 6da35a0 | 2009-10-21 00:38:13 -0700 | [diff] [blame] | 1945 | return true; |
| 1946 | } |
| 1947 | |
| 1948 | return false; |
| 1949 | } |
| 1950 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1951 | void buildInputMethodListLocked(ArrayList<InputMethodInfo> list, |
| 1952 | HashMap<String, InputMethodInfo> map) { |
| 1953 | list.clear(); |
| 1954 | map.clear(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1955 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1956 | PackageManager pm = mContext.getPackageManager(); |
Dianne Hackborn | 7d3a5bc | 2010-11-29 22:52:12 -0800 | [diff] [blame] | 1957 | final Configuration config = mRes.getConfiguration(); |
Amith Yamasani | e861ec1 | 2010-03-24 21:39:27 -0700 | [diff] [blame] | 1958 | final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY; |
| 1959 | String disabledSysImes = Settings.Secure.getString(mContext.getContentResolver(), |
| 1960 | Secure.DISABLED_SYSTEM_INPUT_METHODS); |
| 1961 | if (disabledSysImes == null) disabledSysImes = ""; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1962 | |
| 1963 | List<ResolveInfo> services = pm.queryIntentServices( |
| 1964 | new Intent(InputMethod.SERVICE_INTERFACE), |
| 1965 | PackageManager.GET_META_DATA); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1966 | |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 1967 | final HashMap<String, List<InputMethodSubtype>> additionalSubtypes = |
| 1968 | mFileManager.getAllAdditionalInputMethodSubtypes(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1969 | for (int i = 0; i < services.size(); ++i) { |
| 1970 | ResolveInfo ri = services.get(i); |
| 1971 | ServiceInfo si = ri.serviceInfo; |
| 1972 | ComponentName compName = new ComponentName(si.packageName, si.name); |
| 1973 | if (!android.Manifest.permission.BIND_INPUT_METHOD.equals( |
| 1974 | si.permission)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1975 | Slog.w(TAG, "Skipping input method " + compName |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1976 | + ": it does not require the permission " |
| 1977 | + android.Manifest.permission.BIND_INPUT_METHOD); |
| 1978 | continue; |
| 1979 | } |
| 1980 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1981 | if (DEBUG) Slog.d(TAG, "Checking " + compName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1982 | |
| 1983 | try { |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 1984 | InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1985 | list.add(p); |
Amith Yamasani | e861ec1 | 2010-03-24 21:39:27 -0700 | [diff] [blame] | 1986 | final String id = p.getId(); |
| 1987 | map.put(id, p); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1988 | |
Amith Yamasani | e861ec1 | 2010-03-24 21:39:27 -0700 | [diff] [blame] | 1989 | // System IMEs are enabled by default, unless there's a hard keyboard |
| 1990 | // and the system IME was explicitly disabled |
| 1991 | if (isSystemIme(p) && (!haveHardKeyboard || disabledSysImes.indexOf(id) < 0)) { |
| 1992 | setInputMethodEnabledLocked(id, true); |
Brandon Ballinger | 6da35a0 | 2009-10-21 00:38:13 -0700 | [diff] [blame] | 1993 | } |
| 1994 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1995 | if (DEBUG) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1996 | 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] | 1997 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1998 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1999 | } catch (XmlPullParserException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2000 | 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] | 2001 | } catch (IOException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2002 | 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] | 2003 | } |
| 2004 | } |
Brandon Ballinger | 6da35a0 | 2009-10-21 00:38:13 -0700 | [diff] [blame] | 2005 | |
| 2006 | String defaultIme = Settings.Secure.getString(mContext |
| 2007 | .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD); |
satok | 913a892 | 2010-08-26 21:53:41 +0900 | [diff] [blame] | 2008 | if (!TextUtils.isEmpty(defaultIme) && !map.containsKey(defaultIme)) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 2009 | if (chooseNewDefaultIMELocked()) { |
Brandon Ballinger | 6da35a0 | 2009-10-21 00:38:13 -0700 | [diff] [blame] | 2010 | updateFromSettingsLocked(); |
| 2011 | } |
| 2012 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2013 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2014 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2015 | // ---------------------------------------------------------------------- |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2016 | |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 2017 | private void showInputMethodMenu() { |
| 2018 | showInputMethodMenuInternal(false); |
| 2019 | } |
| 2020 | |
| 2021 | private void showInputMethodSubtypeMenu() { |
| 2022 | showInputMethodMenuInternal(true); |
| 2023 | } |
| 2024 | |
satok | 217f548 | 2010-12-15 05:19:19 +0900 | [diff] [blame] | 2025 | private void showInputMethodAndSubtypeEnabler(String inputMethodId) { |
Tadashi G. Takaoka | f49688f | 2011-01-20 17:56:13 +0900 | [diff] [blame] | 2026 | Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS); |
satok | 47a4491 | 2010-10-06 16:03:58 +0900 | [diff] [blame] | 2027 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
satok | 86417ea | 2010-10-27 14:11:03 +0900 | [diff] [blame] | 2028 | | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED |
| 2029 | | Intent.FLAG_ACTIVITY_CLEAR_TOP); |
satok | 7fee71f | 2010-12-17 18:54:26 +0900 | [diff] [blame] | 2030 | if (!TextUtils.isEmpty(inputMethodId)) { |
Tadashi G. Takaoka | 2548020 | 2011-01-20 23:13:02 +0900 | [diff] [blame] | 2031 | intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId); |
satok | 7fee71f | 2010-12-17 18:54:26 +0900 | [diff] [blame] | 2032 | } |
satok | 217f548 | 2010-12-15 05:19:19 +0900 | [diff] [blame] | 2033 | mContext.startActivity(intent); |
| 2034 | } |
| 2035 | |
| 2036 | private void showConfigureInputMethods() { |
| 2037 | Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS); |
| 2038 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
| 2039 | | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED |
| 2040 | | Intent.FLAG_ACTIVITY_CLEAR_TOP); |
satok | 47a4491 | 2010-10-06 16:03:58 +0900 | [diff] [blame] | 2041 | mContext.startActivity(intent); |
| 2042 | } |
| 2043 | |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 2044 | private void showInputMethodMenuInternal(boolean showSubtypes) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2045 | if (DEBUG) Slog.v(TAG, "Show switching menu"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2046 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2047 | final Context context = mContext; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2048 | final PackageManager pm = context.getPackageManager(); |
satok | bc81b69 | 2011-08-26 16:22:22 +0900 | [diff] [blame] | 2049 | final boolean isScreenLocked = mKeyguardManager != null |
| 2050 | && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure(); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2051 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2052 | String lastInputMethodId = Settings.Secure.getString(context |
| 2053 | .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD); |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 2054 | int lastInputMethodSubtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2055 | if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2056 | |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 2057 | synchronized (mMethodMap) { |
satok | bb4aa06 | 2011-01-19 21:40:27 +0900 | [diff] [blame] | 2058 | final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis = |
| 2059 | getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(); |
satok | 7f35c8c | 2010-10-07 21:13:11 +0900 | [diff] [blame] | 2060 | if (immis == null || immis.size() == 0) { |
| 2061 | return; |
| 2062 | } |
| 2063 | |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 2064 | hideInputMethodMenuLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2065 | |
Ken Wakasa | 761eb37 | 2011-03-04 19:06:18 +0900 | [diff] [blame] | 2066 | final TreeMap<InputMethodInfo, List<InputMethodSubtype>> sortedImmis = |
| 2067 | new TreeMap<InputMethodInfo, List<InputMethodSubtype>>( |
| 2068 | new Comparator<InputMethodInfo>() { |
| 2069 | @Override |
| 2070 | public int compare(InputMethodInfo imi1, InputMethodInfo imi2) { |
| 2071 | if (imi2 == null) return 0; |
| 2072 | if (imi1 == null) return 1; |
| 2073 | if (pm == null) { |
| 2074 | return imi1.getId().compareTo(imi2.getId()); |
| 2075 | } |
| 2076 | CharSequence imiId1 = imi1.loadLabel(pm) + "/" + imi1.getId(); |
| 2077 | CharSequence imiId2 = imi2.loadLabel(pm) + "/" + imi2.getId(); |
| 2078 | return imiId1.toString().compareTo(imiId2.toString()); |
| 2079 | } |
| 2080 | }); |
satok | 913a892 | 2010-08-26 21:53:41 +0900 | [diff] [blame] | 2081 | |
Ken Wakasa | 761eb37 | 2011-03-04 19:06:18 +0900 | [diff] [blame] | 2082 | sortedImmis.putAll(immis); |
| 2083 | |
Ken Wakasa | 05dbb65 | 2011-08-22 15:22:43 +0900 | [diff] [blame] | 2084 | final ArrayList<ImeSubtypeListItem> imList = new ArrayList<ImeSubtypeListItem>(); |
Ken Wakasa | 761eb37 | 2011-03-04 19:06:18 +0900 | [diff] [blame] | 2085 | |
| 2086 | for (InputMethodInfo imi : sortedImmis.keySet()) { |
satok | bb4aa06 | 2011-01-19 21:40:27 +0900 | [diff] [blame] | 2087 | if (imi == null) continue; |
| 2088 | List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypeList = immis.get(imi); |
satok | 7f35c8c | 2010-10-07 21:13:11 +0900 | [diff] [blame] | 2089 | HashSet<String> enabledSubtypeSet = new HashSet<String>(); |
satok | bb4aa06 | 2011-01-19 21:40:27 +0900 | [diff] [blame] | 2090 | for (InputMethodSubtype subtype: explicitlyOrImplicitlyEnabledSubtypeList) { |
| 2091 | enabledSubtypeSet.add(String.valueOf(subtype.hashCode())); |
satok | 7f35c8c | 2010-10-07 21:13:11 +0900 | [diff] [blame] | 2092 | } |
satok | bb4aa06 | 2011-01-19 21:40:27 +0900 | [diff] [blame] | 2093 | ArrayList<InputMethodSubtype> subtypes = getSubtypes(imi); |
Ken Wakasa | 05dbb65 | 2011-08-22 15:22:43 +0900 | [diff] [blame] | 2094 | final CharSequence imeLabel = imi.loadLabel(pm); |
satok | 7f35c8c | 2010-10-07 21:13:11 +0900 | [diff] [blame] | 2095 | if (showSubtypes && enabledSubtypeSet.size() > 0) { |
satok | bb4aa06 | 2011-01-19 21:40:27 +0900 | [diff] [blame] | 2096 | final int subtypeCount = imi.getSubtypeCount(); |
satok | 4a28bde | 2011-06-29 21:03:40 +0900 | [diff] [blame] | 2097 | if (DEBUG) { |
| 2098 | Slog.v(TAG, "Add subtypes: " + subtypeCount + ", " + imi.getId()); |
| 2099 | } |
Ken Wakasa | 586f051 | 2011-01-20 22:31:01 +0900 | [diff] [blame] | 2100 | for (int j = 0; j < subtypeCount; ++j) { |
satok | 4a28bde | 2011-06-29 21:03:40 +0900 | [diff] [blame] | 2101 | final InputMethodSubtype subtype = imi.getSubtypeAt(j); |
| 2102 | final String subtypeHashCode = String.valueOf(subtype.hashCode()); |
| 2103 | // We show all enabled IMEs and subtypes when an IME is shown. |
| 2104 | if (enabledSubtypeSet.contains(subtypeHashCode) |
satok | bc81b69 | 2011-08-26 16:22:22 +0900 | [diff] [blame] | 2105 | && ((mInputShown && !isScreenLocked) || !subtype.isAuxiliary())) { |
satok | 38aac04 | 2011-09-06 14:41:33 +0900 | [diff] [blame] | 2106 | final CharSequence subtypeLabel = |
| 2107 | subtype.overridesImplicitlyEnabledSubtype() ? null |
| 2108 | : subtype.getDisplayName(context, imi.getPackageName(), |
| 2109 | imi.getServiceInfo().applicationInfo); |
Ken Wakasa | 05dbb65 | 2011-08-22 15:22:43 +0900 | [diff] [blame] | 2110 | imList.add(new ImeSubtypeListItem(imeLabel, subtypeLabel, imi, j)); |
| 2111 | |
satok | 4a28bde | 2011-06-29 21:03:40 +0900 | [diff] [blame] | 2112 | // Removing this subtype from enabledSubtypeSet because we no longer |
| 2113 | // need to add an entry of this subtype to imList to avoid duplicated |
| 2114 | // entries. |
| 2115 | enabledSubtypeSet.remove(subtypeHashCode); |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 2116 | } |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 2117 | } |
| 2118 | } else { |
Ken Wakasa | 05dbb65 | 2011-08-22 15:22:43 +0900 | [diff] [blame] | 2119 | imList.add(new ImeSubtypeListItem(imeLabel, null, imi, NOT_A_SUBTYPE_ID)); |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 2120 | } |
Dianne Hackborn | 97106ab | 2010-03-03 00:08:31 -0800 | [diff] [blame] | 2121 | } |
satok | 913a892 | 2010-08-26 21:53:41 +0900 | [diff] [blame] | 2122 | |
Ken Wakasa | 761eb37 | 2011-03-04 19:06:18 +0900 | [diff] [blame] | 2123 | final int N = imList.size(); |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 2124 | mIms = new InputMethodInfo[N]; |
| 2125 | mSubtypeIds = new int[N]; |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 2126 | int checkedItem = 0; |
| 2127 | for (int i = 0; i < N; ++i) { |
Ken Wakasa | 05dbb65 | 2011-08-22 15:22:43 +0900 | [diff] [blame] | 2128 | final ImeSubtypeListItem item = imList.get(i); |
| 2129 | mIms[i] = item.mImi; |
| 2130 | mSubtypeIds[i] = item.mSubtypeId; |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 2131 | if (mIms[i].getId().equals(lastInputMethodId)) { |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 2132 | int subtypeId = mSubtypeIds[i]; |
| 2133 | if ((subtypeId == NOT_A_SUBTYPE_ID) |
| 2134 | || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0) |
| 2135 | || (subtypeId == lastInputMethodSubtypeId)) { |
| 2136 | checkedItem = i; |
| 2137 | } |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 2138 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2139 | } |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 2140 | |
Ken Wakasa | 05dbb65 | 2011-08-22 15:22:43 +0900 | [diff] [blame] | 2141 | final TypedArray a = context.obtainStyledAttributes(null, |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 2142 | com.android.internal.R.styleable.DialogPreference, |
| 2143 | com.android.internal.R.attr.alertDialogStyle, 0); |
| 2144 | mDialogBuilder = new AlertDialog.Builder(context) |
| 2145 | .setTitle(com.android.internal.R.string.select_input_method) |
| 2146 | .setOnCancelListener(new OnCancelListener() { |
satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 2147 | @Override |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 2148 | public void onCancel(DialogInterface dialog) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2149 | hideInputMethodMenu(); |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 2150 | } |
| 2151 | }) |
| 2152 | .setIcon(a.getDrawable( |
| 2153 | com.android.internal.R.styleable.DialogPreference_dialogTitle)); |
| 2154 | a.recycle(); |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2155 | |
Ken Wakasa | 05dbb65 | 2011-08-22 15:22:43 +0900 | [diff] [blame] | 2156 | final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(context, |
| 2157 | com.android.internal.R.layout.simple_list_item_2_single_choice, imList, |
| 2158 | checkedItem); |
| 2159 | |
| 2160 | mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 2161 | new AlertDialog.OnClickListener() { |
satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 2162 | @Override |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 2163 | public void onClick(DialogInterface dialog, int which) { |
| 2164 | synchronized (mMethodMap) { |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 2165 | if (mIms == null || mIms.length <= which |
| 2166 | || mSubtypeIds == null || mSubtypeIds.length <= which) { |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 2167 | return; |
| 2168 | } |
| 2169 | InputMethodInfo im = mIms[which]; |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 2170 | int subtypeId = mSubtypeIds[which]; |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 2171 | hideInputMethodMenu(); |
| 2172 | if (im != null) { |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 2173 | if ((subtypeId < 0) |
Ken Wakasa | 586f051 | 2011-01-20 22:31:01 +0900 | [diff] [blame] | 2174 | || (subtypeId >= im.getSubtypeCount())) { |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 2175 | subtypeId = NOT_A_SUBTYPE_ID; |
| 2176 | } |
| 2177 | setInputMethodLocked(im.getId(), subtypeId); |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 2178 | } |
Dianne Hackborn | 20cb56e | 2010-03-04 00:58:29 -0800 | [diff] [blame] | 2179 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2180 | } |
Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 2181 | }); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2182 | |
satok | bc81b69 | 2011-08-26 16:22:22 +0900 | [diff] [blame] | 2183 | if (showSubtypes && !isScreenLocked) { |
satok | 82beadf | 2010-12-27 19:03:06 +0900 | [diff] [blame] | 2184 | mDialogBuilder.setPositiveButton( |
| 2185 | com.android.internal.R.string.configure_input_methods, |
satok | 7f35c8c | 2010-10-07 21:13:11 +0900 | [diff] [blame] | 2186 | new DialogInterface.OnClickListener() { |
satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 2187 | @Override |
satok | 7f35c8c | 2010-10-07 21:13:11 +0900 | [diff] [blame] | 2188 | public void onClick(DialogInterface dialog, int whichButton) { |
satok | 217f548 | 2010-12-15 05:19:19 +0900 | [diff] [blame] | 2189 | showConfigureInputMethods(); |
satok | 7f35c8c | 2010-10-07 21:13:11 +0900 | [diff] [blame] | 2190 | } |
| 2191 | }); |
| 2192 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2193 | mSwitchingDialog = mDialogBuilder.create(); |
Dianne Hackborn | e3a7f62 | 2011-03-03 21:48:24 -0800 | [diff] [blame] | 2194 | mSwitchingDialog.setCanceledOnTouchOutside(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2195 | mSwitchingDialog.getWindow().setType( |
| 2196 | WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG); |
Dianne Hackborn | e3a7f62 | 2011-03-03 21:48:24 -0800 | [diff] [blame] | 2197 | mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2198 | mSwitchingDialog.show(); |
| 2199 | } |
| 2200 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2201 | |
Ken Wakasa | 05dbb65 | 2011-08-22 15:22:43 +0900 | [diff] [blame] | 2202 | private static class ImeSubtypeListItem { |
| 2203 | public final CharSequence mImeName; |
| 2204 | public final CharSequence mSubtypeName; |
| 2205 | public final InputMethodInfo mImi; |
| 2206 | public final int mSubtypeId; |
| 2207 | public ImeSubtypeListItem(CharSequence imeName, CharSequence subtypeName, |
| 2208 | InputMethodInfo imi, int subtypeId) { |
| 2209 | mImeName = imeName; |
| 2210 | mSubtypeName = subtypeName; |
| 2211 | mImi = imi; |
| 2212 | mSubtypeId = subtypeId; |
| 2213 | } |
| 2214 | } |
| 2215 | |
| 2216 | private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> { |
| 2217 | private final LayoutInflater mInflater; |
| 2218 | private final int mTextViewResourceId; |
| 2219 | private final List<ImeSubtypeListItem> mItemsList; |
| 2220 | private final int mCheckedItem; |
| 2221 | public ImeSubtypeListAdapter(Context context, int textViewResourceId, |
| 2222 | List<ImeSubtypeListItem> itemsList, int checkedItem) { |
| 2223 | super(context, textViewResourceId, itemsList); |
| 2224 | mTextViewResourceId = textViewResourceId; |
| 2225 | mItemsList = itemsList; |
| 2226 | mCheckedItem = checkedItem; |
| 2227 | mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
| 2228 | } |
| 2229 | |
| 2230 | @Override |
| 2231 | public View getView(int position, View convertView, ViewGroup parent) { |
| 2232 | final View view = convertView != null ? convertView |
| 2233 | : mInflater.inflate(mTextViewResourceId, null); |
| 2234 | if (position < 0 || position >= mItemsList.size()) return view; |
| 2235 | final ImeSubtypeListItem item = mItemsList.get(position); |
| 2236 | final CharSequence imeName = item.mImeName; |
| 2237 | final CharSequence subtypeName = item.mSubtypeName; |
| 2238 | final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1); |
| 2239 | final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2); |
| 2240 | if (TextUtils.isEmpty(subtypeName)) { |
| 2241 | firstTextView.setText(imeName); |
| 2242 | secondTextView.setVisibility(View.GONE); |
| 2243 | } else { |
| 2244 | firstTextView.setText(subtypeName); |
| 2245 | secondTextView.setText(imeName); |
| 2246 | secondTextView.setVisibility(View.VISIBLE); |
| 2247 | } |
| 2248 | final RadioButton radioButton = |
| 2249 | (RadioButton)view.findViewById(com.android.internal.R.id.radio); |
| 2250 | radioButton.setChecked(position == mCheckedItem); |
| 2251 | return view; |
| 2252 | } |
| 2253 | } |
| 2254 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2255 | void hideInputMethodMenu() { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2256 | synchronized (mMethodMap) { |
| 2257 | hideInputMethodMenuLocked(); |
| 2258 | } |
| 2259 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2260 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2261 | void hideInputMethodMenuLocked() { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2262 | if (DEBUG) Slog.v(TAG, "Hide switching menu"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2263 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2264 | if (mSwitchingDialog != null) { |
| 2265 | mSwitchingDialog.dismiss(); |
| 2266 | mSwitchingDialog = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2267 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2268 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2269 | mDialogBuilder = null; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2270 | mIms = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2271 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2272 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2273 | // ---------------------------------------------------------------------- |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2274 | |
satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 2275 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2276 | public boolean setInputMethodEnabled(String id, boolean enabled) { |
| 2277 | synchronized (mMethodMap) { |
| 2278 | if (mContext.checkCallingOrSelfPermission( |
| 2279 | android.Manifest.permission.WRITE_SECURE_SETTINGS) |
| 2280 | != PackageManager.PERMISSION_GRANTED) { |
| 2281 | throw new SecurityException( |
| 2282 | "Requires permission " |
| 2283 | + android.Manifest.permission.WRITE_SECURE_SETTINGS); |
| 2284 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 2285 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2286 | long ident = Binder.clearCallingIdentity(); |
| 2287 | try { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 2288 | return setInputMethodEnabledLocked(id, enabled); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2289 | } finally { |
| 2290 | Binder.restoreCallingIdentity(ident); |
| 2291 | } |
| 2292 | } |
| 2293 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 2294 | |
| 2295 | boolean setInputMethodEnabledLocked(String id, boolean enabled) { |
| 2296 | // Make sure this is a valid input method. |
| 2297 | InputMethodInfo imm = mMethodMap.get(id); |
| 2298 | if (imm == null) { |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2299 | throw new IllegalArgumentException("Unknown id: " + mCurMethodId); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 2300 | } |
| 2301 | |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2302 | List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings |
| 2303 | .getEnabledInputMethodsAndSubtypeListLocked(); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 2304 | |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2305 | if (enabled) { |
| 2306 | for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) { |
| 2307 | if (pair.first.equals(id)) { |
| 2308 | // We are enabling this input method, but it is already enabled. |
| 2309 | // Nothing to do. The previous state was enabled. |
| 2310 | return true; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 2311 | } |
| 2312 | } |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2313 | mSettings.appendAndPutEnabledInputMethodLocked(id, false); |
| 2314 | // Previous state was disabled. |
| 2315 | return false; |
| 2316 | } else { |
| 2317 | StringBuilder builder = new StringBuilder(); |
| 2318 | if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked( |
| 2319 | builder, enabledInputMethodsList, id)) { |
| 2320 | // Disabled input method is currently selected, switch to another one. |
| 2321 | String selId = Settings.Secure.getString(mContext.getContentResolver(), |
| 2322 | Settings.Secure.DEFAULT_INPUT_METHOD); |
satok | 03eb319a | 2010-11-11 18:17:42 +0900 | [diff] [blame] | 2323 | if (id.equals(selId) && !chooseNewDefaultIMELocked()) { |
| 2324 | Slog.i(TAG, "Can't find new IME, unsetting the current input method."); |
| 2325 | resetSelectedInputMethodAndSubtypeLocked(""); |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2326 | } |
| 2327 | // Previous state was enabled. |
| 2328 | return true; |
| 2329 | } else { |
| 2330 | // We are disabling the input method but it is already disabled. |
| 2331 | // Nothing to do. The previous state was disabled. |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 2332 | return false; |
| 2333 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 2334 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 2335 | } |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 2336 | |
satok | 57ffc00 | 2011-01-25 00:11:47 +0900 | [diff] [blame] | 2337 | private boolean canAddToLastInputMethod(InputMethodSubtype subtype) { |
| 2338 | if (subtype == null) return true; |
satok | 9b41579 | 2011-05-30 12:37:52 +0900 | [diff] [blame] | 2339 | return !subtype.isAuxiliary(); |
satok | 57ffc00 | 2011-01-25 00:11:47 +0900 | [diff] [blame] | 2340 | } |
| 2341 | |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 2342 | private void saveCurrentInputMethodAndSubtypeToHistory() { |
| 2343 | String subtypeId = NOT_A_SUBTYPE_ID_STR; |
| 2344 | if (mCurrentSubtype != null) { |
| 2345 | subtypeId = String.valueOf(mCurrentSubtype.hashCode()); |
| 2346 | } |
satok | 57ffc00 | 2011-01-25 00:11:47 +0900 | [diff] [blame] | 2347 | if (canAddToLastInputMethod(mCurrentSubtype)) { |
| 2348 | mSettings.addSubtypeToHistory(mCurMethodId, subtypeId); |
| 2349 | } |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 2350 | } |
| 2351 | |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 2352 | private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId, |
| 2353 | boolean setSubtypeOnly) { |
| 2354 | // Update the history of InputMethod and Subtype |
| 2355 | saveCurrentInputMethodAndSubtypeToHistory(); |
| 2356 | |
| 2357 | // Set Subtype here |
| 2358 | if (imi == null || subtypeId < 0) { |
| 2359 | mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID); |
Tadashi G. Takaoka | 0ba75bb | 2010-11-09 12:19:32 -0800 | [diff] [blame] | 2360 | mCurrentSubtype = null; |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 2361 | } else { |
Ken Wakasa | 586f051 | 2011-01-20 22:31:01 +0900 | [diff] [blame] | 2362 | if (subtypeId < imi.getSubtypeCount()) { |
| 2363 | InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId); |
| 2364 | mSettings.putSelectedSubtype(subtype.hashCode()); |
| 2365 | mCurrentSubtype = subtype; |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 2366 | } else { |
| 2367 | mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID); |
| 2368 | mCurrentSubtype = null; |
| 2369 | } |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 2370 | } |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 2371 | |
| 2372 | if (!setSubtypeOnly) { |
| 2373 | // Set InputMethod here |
| 2374 | mSettings.putSelectedInputMethod(imi != null ? imi.getId() : ""); |
| 2375 | } |
| 2376 | } |
| 2377 | |
| 2378 | private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) { |
| 2379 | InputMethodInfo imi = mMethodMap.get(newDefaultIme); |
| 2380 | int lastSubtypeId = NOT_A_SUBTYPE_ID; |
| 2381 | // newDefaultIme is empty when there is no candidate for the selected IME. |
| 2382 | if (imi != null && !TextUtils.isEmpty(newDefaultIme)) { |
| 2383 | String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme); |
| 2384 | if (subtypeHashCode != null) { |
| 2385 | try { |
| 2386 | lastSubtypeId = getSubtypeIdFromHashCode( |
| 2387 | imi, Integer.valueOf(subtypeHashCode)); |
| 2388 | } catch (NumberFormatException e) { |
| 2389 | Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e); |
| 2390 | } |
| 2391 | } |
| 2392 | } |
| 2393 | setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false); |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 2394 | } |
| 2395 | |
| 2396 | private int getSelectedInputMethodSubtypeId(String id) { |
| 2397 | InputMethodInfo imi = mMethodMap.get(id); |
| 2398 | if (imi == null) { |
| 2399 | return NOT_A_SUBTYPE_ID; |
| 2400 | } |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 2401 | int subtypeId; |
| 2402 | try { |
| 2403 | subtypeId = Settings.Secure.getInt(mContext.getContentResolver(), |
| 2404 | Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE); |
| 2405 | } catch (SettingNotFoundException e) { |
| 2406 | return NOT_A_SUBTYPE_ID; |
| 2407 | } |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 2408 | return getSubtypeIdFromHashCode(imi, subtypeId); |
| 2409 | } |
| 2410 | |
| 2411 | private int getSubtypeIdFromHashCode(InputMethodInfo imi, int subtypeHashCode) { |
satok | 2820351 | 2010-11-24 11:06:49 +0900 | [diff] [blame] | 2412 | if (imi != null) { |
Ken Wakasa | 586f051 | 2011-01-20 22:31:01 +0900 | [diff] [blame] | 2413 | final int subtypeCount = imi.getSubtypeCount(); |
| 2414 | for (int i = 0; i < subtypeCount; ++i) { |
| 2415 | InputMethodSubtype ims = imi.getSubtypeAt(i); |
satok | 2820351 | 2010-11-24 11:06:49 +0900 | [diff] [blame] | 2416 | if (subtypeHashCode == ims.hashCode()) { |
| 2417 | return i; |
| 2418 | } |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 2419 | } |
| 2420 | } |
| 2421 | return NOT_A_SUBTYPE_ID; |
| 2422 | } |
| 2423 | |
satok | a86f5e4 | 2011-09-02 17:12:42 +0900 | [diff] [blame] | 2424 | private static ArrayList<InputMethodSubtype> getImplicitlyApplicableSubtypesLocked( |
| 2425 | Resources res, InputMethodInfo imi) { |
| 2426 | final List<InputMethodSubtype> subtypes = getSubtypes(imi); |
satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 2427 | final String systemLocale = res.getConfiguration().locale.toString(); |
satok | 3da9223 | 2011-01-11 22:46:30 +0900 | [diff] [blame] | 2428 | if (TextUtils.isEmpty(systemLocale)) return new ArrayList<InputMethodSubtype>(); |
| 2429 | HashMap<String, InputMethodSubtype> applicableModeAndSubtypesMap = |
| 2430 | new HashMap<String, InputMethodSubtype>(); |
satok | 16331c8 | 2010-12-20 23:48:46 +0900 | [diff] [blame] | 2431 | final int N = subtypes.size(); |
| 2432 | boolean containsKeyboardSubtype = false; |
| 2433 | for (int i = 0; i < N; ++i) { |
satok | a86f5e4 | 2011-09-02 17:12:42 +0900 | [diff] [blame] | 2434 | // scan overriding implicitly enabled subtypes. |
| 2435 | InputMethodSubtype subtype = subtypes.get(i); |
| 2436 | if (subtype.overridesImplicitlyEnabledSubtype()) { |
| 2437 | final String mode = subtype.getMode(); |
| 2438 | if (!applicableModeAndSubtypesMap.containsKey(mode)) { |
| 2439 | applicableModeAndSubtypesMap.put(mode, subtype); |
| 2440 | } |
| 2441 | } |
| 2442 | } |
| 2443 | if (applicableModeAndSubtypesMap.size() > 0) { |
| 2444 | return new ArrayList<InputMethodSubtype>(applicableModeAndSubtypesMap.values()); |
| 2445 | } |
| 2446 | for (int i = 0; i < N; ++i) { |
satok | 16331c8 | 2010-12-20 23:48:46 +0900 | [diff] [blame] | 2447 | InputMethodSubtype subtype = subtypes.get(i); |
satok | 3da9223 | 2011-01-11 22:46:30 +0900 | [diff] [blame] | 2448 | final String locale = subtype.getLocale(); |
| 2449 | final String mode = subtype.getMode(); |
| 2450 | // When system locale starts with subtype's locale, that subtype will be applicable |
| 2451 | // for system locale |
| 2452 | // For instance, it's clearly applicable for cases like system locale = en_US and |
| 2453 | // subtype = en, but it is not necessarily considered applicable for cases like system |
| 2454 | // locale = en and subtype = en_US. |
| 2455 | // We just call systemLocale.startsWith(locale) in this function because there is no |
| 2456 | // need to find applicable subtypes aggressively unlike |
| 2457 | // findLastResortApplicableSubtypeLocked. |
| 2458 | if (systemLocale.startsWith(locale)) { |
| 2459 | InputMethodSubtype applicableSubtype = applicableModeAndSubtypesMap.get(mode); |
| 2460 | // If more applicable subtypes are contained, skip. |
| 2461 | if (applicableSubtype != null |
| 2462 | && systemLocale.equals(applicableSubtype.getLocale())) continue; |
| 2463 | applicableModeAndSubtypesMap.put(mode, subtype); |
satok | 16331c8 | 2010-12-20 23:48:46 +0900 | [diff] [blame] | 2464 | if (!containsKeyboardSubtype |
| 2465 | && SUBTYPE_MODE_KEYBOARD.equalsIgnoreCase(subtype.getMode())) { |
| 2466 | containsKeyboardSubtype = true; |
| 2467 | } |
| 2468 | } |
| 2469 | } |
satok | 4a28bde | 2011-06-29 21:03:40 +0900 | [diff] [blame] | 2470 | final ArrayList<InputMethodSubtype> applicableSubtypes = new ArrayList<InputMethodSubtype>( |
satok | 3da9223 | 2011-01-11 22:46:30 +0900 | [diff] [blame] | 2471 | applicableModeAndSubtypesMap.values()); |
satok | 16331c8 | 2010-12-20 23:48:46 +0900 | [diff] [blame] | 2472 | if (!containsKeyboardSubtype) { |
| 2473 | InputMethodSubtype lastResortKeyboardSubtype = findLastResortApplicableSubtypeLocked( |
satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 2474 | res, subtypes, SUBTYPE_MODE_KEYBOARD, systemLocale, true); |
satok | 16331c8 | 2010-12-20 23:48:46 +0900 | [diff] [blame] | 2475 | if (lastResortKeyboardSubtype != null) { |
| 2476 | applicableSubtypes.add(lastResortKeyboardSubtype); |
| 2477 | } |
| 2478 | } |
| 2479 | return applicableSubtypes; |
| 2480 | } |
| 2481 | |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2482 | /** |
| 2483 | * If there are no selected subtypes, tries finding the most applicable one according to the |
| 2484 | * given locale. |
| 2485 | * @param subtypes this function will search the most applicable subtype in subtypes |
| 2486 | * @param mode subtypes will be filtered by mode |
| 2487 | * @param locale subtypes will be filtered by locale |
satok | 7599a7f | 2010-12-22 13:45:23 +0900 | [diff] [blame] | 2488 | * @param canIgnoreLocaleAsLastResort if this function can't find the most applicable subtype, |
| 2489 | * it will return the first subtype matched with mode |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2490 | * @return the most applicable subtypeId |
| 2491 | */ |
satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 2492 | private static InputMethodSubtype findLastResortApplicableSubtypeLocked( |
| 2493 | Resources res, List<InputMethodSubtype> subtypes, String mode, String locale, |
satok | 7599a7f | 2010-12-22 13:45:23 +0900 | [diff] [blame] | 2494 | boolean canIgnoreLocaleAsLastResort) { |
satok | 8fbb1e8 | 2010-11-02 23:15:58 +0900 | [diff] [blame] | 2495 | if (subtypes == null || subtypes.size() == 0) { |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 2496 | return null; |
satok | 8fbb1e8 | 2010-11-02 23:15:58 +0900 | [diff] [blame] | 2497 | } |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2498 | if (TextUtils.isEmpty(locale)) { |
satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 2499 | locale = res.getConfiguration().locale.toString(); |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2500 | } |
satok | 8fbb1e8 | 2010-11-02 23:15:58 +0900 | [diff] [blame] | 2501 | final String language = locale.substring(0, 2); |
| 2502 | boolean partialMatchFound = false; |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 2503 | InputMethodSubtype applicableSubtype = null; |
satok | 7599a7f | 2010-12-22 13:45:23 +0900 | [diff] [blame] | 2504 | InputMethodSubtype firstMatchedModeSubtype = null; |
satok | 16331c8 | 2010-12-20 23:48:46 +0900 | [diff] [blame] | 2505 | final int N = subtypes.size(); |
| 2506 | for (int i = 0; i < N; ++i) { |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 2507 | InputMethodSubtype subtype = subtypes.get(i); |
| 2508 | final String subtypeLocale = subtype.getLocale(); |
satok | d871343 | 2011-01-18 00:55:13 +0900 | [diff] [blame] | 2509 | // An applicable subtype should match "mode". If mode is null, mode will be ignored, |
| 2510 | // and all subtypes with all modes can be candidates. |
| 2511 | if (mode == null || subtypes.get(i).getMode().equalsIgnoreCase(mode)) { |
satok | 7599a7f | 2010-12-22 13:45:23 +0900 | [diff] [blame] | 2512 | if (firstMatchedModeSubtype == null) { |
| 2513 | firstMatchedModeSubtype = subtype; |
| 2514 | } |
satok | 9ef0283 | 2010-11-04 21:17:48 +0900 | [diff] [blame] | 2515 | if (locale.equals(subtypeLocale)) { |
| 2516 | // 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] | 2517 | applicableSubtype = subtype; |
satok | 9ef0283 | 2010-11-04 21:17:48 +0900 | [diff] [blame] | 2518 | break; |
| 2519 | } else if (!partialMatchFound && subtypeLocale.startsWith(language)) { |
| 2520 | // 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] | 2521 | applicableSubtype = subtype; |
satok | 9ef0283 | 2010-11-04 21:17:48 +0900 | [diff] [blame] | 2522 | partialMatchFound = true; |
| 2523 | } |
satok | 8fbb1e8 | 2010-11-02 23:15:58 +0900 | [diff] [blame] | 2524 | } |
| 2525 | } |
| 2526 | |
satok | 7599a7f | 2010-12-22 13:45:23 +0900 | [diff] [blame] | 2527 | if (applicableSubtype == null && canIgnoreLocaleAsLastResort) { |
| 2528 | return firstMatchedModeSubtype; |
satok | 16331c8 | 2010-12-20 23:48:46 +0900 | [diff] [blame] | 2529 | } |
| 2530 | |
satok | 8fbb1e8 | 2010-11-02 23:15:58 +0900 | [diff] [blame] | 2531 | // The first subtype applicable to the system locale will be defined as the most applicable |
| 2532 | // subtype. |
| 2533 | if (DEBUG) { |
satok | 16331c8 | 2010-12-20 23:48:46 +0900 | [diff] [blame] | 2534 | if (applicableSubtype != null) { |
| 2535 | Slog.d(TAG, "Applicable InputMethodSubtype was found: " |
| 2536 | + applicableSubtype.getMode() + "," + applicableSubtype.getLocale()); |
| 2537 | } |
satok | 8fbb1e8 | 2010-11-02 23:15:58 +0900 | [diff] [blame] | 2538 | } |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 2539 | return applicableSubtype; |
satok | 8fbb1e8 | 2010-11-02 23:15:58 +0900 | [diff] [blame] | 2540 | } |
| 2541 | |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2542 | // If there are no selected shortcuts, tries finding the most applicable ones. |
| 2543 | private Pair<InputMethodInfo, InputMethodSubtype> |
| 2544 | findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) { |
| 2545 | List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked(); |
| 2546 | InputMethodInfo mostApplicableIMI = null; |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 2547 | InputMethodSubtype mostApplicableSubtype = null; |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2548 | boolean foundInSystemIME = false; |
| 2549 | |
| 2550 | // Search applicable subtype for each InputMethodInfo |
| 2551 | for (InputMethodInfo imi: imis) { |
satok | 7599a7f | 2010-12-22 13:45:23 +0900 | [diff] [blame] | 2552 | final String imiId = imi.getId(); |
| 2553 | if (foundInSystemIME && !imiId.equals(mCurMethodId)) { |
| 2554 | continue; |
| 2555 | } |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 2556 | InputMethodSubtype subtype = null; |
satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 2557 | final List<InputMethodSubtype> enabledSubtypes = |
| 2558 | getEnabledInputMethodSubtypeList(imi, true); |
| 2559 | // 1. Search by the current subtype's locale from enabledSubtypes. |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2560 | if (mCurrentSubtype != null) { |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 2561 | subtype = findLastResortApplicableSubtypeLocked( |
satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 2562 | mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false); |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2563 | } |
satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 2564 | // 2. Search by the system locale from enabledSubtypes. |
| 2565 | // 3. Search the first enabled subtype matched with mode from enabledSubtypes. |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 2566 | if (subtype == null) { |
| 2567 | subtype = findLastResortApplicableSubtypeLocked( |
satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 2568 | mRes, enabledSubtypes, mode, null, true); |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2569 | } |
satok | a86f5e4 | 2011-09-02 17:12:42 +0900 | [diff] [blame] | 2570 | final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes = |
| 2571 | getOverridingImplicitlyEnabledSubtypes(imi, mode); |
| 2572 | final ArrayList<InputMethodSubtype> subtypesForSearch = |
| 2573 | overridingImplicitlyEnabledSubtypes.isEmpty() |
| 2574 | ? getSubtypes(imi) : overridingImplicitlyEnabledSubtypes; |
satok | 7599a7f | 2010-12-22 13:45:23 +0900 | [diff] [blame] | 2575 | // 4. Search by the current subtype's locale from all subtypes. |
| 2576 | if (subtype == null && mCurrentSubtype != null) { |
| 2577 | subtype = findLastResortApplicableSubtypeLocked( |
satok | a86f5e4 | 2011-09-02 17:12:42 +0900 | [diff] [blame] | 2578 | mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false); |
satok | 7599a7f | 2010-12-22 13:45:23 +0900 | [diff] [blame] | 2579 | } |
| 2580 | // 5. Search by the system locale from all subtypes. |
| 2581 | // 6. Search the first enabled subtype matched with mode from all subtypes. |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 2582 | if (subtype == null) { |
satok | 7599a7f | 2010-12-22 13:45:23 +0900 | [diff] [blame] | 2583 | subtype = findLastResortApplicableSubtypeLocked( |
satok | a86f5e4 | 2011-09-02 17:12:42 +0900 | [diff] [blame] | 2584 | mRes, subtypesForSearch, mode, null, true); |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2585 | } |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 2586 | if (subtype != null) { |
satok | 7599a7f | 2010-12-22 13:45:23 +0900 | [diff] [blame] | 2587 | if (imiId.equals(mCurMethodId)) { |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2588 | // The current input method is the most applicable IME. |
| 2589 | mostApplicableIMI = imi; |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 2590 | mostApplicableSubtype = subtype; |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2591 | break; |
satok | 7599a7f | 2010-12-22 13:45:23 +0900 | [diff] [blame] | 2592 | } else if (!foundInSystemIME) { |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2593 | // The system input method is 2nd applicable IME. |
| 2594 | mostApplicableIMI = imi; |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 2595 | mostApplicableSubtype = subtype; |
satok | 7599a7f | 2010-12-22 13:45:23 +0900 | [diff] [blame] | 2596 | if ((imi.getServiceInfo().applicationInfo.flags |
| 2597 | & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 2598 | foundInSystemIME = true; |
| 2599 | } |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2600 | } |
| 2601 | } |
| 2602 | } |
| 2603 | if (DEBUG) { |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 2604 | if (mostApplicableIMI != null) { |
| 2605 | Slog.w(TAG, "Most applicable shortcut input method was:" |
| 2606 | + mostApplicableIMI.getId()); |
| 2607 | if (mostApplicableSubtype != null) { |
| 2608 | Slog.w(TAG, "Most applicable shortcut input method subtype was:" |
| 2609 | + "," + mostApplicableSubtype.getMode() + "," |
| 2610 | + mostApplicableSubtype.getLocale()); |
| 2611 | } |
| 2612 | } |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2613 | } |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 2614 | if (mostApplicableIMI != null) { |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2615 | return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI, |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 2616 | mostApplicableSubtype); |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2617 | } else { |
| 2618 | return null; |
| 2619 | } |
| 2620 | } |
| 2621 | |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 2622 | /** |
| 2623 | * @return Return the current subtype of this input method. |
| 2624 | */ |
satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 2625 | @Override |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 2626 | public InputMethodSubtype getCurrentInputMethodSubtype() { |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2627 | boolean subtypeIsSelected = false; |
| 2628 | try { |
| 2629 | subtypeIsSelected = Settings.Secure.getInt(mContext.getContentResolver(), |
| 2630 | Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE) != NOT_A_SUBTYPE_ID; |
| 2631 | } catch (SettingNotFoundException e) { |
| 2632 | } |
satok | 3ef8b29 | 2010-11-23 06:06:29 +0900 | [diff] [blame] | 2633 | synchronized (mMethodMap) { |
satok | 3ef8b29 | 2010-11-23 06:06:29 +0900 | [diff] [blame] | 2634 | if (!subtypeIsSelected || mCurrentSubtype == null) { |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2635 | String lastInputMethodId = Settings.Secure.getString( |
| 2636 | mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD); |
satok | 3ef8b29 | 2010-11-23 06:06:29 +0900 | [diff] [blame] | 2637 | int subtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId); |
| 2638 | if (subtypeId == NOT_A_SUBTYPE_ID) { |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2639 | InputMethodInfo imi = mMethodMap.get(lastInputMethodId); |
| 2640 | if (imi != null) { |
| 2641 | // If there are no selected subtypes, the framework will try to find |
satok | d871343 | 2011-01-18 00:55:13 +0900 | [diff] [blame] | 2642 | // the most applicable subtype from explicitly or implicitly enabled |
| 2643 | // subtypes. |
| 2644 | List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes = |
| 2645 | getEnabledInputMethodSubtypeList(imi, true); |
| 2646 | // If there is only one explicitly or implicitly enabled subtype, |
| 2647 | // just returns it. |
| 2648 | if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) { |
| 2649 | mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0); |
| 2650 | } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) { |
| 2651 | mCurrentSubtype = findLastResortApplicableSubtypeLocked( |
| 2652 | mRes, explicitlyOrImplicitlyEnabledSubtypes, |
| 2653 | SUBTYPE_MODE_KEYBOARD, null, true); |
| 2654 | if (mCurrentSubtype == null) { |
| 2655 | mCurrentSubtype = findLastResortApplicableSubtypeLocked( |
| 2656 | mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null, |
| 2657 | true); |
| 2658 | } |
| 2659 | } |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2660 | } |
satok | cd7cd29 | 2010-11-20 15:46:23 +0900 | [diff] [blame] | 2661 | } else { |
satok | 3ef8b29 | 2010-11-23 06:06:29 +0900 | [diff] [blame] | 2662 | mCurrentSubtype = |
Ken Wakasa | 586f051 | 2011-01-20 22:31:01 +0900 | [diff] [blame] | 2663 | getSubtypes(mMethodMap.get(lastInputMethodId)).get(subtypeId); |
satok | 3ef8b29 | 2010-11-23 06:06:29 +0900 | [diff] [blame] | 2664 | } |
satok | 8fbb1e8 | 2010-11-02 23:15:58 +0900 | [diff] [blame] | 2665 | } |
satok | 3ef8b29 | 2010-11-23 06:06:29 +0900 | [diff] [blame] | 2666 | return mCurrentSubtype; |
satok | 8fbb1e8 | 2010-11-02 23:15:58 +0900 | [diff] [blame] | 2667 | } |
satok | ab751aa | 2010-09-14 19:17:36 +0900 | [diff] [blame] | 2668 | } |
| 2669 | |
satok | f3db1af | 2010-11-23 13:34:33 +0900 | [diff] [blame] | 2670 | private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi, |
| 2671 | InputMethodSubtype subtype) { |
| 2672 | if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) { |
| 2673 | mShortcutInputMethodsAndSubtypes.get(imi).add(subtype); |
| 2674 | } else { |
| 2675 | ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>(); |
| 2676 | subtypes.add(subtype); |
| 2677 | mShortcutInputMethodsAndSubtypes.put(imi, subtypes); |
| 2678 | } |
| 2679 | } |
| 2680 | |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2681 | // TODO: We should change the return type from List to List<Parcelable> |
satok | dbf2950 | 2011-08-25 15:28:23 +0900 | [diff] [blame] | 2682 | @SuppressWarnings("rawtypes") |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 2683 | @Override |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2684 | public List getShortcutInputMethodsAndSubtypes() { |
| 2685 | synchronized (mMethodMap) { |
satok | 3da9223 | 2011-01-11 22:46:30 +0900 | [diff] [blame] | 2686 | ArrayList<Object> ret = new ArrayList<Object>(); |
satok | f3db1af | 2010-11-23 13:34:33 +0900 | [diff] [blame] | 2687 | if (mShortcutInputMethodsAndSubtypes.size() == 0) { |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2688 | // If there are no selected shortcut subtypes, the framework will try to find |
| 2689 | // the most applicable subtype from all subtypes whose mode is |
| 2690 | // 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] | 2691 | Pair<InputMethodInfo, InputMethodSubtype> info = |
| 2692 | findLastResortApplicableShortcutInputMethodAndSubtypeLocked( |
| 2693 | SUBTYPE_MODE_VOICE); |
satok | 7599a7f | 2010-12-22 13:45:23 +0900 | [diff] [blame] | 2694 | if (info != null) { |
satok | 3da9223 | 2011-01-11 22:46:30 +0900 | [diff] [blame] | 2695 | ret.add(info.first); |
| 2696 | ret.add(info.second); |
satok | 7599a7f | 2010-12-22 13:45:23 +0900 | [diff] [blame] | 2697 | } |
satok | 3da9223 | 2011-01-11 22:46:30 +0900 | [diff] [blame] | 2698 | return ret; |
satok | f3db1af | 2010-11-23 13:34:33 +0900 | [diff] [blame] | 2699 | } |
satok | f3db1af | 2010-11-23 13:34:33 +0900 | [diff] [blame] | 2700 | for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) { |
| 2701 | ret.add(imi); |
| 2702 | for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) { |
| 2703 | ret.add(subtype); |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2704 | } |
| 2705 | } |
satok | f3db1af | 2010-11-23 13:34:33 +0900 | [diff] [blame] | 2706 | return ret; |
satok | 4e4569d | 2010-11-19 18:45:53 +0900 | [diff] [blame] | 2707 | } |
| 2708 | } |
| 2709 | |
satok | 42c5a16 | 2011-05-26 16:46:14 +0900 | [diff] [blame] | 2710 | @Override |
satok | b66d287 | 2010-11-10 01:04:04 +0900 | [diff] [blame] | 2711 | public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) { |
| 2712 | synchronized (mMethodMap) { |
| 2713 | if (subtype != null && mCurMethodId != null) { |
| 2714 | InputMethodInfo imi = mMethodMap.get(mCurMethodId); |
| 2715 | int subtypeId = getSubtypeIdFromHashCode(imi, subtype.hashCode()); |
| 2716 | if (subtypeId != NOT_A_SUBTYPE_ID) { |
| 2717 | setInputMethodLocked(mCurMethodId, subtypeId); |
| 2718 | return true; |
| 2719 | } |
| 2720 | } |
| 2721 | return false; |
| 2722 | } |
| 2723 | } |
| 2724 | |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2725 | /** |
| 2726 | * Utility class for putting and getting settings for InputMethod |
| 2727 | * TODO: Move all putters and getters of settings to this class. |
| 2728 | */ |
| 2729 | private static class InputMethodSettings { |
| 2730 | // The string for enabled input method is saved as follows: |
| 2731 | // example: ("ime0;subtype0;subtype1;subtype2:ime1:ime2;subtype0") |
| 2732 | private static final char INPUT_METHOD_SEPARATER = ':'; |
| 2733 | private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';'; |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 2734 | private final TextUtils.SimpleStringSplitter mInputMethodSplitter = |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2735 | new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER); |
| 2736 | |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 2737 | private final TextUtils.SimpleStringSplitter mSubtypeSplitter = |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2738 | new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATER); |
| 2739 | |
satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 2740 | private final Resources mRes; |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2741 | private final ContentResolver mResolver; |
| 2742 | private final HashMap<String, InputMethodInfo> mMethodMap; |
| 2743 | private final ArrayList<InputMethodInfo> mMethodList; |
| 2744 | |
| 2745 | private String mEnabledInputMethodsStrCache; |
| 2746 | |
| 2747 | private static void buildEnabledInputMethodsSettingString( |
| 2748 | StringBuilder builder, Pair<String, ArrayList<String>> pair) { |
| 2749 | String id = pair.first; |
| 2750 | ArrayList<String> subtypes = pair.second; |
| 2751 | builder.append(id); |
satok | 57c767c | 2010-11-01 22:34:08 +0900 | [diff] [blame] | 2752 | // Inputmethod and subtypes are saved in the settings as follows: |
| 2753 | // ime0;subtype0;subtype1:ime1;subtype0:ime2:ime3;subtype0;subtype1 |
| 2754 | for (String subtypeId: subtypes) { |
| 2755 | builder.append(INPUT_METHOD_SUBTYPE_SEPARATER).append(subtypeId); |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2756 | } |
| 2757 | } |
| 2758 | |
| 2759 | public InputMethodSettings( |
satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 2760 | Resources res, ContentResolver resolver, |
| 2761 | HashMap<String, InputMethodInfo> methodMap, ArrayList<InputMethodInfo> methodList) { |
| 2762 | mRes = res; |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2763 | mResolver = resolver; |
| 2764 | mMethodMap = methodMap; |
| 2765 | mMethodList = methodList; |
| 2766 | } |
| 2767 | |
| 2768 | public List<InputMethodInfo> getEnabledInputMethodListLocked() { |
| 2769 | return createEnabledInputMethodListLocked( |
| 2770 | getEnabledInputMethodsAndSubtypeListLocked()); |
| 2771 | } |
| 2772 | |
satok | 7f35c8c | 2010-10-07 21:13:11 +0900 | [diff] [blame] | 2773 | public List<Pair<InputMethodInfo, ArrayList<String>>> |
satok | 67ddf9c | 2010-11-17 09:45:54 +0900 | [diff] [blame] | 2774 | getEnabledInputMethodAndSubtypeHashCodeListLocked() { |
| 2775 | return createEnabledInputMethodAndSubtypeHashCodeListLocked( |
satok | 7f35c8c | 2010-10-07 21:13:11 +0900 | [diff] [blame] | 2776 | getEnabledInputMethodsAndSubtypeListLocked()); |
| 2777 | } |
| 2778 | |
satok | 67ddf9c | 2010-11-17 09:45:54 +0900 | [diff] [blame] | 2779 | public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked( |
| 2780 | InputMethodInfo imi) { |
| 2781 | List<Pair<String, ArrayList<String>>> imsList = |
| 2782 | getEnabledInputMethodsAndSubtypeListLocked(); |
| 2783 | ArrayList<InputMethodSubtype> enabledSubtypes = |
| 2784 | new ArrayList<InputMethodSubtype>(); |
satok | 884ef9a | 2010-11-18 10:39:46 +0900 | [diff] [blame] | 2785 | if (imi != null) { |
| 2786 | for (Pair<String, ArrayList<String>> imsPair : imsList) { |
| 2787 | InputMethodInfo info = mMethodMap.get(imsPair.first); |
| 2788 | if (info != null && info.getId().equals(imi.getId())) { |
Ken Wakasa | 586f051 | 2011-01-20 22:31:01 +0900 | [diff] [blame] | 2789 | final int subtypeCount = info.getSubtypeCount(); |
| 2790 | for (int i = 0; i < subtypeCount; ++i) { |
| 2791 | InputMethodSubtype ims = info.getSubtypeAt(i); |
satok | 884ef9a | 2010-11-18 10:39:46 +0900 | [diff] [blame] | 2792 | for (String s: imsPair.second) { |
| 2793 | if (String.valueOf(ims.hashCode()).equals(s)) { |
| 2794 | enabledSubtypes.add(ims); |
| 2795 | } |
satok | 67ddf9c | 2010-11-17 09:45:54 +0900 | [diff] [blame] | 2796 | } |
| 2797 | } |
satok | 884ef9a | 2010-11-18 10:39:46 +0900 | [diff] [blame] | 2798 | break; |
satok | 67ddf9c | 2010-11-17 09:45:54 +0900 | [diff] [blame] | 2799 | } |
satok | 67ddf9c | 2010-11-17 09:45:54 +0900 | [diff] [blame] | 2800 | } |
| 2801 | } |
| 2802 | return enabledSubtypes; |
| 2803 | } |
| 2804 | |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2805 | // At the initial boot, the settings for input methods are not set, |
| 2806 | // so we need to enable IME in that case. |
| 2807 | public void enableAllIMEsIfThereIsNoEnabledIME() { |
| 2808 | if (TextUtils.isEmpty(getEnabledInputMethodsStr())) { |
| 2809 | StringBuilder sb = new StringBuilder(); |
| 2810 | final int N = mMethodList.size(); |
| 2811 | for (int i = 0; i < N; i++) { |
| 2812 | InputMethodInfo imi = mMethodList.get(i); |
| 2813 | Slog.i(TAG, "Adding: " + imi.getId()); |
| 2814 | if (i > 0) sb.append(':'); |
| 2815 | sb.append(imi.getId()); |
| 2816 | } |
| 2817 | putEnabledInputMethodsStr(sb.toString()); |
| 2818 | } |
| 2819 | } |
| 2820 | |
satok | bb4aa06 | 2011-01-19 21:40:27 +0900 | [diff] [blame] | 2821 | private List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() { |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2822 | ArrayList<Pair<String, ArrayList<String>>> imsList |
| 2823 | = new ArrayList<Pair<String, ArrayList<String>>>(); |
| 2824 | final String enabledInputMethodsStr = getEnabledInputMethodsStr(); |
| 2825 | if (TextUtils.isEmpty(enabledInputMethodsStr)) { |
| 2826 | return imsList; |
| 2827 | } |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 2828 | mInputMethodSplitter.setString(enabledInputMethodsStr); |
| 2829 | while (mInputMethodSplitter.hasNext()) { |
| 2830 | String nextImsStr = mInputMethodSplitter.next(); |
| 2831 | mSubtypeSplitter.setString(nextImsStr); |
| 2832 | if (mSubtypeSplitter.hasNext()) { |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2833 | ArrayList<String> subtypeHashes = new ArrayList<String>(); |
| 2834 | // The first element is ime id. |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 2835 | String imeId = mSubtypeSplitter.next(); |
| 2836 | while (mSubtypeSplitter.hasNext()) { |
| 2837 | subtypeHashes.add(mSubtypeSplitter.next()); |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2838 | } |
| 2839 | imsList.add(new Pair<String, ArrayList<String>>(imeId, subtypeHashes)); |
| 2840 | } |
| 2841 | } |
| 2842 | return imsList; |
| 2843 | } |
| 2844 | |
| 2845 | public void appendAndPutEnabledInputMethodLocked(String id, boolean reloadInputMethodStr) { |
| 2846 | if (reloadInputMethodStr) { |
| 2847 | getEnabledInputMethodsStr(); |
| 2848 | } |
| 2849 | if (TextUtils.isEmpty(mEnabledInputMethodsStrCache)) { |
| 2850 | // Add in the newly enabled input method. |
| 2851 | putEnabledInputMethodsStr(id); |
| 2852 | } else { |
| 2853 | putEnabledInputMethodsStr( |
| 2854 | mEnabledInputMethodsStrCache + INPUT_METHOD_SEPARATER + id); |
| 2855 | } |
| 2856 | } |
| 2857 | |
| 2858 | /** |
| 2859 | * Build and put a string of EnabledInputMethods with removing specified Id. |
| 2860 | * @return the specified id was removed or not. |
| 2861 | */ |
| 2862 | public boolean buildAndPutEnabledInputMethodsStrRemovingIdLocked( |
| 2863 | StringBuilder builder, List<Pair<String, ArrayList<String>>> imsList, String id) { |
| 2864 | boolean isRemoved = false; |
| 2865 | boolean needsAppendSeparator = false; |
| 2866 | for (Pair<String, ArrayList<String>> ims: imsList) { |
| 2867 | String curId = ims.first; |
| 2868 | if (curId.equals(id)) { |
| 2869 | // We are disabling this input method, and it is |
| 2870 | // currently enabled. Skip it to remove from the |
| 2871 | // new list. |
| 2872 | isRemoved = true; |
| 2873 | } else { |
| 2874 | if (needsAppendSeparator) { |
| 2875 | builder.append(INPUT_METHOD_SEPARATER); |
| 2876 | } else { |
| 2877 | needsAppendSeparator = true; |
| 2878 | } |
| 2879 | buildEnabledInputMethodsSettingString(builder, ims); |
| 2880 | } |
| 2881 | } |
| 2882 | if (isRemoved) { |
| 2883 | // Update the setting with the new list of input methods. |
| 2884 | putEnabledInputMethodsStr(builder.toString()); |
| 2885 | } |
| 2886 | return isRemoved; |
| 2887 | } |
| 2888 | |
| 2889 | private List<InputMethodInfo> createEnabledInputMethodListLocked( |
| 2890 | List<Pair<String, ArrayList<String>>> imsList) { |
| 2891 | final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>(); |
| 2892 | for (Pair<String, ArrayList<String>> ims: imsList) { |
| 2893 | InputMethodInfo info = mMethodMap.get(ims.first); |
| 2894 | if (info != null) { |
| 2895 | res.add(info); |
| 2896 | } |
| 2897 | } |
| 2898 | return res; |
| 2899 | } |
| 2900 | |
satok | 7f35c8c | 2010-10-07 21:13:11 +0900 | [diff] [blame] | 2901 | private List<Pair<InputMethodInfo, ArrayList<String>>> |
satok | 67ddf9c | 2010-11-17 09:45:54 +0900 | [diff] [blame] | 2902 | createEnabledInputMethodAndSubtypeHashCodeListLocked( |
satok | 7f35c8c | 2010-10-07 21:13:11 +0900 | [diff] [blame] | 2903 | List<Pair<String, ArrayList<String>>> imsList) { |
| 2904 | final ArrayList<Pair<InputMethodInfo, ArrayList<String>>> res |
| 2905 | = new ArrayList<Pair<InputMethodInfo, ArrayList<String>>>(); |
| 2906 | for (Pair<String, ArrayList<String>> ims : imsList) { |
| 2907 | InputMethodInfo info = mMethodMap.get(ims.first); |
| 2908 | if (info != null) { |
| 2909 | res.add(new Pair<InputMethodInfo, ArrayList<String>>(info, ims.second)); |
| 2910 | } |
| 2911 | } |
| 2912 | return res; |
| 2913 | } |
| 2914 | |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2915 | private void putEnabledInputMethodsStr(String str) { |
| 2916 | Settings.Secure.putString(mResolver, Settings.Secure.ENABLED_INPUT_METHODS, str); |
| 2917 | mEnabledInputMethodsStrCache = str; |
| 2918 | } |
| 2919 | |
| 2920 | private String getEnabledInputMethodsStr() { |
| 2921 | mEnabledInputMethodsStrCache = Settings.Secure.getString( |
| 2922 | mResolver, Settings.Secure.ENABLED_INPUT_METHODS); |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 2923 | if (DEBUG) { |
| 2924 | Slog.d(TAG, "getEnabledInputMethodsStr: " + mEnabledInputMethodsStrCache); |
| 2925 | } |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 2926 | return mEnabledInputMethodsStrCache; |
| 2927 | } |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 2928 | |
| 2929 | private void saveSubtypeHistory( |
| 2930 | List<Pair<String, String>> savedImes, String newImeId, String newSubtypeId) { |
| 2931 | StringBuilder builder = new StringBuilder(); |
| 2932 | boolean isImeAdded = false; |
| 2933 | if (!TextUtils.isEmpty(newImeId) && !TextUtils.isEmpty(newSubtypeId)) { |
| 2934 | builder.append(newImeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append( |
| 2935 | newSubtypeId); |
| 2936 | isImeAdded = true; |
| 2937 | } |
| 2938 | for (Pair<String, String> ime: savedImes) { |
| 2939 | String imeId = ime.first; |
| 2940 | String subtypeId = ime.second; |
| 2941 | if (TextUtils.isEmpty(subtypeId)) { |
| 2942 | subtypeId = NOT_A_SUBTYPE_ID_STR; |
| 2943 | } |
| 2944 | if (isImeAdded) { |
| 2945 | builder.append(INPUT_METHOD_SEPARATER); |
| 2946 | } else { |
| 2947 | isImeAdded = true; |
| 2948 | } |
| 2949 | builder.append(imeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append( |
| 2950 | subtypeId); |
| 2951 | } |
| 2952 | // Remove the last INPUT_METHOD_SEPARATER |
| 2953 | putSubtypeHistoryStr(builder.toString()); |
| 2954 | } |
| 2955 | |
| 2956 | public void addSubtypeToHistory(String imeId, String subtypeId) { |
| 2957 | List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked(); |
| 2958 | for (Pair<String, String> ime: subtypeHistory) { |
| 2959 | if (ime.first.equals(imeId)) { |
| 2960 | if (DEBUG) { |
satok | bb4aa06 | 2011-01-19 21:40:27 +0900 | [diff] [blame] | 2961 | Slog.v(TAG, "Subtype found in the history: " + imeId + ", " |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 2962 | + ime.second); |
| 2963 | } |
| 2964 | // We should break here |
| 2965 | subtypeHistory.remove(ime); |
| 2966 | break; |
| 2967 | } |
| 2968 | } |
satok | bb4aa06 | 2011-01-19 21:40:27 +0900 | [diff] [blame] | 2969 | if (DEBUG) { |
| 2970 | Slog.v(TAG, "Add subtype to the history: " + imeId + ", " + subtypeId); |
| 2971 | } |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 2972 | saveSubtypeHistory(subtypeHistory, imeId, subtypeId); |
| 2973 | } |
| 2974 | |
| 2975 | private void putSubtypeHistoryStr(String str) { |
| 2976 | if (DEBUG) { |
| 2977 | Slog.d(TAG, "putSubtypeHistoryStr: " + str); |
| 2978 | } |
| 2979 | Settings.Secure.putString( |
| 2980 | mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, str); |
| 2981 | } |
| 2982 | |
| 2983 | public Pair<String, String> getLastInputMethodAndSubtypeLocked() { |
| 2984 | // Gets the first one from the history |
| 2985 | return getLastSubtypeForInputMethodLockedInternal(null); |
| 2986 | } |
| 2987 | |
| 2988 | public String getLastSubtypeForInputMethodLocked(String imeId) { |
| 2989 | Pair<String, String> ime = getLastSubtypeForInputMethodLockedInternal(imeId); |
| 2990 | if (ime != null) { |
| 2991 | return ime.second; |
| 2992 | } else { |
| 2993 | return null; |
| 2994 | } |
| 2995 | } |
| 2996 | |
| 2997 | private Pair<String, String> getLastSubtypeForInputMethodLockedInternal(String imeId) { |
| 2998 | List<Pair<String, ArrayList<String>>> enabledImes = |
| 2999 | getEnabledInputMethodsAndSubtypeListLocked(); |
| 3000 | List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked(); |
satok | 4fc87d6 | 2011-05-20 16:13:43 +0900 | [diff] [blame] | 3001 | for (Pair<String, String> imeAndSubtype : subtypeHistory) { |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 3002 | final String imeInTheHistory = imeAndSubtype.first; |
| 3003 | // If imeId is empty, returns the first IME and subtype in the history |
| 3004 | if (TextUtils.isEmpty(imeId) || imeInTheHistory.equals(imeId)) { |
| 3005 | final String subtypeInTheHistory = imeAndSubtype.second; |
satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 3006 | final String subtypeHashCode = |
| 3007 | getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked( |
| 3008 | enabledImes, imeInTheHistory, subtypeInTheHistory); |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 3009 | if (!TextUtils.isEmpty(subtypeHashCode)) { |
| 3010 | if (DEBUG) { |
satok | bb4aa06 | 2011-01-19 21:40:27 +0900 | [diff] [blame] | 3011 | Slog.d(TAG, "Enabled subtype found in the history: " + subtypeHashCode); |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 3012 | } |
| 3013 | return new Pair<String, String>(imeInTheHistory, subtypeHashCode); |
| 3014 | } |
| 3015 | } |
| 3016 | } |
| 3017 | if (DEBUG) { |
| 3018 | Slog.d(TAG, "No enabled IME found in the history"); |
| 3019 | } |
| 3020 | return null; |
| 3021 | } |
| 3022 | |
satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 3023 | private String getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(List<Pair<String, |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 3024 | ArrayList<String>>> enabledImes, String imeId, String subtypeHashCode) { |
| 3025 | for (Pair<String, ArrayList<String>> enabledIme: enabledImes) { |
| 3026 | if (enabledIme.first.equals(imeId)) { |
satok | f6cafb6 | 2011-01-17 16:29:02 +0900 | [diff] [blame] | 3027 | final ArrayList<String> explicitlyEnabledSubtypes = enabledIme.second; |
| 3028 | if (explicitlyEnabledSubtypes.size() == 0) { |
| 3029 | // If there are no explicitly enabled subtypes, applicable subtypes are |
| 3030 | // enabled implicitly. |
satok | a86f5e4 | 2011-09-02 17:12:42 +0900 | [diff] [blame] | 3031 | InputMethodInfo imi = mMethodMap.get(imeId); |
satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 3032 | // If IME is enabled and no subtypes are enabled, applicable subtypes |
| 3033 | // are enabled implicitly, so needs to treat them to be enabled. |
satok | a86f5e4 | 2011-09-02 17:12:42 +0900 | [diff] [blame] | 3034 | if (imi != null && imi.getSubtypeCount() > 0) { |
satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 3035 | List<InputMethodSubtype> implicitlySelectedSubtypes = |
satok | a86f5e4 | 2011-09-02 17:12:42 +0900 | [diff] [blame] | 3036 | getImplicitlyApplicableSubtypesLocked(mRes, imi); |
satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 3037 | if (implicitlySelectedSubtypes != null) { |
| 3038 | final int N = implicitlySelectedSubtypes.size(); |
| 3039 | for (int i = 0; i < N; ++i) { |
| 3040 | final InputMethodSubtype st = implicitlySelectedSubtypes.get(i); |
| 3041 | if (String.valueOf(st.hashCode()).equals(subtypeHashCode)) { |
| 3042 | return subtypeHashCode; |
| 3043 | } |
| 3044 | } |
| 3045 | } |
| 3046 | } |
| 3047 | } else { |
satok | f6cafb6 | 2011-01-17 16:29:02 +0900 | [diff] [blame] | 3048 | for (String s: explicitlyEnabledSubtypes) { |
satok | df31ae6 | 2011-01-15 06:19:44 +0900 | [diff] [blame] | 3049 | if (s.equals(subtypeHashCode)) { |
| 3050 | // If both imeId and subtypeId are enabled, return subtypeId. |
| 3051 | return s; |
| 3052 | } |
satok | 723a27e | 2010-11-11 14:58:11 +0900 | [diff] [blame] | 3053 | } |
| 3054 | } |
| 3055 | // If imeId was enabled but subtypeId was disabled. |
| 3056 | return NOT_A_SUBTYPE_ID_STR; |
| 3057 | } |
| 3058 | } |
| 3059 | // If both imeId and subtypeId are disabled, return null |
| 3060 | return null; |
| 3061 | } |
| 3062 | |
| 3063 | private List<Pair<String, String>> loadInputMethodAndSubtypeHistoryLocked() { |
| 3064 | ArrayList<Pair<String, String>> imsList = new ArrayList<Pair<String, String>>(); |
| 3065 | final String subtypeHistoryStr = getSubtypeHistoryStr(); |
| 3066 | if (TextUtils.isEmpty(subtypeHistoryStr)) { |
| 3067 | return imsList; |
| 3068 | } |
| 3069 | mInputMethodSplitter.setString(subtypeHistoryStr); |
| 3070 | while (mInputMethodSplitter.hasNext()) { |
| 3071 | String nextImsStr = mInputMethodSplitter.next(); |
| 3072 | mSubtypeSplitter.setString(nextImsStr); |
| 3073 | if (mSubtypeSplitter.hasNext()) { |
| 3074 | String subtypeId = NOT_A_SUBTYPE_ID_STR; |
| 3075 | // The first element is ime id. |
| 3076 | String imeId = mSubtypeSplitter.next(); |
| 3077 | while (mSubtypeSplitter.hasNext()) { |
| 3078 | subtypeId = mSubtypeSplitter.next(); |
| 3079 | break; |
| 3080 | } |
| 3081 | imsList.add(new Pair<String, String>(imeId, subtypeId)); |
| 3082 | } |
| 3083 | } |
| 3084 | return imsList; |
| 3085 | } |
| 3086 | |
| 3087 | private String getSubtypeHistoryStr() { |
| 3088 | if (DEBUG) { |
| 3089 | Slog.d(TAG, "getSubtypeHistoryStr: " + Settings.Secure.getString( |
| 3090 | mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY)); |
| 3091 | } |
| 3092 | return Settings.Secure.getString( |
| 3093 | mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY); |
| 3094 | } |
| 3095 | |
| 3096 | public void putSelectedInputMethod(String imeId) { |
| 3097 | Settings.Secure.putString(mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, imeId); |
| 3098 | } |
| 3099 | |
| 3100 | public void putSelectedSubtype(int subtypeId) { |
| 3101 | Settings.Secure.putInt( |
| 3102 | mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, subtypeId); |
| 3103 | } |
satok | d87c259 | 2010-09-29 11:52:06 +0900 | [diff] [blame] | 3104 | } |
| 3105 | |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 3106 | private static class InputMethodFileManager { |
| 3107 | private static final String SYSTEM_PATH = "system"; |
| 3108 | private static final String INPUT_METHOD_PATH = "inputmethod"; |
| 3109 | private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml"; |
| 3110 | private static final String NODE_SUBTYPES = "subtypes"; |
| 3111 | private static final String NODE_SUBTYPE = "subtype"; |
| 3112 | private static final String NODE_IMI = "imi"; |
| 3113 | private static final String ATTR_ID = "id"; |
| 3114 | private static final String ATTR_LABEL = "label"; |
| 3115 | private static final String ATTR_ICON = "icon"; |
| 3116 | private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale"; |
| 3117 | private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode"; |
| 3118 | private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue"; |
| 3119 | private static final String ATTR_IS_AUXILIARY = "isAuxiliary"; |
| 3120 | private final AtomicFile mAdditionalInputMethodSubtypeFile; |
| 3121 | private final HashMap<String, InputMethodInfo> mMethodMap; |
| 3122 | private final HashMap<String, List<InputMethodSubtype>> mSubtypesMap = |
| 3123 | new HashMap<String, List<InputMethodSubtype>>(); |
| 3124 | public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap) { |
| 3125 | if (methodMap == null) { |
| 3126 | throw new NullPointerException("methodMap is null"); |
| 3127 | } |
| 3128 | mMethodMap = methodMap; |
| 3129 | final File systemDir = new File(Environment.getDataDirectory(), SYSTEM_PATH); |
| 3130 | final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH); |
| 3131 | if (!inputMethodDir.mkdirs()) { |
| 3132 | Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath()); |
| 3133 | } |
| 3134 | final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME); |
| 3135 | mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile); |
| 3136 | if (!subtypeFile.exists()) { |
| 3137 | // If "subtypes.xml" doesn't exist, create a blank file. |
| 3138 | writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile, |
| 3139 | methodMap); |
| 3140 | } else { |
| 3141 | readAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile); |
| 3142 | } |
| 3143 | } |
| 3144 | |
| 3145 | private void deleteAllInputMethodSubtypes(String imiId) { |
| 3146 | synchronized (mMethodMap) { |
| 3147 | mSubtypesMap.remove(imiId); |
| 3148 | writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile, |
| 3149 | mMethodMap); |
| 3150 | } |
| 3151 | } |
| 3152 | |
| 3153 | public void addInputMethodSubtypes( |
satok | 4a28bde | 2011-06-29 21:03:40 +0900 | [diff] [blame] | 3154 | InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) { |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 3155 | synchronized (mMethodMap) { |
| 3156 | final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>(); |
| 3157 | final int N = additionalSubtypes.length; |
| 3158 | for (int i = 0; i < N; ++i) { |
| 3159 | final InputMethodSubtype subtype = additionalSubtypes[i]; |
satok | ed2b24e | 2011-08-31 18:03:21 +0900 | [diff] [blame] | 3160 | if (!subtypes.contains(subtype)) { |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 3161 | subtypes.add(subtype); |
| 3162 | } |
| 3163 | } |
satok | 4a28bde | 2011-06-29 21:03:40 +0900 | [diff] [blame] | 3164 | mSubtypesMap.put(imi.getId(), subtypes); |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 3165 | writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile, |
| 3166 | mMethodMap); |
| 3167 | } |
| 3168 | } |
| 3169 | |
| 3170 | public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() { |
| 3171 | synchronized (mMethodMap) { |
| 3172 | return mSubtypesMap; |
| 3173 | } |
| 3174 | } |
| 3175 | |
| 3176 | private static void writeAdditionalInputMethodSubtypes( |
| 3177 | HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile, |
| 3178 | HashMap<String, InputMethodInfo> methodMap) { |
| 3179 | // Safety net for the case that this function is called before methodMap is set. |
| 3180 | final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0; |
| 3181 | FileOutputStream fos = null; |
| 3182 | try { |
| 3183 | fos = subtypesFile.startWrite(); |
| 3184 | final XmlSerializer out = new FastXmlSerializer(); |
| 3185 | out.setOutput(fos, "utf-8"); |
| 3186 | out.startDocument(null, true); |
| 3187 | out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); |
| 3188 | out.startTag(null, NODE_SUBTYPES); |
| 3189 | for (String imiId : allSubtypes.keySet()) { |
| 3190 | if (isSetMethodMap && !methodMap.containsKey(imiId)) { |
| 3191 | Slog.w(TAG, "IME uninstalled or not valid.: " + imiId); |
| 3192 | continue; |
| 3193 | } |
| 3194 | out.startTag(null, NODE_IMI); |
| 3195 | out.attribute(null, ATTR_ID, imiId); |
| 3196 | final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId); |
| 3197 | final int N = subtypesList.size(); |
| 3198 | for (int i = 0; i < N; ++i) { |
| 3199 | final InputMethodSubtype subtype = subtypesList.get(i); |
| 3200 | out.startTag(null, NODE_SUBTYPE); |
| 3201 | out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId())); |
| 3202 | out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId())); |
| 3203 | out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale()); |
| 3204 | out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode()); |
| 3205 | out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue()); |
| 3206 | out.attribute(null, ATTR_IS_AUXILIARY, |
| 3207 | String.valueOf(subtype.isAuxiliary() ? 1 : 0)); |
| 3208 | out.endTag(null, NODE_SUBTYPE); |
| 3209 | } |
| 3210 | out.endTag(null, NODE_IMI); |
| 3211 | } |
| 3212 | out.endTag(null, NODE_SUBTYPES); |
| 3213 | out.endDocument(); |
| 3214 | subtypesFile.finishWrite(fos); |
| 3215 | } catch (java.io.IOException e) { |
| 3216 | Slog.w(TAG, "Error writing subtypes", e); |
| 3217 | if (fos != null) { |
| 3218 | subtypesFile.failWrite(fos); |
| 3219 | } |
| 3220 | } |
| 3221 | } |
| 3222 | |
| 3223 | private static void readAdditionalInputMethodSubtypes( |
| 3224 | HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) { |
| 3225 | if (allSubtypes == null || subtypesFile == null) return; |
| 3226 | allSubtypes.clear(); |
| 3227 | FileInputStream fis = null; |
| 3228 | try { |
| 3229 | fis = subtypesFile.openRead(); |
| 3230 | final XmlPullParser parser = Xml.newPullParser(); |
| 3231 | parser.setInput(fis, null); |
| 3232 | int type = parser.getEventType(); |
| 3233 | // Skip parsing until START_TAG |
| 3234 | while ((type = parser.next()) != XmlPullParser.START_TAG |
| 3235 | && type != XmlPullParser.END_DOCUMENT) {} |
| 3236 | String firstNodeName = parser.getName(); |
| 3237 | if (!NODE_SUBTYPES.equals(firstNodeName)) { |
| 3238 | throw new XmlPullParserException("Xml doesn't start with subtypes"); |
| 3239 | } |
| 3240 | final int depth =parser.getDepth(); |
| 3241 | String currentImiId = null; |
| 3242 | ArrayList<InputMethodSubtype> tempSubtypesArray = null; |
| 3243 | while (((type = parser.next()) != XmlPullParser.END_TAG |
| 3244 | || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) { |
| 3245 | if (type != XmlPullParser.START_TAG) |
| 3246 | continue; |
| 3247 | final String nodeName = parser.getName(); |
| 3248 | if (NODE_IMI.equals(nodeName)) { |
| 3249 | currentImiId = parser.getAttributeValue(null, ATTR_ID); |
| 3250 | if (TextUtils.isEmpty(currentImiId)) { |
| 3251 | Slog.w(TAG, "Invalid imi id found in subtypes.xml"); |
| 3252 | continue; |
| 3253 | } |
| 3254 | tempSubtypesArray = new ArrayList<InputMethodSubtype>(); |
| 3255 | allSubtypes.put(currentImiId, tempSubtypesArray); |
| 3256 | } else if (NODE_SUBTYPE.equals(nodeName)) { |
| 3257 | if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) { |
| 3258 | Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId); |
| 3259 | continue; |
| 3260 | } |
| 3261 | final int icon = Integer.valueOf( |
| 3262 | parser.getAttributeValue(null, ATTR_ICON)); |
| 3263 | final int label = Integer.valueOf( |
| 3264 | parser.getAttributeValue(null, ATTR_LABEL)); |
| 3265 | final String imeSubtypeLocale = |
| 3266 | parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE); |
| 3267 | final String imeSubtypeMode = |
| 3268 | parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE); |
| 3269 | final String imeSubtypeExtraValue = |
| 3270 | parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE); |
satok | 4a28bde | 2011-06-29 21:03:40 +0900 | [diff] [blame] | 3271 | final boolean isAuxiliary = "1".equals(String.valueOf( |
| 3272 | parser.getAttributeValue(null, ATTR_IS_AUXILIARY))); |
satok | e7c6998e | 2011-06-03 17:57:59 +0900 | [diff] [blame] | 3273 | final InputMethodSubtype subtype = |
| 3274 | new InputMethodSubtype(label, icon, imeSubtypeLocale, |
| 3275 | imeSubtypeMode, imeSubtypeExtraValue, isAuxiliary); |
| 3276 | tempSubtypesArray.add(subtype); |
| 3277 | } |
| 3278 | } |
| 3279 | } catch (XmlPullParserException e) { |
| 3280 | Slog.w(TAG, "Error reading subtypes: " + e); |
| 3281 | return; |
| 3282 | } catch (java.io.IOException e) { |
| 3283 | Slog.w(TAG, "Error reading subtypes: " + e); |
| 3284 | return; |
| 3285 | } catch (NumberFormatException e) { |
| 3286 | Slog.w(TAG, "Error reading subtypes: " + e); |
| 3287 | return; |
| 3288 | } finally { |
| 3289 | if (fis != null) { |
| 3290 | try { |
| 3291 | fis.close(); |
| 3292 | } catch (java.io.IOException e1) { |
| 3293 | Slog.w(TAG, "Failed to close."); |
| 3294 | } |
| 3295 | } |
| 3296 | } |
| 3297 | } |
| 3298 | } |
| 3299 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3300 | // ---------------------------------------------------------------------- |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3301 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3302 | @Override |
| 3303 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 3304 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 3305 | != PackageManager.PERMISSION_GRANTED) { |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3306 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3307 | pw.println("Permission Denial: can't dump InputMethodManager from from pid=" |
| 3308 | + Binder.getCallingPid() |
| 3309 | + ", uid=" + Binder.getCallingUid()); |
| 3310 | return; |
| 3311 | } |
| 3312 | |
| 3313 | IInputMethod method; |
| 3314 | ClientState client; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3315 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3316 | final Printer p = new PrintWriterPrinter(pw); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3317 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3318 | synchronized (mMethodMap) { |
| 3319 | p.println("Current Input Method Manager state:"); |
| 3320 | int N = mMethodList.size(); |
| 3321 | p.println(" Input Methods:"); |
| 3322 | for (int i=0; i<N; i++) { |
| 3323 | InputMethodInfo info = mMethodList.get(i); |
| 3324 | p.println(" InputMethod #" + i + ":"); |
| 3325 | info.dump(p, " "); |
| 3326 | } |
| 3327 | p.println(" Clients:"); |
| 3328 | for (ClientState ci : mClients.values()) { |
| 3329 | p.println(" Client " + ci + ":"); |
| 3330 | p.println(" client=" + ci.client); |
| 3331 | p.println(" inputContext=" + ci.inputContext); |
| 3332 | p.println(" sessionRequested=" + ci.sessionRequested); |
| 3333 | p.println(" curSession=" + ci.curSession); |
| 3334 | } |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 3335 | p.println(" mCurMethodId=" + mCurMethodId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3336 | client = mCurClient; |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 3337 | p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq); |
| 3338 | p.println(" mCurFocusedWindow=" + mCurFocusedWindow); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3339 | p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection |
| 3340 | + " mBoundToMethod=" + mBoundToMethod); |
| 3341 | p.println(" mCurToken=" + mCurToken); |
| 3342 | p.println(" mCurIntent=" + mCurIntent); |
| 3343 | method = mCurMethod; |
| 3344 | p.println(" mCurMethod=" + mCurMethod); |
| 3345 | p.println(" mEnabledSession=" + mEnabledSession); |
| 3346 | p.println(" mShowRequested=" + mShowRequested |
| 3347 | + " mShowExplicitlyRequested=" + mShowExplicitlyRequested |
| 3348 | + " mShowForced=" + mShowForced |
| 3349 | + " mInputShown=" + mInputShown); |
Dianne Hackborn | cc27870 | 2009-09-02 23:07:23 -0700 | [diff] [blame] | 3350 | p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3351 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3352 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 3353 | p.println(" "); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3354 | if (client != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3355 | pw.flush(); |
| 3356 | try { |
| 3357 | client.client.asBinder().dump(fd, args); |
| 3358 | } catch (RemoteException e) { |
| 3359 | p.println("Input method client dead: " + e); |
| 3360 | } |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 3361 | } else { |
| 3362 | p.println("No input method client."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3363 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3364 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 3365 | p.println(" "); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3366 | if (method != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3367 | pw.flush(); |
| 3368 | try { |
| 3369 | method.asBinder().dump(fd, args); |
| 3370 | } catch (RemoteException e) { |
| 3371 | p.println("Input method service dead: " + e); |
| 3372 | } |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 3373 | } else { |
| 3374 | p.println("No input method service."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3375 | } |
| 3376 | } |
| 3377 | } |