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