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