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