| 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; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | import com.android.internal.os.HandlerCaller; |
| 21 | import com.android.internal.view.IInputContext; |
| 22 | import com.android.internal.view.IInputMethod; |
| 23 | import com.android.internal.view.IInputMethodCallback; |
| 24 | import com.android.internal.view.IInputMethodClient; |
| 25 | import com.android.internal.view.IInputMethodManager; |
| 26 | import com.android.internal.view.IInputMethodSession; |
| 27 | import com.android.internal.view.InputBindResult; |
| 28 | |
| Joe Onorato | 7a0f36b | 2010-06-07 10:24:36 -0700 | [diff] [blame] | 29 | import com.android.server.StatusBarManagerService; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | |
| 31 | import org.xmlpull.v1.XmlPullParserException; |
| 32 | |
| 33 | import android.app.ActivityManagerNative; |
| 34 | import android.app.AlertDialog; |
| Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 35 | import android.app.PendingIntent; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | import android.content.ComponentName; |
| 37 | import android.content.ContentResolver; |
| 38 | import android.content.Context; |
| 39 | import android.content.DialogInterface; |
| 40 | import android.content.IntentFilter; |
| 41 | import android.content.DialogInterface.OnCancelListener; |
| 42 | import android.content.Intent; |
| 43 | import android.content.ServiceConnection; |
| Brandon Ballinger | 6da35a0 | 2009-10-21 00:38:13 -0700 | [diff] [blame] | 44 | import android.content.pm.ApplicationInfo; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | import android.content.pm.PackageManager; |
| 46 | import android.content.pm.ResolveInfo; |
| 47 | import android.content.pm.ServiceInfo; |
| Amith Yamasani | e861ec1 | 2010-03-24 21:39:27 -0700 | [diff] [blame] | 48 | import android.content.res.Configuration; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | import android.content.res.Resources; |
| 50 | import android.content.res.TypedArray; |
| 51 | import android.database.ContentObserver; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | import android.os.Binder; |
| 53 | import android.os.Handler; |
| 54 | import android.os.IBinder; |
| 55 | import android.os.IInterface; |
| 56 | import android.os.Message; |
| 57 | import android.os.Parcel; |
| 58 | import android.os.RemoteException; |
| The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 59 | import android.os.ResultReceiver; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | import android.os.ServiceManager; |
| 61 | import android.os.SystemClock; |
| 62 | import android.provider.Settings; |
| Amith Yamasani | e861ec1 | 2010-03-24 21:39:27 -0700 | [diff] [blame] | 63 | import android.provider.Settings.Secure; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | import android.text.TextUtils; |
| 65 | import android.util.EventLog; |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 66 | import android.util.Slog; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | import android.util.PrintWriterPrinter; |
| 68 | import android.util.Printer; |
| 69 | import android.view.IWindowManager; |
| 70 | import android.view.WindowManager; |
| 71 | import android.view.inputmethod.InputBinding; |
| 72 | import android.view.inputmethod.InputMethod; |
| 73 | import android.view.inputmethod.InputMethodInfo; |
| 74 | import android.view.inputmethod.InputMethodManager; |
| 75 | import android.view.inputmethod.EditorInfo; |
| 76 | |
| 77 | import java.io.FileDescriptor; |
| 78 | import java.io.IOException; |
| 79 | import java.io.PrintWriter; |
| 80 | import java.util.ArrayList; |
| 81 | import java.util.HashMap; |
| 82 | import java.util.List; |
| 83 | |
| 84 | /** |
| 85 | * This class provides a system service that manages input methods. |
| 86 | */ |
| 87 | public class InputMethodManagerService extends IInputMethodManager.Stub |
| 88 | implements ServiceConnection, Handler.Callback { |
| 89 | static final boolean DEBUG = false; |
| 90 | static final String TAG = "InputManagerService"; |
| 91 | |
| 92 | static final int MSG_SHOW_IM_PICKER = 1; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 93 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 94 | static final int MSG_UNBIND_INPUT = 1000; |
| 95 | static final int MSG_BIND_INPUT = 1010; |
| 96 | static final int MSG_SHOW_SOFT_INPUT = 1020; |
| 97 | static final int MSG_HIDE_SOFT_INPUT = 1030; |
| 98 | static final int MSG_ATTACH_TOKEN = 1040; |
| 99 | static final int MSG_CREATE_SESSION = 1050; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 100 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | static final int MSG_START_INPUT = 2000; |
| 102 | static final int MSG_RESTART_INPUT = 2010; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 103 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 104 | static final int MSG_UNBIND_METHOD = 3000; |
| 105 | static final int MSG_BIND_METHOD = 3010; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 106 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | static final long TIME_TO_RECONNECT = 10*1000; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 108 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 109 | final Context mContext; |
| 110 | final Handler mHandler; |
| 111 | final SettingsObserver mSettingsObserver; |
| Joe Onorato | 089de88 | 2010-04-12 08:18:45 -0700 | [diff] [blame] | 112 | final StatusBarManagerService mStatusBar; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | final IWindowManager mIWindowManager; |
| 114 | final HandlerCaller mCaller; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 115 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | final InputBindResult mNoBinding = new InputBindResult(null, null, -1); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 117 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | // All known input methods. mMethodMap also serves as the global |
| 119 | // lock for this class. |
| 120 | final ArrayList<InputMethodInfo> mMethodList |
| 121 | = new ArrayList<InputMethodInfo>(); |
| 122 | final HashMap<String, InputMethodInfo> mMethodMap |
| 123 | = new HashMap<String, InputMethodInfo>(); |
| 124 | |
| 125 | final TextUtils.SimpleStringSplitter mStringColonSplitter |
| 126 | = new TextUtils.SimpleStringSplitter(':'); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 127 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 128 | class SessionState { |
| 129 | final ClientState client; |
| 130 | final IInputMethod method; |
| 131 | final IInputMethodSession session; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 132 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | @Override |
| 134 | public String toString() { |
| 135 | return "SessionState{uid " + client.uid + " pid " + client.pid |
| 136 | + " method " + Integer.toHexString( |
| 137 | System.identityHashCode(method)) |
| 138 | + " session " + Integer.toHexString( |
| 139 | System.identityHashCode(session)) |
| 140 | + "}"; |
| 141 | } |
| 142 | |
| 143 | SessionState(ClientState _client, IInputMethod _method, |
| 144 | IInputMethodSession _session) { |
| 145 | client = _client; |
| 146 | method = _method; |
| 147 | session = _session; |
| 148 | } |
| 149 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 150 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 151 | class ClientState { |
| 152 | final IInputMethodClient client; |
| 153 | final IInputContext inputContext; |
| 154 | final int uid; |
| 155 | final int pid; |
| 156 | final InputBinding binding; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 157 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 158 | boolean sessionRequested; |
| 159 | SessionState curSession; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 160 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 161 | @Override |
| 162 | public String toString() { |
| 163 | return "ClientState{" + Integer.toHexString( |
| 164 | System.identityHashCode(this)) + " uid " + uid |
| 165 | + " pid " + pid + "}"; |
| 166 | } |
| 167 | |
| 168 | ClientState(IInputMethodClient _client, IInputContext _inputContext, |
| 169 | int _uid, int _pid) { |
| 170 | client = _client; |
| 171 | inputContext = _inputContext; |
| 172 | uid = _uid; |
| 173 | pid = _pid; |
| 174 | binding = new InputBinding(null, inputContext.asBinder(), uid, pid); |
| 175 | } |
| 176 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 177 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 178 | final HashMap<IBinder, ClientState> mClients |
| 179 | = new HashMap<IBinder, ClientState>(); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 180 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 181 | /** |
| Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 182 | * Set once the system is ready to run third party code. |
| 183 | */ |
| 184 | boolean mSystemReady; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 185 | |
| Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 186 | /** |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 187 | * Id of the currently selected input method. |
| 188 | */ |
| 189 | String mCurMethodId; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 190 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 191 | /** |
| 192 | * The current binding sequence number, incremented every time there is |
| 193 | * a new bind performed. |
| 194 | */ |
| 195 | int mCurSeq; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 196 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 197 | /** |
| 198 | * The client that is currently bound to an input method. |
| 199 | */ |
| 200 | ClientState mCurClient; |
| 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 | /** |
| The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 203 | * The last window token that gained focus. |
| 204 | */ |
| 205 | IBinder mCurFocusedWindow; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 206 | |
| The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 207 | /** |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 208 | * The input context last provided by the current client. |
| 209 | */ |
| 210 | IInputContext mCurInputContext; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 211 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 212 | /** |
| 213 | * The attributes last provided by the current client. |
| 214 | */ |
| 215 | EditorInfo mCurAttribute; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 216 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 217 | /** |
| 218 | * The input method ID of the input method service that we are currently |
| 219 | * connected to or in the process of connecting to. |
| 220 | */ |
| 221 | String mCurId; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 222 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 223 | /** |
| 224 | * Set to true if our ServiceConnection is currently actively bound to |
| 225 | * a service (whether or not we have gotten its IBinder back yet). |
| 226 | */ |
| 227 | boolean mHaveConnection; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 228 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 229 | /** |
| 230 | * Set if the client has asked for the input method to be shown. |
| 231 | */ |
| 232 | boolean mShowRequested; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 233 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 234 | /** |
| 235 | * Set if we were explicitly told to show the input method. |
| 236 | */ |
| 237 | boolean mShowExplicitlyRequested; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 238 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 239 | /** |
| 240 | * Set if we were forced to be shown. |
| 241 | */ |
| 242 | boolean mShowForced; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 243 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 244 | /** |
| 245 | * Set if we last told the input method to show itself. |
| 246 | */ |
| 247 | boolean mInputShown; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 248 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 249 | /** |
| 250 | * The Intent used to connect to the current input method. |
| 251 | */ |
| 252 | Intent mCurIntent; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 253 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 254 | /** |
| 255 | * The token we have made for the currently active input method, to |
| 256 | * identify it in the future. |
| 257 | */ |
| 258 | IBinder mCurToken; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 259 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 260 | /** |
| 261 | * If non-null, this is the input method service we are currently connected |
| 262 | * to. |
| 263 | */ |
| 264 | IInputMethod mCurMethod; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 265 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 266 | /** |
| 267 | * Time that we last initiated a bind to the input method, to determine |
| 268 | * if we should try to disconnect and reconnect to it. |
| 269 | */ |
| 270 | long mLastBindTime; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 271 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 272 | /** |
| 273 | * Have we called mCurMethod.bindInput()? |
| 274 | */ |
| 275 | boolean mBoundToMethod; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 276 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 277 | /** |
| 278 | * Currently enabled session. Only touched by service thread, not |
| 279 | * protected by a lock. |
| 280 | */ |
| 281 | SessionState mEnabledSession; |
| 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 | * True if the screen is on. The value is true initially. |
| 285 | */ |
| 286 | boolean mScreenOn = true; |
| 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 | AlertDialog.Builder mDialogBuilder; |
| 289 | AlertDialog mSwitchingDialog; |
| 290 | InputMethodInfo[] mIms; |
| 291 | CharSequence[] mItems; |
| 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 | class SettingsObserver extends ContentObserver { |
| 294 | SettingsObserver(Handler handler) { |
| 295 | super(handler); |
| 296 | ContentResolver resolver = mContext.getContentResolver(); |
| 297 | resolver.registerContentObserver(Settings.Secure.getUriFor( |
| 298 | Settings.Secure.DEFAULT_INPUT_METHOD), false, this); |
| 299 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 300 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 301 | @Override public void onChange(boolean selfChange) { |
| 302 | synchronized (mMethodMap) { |
| 303 | updateFromSettingsLocked(); |
| 304 | } |
| 305 | } |
| 306 | } |
| 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 | class ScreenOnOffReceiver extends android.content.BroadcastReceiver { |
| 309 | @Override |
| 310 | public void onReceive(Context context, Intent intent) { |
| 311 | if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) { |
| 312 | mScreenOn = true; |
| 313 | } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { |
| 314 | mScreenOn = false; |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 315 | } else if (intent.getAction().equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) { |
| 316 | hideInputMethodMenu(); |
| 317 | return; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 318 | } else { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 319 | Slog.w(TAG, "Unexpected intent " + intent); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | // Inform the current client of the change in active status |
| 323 | try { |
| 324 | if (mCurClient != null && mCurClient.client != null) { |
| 325 | mCurClient.client.setActive(mScreenOn); |
| 326 | } |
| 327 | } catch (RemoteException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 328 | 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] | 329 | + mCurClient.pid + " uid " + mCurClient.uid); |
| 330 | } |
| 331 | } |
| 332 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 333 | |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 334 | class MyPackageMonitor extends PackageMonitor { |
| 335 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 336 | @Override |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 337 | 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] | 338 | synchronized (mMethodMap) { |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 339 | String curInputMethodId = Settings.Secure.getString(mContext |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 340 | .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD); |
| 341 | final int N = mMethodList.size(); |
| 342 | if (curInputMethodId != null) { |
| 343 | for (int i=0; i<N; i++) { |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 344 | InputMethodInfo imi = mMethodList.get(i); |
| 345 | if (imi.getId().equals(curInputMethodId)) { |
| 346 | for (String pkg : packages) { |
| 347 | if (imi.getPackageName().equals(pkg)) { |
| 348 | if (!doit) { |
| 349 | return true; |
| 350 | } |
| 351 | |
| 352 | Settings.Secure.putString(mContext.getContentResolver(), |
| 353 | Settings.Secure.DEFAULT_INPUT_METHOD, ""); |
| 354 | chooseNewDefaultIMELocked(); |
| 355 | return true; |
| 356 | } |
| 357 | } |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | return false; |
| 363 | } |
| 364 | |
| 365 | @Override |
| 366 | public void onSomePackagesChanged() { |
| 367 | synchronized (mMethodMap) { |
| 368 | InputMethodInfo curIm = null; |
| 369 | String curInputMethodId = Settings.Secure.getString(mContext |
| 370 | .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD); |
| 371 | final int N = mMethodList.size(); |
| 372 | if (curInputMethodId != null) { |
| 373 | for (int i=0; i<N; i++) { |
| 374 | InputMethodInfo imi = mMethodList.get(i); |
| 375 | if (imi.getId().equals(curInputMethodId)) { |
| 376 | curIm = imi; |
| 377 | } |
| 378 | int change = isPackageDisappearing(imi.getPackageName()); |
| 379 | if (change == PACKAGE_TEMPORARY_CHANGE |
| 380 | || change == PACKAGE_PERMANENT_CHANGE) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 381 | Slog.i(TAG, "Input method uninstalled, disabling: " |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 382 | + imi.getComponent()); |
| 383 | setInputMethodEnabledLocked(imi.getId(), false); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 384 | } |
| 385 | } |
| 386 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 387 | |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 388 | buildInputMethodListLocked(mMethodList, mMethodMap); |
| 389 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 390 | boolean changed = false; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 391 | |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 392 | if (curIm != null) { |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 393 | int change = isPackageDisappearing(curIm.getPackageName()); |
| 394 | if (change == PACKAGE_TEMPORARY_CHANGE |
| 395 | || change == PACKAGE_PERMANENT_CHANGE) { |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 396 | ServiceInfo si = null; |
| 397 | try { |
| 398 | si = mContext.getPackageManager().getServiceInfo( |
| 399 | curIm.getComponent(), 0); |
| 400 | } catch (PackageManager.NameNotFoundException ex) { |
| 401 | } |
| 402 | if (si == null) { |
| 403 | // Uh oh, current input method is no longer around! |
| 404 | // Pick another one... |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 405 | Slog.i(TAG, "Current input method removed: " + curInputMethodId); |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 406 | if (!chooseNewDefaultIMELocked()) { |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 407 | changed = true; |
| 408 | curIm = null; |
| 409 | curInputMethodId = ""; |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 410 | Slog.i(TAG, "Unsetting current input method"); |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 411 | Settings.Secure.putString(mContext.getContentResolver(), |
| 412 | Settings.Secure.DEFAULT_INPUT_METHOD, |
| 413 | curInputMethodId); |
| 414 | } |
| 415 | } |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 416 | } |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | if (curIm == null) { |
| 420 | // We currently don't have a default input method... is |
| 421 | // one now available? |
| 422 | changed = chooseNewDefaultIMELocked(); |
| 423 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 424 | |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 425 | if (changed) { |
| 426 | updateFromSettingsLocked(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 427 | } |
| 428 | } |
| 429 | } |
| 430 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 431 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 432 | class MethodCallback extends IInputMethodCallback.Stub { |
| 433 | final IInputMethod mMethod; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 434 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 435 | MethodCallback(IInputMethod method) { |
| 436 | mMethod = method; |
| 437 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 438 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 439 | public void finishedEvent(int seq, boolean handled) throws RemoteException { |
| 440 | } |
| 441 | |
| 442 | public void sessionCreated(IInputMethodSession session) throws RemoteException { |
| 443 | onSessionCreated(mMethod, session); |
| 444 | } |
| 445 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 446 | |
| Joe Onorato | 089de88 | 2010-04-12 08:18:45 -0700 | [diff] [blame] | 447 | public InputMethodManagerService(Context context, StatusBarManagerService statusBar) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 448 | mContext = context; |
| 449 | mHandler = new Handler(this); |
| 450 | mIWindowManager = IWindowManager.Stub.asInterface( |
| 451 | ServiceManager.getService(Context.WINDOW_SERVICE)); |
| 452 | mCaller = new HandlerCaller(context, new HandlerCaller.Callback() { |
| 453 | public void executeMessage(Message msg) { |
| 454 | handleMessage(msg); |
| 455 | } |
| 456 | }); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 457 | |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 458 | (new MyPackageMonitor()).register(mContext, true); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 459 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 460 | IntentFilter screenOnOffFilt = new IntentFilter(); |
| 461 | screenOnOffFilt.addAction(Intent.ACTION_SCREEN_ON); |
| 462 | screenOnOffFilt.addAction(Intent.ACTION_SCREEN_OFF); |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 463 | screenOnOffFilt.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 464 | mContext.registerReceiver(new ScreenOnOffReceiver(), screenOnOffFilt); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 465 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 466 | buildInputMethodListLocked(mMethodList, mMethodMap); |
| 467 | |
| 468 | final String enabledStr = Settings.Secure.getString( |
| 469 | mContext.getContentResolver(), |
| 470 | Settings.Secure.ENABLED_INPUT_METHODS); |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 471 | Slog.i(TAG, "Enabled input methods: " + enabledStr); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 472 | if (enabledStr == null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 473 | Slog.i(TAG, "Enabled input methods has not been set, enabling all"); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 474 | InputMethodInfo defIm = null; |
| 475 | StringBuilder sb = new StringBuilder(256); |
| 476 | final int N = mMethodList.size(); |
| 477 | for (int i=0; i<N; i++) { |
| 478 | InputMethodInfo imi = mMethodList.get(i); |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 479 | Slog.i(TAG, "Adding: " + imi.getId()); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 480 | if (i > 0) sb.append(':'); |
| 481 | sb.append(imi.getId()); |
| 482 | if (defIm == null && imi.getIsDefaultResourceId() != 0) { |
| 483 | try { |
| 484 | Resources res = mContext.createPackageContext( |
| 485 | imi.getPackageName(), 0).getResources(); |
| 486 | if (res.getBoolean(imi.getIsDefaultResourceId())) { |
| 487 | defIm = imi; |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 488 | Slog.i(TAG, "Selected default: " + imi.getId()); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 489 | } |
| 490 | } catch (PackageManager.NameNotFoundException ex) { |
| 491 | } catch (Resources.NotFoundException ex) { |
| 492 | } |
| 493 | } |
| 494 | } |
| 495 | if (defIm == null && N > 0) { |
| 496 | defIm = mMethodList.get(0); |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 497 | Slog.i(TAG, "No default found, using " + defIm.getId()); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 498 | } |
| 499 | Settings.Secure.putString(mContext.getContentResolver(), |
| 500 | Settings.Secure.ENABLED_INPUT_METHODS, sb.toString()); |
| 501 | if (defIm != null) { |
| 502 | Settings.Secure.putString(mContext.getContentResolver(), |
| 503 | Settings.Secure.DEFAULT_INPUT_METHOD, defIm.getId()); |
| 504 | } |
| 505 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 506 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 507 | mStatusBar = statusBar; |
| Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 508 | statusBar.setIconVisibility("ime", false); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 509 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 510 | mSettingsObserver = new SettingsObserver(mHandler); |
| 511 | updateFromSettingsLocked(); |
| 512 | } |
| 513 | |
| 514 | @Override |
| 515 | public boolean onTransact(int code, Parcel data, Parcel reply, int flags) |
| 516 | throws RemoteException { |
| 517 | try { |
| 518 | return super.onTransact(code, data, reply, flags); |
| 519 | } catch (RuntimeException e) { |
| 520 | // The input method manager only throws security exceptions, so let's |
| 521 | // log all others. |
| 522 | if (!(e instanceof SecurityException)) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 523 | Slog.e(TAG, "Input Method Manager Crash", e); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 524 | } |
| 525 | throw e; |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | public void systemReady() { |
| Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 530 | synchronized (mMethodMap) { |
| 531 | if (!mSystemReady) { |
| 532 | mSystemReady = true; |
| Dianne Hackborn | cc27870 | 2009-09-02 23:07:23 -0700 | [diff] [blame] | 533 | try { |
| 534 | startInputInnerLocked(); |
| 535 | } catch (RuntimeException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 536 | Slog.w(TAG, "Unexpected exception", e); |
| Dianne Hackborn | cc27870 | 2009-09-02 23:07:23 -0700 | [diff] [blame] | 537 | } |
| Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 538 | } |
| 539 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 540 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 541 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 542 | public List<InputMethodInfo> getInputMethodList() { |
| 543 | synchronized (mMethodMap) { |
| 544 | return new ArrayList<InputMethodInfo>(mMethodList); |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | public List<InputMethodInfo> getEnabledInputMethodList() { |
| 549 | synchronized (mMethodMap) { |
| 550 | return getEnabledInputMethodListLocked(); |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | List<InputMethodInfo> getEnabledInputMethodListLocked() { |
| 555 | final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>(); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 556 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 557 | final String enabledStr = Settings.Secure.getString( |
| 558 | mContext.getContentResolver(), |
| 559 | Settings.Secure.ENABLED_INPUT_METHODS); |
| 560 | if (enabledStr != null) { |
| 561 | final TextUtils.SimpleStringSplitter splitter = mStringColonSplitter; |
| 562 | splitter.setString(enabledStr); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 563 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 564 | while (splitter.hasNext()) { |
| 565 | InputMethodInfo info = mMethodMap.get(splitter.next()); |
| 566 | if (info != null) { |
| 567 | res.add(info); |
| 568 | } |
| 569 | } |
| 570 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 571 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 572 | return res; |
| 573 | } |
| 574 | |
| 575 | public void addClient(IInputMethodClient client, |
| 576 | IInputContext inputContext, int uid, int pid) { |
| 577 | synchronized (mMethodMap) { |
| 578 | mClients.put(client.asBinder(), new ClientState(client, |
| 579 | inputContext, uid, pid)); |
| 580 | } |
| 581 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 582 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 583 | public void removeClient(IInputMethodClient client) { |
| 584 | synchronized (mMethodMap) { |
| 585 | mClients.remove(client.asBinder()); |
| 586 | } |
| 587 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 588 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 589 | void executeOrSendMessage(IInterface target, Message msg) { |
| 590 | if (target.asBinder() instanceof Binder) { |
| 591 | mCaller.sendMessage(msg); |
| 592 | } else { |
| 593 | handleMessage(msg); |
| 594 | msg.recycle(); |
| 595 | } |
| 596 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 597 | |
| The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 598 | void unbindCurrentClientLocked() { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 599 | if (mCurClient != null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 600 | if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = " |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 601 | + mCurClient.client.asBinder()); |
| 602 | if (mBoundToMethod) { |
| 603 | mBoundToMethod = false; |
| 604 | if (mCurMethod != null) { |
| 605 | executeOrSendMessage(mCurMethod, mCaller.obtainMessageO( |
| 606 | MSG_UNBIND_INPUT, mCurMethod)); |
| 607 | } |
| 608 | } |
| 609 | executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO( |
| 610 | MSG_UNBIND_METHOD, mCurSeq, mCurClient.client)); |
| 611 | mCurClient.sessionRequested = false; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 612 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 613 | // Call setActive(false) on the old client |
| 614 | try { |
| 615 | mCurClient.client.setActive(false); |
| 616 | } catch (RemoteException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 617 | 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] | 618 | + mCurClient.pid + " uid " + mCurClient.uid); |
| 619 | } |
| 620 | mCurClient = null; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 621 | |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 622 | hideInputMethodMenuLocked(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 623 | } |
| 624 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 625 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 626 | private int getImeShowFlags() { |
| 627 | int flags = 0; |
| 628 | if (mShowForced) { |
| 629 | flags |= InputMethod.SHOW_FORCED |
| 630 | | InputMethod.SHOW_EXPLICIT; |
| 631 | } else if (mShowExplicitlyRequested) { |
| 632 | flags |= InputMethod.SHOW_EXPLICIT; |
| 633 | } |
| 634 | return flags; |
| 635 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 636 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 637 | private int getAppShowFlags() { |
| 638 | int flags = 0; |
| 639 | if (mShowForced) { |
| 640 | flags |= InputMethodManager.SHOW_FORCED; |
| 641 | } else if (!mShowExplicitlyRequested) { |
| 642 | flags |= InputMethodManager.SHOW_IMPLICIT; |
| 643 | } |
| 644 | return flags; |
| 645 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 646 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 647 | InputBindResult attachNewInputLocked(boolean initial, boolean needResult) { |
| 648 | if (!mBoundToMethod) { |
| 649 | executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO( |
| 650 | MSG_BIND_INPUT, mCurMethod, mCurClient.binding)); |
| 651 | mBoundToMethod = true; |
| 652 | } |
| 653 | final SessionState session = mCurClient.curSession; |
| 654 | if (initial) { |
| 655 | executeOrSendMessage(session.method, mCaller.obtainMessageOOO( |
| 656 | MSG_START_INPUT, session, mCurInputContext, mCurAttribute)); |
| 657 | } else { |
| 658 | executeOrSendMessage(session.method, mCaller.obtainMessageOOO( |
| 659 | MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute)); |
| 660 | } |
| 661 | if (mShowRequested) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 662 | 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] | 663 | showCurrentInputLocked(getAppShowFlags(), null); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 664 | } |
| 665 | return needResult |
| 666 | ? new InputBindResult(session.session, mCurId, mCurSeq) |
| 667 | : null; |
| 668 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 669 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 670 | InputBindResult startInputLocked(IInputMethodClient client, |
| 671 | IInputContext inputContext, EditorInfo attribute, |
| 672 | boolean initial, boolean needResult) { |
| 673 | // If no method is currently selected, do nothing. |
| 674 | if (mCurMethodId == null) { |
| 675 | return mNoBinding; |
| 676 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 677 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 678 | ClientState cs = mClients.get(client.asBinder()); |
| 679 | if (cs == null) { |
| 680 | throw new IllegalArgumentException("unknown client " |
| 681 | + client.asBinder()); |
| 682 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 683 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 684 | try { |
| 685 | if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) { |
| 686 | // Check with the window manager to make sure this client actually |
| 687 | // has a window with focus. If not, reject. This is thread safe |
| 688 | // because if the focus changes some time before or after, the |
| 689 | // next client receiving focus that has any interest in input will |
| 690 | // be calling through here after that change happens. |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 691 | 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] | 692 | + " (uid=" + cs.uid + " pid=" + cs.pid + ")"); |
| 693 | return null; |
| 694 | } |
| 695 | } catch (RemoteException e) { |
| 696 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 697 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 698 | if (mCurClient != cs) { |
| 699 | // If the client is changing, we need to switch over to the new |
| 700 | // one. |
| The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 701 | unbindCurrentClientLocked(); |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 702 | if (DEBUG) Slog.v(TAG, "switching to client: client = " |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 703 | + cs.client.asBinder()); |
| 704 | |
| 705 | // If the screen is on, inform the new client it is active |
| 706 | if (mScreenOn) { |
| 707 | try { |
| 708 | cs.client.setActive(mScreenOn); |
| 709 | } catch (RemoteException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 710 | 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] | 711 | + cs.pid + " uid " + cs.uid); |
| 712 | } |
| 713 | } |
| 714 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 715 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 716 | // Bump up the sequence for this client and attach it. |
| 717 | mCurSeq++; |
| 718 | if (mCurSeq <= 0) mCurSeq = 1; |
| 719 | mCurClient = cs; |
| 720 | mCurInputContext = inputContext; |
| 721 | mCurAttribute = attribute; |
| 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 | // Check if the input method is changing. |
| 724 | if (mCurId != null && mCurId.equals(mCurMethodId)) { |
| 725 | if (cs.curSession != null) { |
| 726 | // Fast case: if we are already connected to the input method, |
| 727 | // then just return it. |
| 728 | return attachNewInputLocked(initial, needResult); |
| 729 | } |
| 730 | if (mHaveConnection) { |
| 731 | if (mCurMethod != null) { |
| 732 | if (!cs.sessionRequested) { |
| 733 | cs.sessionRequested = true; |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 734 | 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] | 735 | executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO( |
| 736 | MSG_CREATE_SESSION, mCurMethod, |
| 737 | new MethodCallback(mCurMethod))); |
| 738 | } |
| 739 | // Return to client, and we will get back with it when |
| 740 | // we have had a session made for it. |
| 741 | return new InputBindResult(null, mCurId, mCurSeq); |
| 742 | } else if (SystemClock.uptimeMillis() |
| 743 | < (mLastBindTime+TIME_TO_RECONNECT)) { |
| 744 | // In this case we have connected to the service, but |
| 745 | // don't yet have its interface. If it hasn't been too |
| 746 | // long since we did the connection, we'll return to |
| 747 | // the client and wait to get the service interface so |
| 748 | // we can report back. If it has been too long, we want |
| 749 | // to fall through so we can try a disconnect/reconnect |
| 750 | // to see if we can get back in touch with the service. |
| 751 | return new InputBindResult(null, mCurId, mCurSeq); |
| 752 | } else { |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 753 | EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, |
| 754 | mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 755 | } |
| 756 | } |
| 757 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 758 | |
| Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 759 | return startInputInnerLocked(); |
| 760 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 761 | |
| Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 762 | InputBindResult startInputInnerLocked() { |
| 763 | if (mCurMethodId == null) { |
| 764 | return mNoBinding; |
| 765 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 766 | |
| Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 767 | if (!mSystemReady) { |
| 768 | // If the system is not yet ready, we shouldn't be running third |
| 769 | // party code. |
| Dianne Hackborn | cc27870 | 2009-09-02 23:07:23 -0700 | [diff] [blame] | 770 | return new InputBindResult(null, mCurMethodId, mCurSeq); |
| Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 771 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 772 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 773 | InputMethodInfo info = mMethodMap.get(mCurMethodId); |
| 774 | if (info == null) { |
| 775 | throw new IllegalArgumentException("Unknown id: " + mCurMethodId); |
| 776 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 777 | |
| The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 778 | unbindCurrentMethodLocked(false); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 779 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 780 | mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE); |
| 781 | mCurIntent.setComponent(info.getComponent()); |
| Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 782 | mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL, |
| 783 | com.android.internal.R.string.input_method_binding_label); |
| 784 | mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity( |
| 785 | 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] | 786 | if (mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE)) { |
| 787 | mLastBindTime = SystemClock.uptimeMillis(); |
| 788 | mHaveConnection = true; |
| 789 | mCurId = info.getId(); |
| 790 | mCurToken = new Binder(); |
| 791 | try { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 792 | if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 793 | mIWindowManager.addWindowToken(mCurToken, |
| 794 | WindowManager.LayoutParams.TYPE_INPUT_METHOD); |
| 795 | } catch (RemoteException e) { |
| 796 | } |
| 797 | return new InputBindResult(null, mCurId, mCurSeq); |
| 798 | } else { |
| 799 | mCurIntent = null; |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 800 | Slog.w(TAG, "Failure connecting to input method service: " |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 801 | + mCurIntent); |
| 802 | } |
| 803 | return null; |
| 804 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 805 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 806 | public InputBindResult startInput(IInputMethodClient client, |
| 807 | IInputContext inputContext, EditorInfo attribute, |
| 808 | boolean initial, boolean needResult) { |
| 809 | synchronized (mMethodMap) { |
| 810 | final long ident = Binder.clearCallingIdentity(); |
| 811 | try { |
| 812 | return startInputLocked(client, inputContext, attribute, |
| 813 | initial, needResult); |
| 814 | } finally { |
| 815 | Binder.restoreCallingIdentity(ident); |
| 816 | } |
| 817 | } |
| 818 | } |
| 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 | public void finishInput(IInputMethodClient client) { |
| 821 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 822 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 823 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 824 | synchronized (mMethodMap) { |
| 825 | if (mCurIntent != null && name.equals(mCurIntent.getComponent())) { |
| 826 | mCurMethod = IInputMethod.Stub.asInterface(service); |
| Dianne Hackborn | cc27870 | 2009-09-02 23:07:23 -0700 | [diff] [blame] | 827 | if (mCurToken == null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 828 | Slog.w(TAG, "Service connected without a token!"); |
| Dianne Hackborn | cc27870 | 2009-09-02 23:07:23 -0700 | [diff] [blame] | 829 | unbindCurrentMethodLocked(false); |
| 830 | return; |
| 831 | } |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 832 | if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken); |
| Dianne Hackborn | cc27870 | 2009-09-02 23:07:23 -0700 | [diff] [blame] | 833 | executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO( |
| 834 | MSG_ATTACH_TOKEN, mCurMethod, mCurToken)); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 835 | if (mCurClient != null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 836 | if (DEBUG) Slog.v(TAG, "Creating first session while with client " |
| Dianne Hackborn | cc27870 | 2009-09-02 23:07:23 -0700 | [diff] [blame] | 837 | + mCurClient); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 838 | executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO( |
| Dianne Hackborn | cc27870 | 2009-09-02 23:07:23 -0700 | [diff] [blame] | 839 | MSG_CREATE_SESSION, mCurMethod, |
| 840 | new MethodCallback(mCurMethod))); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 841 | } |
| 842 | } |
| 843 | } |
| 844 | } |
| 845 | |
| 846 | void onSessionCreated(IInputMethod method, IInputMethodSession session) { |
| 847 | synchronized (mMethodMap) { |
| 848 | if (mCurMethod != null && method != null |
| 849 | && mCurMethod.asBinder() == method.asBinder()) { |
| 850 | if (mCurClient != null) { |
| 851 | mCurClient.curSession = new SessionState(mCurClient, |
| 852 | method, session); |
| 853 | mCurClient.sessionRequested = false; |
| 854 | InputBindResult res = attachNewInputLocked(true, true); |
| 855 | if (res.method != null) { |
| 856 | executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO( |
| 857 | MSG_BIND_METHOD, mCurClient.client, res)); |
| 858 | } |
| 859 | } |
| 860 | } |
| 861 | } |
| 862 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 863 | |
| The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 864 | void unbindCurrentMethodLocked(boolean reportToClient) { |
| 865 | if (mHaveConnection) { |
| 866 | mContext.unbindService(this); |
| 867 | mHaveConnection = false; |
| 868 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 869 | |
| The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 870 | if (mCurToken != null) { |
| 871 | try { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 872 | if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken); |
| The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 873 | mIWindowManager.removeWindowToken(mCurToken); |
| 874 | } catch (RemoteException e) { |
| 875 | } |
| 876 | mCurToken = null; |
| 877 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 878 | |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 879 | mCurId = null; |
| The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 880 | clearCurMethodLocked(); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 881 | |
| The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 882 | if (reportToClient && mCurClient != null) { |
| 883 | executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO( |
| 884 | MSG_UNBIND_METHOD, mCurSeq, mCurClient.client)); |
| 885 | } |
| 886 | } |
| 887 | |
| Devin Taylor | 0c33ed2 | 2010-02-23 13:26:46 -0600 | [diff] [blame] | 888 | private void finishSession(SessionState sessionState) { |
| 889 | if (sessionState != null && sessionState.session != null) { |
| 890 | try { |
| 891 | sessionState.session.finishSession(); |
| 892 | } catch (RemoteException e) { |
| Jean-Baptiste Queru | 9d0f6df | 2010-03-29 12:55:09 -0700 | [diff] [blame] | 893 | Slog.w(TAG, "Session failed to close due to remote exception", e); |
| Devin Taylor | 0c33ed2 | 2010-02-23 13:26:46 -0600 | [diff] [blame] | 894 | } |
| 895 | } |
| 896 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 897 | |
| The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 898 | void clearCurMethodLocked() { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 899 | if (mCurMethod != null) { |
| 900 | for (ClientState cs : mClients.values()) { |
| 901 | cs.sessionRequested = false; |
| Devin Taylor | 0c33ed2 | 2010-02-23 13:26:46 -0600 | [diff] [blame] | 902 | finishSession(cs.curSession); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 903 | cs.curSession = null; |
| 904 | } |
| Devin Taylor | 0c33ed2 | 2010-02-23 13:26:46 -0600 | [diff] [blame] | 905 | |
| 906 | finishSession(mEnabledSession); |
| 907 | mEnabledSession = null; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 908 | mCurMethod = null; |
| 909 | } |
| Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 910 | mStatusBar.setIconVisibility("ime", false); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 911 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 912 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 913 | public void onServiceDisconnected(ComponentName name) { |
| 914 | synchronized (mMethodMap) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 915 | if (DEBUG) Slog.v(TAG, "Service disconnected: " + name |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 916 | + " mCurIntent=" + mCurIntent); |
| 917 | if (mCurMethod != null && mCurIntent != null |
| 918 | && name.equals(mCurIntent.getComponent())) { |
| The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 919 | clearCurMethodLocked(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 920 | // We consider this to be a new bind attempt, since the system |
| 921 | // should now try to restart the service for us. |
| 922 | mLastBindTime = SystemClock.uptimeMillis(); |
| 923 | mShowRequested = mInputShown; |
| 924 | mInputShown = false; |
| 925 | if (mCurClient != null) { |
| 926 | executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO( |
| 927 | MSG_UNBIND_METHOD, mCurSeq, mCurClient.client)); |
| 928 | } |
| 929 | } |
| 930 | } |
| 931 | } |
| 932 | |
| 933 | public void updateStatusIcon(IBinder token, String packageName, int iconId) { |
| 934 | long ident = Binder.clearCallingIdentity(); |
| 935 | try { |
| 936 | if (token == null || mCurToken != token) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 937 | Slog.w(TAG, "Ignoring setInputMethod of token: " + token); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 938 | return; |
| 939 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 940 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 941 | synchronized (mMethodMap) { |
| 942 | if (iconId == 0) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 943 | 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] | 944 | mStatusBar.setIconVisibility("ime", false); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 945 | } else if (packageName != null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 946 | if (DEBUG) Slog.d(TAG, "show a small icon for the input method"); |
| Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 947 | mStatusBar.setIcon("ime", packageName, iconId, 0); |
| 948 | mStatusBar.setIconVisibility("ime", true); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 949 | } |
| 950 | } |
| 951 | } finally { |
| 952 | Binder.restoreCallingIdentity(ident); |
| 953 | } |
| 954 | } |
| 955 | |
| 956 | void updateFromSettingsLocked() { |
| The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 957 | // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and |
| 958 | // ENABLED_INPUT_METHODS is taking care of keeping them correctly in |
| 959 | // sync, so we will never have a DEFAULT_INPUT_METHOD that is not |
| 960 | // enabled. |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 961 | String id = Settings.Secure.getString(mContext.getContentResolver(), |
| 962 | Settings.Secure.DEFAULT_INPUT_METHOD); |
| The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 963 | if (id != null && id.length() > 0) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 964 | try { |
| 965 | setInputMethodLocked(id); |
| 966 | } catch (IllegalArgumentException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 967 | 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] | 968 | mCurMethodId = null; |
| The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 969 | unbindCurrentMethodLocked(true); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 970 | } |
| The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 971 | } else { |
| 972 | // 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] | 973 | mCurMethodId = null; |
| The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 974 | unbindCurrentMethodLocked(true); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 975 | } |
| 976 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 977 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 978 | void setInputMethodLocked(String id) { |
| 979 | InputMethodInfo info = mMethodMap.get(id); |
| 980 | if (info == null) { |
| 981 | throw new IllegalArgumentException("Unknown id: " + mCurMethodId); |
| 982 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 983 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 984 | if (id.equals(mCurMethodId)) { |
| 985 | return; |
| 986 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 987 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 988 | final long ident = Binder.clearCallingIdentity(); |
| 989 | try { |
| 990 | mCurMethodId = id; |
| 991 | Settings.Secure.putString(mContext.getContentResolver(), |
| 992 | Settings.Secure.DEFAULT_INPUT_METHOD, id); |
| 993 | |
| 994 | if (ActivityManagerNative.isSystemReady()) { |
| 995 | Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED); |
| Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 996 | intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 997 | intent.putExtra("input_method_id", id); |
| 998 | mContext.sendBroadcast(intent); |
| 999 | } |
| The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1000 | unbindCurrentClientLocked(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1001 | } finally { |
| 1002 | Binder.restoreCallingIdentity(ident); |
| 1003 | } |
| 1004 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1005 | |
| The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1006 | public boolean showSoftInput(IInputMethodClient client, int flags, |
| 1007 | ResultReceiver resultReceiver) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1008 | long ident = Binder.clearCallingIdentity(); |
| 1009 | try { |
| 1010 | synchronized (mMethodMap) { |
| 1011 | if (mCurClient == null || client == null |
| 1012 | || mCurClient.client.asBinder() != client.asBinder()) { |
| 1013 | try { |
| 1014 | // We need to check if this is the current client with |
| 1015 | // focus in the window manager, to allow this call to |
| 1016 | // be made before input is started in it. |
| 1017 | if (!mIWindowManager.inputMethodClientHasFocus(client)) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1018 | Slog.w(TAG, "Ignoring showSoftInput of: " + client); |
| The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1019 | return false; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1020 | } |
| 1021 | } catch (RemoteException e) { |
| The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1022 | return false; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1023 | } |
| 1024 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1025 | |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1026 | 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] | 1027 | return showCurrentInputLocked(flags, resultReceiver); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1028 | } |
| 1029 | } finally { |
| 1030 | Binder.restoreCallingIdentity(ident); |
| 1031 | } |
| 1032 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1033 | |
| The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1034 | boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1035 | mShowRequested = true; |
| 1036 | if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) { |
| 1037 | mShowExplicitlyRequested = true; |
| 1038 | } |
| 1039 | if ((flags&InputMethodManager.SHOW_FORCED) != 0) { |
| 1040 | mShowExplicitlyRequested = true; |
| 1041 | mShowForced = true; |
| 1042 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1043 | |
| Dianne Hackborn | cc27870 | 2009-09-02 23:07:23 -0700 | [diff] [blame] | 1044 | if (!mSystemReady) { |
| 1045 | return false; |
| 1046 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1047 | |
| The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1048 | boolean res = false; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1049 | if (mCurMethod != null) { |
| The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1050 | executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO( |
| 1051 | MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod, |
| 1052 | resultReceiver)); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1053 | mInputShown = true; |
| The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1054 | res = true; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1055 | } else if (mHaveConnection && SystemClock.uptimeMillis() |
| 1056 | < (mLastBindTime+TIME_TO_RECONNECT)) { |
| 1057 | // The client has asked to have the input method shown, but |
| 1058 | // we have been sitting here too long with a connection to the |
| 1059 | // service and no interface received, so let's disconnect/connect |
| 1060 | // to try to prod things along. |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1061 | EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1062 | SystemClock.uptimeMillis()-mLastBindTime,1); |
| 1063 | mContext.unbindService(this); |
| 1064 | mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE); |
| 1065 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1066 | |
| The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1067 | return res; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1068 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1069 | |
| The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1070 | public boolean hideSoftInput(IInputMethodClient client, int flags, |
| 1071 | ResultReceiver resultReceiver) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1072 | long ident = Binder.clearCallingIdentity(); |
| 1073 | try { |
| 1074 | synchronized (mMethodMap) { |
| 1075 | if (mCurClient == null || client == null |
| 1076 | || mCurClient.client.asBinder() != client.asBinder()) { |
| 1077 | try { |
| 1078 | // We need to check if this is the current client with |
| 1079 | // focus in the window manager, to allow this call to |
| 1080 | // be made before input is started in it. |
| 1081 | if (!mIWindowManager.inputMethodClientHasFocus(client)) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1082 | Slog.w(TAG, "Ignoring hideSoftInput of: " + client); |
| The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1083 | return false; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1084 | } |
| 1085 | } catch (RemoteException e) { |
| The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1086 | return false; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1087 | } |
| 1088 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1089 | |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1090 | 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] | 1091 | return hideCurrentInputLocked(flags, resultReceiver); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1092 | } |
| 1093 | } finally { |
| 1094 | Binder.restoreCallingIdentity(ident); |
| 1095 | } |
| 1096 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1097 | |
| The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1098 | boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1099 | if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0 |
| 1100 | && (mShowExplicitlyRequested || mShowForced)) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1101 | if (DEBUG) Slog.v(TAG, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1102 | "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] | 1103 | return false; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1104 | } |
| 1105 | if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1106 | if (DEBUG) Slog.v(TAG, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1107 | "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] | 1108 | return false; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1109 | } |
| The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1110 | boolean res; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1111 | if (mInputShown && mCurMethod != null) { |
| The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1112 | executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO( |
| 1113 | MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver)); |
| 1114 | res = true; |
| 1115 | } else { |
| 1116 | res = false; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1117 | } |
| 1118 | mInputShown = false; |
| 1119 | mShowRequested = false; |
| 1120 | mShowExplicitlyRequested = false; |
| 1121 | mShowForced = false; |
| The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1122 | return res; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1123 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1124 | |
| The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1125 | public void windowGainedFocus(IInputMethodClient client, IBinder windowToken, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1126 | boolean viewHasFocus, boolean isTextEditor, int softInputMode, |
| 1127 | boolean first, int windowFlags) { |
| 1128 | long ident = Binder.clearCallingIdentity(); |
| 1129 | try { |
| 1130 | synchronized (mMethodMap) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1131 | if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder() |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1132 | + " viewHasFocus=" + viewHasFocus |
| 1133 | + " isTextEditor=" + isTextEditor |
| 1134 | + " softInputMode=#" + Integer.toHexString(softInputMode) |
| 1135 | + " first=" + first + " flags=#" |
| 1136 | + Integer.toHexString(windowFlags)); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1137 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1138 | if (mCurClient == null || client == null |
| 1139 | || mCurClient.client.asBinder() != client.asBinder()) { |
| 1140 | try { |
| 1141 | // We need to check if this is the current client with |
| 1142 | // focus in the window manager, to allow this call to |
| 1143 | // be made before input is started in it. |
| 1144 | if (!mIWindowManager.inputMethodClientHasFocus(client)) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1145 | 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] | 1146 | return; |
| 1147 | } |
| 1148 | } catch (RemoteException e) { |
| 1149 | } |
| 1150 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1151 | |
| The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1152 | if (mCurFocusedWindow == windowToken) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1153 | 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] | 1154 | return; |
| 1155 | } |
| 1156 | mCurFocusedWindow = windowToken; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1157 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1158 | switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) { |
| 1159 | case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED: |
| 1160 | if (!isTextEditor || (softInputMode & |
| 1161 | WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST) |
| 1162 | != WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE) { |
| 1163 | if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) { |
| 1164 | // There is no focus view, and this window will |
| 1165 | // be behind any soft input window, so hide the |
| 1166 | // soft input window if it is shown. |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1167 | 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] | 1168 | hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1169 | } |
| 1170 | } else if (isTextEditor && (softInputMode & |
| 1171 | WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST) |
| 1172 | == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE |
| 1173 | && (softInputMode & |
| 1174 | WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) { |
| 1175 | // There is a focus view, and we are navigating forward |
| 1176 | // into the window, so show the input window for the user. |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1177 | 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] | 1178 | showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1179 | } |
| 1180 | break; |
| 1181 | case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED: |
| 1182 | // Do nothing. |
| 1183 | break; |
| 1184 | case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN: |
| 1185 | if ((softInputMode & |
| 1186 | WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1187 | 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] | 1188 | hideCurrentInputLocked(0, null); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1189 | } |
| 1190 | break; |
| 1191 | case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN: |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1192 | 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] | 1193 | hideCurrentInputLocked(0, null); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1194 | break; |
| 1195 | case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE: |
| 1196 | if ((softInputMode & |
| 1197 | WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1198 | 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] | 1199 | showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1200 | } |
| 1201 | break; |
| 1202 | case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE: |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1203 | 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] | 1204 | showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1205 | break; |
| 1206 | } |
| 1207 | } |
| 1208 | } finally { |
| 1209 | Binder.restoreCallingIdentity(ident); |
| 1210 | } |
| 1211 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1212 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1213 | public void showInputMethodPickerFromClient(IInputMethodClient client) { |
| 1214 | synchronized (mMethodMap) { |
| 1215 | if (mCurClient == null || client == null |
| 1216 | || mCurClient.client.asBinder() != client.asBinder()) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1217 | Slog.w(TAG, "Ignoring showInputMethodDialogFromClient of: " + client); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1218 | } |
| 1219 | |
| 1220 | mHandler.sendEmptyMessage(MSG_SHOW_IM_PICKER); |
| 1221 | } |
| 1222 | } |
| 1223 | |
| 1224 | public void setInputMethod(IBinder token, String id) { |
| 1225 | synchronized (mMethodMap) { |
| 1226 | if (token == null) { |
| 1227 | if (mContext.checkCallingOrSelfPermission( |
| 1228 | android.Manifest.permission.WRITE_SECURE_SETTINGS) |
| 1229 | != PackageManager.PERMISSION_GRANTED) { |
| 1230 | throw new SecurityException( |
| 1231 | "Using null token requires permission " |
| 1232 | + android.Manifest.permission.WRITE_SECURE_SETTINGS); |
| 1233 | } |
| 1234 | } else if (mCurToken != token) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1235 | Slog.w(TAG, "Ignoring setInputMethod of token: " + token); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1236 | return; |
| 1237 | } |
| 1238 | |
| 1239 | long ident = Binder.clearCallingIdentity(); |
| 1240 | try { |
| 1241 | setInputMethodLocked(id); |
| 1242 | } finally { |
| 1243 | Binder.restoreCallingIdentity(ident); |
| 1244 | } |
| 1245 | } |
| 1246 | } |
| 1247 | |
| 1248 | public void hideMySoftInput(IBinder token, int flags) { |
| 1249 | synchronized (mMethodMap) { |
| 1250 | if (token == null || mCurToken != token) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1251 | Slog.w(TAG, "Ignoring hideInputMethod of token: " + token); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1252 | return; |
| 1253 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1254 | long ident = Binder.clearCallingIdentity(); |
| 1255 | try { |
| The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1256 | hideCurrentInputLocked(flags, null); |
| 1257 | } finally { |
| 1258 | Binder.restoreCallingIdentity(ident); |
| 1259 | } |
| 1260 | } |
| 1261 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1262 | |
| The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1263 | public void showMySoftInput(IBinder token, int flags) { |
| 1264 | synchronized (mMethodMap) { |
| 1265 | if (token == null || mCurToken != token) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1266 | Slog.w(TAG, "Ignoring hideInputMethod of token: " + token); |
| The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1267 | return; |
| 1268 | } |
| 1269 | long ident = Binder.clearCallingIdentity(); |
| 1270 | try { |
| 1271 | showCurrentInputLocked(flags, null); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1272 | } finally { |
| 1273 | Binder.restoreCallingIdentity(ident); |
| 1274 | } |
| 1275 | } |
| 1276 | } |
| 1277 | |
| 1278 | void setEnabledSessionInMainThread(SessionState session) { |
| 1279 | if (mEnabledSession != session) { |
| 1280 | if (mEnabledSession != null) { |
| 1281 | try { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1282 | if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1283 | mEnabledSession.method.setSessionEnabled( |
| 1284 | mEnabledSession.session, false); |
| 1285 | } catch (RemoteException e) { |
| 1286 | } |
| 1287 | } |
| 1288 | mEnabledSession = session; |
| 1289 | try { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1290 | if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1291 | session.method.setSessionEnabled( |
| 1292 | session.session, true); |
| 1293 | } catch (RemoteException e) { |
| 1294 | } |
| 1295 | } |
| 1296 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1297 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1298 | public boolean handleMessage(Message msg) { |
| 1299 | HandlerCaller.SomeArgs args; |
| 1300 | switch (msg.what) { |
| 1301 | case MSG_SHOW_IM_PICKER: |
| 1302 | showInputMethodMenu(); |
| 1303 | return true; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1304 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1305 | // --------------------------------------------------------- |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1306 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1307 | case MSG_UNBIND_INPUT: |
| 1308 | try { |
| 1309 | ((IInputMethod)msg.obj).unbindInput(); |
| 1310 | } catch (RemoteException e) { |
| 1311 | // There is nothing interesting about the method dying. |
| 1312 | } |
| 1313 | return true; |
| 1314 | case MSG_BIND_INPUT: |
| 1315 | args = (HandlerCaller.SomeArgs)msg.obj; |
| 1316 | try { |
| 1317 | ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2); |
| 1318 | } catch (RemoteException e) { |
| 1319 | } |
| 1320 | return true; |
| 1321 | case MSG_SHOW_SOFT_INPUT: |
| The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1322 | args = (HandlerCaller.SomeArgs)msg.obj; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1323 | try { |
| The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1324 | ((IInputMethod)args.arg1).showSoftInput(msg.arg1, |
| 1325 | (ResultReceiver)args.arg2); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1326 | } catch (RemoteException e) { |
| 1327 | } |
| 1328 | return true; |
| 1329 | case MSG_HIDE_SOFT_INPUT: |
| The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1330 | args = (HandlerCaller.SomeArgs)msg.obj; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1331 | try { |
| The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1332 | ((IInputMethod)args.arg1).hideSoftInput(0, |
| 1333 | (ResultReceiver)args.arg2); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1334 | } catch (RemoteException e) { |
| 1335 | } |
| 1336 | return true; |
| 1337 | case MSG_ATTACH_TOKEN: |
| 1338 | args = (HandlerCaller.SomeArgs)msg.obj; |
| 1339 | try { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1340 | 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] | 1341 | ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2); |
| 1342 | } catch (RemoteException e) { |
| 1343 | } |
| 1344 | return true; |
| 1345 | case MSG_CREATE_SESSION: |
| 1346 | args = (HandlerCaller.SomeArgs)msg.obj; |
| 1347 | try { |
| 1348 | ((IInputMethod)args.arg1).createSession( |
| 1349 | (IInputMethodCallback)args.arg2); |
| 1350 | } catch (RemoteException e) { |
| 1351 | } |
| 1352 | return true; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1353 | // --------------------------------------------------------- |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1354 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1355 | case MSG_START_INPUT: |
| 1356 | args = (HandlerCaller.SomeArgs)msg.obj; |
| 1357 | try { |
| 1358 | SessionState session = (SessionState)args.arg1; |
| 1359 | setEnabledSessionInMainThread(session); |
| 1360 | session.method.startInput((IInputContext)args.arg2, |
| 1361 | (EditorInfo)args.arg3); |
| 1362 | } catch (RemoteException e) { |
| 1363 | } |
| 1364 | return true; |
| 1365 | case MSG_RESTART_INPUT: |
| 1366 | args = (HandlerCaller.SomeArgs)msg.obj; |
| 1367 | try { |
| 1368 | SessionState session = (SessionState)args.arg1; |
| 1369 | setEnabledSessionInMainThread(session); |
| 1370 | session.method.restartInput((IInputContext)args.arg2, |
| 1371 | (EditorInfo)args.arg3); |
| 1372 | } catch (RemoteException e) { |
| 1373 | } |
| 1374 | return true; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1375 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1376 | // --------------------------------------------------------- |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1377 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1378 | case MSG_UNBIND_METHOD: |
| 1379 | try { |
| 1380 | ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1); |
| 1381 | } catch (RemoteException e) { |
| 1382 | // There is nothing interesting about the last client dying. |
| 1383 | } |
| 1384 | return true; |
| 1385 | case MSG_BIND_METHOD: |
| 1386 | args = (HandlerCaller.SomeArgs)msg.obj; |
| 1387 | try { |
| 1388 | ((IInputMethodClient)args.arg1).onBindMethod( |
| 1389 | (InputBindResult)args.arg2); |
| 1390 | } catch (RemoteException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1391 | 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] | 1392 | } |
| 1393 | return true; |
| 1394 | } |
| 1395 | return false; |
| 1396 | } |
| 1397 | |
| Brandon Ballinger | 6da35a0 | 2009-10-21 00:38:13 -0700 | [diff] [blame] | 1398 | private boolean isSystemIme(InputMethodInfo inputMethod) { |
| 1399 | return (inputMethod.getServiceInfo().applicationInfo.flags |
| 1400 | & ApplicationInfo.FLAG_SYSTEM) != 0; |
| 1401 | } |
| 1402 | |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1403 | private boolean chooseNewDefaultIMELocked() { |
| Brandon Ballinger | 6da35a0 | 2009-10-21 00:38:13 -0700 | [diff] [blame] | 1404 | List<InputMethodInfo> enabled = getEnabledInputMethodListLocked(); |
| 1405 | if (enabled != null && enabled.size() > 0) { |
| Dianne Hackborn | 83e48f5 | 2010-03-23 23:03:25 -0700 | [diff] [blame] | 1406 | // We'd prefer to fall back on a system IME, since that is safer. |
| 1407 | int i=enabled.size(); |
| 1408 | while (i > 0) { |
| 1409 | i--; |
| 1410 | if ((enabled.get(i).getServiceInfo().applicationInfo.flags |
| 1411 | & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 1412 | break; |
| 1413 | } |
| 1414 | } |
| Brandon Ballinger | 6da35a0 | 2009-10-21 00:38:13 -0700 | [diff] [blame] | 1415 | Settings.Secure.putString(mContext.getContentResolver(), |
| 1416 | Settings.Secure.DEFAULT_INPUT_METHOD, |
| Dianne Hackborn | 83e48f5 | 2010-03-23 23:03:25 -0700 | [diff] [blame] | 1417 | enabled.get(i).getId()); |
| Brandon Ballinger | 6da35a0 | 2009-10-21 00:38:13 -0700 | [diff] [blame] | 1418 | return true; |
| 1419 | } |
| 1420 | |
| 1421 | return false; |
| 1422 | } |
| 1423 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1424 | void buildInputMethodListLocked(ArrayList<InputMethodInfo> list, |
| 1425 | HashMap<String, InputMethodInfo> map) { |
| 1426 | list.clear(); |
| 1427 | map.clear(); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1428 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1429 | PackageManager pm = mContext.getPackageManager(); |
| Amith Yamasani | e861ec1 | 2010-03-24 21:39:27 -0700 | [diff] [blame] | 1430 | final Configuration config = mContext.getResources().getConfiguration(); |
| 1431 | final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY; |
| 1432 | String disabledSysImes = Settings.Secure.getString(mContext.getContentResolver(), |
| 1433 | Secure.DISABLED_SYSTEM_INPUT_METHODS); |
| 1434 | if (disabledSysImes == null) disabledSysImes = ""; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1435 | |
| 1436 | List<ResolveInfo> services = pm.queryIntentServices( |
| 1437 | new Intent(InputMethod.SERVICE_INTERFACE), |
| 1438 | PackageManager.GET_META_DATA); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1439 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1440 | for (int i = 0; i < services.size(); ++i) { |
| 1441 | ResolveInfo ri = services.get(i); |
| 1442 | ServiceInfo si = ri.serviceInfo; |
| 1443 | ComponentName compName = new ComponentName(si.packageName, si.name); |
| 1444 | if (!android.Manifest.permission.BIND_INPUT_METHOD.equals( |
| 1445 | si.permission)) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1446 | Slog.w(TAG, "Skipping input method " + compName |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1447 | + ": it does not require the permission " |
| 1448 | + android.Manifest.permission.BIND_INPUT_METHOD); |
| 1449 | continue; |
| 1450 | } |
| 1451 | |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1452 | if (DEBUG) Slog.d(TAG, "Checking " + compName); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1453 | |
| 1454 | try { |
| 1455 | InputMethodInfo p = new InputMethodInfo(mContext, ri); |
| 1456 | list.add(p); |
| Amith Yamasani | e861ec1 | 2010-03-24 21:39:27 -0700 | [diff] [blame] | 1457 | final String id = p.getId(); |
| 1458 | map.put(id, p); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1459 | |
| Amith Yamasani | e861ec1 | 2010-03-24 21:39:27 -0700 | [diff] [blame] | 1460 | // System IMEs are enabled by default, unless there's a hard keyboard |
| 1461 | // and the system IME was explicitly disabled |
| 1462 | if (isSystemIme(p) && (!haveHardKeyboard || disabledSysImes.indexOf(id) < 0)) { |
| 1463 | setInputMethodEnabledLocked(id, true); |
| Brandon Ballinger | 6da35a0 | 2009-10-21 00:38:13 -0700 | [diff] [blame] | 1464 | } |
| 1465 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1466 | if (DEBUG) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1467 | 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] | 1468 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1469 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1470 | } catch (XmlPullParserException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1471 | 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] | 1472 | } catch (IOException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1473 | 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] | 1474 | } |
| 1475 | } |
| Brandon Ballinger | 6da35a0 | 2009-10-21 00:38:13 -0700 | [diff] [blame] | 1476 | |
| 1477 | String defaultIme = Settings.Secure.getString(mContext |
| 1478 | .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD); |
| 1479 | if (!map.containsKey(defaultIme)) { |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1480 | if (chooseNewDefaultIMELocked()) { |
| Brandon Ballinger | 6da35a0 | 2009-10-21 00:38:13 -0700 | [diff] [blame] | 1481 | updateFromSettingsLocked(); |
| 1482 | } |
| 1483 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1484 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1485 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1486 | // ---------------------------------------------------------------------- |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1487 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1488 | void showInputMethodMenu() { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1489 | if (DEBUG) Slog.v(TAG, "Show switching menu"); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1490 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1491 | final Context context = mContext; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1492 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1493 | final PackageManager pm = context.getPackageManager(); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1494 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1495 | String lastInputMethodId = Settings.Secure.getString(context |
| 1496 | .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD); |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1497 | if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1498 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1499 | final List<InputMethodInfo> immis = getEnabledInputMethodList(); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1500 | |
| Dianne Hackborn | 97106ab | 2010-03-03 00:08:31 -0800 | [diff] [blame] | 1501 | if (immis == null) { |
| 1502 | return; |
| 1503 | } |
| 1504 | |
| Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 1505 | synchronized (mMethodMap) { |
| 1506 | hideInputMethodMenuLocked(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1507 | |
| Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 1508 | int N = immis.size(); |
| 1509 | |
| 1510 | mItems = new CharSequence[N]; |
| 1511 | mIms = new InputMethodInfo[N]; |
| 1512 | |
| 1513 | int j = 0; |
| 1514 | for (int i = 0; i < N; ++i) { |
| 1515 | InputMethodInfo property = immis.get(i); |
| 1516 | if (property == null) { |
| 1517 | continue; |
| 1518 | } |
| 1519 | mItems[j] = property.loadLabel(pm); |
| 1520 | mIms[j] = property; |
| 1521 | j++; |
| Dianne Hackborn | 97106ab | 2010-03-03 00:08:31 -0800 | [diff] [blame] | 1522 | } |
| Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 1523 | |
| 1524 | int checkedItem = 0; |
| 1525 | for (int i = 0; i < N; ++i) { |
| 1526 | if (mIms[i].getId().equals(lastInputMethodId)) { |
| 1527 | checkedItem = i; |
| 1528 | break; |
| 1529 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1530 | } |
| Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 1531 | |
| 1532 | AlertDialog.OnClickListener adocl = new AlertDialog.OnClickListener() { |
| 1533 | public void onClick(DialogInterface dialog, int which) { |
| 1534 | hideInputMethodMenu(); |
| 1535 | } |
| 1536 | }; |
| 1537 | |
| 1538 | TypedArray a = context.obtainStyledAttributes(null, |
| 1539 | com.android.internal.R.styleable.DialogPreference, |
| 1540 | com.android.internal.R.attr.alertDialogStyle, 0); |
| 1541 | mDialogBuilder = new AlertDialog.Builder(context) |
| 1542 | .setTitle(com.android.internal.R.string.select_input_method) |
| 1543 | .setOnCancelListener(new OnCancelListener() { |
| 1544 | public void onCancel(DialogInterface dialog) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1545 | hideInputMethodMenu(); |
| Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 1546 | } |
| 1547 | }) |
| 1548 | .setIcon(a.getDrawable( |
| 1549 | com.android.internal.R.styleable.DialogPreference_dialogTitle)); |
| 1550 | a.recycle(); |
| 1551 | |
| 1552 | mDialogBuilder.setSingleChoiceItems(mItems, checkedItem, |
| 1553 | new AlertDialog.OnClickListener() { |
| 1554 | public void onClick(DialogInterface dialog, int which) { |
| 1555 | synchronized (mMethodMap) { |
| 1556 | if (mIms == null || mIms.length <= which) { |
| 1557 | return; |
| 1558 | } |
| 1559 | InputMethodInfo im = mIms[which]; |
| 1560 | hideInputMethodMenu(); |
| 1561 | if (im != null) { |
| 1562 | setInputMethodLocked(im.getId()); |
| 1563 | } |
| Dianne Hackborn | 20cb56e | 2010-03-04 00:58:29 -0800 | [diff] [blame] | 1564 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1565 | } |
| Dianne Hackborn | 8cf1bcd | 2010-03-16 13:06:10 -0700 | [diff] [blame] | 1566 | }); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1567 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1568 | mSwitchingDialog = mDialogBuilder.create(); |
| 1569 | mSwitchingDialog.getWindow().setType( |
| 1570 | WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG); |
| 1571 | mSwitchingDialog.show(); |
| 1572 | } |
| 1573 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1574 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1575 | void hideInputMethodMenu() { |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1576 | synchronized (mMethodMap) { |
| 1577 | hideInputMethodMenuLocked(); |
| 1578 | } |
| 1579 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1580 | |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1581 | void hideInputMethodMenuLocked() { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1582 | if (DEBUG) Slog.v(TAG, "Hide switching menu"); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1583 | |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1584 | if (mSwitchingDialog != null) { |
| 1585 | mSwitchingDialog.dismiss(); |
| 1586 | mSwitchingDialog = null; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1587 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1588 | |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1589 | mDialogBuilder = null; |
| 1590 | mItems = null; |
| 1591 | mIms = null; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1592 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1593 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1594 | // ---------------------------------------------------------------------- |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1595 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1596 | public boolean setInputMethodEnabled(String id, boolean enabled) { |
| 1597 | synchronized (mMethodMap) { |
| 1598 | if (mContext.checkCallingOrSelfPermission( |
| 1599 | android.Manifest.permission.WRITE_SECURE_SETTINGS) |
| 1600 | != PackageManager.PERMISSION_GRANTED) { |
| 1601 | throw new SecurityException( |
| 1602 | "Requires permission " |
| 1603 | + android.Manifest.permission.WRITE_SECURE_SETTINGS); |
| 1604 | } |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1605 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1606 | long ident = Binder.clearCallingIdentity(); |
| 1607 | try { |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1608 | return setInputMethodEnabledLocked(id, enabled); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1609 | } finally { |
| 1610 | Binder.restoreCallingIdentity(ident); |
| 1611 | } |
| 1612 | } |
| 1613 | } |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1614 | |
| 1615 | boolean setInputMethodEnabledLocked(String id, boolean enabled) { |
| 1616 | // Make sure this is a valid input method. |
| 1617 | InputMethodInfo imm = mMethodMap.get(id); |
| 1618 | if (imm == null) { |
| 1619 | if (imm == null) { |
| 1620 | throw new IllegalArgumentException("Unknown id: " + mCurMethodId); |
| 1621 | } |
| 1622 | } |
| 1623 | |
| 1624 | StringBuilder builder = new StringBuilder(256); |
| 1625 | |
| 1626 | boolean removed = false; |
| 1627 | String firstId = null; |
| 1628 | |
| 1629 | // Look through the currently enabled input methods. |
| 1630 | String enabledStr = Settings.Secure.getString(mContext.getContentResolver(), |
| 1631 | Settings.Secure.ENABLED_INPUT_METHODS); |
| 1632 | if (enabledStr != null) { |
| 1633 | final TextUtils.SimpleStringSplitter splitter = mStringColonSplitter; |
| 1634 | splitter.setString(enabledStr); |
| 1635 | while (splitter.hasNext()) { |
| 1636 | String curId = splitter.next(); |
| 1637 | if (curId.equals(id)) { |
| 1638 | if (enabled) { |
| 1639 | // We are enabling this input method, but it is |
| 1640 | // already enabled. Nothing to do. The previous |
| 1641 | // state was enabled. |
| 1642 | return true; |
| 1643 | } |
| 1644 | // We are disabling this input method, and it is |
| 1645 | // currently enabled. Skip it to remove from the |
| 1646 | // new list. |
| 1647 | removed = true; |
| 1648 | } else if (!enabled) { |
| 1649 | // We are building a new list of input methods that |
| 1650 | // doesn't contain the given one. |
| 1651 | if (firstId == null) firstId = curId; |
| 1652 | if (builder.length() > 0) builder.append(':'); |
| 1653 | builder.append(curId); |
| 1654 | } |
| 1655 | } |
| 1656 | } |
| 1657 | |
| 1658 | if (!enabled) { |
| 1659 | if (!removed) { |
| 1660 | // We are disabling the input method but it is already |
| 1661 | // disabled. Nothing to do. The previous state was |
| 1662 | // disabled. |
| 1663 | return false; |
| 1664 | } |
| 1665 | // Update the setting with the new list of input methods. |
| 1666 | Settings.Secure.putString(mContext.getContentResolver(), |
| 1667 | Settings.Secure.ENABLED_INPUT_METHODS, builder.toString()); |
| 1668 | // We the disabled input method is currently selected, switch |
| 1669 | // to another one. |
| 1670 | String selId = Settings.Secure.getString(mContext.getContentResolver(), |
| 1671 | Settings.Secure.DEFAULT_INPUT_METHOD); |
| 1672 | if (id.equals(selId)) { |
| 1673 | Settings.Secure.putString(mContext.getContentResolver(), |
| 1674 | Settings.Secure.DEFAULT_INPUT_METHOD, |
| 1675 | firstId != null ? firstId : ""); |
| 1676 | } |
| 1677 | // Previous state was enabled. |
| 1678 | return true; |
| 1679 | } |
| 1680 | |
| 1681 | // Add in the newly enabled input method. |
| 1682 | if (enabledStr == null || enabledStr.length() == 0) { |
| 1683 | enabledStr = id; |
| 1684 | } else { |
| 1685 | enabledStr = enabledStr + ':' + id; |
| 1686 | } |
| 1687 | |
| 1688 | Settings.Secure.putString(mContext.getContentResolver(), |
| 1689 | Settings.Secure.ENABLED_INPUT_METHODS, enabledStr); |
| 1690 | |
| 1691 | // Previous state was disabled. |
| 1692 | return false; |
| 1693 | } |
| The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1694 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1695 | // ---------------------------------------------------------------------- |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1696 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1697 | @Override |
| 1698 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 1699 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 1700 | != PackageManager.PERMISSION_GRANTED) { |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1701 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1702 | pw.println("Permission Denial: can't dump InputMethodManager from from pid=" |
| 1703 | + Binder.getCallingPid() |
| 1704 | + ", uid=" + Binder.getCallingUid()); |
| 1705 | return; |
| 1706 | } |
| 1707 | |
| 1708 | IInputMethod method; |
| 1709 | ClientState client; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1710 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1711 | final Printer p = new PrintWriterPrinter(pw); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1712 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1713 | synchronized (mMethodMap) { |
| 1714 | p.println("Current Input Method Manager state:"); |
| 1715 | int N = mMethodList.size(); |
| 1716 | p.println(" Input Methods:"); |
| 1717 | for (int i=0; i<N; i++) { |
| 1718 | InputMethodInfo info = mMethodList.get(i); |
| 1719 | p.println(" InputMethod #" + i + ":"); |
| 1720 | info.dump(p, " "); |
| 1721 | } |
| 1722 | p.println(" Clients:"); |
| 1723 | for (ClientState ci : mClients.values()) { |
| 1724 | p.println(" Client " + ci + ":"); |
| 1725 | p.println(" client=" + ci.client); |
| 1726 | p.println(" inputContext=" + ci.inputContext); |
| 1727 | p.println(" sessionRequested=" + ci.sessionRequested); |
| 1728 | p.println(" curSession=" + ci.curSession); |
| 1729 | } |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1730 | p.println(" mCurMethodId=" + mCurMethodId); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1731 | client = mCurClient; |
| The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1732 | p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq); |
| 1733 | p.println(" mCurFocusedWindow=" + mCurFocusedWindow); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1734 | p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection |
| 1735 | + " mBoundToMethod=" + mBoundToMethod); |
| 1736 | p.println(" mCurToken=" + mCurToken); |
| 1737 | p.println(" mCurIntent=" + mCurIntent); |
| 1738 | method = mCurMethod; |
| 1739 | p.println(" mCurMethod=" + mCurMethod); |
| 1740 | p.println(" mEnabledSession=" + mEnabledSession); |
| 1741 | p.println(" mShowRequested=" + mShowRequested |
| 1742 | + " mShowExplicitlyRequested=" + mShowExplicitlyRequested |
| 1743 | + " mShowForced=" + mShowForced |
| 1744 | + " mInputShown=" + mInputShown); |
| Dianne Hackborn | cc27870 | 2009-09-02 23:07:23 -0700 | [diff] [blame] | 1745 | p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1746 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1747 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1748 | if (client != null) { |
| 1749 | p.println(" "); |
| 1750 | pw.flush(); |
| 1751 | try { |
| 1752 | client.client.asBinder().dump(fd, args); |
| 1753 | } catch (RemoteException e) { |
| 1754 | p.println("Input method client dead: " + e); |
| 1755 | } |
| 1756 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1757 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1758 | if (method != null) { |
| 1759 | p.println(" "); |
| 1760 | pw.flush(); |
| 1761 | try { |
| 1762 | method.asBinder().dump(fd, args); |
| 1763 | } catch (RemoteException e) { |
| 1764 | p.println("Input method service dead: " + e); |
| 1765 | } |
| 1766 | } |
| 1767 | } |
| 1768 | } |