satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server; |
| 18 | |
| 19 | import com.android.internal.content.PackageMonitor; |
| 20 | import com.android.internal.textservice.ISpellCheckerService; |
| 21 | import com.android.internal.textservice.ISpellCheckerSession; |
| 22 | import com.android.internal.textservice.ISpellCheckerSessionListener; |
| 23 | import com.android.internal.textservice.ITextServicesManager; |
| 24 | import com.android.internal.textservice.ITextServicesSessionListener; |
| 25 | |
| 26 | import android.content.ComponentName; |
| 27 | import android.content.Context; |
| 28 | import android.content.Intent; |
| 29 | import android.content.ServiceConnection; |
| 30 | import android.content.pm.PackageManager; |
| 31 | import android.content.pm.ResolveInfo; |
| 32 | import android.content.pm.ServiceInfo; |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 33 | import android.os.Binder; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 34 | import android.os.IBinder; |
| 35 | import android.os.RemoteException; |
| 36 | import android.os.SystemClock; |
| 37 | import android.provider.Settings; |
| 38 | import android.text.TextUtils; |
| 39 | import android.service.textservice.SpellCheckerService; |
| 40 | import android.util.Log; |
| 41 | import android.util.Slog; |
| 42 | import android.view.textservice.SpellCheckerInfo; |
| 43 | |
| 44 | import java.util.ArrayList; |
| 45 | import java.util.HashMap; |
| 46 | import java.util.HashSet; |
| 47 | import java.util.List; |
| 48 | |
| 49 | public class TextServicesManagerService extends ITextServicesManager.Stub { |
| 50 | private static final String TAG = TextServicesManagerService.class.getSimpleName(); |
| 51 | private static final boolean DBG = false; |
| 52 | |
| 53 | private final Context mContext; |
| 54 | private boolean mSystemReady; |
| 55 | private final TextServicesMonitor mMonitor; |
| 56 | private final HashMap<String, SpellCheckerInfo> mSpellCheckerMap = |
| 57 | new HashMap<String, SpellCheckerInfo>(); |
| 58 | private final ArrayList<SpellCheckerInfo> mSpellCheckerList = new ArrayList<SpellCheckerInfo>(); |
| 59 | private final HashMap<String, SpellCheckerBindGroup> mSpellCheckerBindGroups = |
| 60 | new HashMap<String, SpellCheckerBindGroup>(); |
| 61 | |
| 62 | public void systemReady() { |
| 63 | if (!mSystemReady) { |
| 64 | mSystemReady = true; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | public TextServicesManagerService(Context context) { |
| 69 | mSystemReady = false; |
| 70 | mContext = context; |
| 71 | mMonitor = new TextServicesMonitor(); |
| 72 | mMonitor.register(context, true); |
| 73 | synchronized (mSpellCheckerMap) { |
| 74 | buildSpellCheckerMapLocked(context, mSpellCheckerList, mSpellCheckerMap); |
| 75 | } |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame^] | 76 | SpellCheckerInfo sci = getCurrentSpellChecker(null); |
| 77 | if (sci == null) { |
| 78 | sci = findAvailSpellCheckerLocked(null, null); |
| 79 | if (sci != null) { |
| 80 | // Set the current spell checker if there is one or more spell checkers |
| 81 | // available. In this case, "sci" is the first one in the available spell |
| 82 | // checkers. |
| 83 | setCurrentSpellCheckerLocked(sci); |
| 84 | } |
| 85 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | private class TextServicesMonitor extends PackageMonitor { |
| 89 | @Override |
| 90 | public void onSomePackagesChanged() { |
| 91 | synchronized (mSpellCheckerMap) { |
| 92 | buildSpellCheckerMapLocked(mContext, mSpellCheckerList, mSpellCheckerMap); |
| 93 | // TODO: Update for each locale |
| 94 | SpellCheckerInfo sci = getCurrentSpellChecker(null); |
satok | da317ef | 2011-07-26 08:02:45 +0900 | [diff] [blame] | 95 | if (sci == null) return; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 96 | final String packageName = sci.getPackageName(); |
| 97 | final int change = isPackageDisappearing(packageName); |
| 98 | if (change == PACKAGE_PERMANENT_CHANGE || change == PACKAGE_TEMPORARY_CHANGE) { |
| 99 | // Package disappearing |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame^] | 100 | setCurrentSpellCheckerLocked(findAvailSpellCheckerLocked(null, packageName)); |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 101 | } else if (isPackageModified(packageName)) { |
| 102 | // Package modified |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame^] | 103 | setCurrentSpellCheckerLocked(findAvailSpellCheckerLocked(null, packageName)); |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | private static void buildSpellCheckerMapLocked(Context context, |
| 110 | ArrayList<SpellCheckerInfo> list, HashMap<String, SpellCheckerInfo> map) { |
| 111 | list.clear(); |
| 112 | map.clear(); |
| 113 | final PackageManager pm = context.getPackageManager(); |
| 114 | List<ResolveInfo> services = pm.queryIntentServices( |
| 115 | new Intent(SpellCheckerService.SERVICE_INTERFACE), PackageManager.GET_META_DATA); |
| 116 | final int N = services.size(); |
| 117 | for (int i = 0; i < N; ++i) { |
| 118 | final ResolveInfo ri = services.get(i); |
| 119 | final ServiceInfo si = ri.serviceInfo; |
| 120 | final ComponentName compName = new ComponentName(si.packageName, si.name); |
| 121 | if (!android.Manifest.permission.BIND_TEXT_SERVICE.equals(si.permission)) { |
| 122 | Slog.w(TAG, "Skipping text service " + compName |
| 123 | + ": it does not require the permission " |
| 124 | + android.Manifest.permission.BIND_TEXT_SERVICE); |
| 125 | continue; |
| 126 | } |
| 127 | if (DBG) Slog.d(TAG, "Add: " + compName); |
| 128 | final SpellCheckerInfo sci = new SpellCheckerInfo(context, ri); |
| 129 | list.add(sci); |
| 130 | map.put(sci.getId(), sci); |
| 131 | } |
satok | da317ef | 2011-07-26 08:02:45 +0900 | [diff] [blame] | 132 | if (DBG) { |
| 133 | Slog.d(TAG, "buildSpellCheckerMapLocked: " + list.size() + "," + map.size()); |
| 134 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | // TODO: find an appropriate spell checker for specified locale |
| 138 | private SpellCheckerInfo findAvailSpellCheckerLocked(String locale, String prefPackage) { |
| 139 | final int spellCheckersCount = mSpellCheckerList.size(); |
| 140 | if (spellCheckersCount == 0) { |
| 141 | Slog.w(TAG, "no available spell checker services found"); |
| 142 | return null; |
| 143 | } |
| 144 | if (prefPackage != null) { |
| 145 | for (int i = 0; i < spellCheckersCount; ++i) { |
| 146 | final SpellCheckerInfo sci = mSpellCheckerList.get(i); |
| 147 | if (prefPackage.equals(sci.getPackageName())) { |
satok | da317ef | 2011-07-26 08:02:45 +0900 | [diff] [blame] | 148 | if (DBG) { |
| 149 | Slog.d(TAG, "findAvailSpellCheckerLocked: " + sci.getPackageName()); |
| 150 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 151 | return sci; |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | if (spellCheckersCount > 1) { |
| 156 | Slog.w(TAG, "more than one spell checker service found, picking first"); |
| 157 | } |
| 158 | return mSpellCheckerList.get(0); |
| 159 | } |
| 160 | |
| 161 | // TODO: Save SpellCheckerService by supported languages. Currently only one spell |
| 162 | // checker is saved. |
| 163 | @Override |
| 164 | public SpellCheckerInfo getCurrentSpellChecker(String locale) { |
| 165 | synchronized (mSpellCheckerMap) { |
satok | da317ef | 2011-07-26 08:02:45 +0900 | [diff] [blame] | 166 | String curSpellCheckerId = |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 167 | Settings.Secure.getString(mContext.getContentResolver(), |
| 168 | Settings.Secure.SPELL_CHECKER_SERVICE); |
satok | 562ab58 | 2011-07-25 10:12:21 +0900 | [diff] [blame] | 169 | if (DBG) { |
| 170 | Slog.w(TAG, "getCurrentSpellChecker: " + curSpellCheckerId); |
| 171 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 172 | if (TextUtils.isEmpty(curSpellCheckerId)) { |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame^] | 173 | return null; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 174 | } |
| 175 | return mSpellCheckerMap.get(curSpellCheckerId); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | @Override |
| 180 | public void getSpellCheckerService(SpellCheckerInfo info, String locale, |
| 181 | ITextServicesSessionListener tsListener, ISpellCheckerSessionListener scListener) { |
| 182 | if (!mSystemReady) { |
| 183 | return; |
| 184 | } |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 185 | if (info == null || tsListener == null || scListener == null) { |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 186 | Slog.e(TAG, "getSpellCheckerService: Invalid input."); |
| 187 | return; |
| 188 | } |
| 189 | final String sciId = info.getId(); |
| 190 | synchronized(mSpellCheckerMap) { |
| 191 | if (!mSpellCheckerMap.containsKey(sciId)) { |
| 192 | return; |
| 193 | } |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame^] | 194 | final int uid = Binder.getCallingUid(); |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 195 | if (mSpellCheckerBindGroups.containsKey(sciId)) { |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 196 | final SpellCheckerBindGroup bindGroup = mSpellCheckerBindGroups.get(sciId); |
| 197 | if (bindGroup != null) { |
| 198 | final InternalDeathRecipient recipient = |
| 199 | mSpellCheckerBindGroups.get(sciId).addListener( |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame^] | 200 | tsListener, locale, scListener, uid); |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 201 | if (recipient == null) { |
| 202 | if (DBG) { |
| 203 | Slog.w(TAG, "Didn't create a death recipient."); |
| 204 | } |
| 205 | return; |
| 206 | } |
| 207 | if (bindGroup.mSpellChecker == null & bindGroup.mConnected) { |
| 208 | Slog.e(TAG, "The state of the spell checker bind group is illegal."); |
| 209 | bindGroup.removeAll(); |
| 210 | } else if (bindGroup.mSpellChecker != null) { |
| 211 | if (DBG) { |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame^] | 212 | Slog.w(TAG, "Existing bind found. Return a spell checker session now. " |
| 213 | + "Listeners count = " + bindGroup.mListeners.size()); |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 214 | } |
| 215 | try { |
| 216 | final ISpellCheckerSession session = |
| 217 | bindGroup.mSpellChecker.getISpellCheckerSession( |
| 218 | recipient.mScLocale, recipient.mScListener); |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame^] | 219 | if (session != null) { |
| 220 | tsListener.onServiceConnected(session); |
| 221 | return; |
| 222 | } else { |
| 223 | if (DBG) { |
| 224 | Slog.w(TAG, "Existing bind already expired. "); |
| 225 | } |
| 226 | bindGroup.removeAll(); |
| 227 | } |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 228 | } catch (RemoteException e) { |
| 229 | Slog.e(TAG, "Exception in getting spell checker session: " + e); |
| 230 | bindGroup.removeAll(); |
| 231 | } |
| 232 | } |
| 233 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 234 | } |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 235 | final long ident = Binder.clearCallingIdentity(); |
| 236 | try { |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame^] | 237 | startSpellCheckerServiceInnerLocked(info, locale, tsListener, scListener, uid); |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 238 | } finally { |
| 239 | Binder.restoreCallingIdentity(ident); |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 240 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 241 | } |
| 242 | return; |
| 243 | } |
| 244 | |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 245 | private void startSpellCheckerServiceInnerLocked(SpellCheckerInfo info, String locale, |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame^] | 246 | ITextServicesSessionListener tsListener, ISpellCheckerSessionListener scListener, |
| 247 | int uid) { |
| 248 | if (DBG) { |
| 249 | Slog.w(TAG, "Start spell checker session inner locked."); |
| 250 | } |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 251 | final String sciId = info.getId(); |
| 252 | final InternalServiceConnection connection = new InternalServiceConnection( |
| 253 | sciId, locale, scListener); |
| 254 | final Intent serviceIntent = new Intent(SpellCheckerService.SERVICE_INTERFACE); |
| 255 | serviceIntent.setComponent(info.getComponent()); |
| 256 | if (DBG) { |
| 257 | Slog.w(TAG, "bind service: " + info.getId()); |
| 258 | } |
| 259 | if (!mContext.bindService(serviceIntent, connection, Context.BIND_AUTO_CREATE)) { |
| 260 | Slog.e(TAG, "Failed to get a spell checker service."); |
| 261 | return; |
| 262 | } |
| 263 | final SpellCheckerBindGroup group = new SpellCheckerBindGroup( |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame^] | 264 | connection, tsListener, locale, scListener, uid); |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 265 | mSpellCheckerBindGroups.put(sciId, group); |
| 266 | } |
| 267 | |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 268 | @Override |
satok | 562ab58 | 2011-07-25 10:12:21 +0900 | [diff] [blame] | 269 | public SpellCheckerInfo[] getEnabledSpellCheckers() { |
satok | da317ef | 2011-07-26 08:02:45 +0900 | [diff] [blame] | 270 | if (DBG) { |
| 271 | Slog.d(TAG, "getEnabledSpellCheckers: " + mSpellCheckerList.size()); |
| 272 | for (int i = 0; i < mSpellCheckerList.size(); ++i) { |
| 273 | Slog.d(TAG, "EnabledSpellCheckers: " + mSpellCheckerList.get(i).getPackageName()); |
| 274 | } |
| 275 | } |
satok | 562ab58 | 2011-07-25 10:12:21 +0900 | [diff] [blame] | 276 | return mSpellCheckerList.toArray(new SpellCheckerInfo[mSpellCheckerList.size()]); |
| 277 | } |
| 278 | |
| 279 | @Override |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 280 | public void finishSpellCheckerService(ISpellCheckerSessionListener listener) { |
satok | da317ef | 2011-07-26 08:02:45 +0900 | [diff] [blame] | 281 | if (DBG) { |
| 282 | Slog.d(TAG, "FinishSpellCheckerService"); |
| 283 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 284 | synchronized(mSpellCheckerMap) { |
| 285 | for (SpellCheckerBindGroup group : mSpellCheckerBindGroups.values()) { |
| 286 | if (group == null) continue; |
| 287 | group.removeListener(listener); |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame^] | 292 | @Override |
| 293 | public void setCurrentSpellChecker(SpellCheckerInfo sci) { |
| 294 | synchronized(mSpellCheckerMap) { |
| 295 | if (mContext.checkCallingOrSelfPermission( |
| 296 | android.Manifest.permission.WRITE_SECURE_SETTINGS) |
| 297 | != PackageManager.PERMISSION_GRANTED) { |
| 298 | throw new SecurityException( |
| 299 | "Requires permission " |
| 300 | + android.Manifest.permission.WRITE_SECURE_SETTINGS); |
| 301 | } |
| 302 | setCurrentSpellCheckerLocked(sci); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | private void setCurrentSpellCheckerLocked(SpellCheckerInfo sci) { |
satok | 562ab58 | 2011-07-25 10:12:21 +0900 | [diff] [blame] | 307 | if (DBG) { |
| 308 | Slog.w(TAG, "setCurrentSpellChecker: " + sci.getId()); |
| 309 | } |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame^] | 310 | if (sci == null || !mSpellCheckerMap.containsKey(sci.getId())) return; |
| 311 | final long ident = Binder.clearCallingIdentity(); |
| 312 | try { |
| 313 | Settings.Secure.putString(mContext.getContentResolver(), |
| 314 | Settings.Secure.SPELL_CHECKER_SERVICE, sci == null ? "" : sci.getId()); |
| 315 | } finally { |
| 316 | Binder.restoreCallingIdentity(ident); |
| 317 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | // SpellCheckerBindGroup contains active text service session listeners. |
| 321 | // If there are no listeners anymore, the SpellCheckerBindGroup instance will be removed from |
| 322 | // mSpellCheckerBindGroups |
| 323 | private class SpellCheckerBindGroup { |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame^] | 324 | private final String TAG = SpellCheckerBindGroup.class.getSimpleName(); |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 325 | private final InternalServiceConnection mInternalConnection; |
| 326 | private final ArrayList<InternalDeathRecipient> mListeners = |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 327 | new ArrayList<InternalDeathRecipient>(); |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 328 | public ISpellCheckerService mSpellChecker; |
| 329 | public boolean mConnected; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 330 | |
| 331 | public SpellCheckerBindGroup(InternalServiceConnection connection, |
| 332 | ITextServicesSessionListener listener, String locale, |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame^] | 333 | ISpellCheckerSessionListener scListener, int uid) { |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 334 | mInternalConnection = connection; |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 335 | mConnected = false; |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame^] | 336 | addListener(listener, locale, scListener, uid); |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | public void onServiceConnected(ISpellCheckerService spellChecker) { |
satok | da317ef | 2011-07-26 08:02:45 +0900 | [diff] [blame] | 340 | if (DBG) { |
| 341 | Slog.d(TAG, "onServiceConnected"); |
| 342 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 343 | synchronized(mSpellCheckerMap) { |
| 344 | for (InternalDeathRecipient listener : mListeners) { |
| 345 | try { |
| 346 | final ISpellCheckerSession session = spellChecker.getISpellCheckerSession( |
| 347 | listener.mScLocale, listener.mScListener); |
| 348 | listener.mTsListener.onServiceConnected(session); |
| 349 | } catch (RemoteException e) { |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame^] | 350 | Slog.e(TAG, "Exception in getting the spell checker session: " + e); |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 351 | removeAll(); |
| 352 | return; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 353 | } |
| 354 | } |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 355 | mSpellChecker = spellChecker; |
| 356 | mConnected = true; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 360 | public InternalDeathRecipient addListener(ITextServicesSessionListener tsListener, |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame^] | 361 | String locale, ISpellCheckerSessionListener scListener, int uid) { |
satok | da317ef | 2011-07-26 08:02:45 +0900 | [diff] [blame] | 362 | if (DBG) { |
| 363 | Slog.d(TAG, "addListener: " + locale); |
| 364 | } |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 365 | InternalDeathRecipient recipient = null; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 366 | synchronized(mSpellCheckerMap) { |
| 367 | try { |
| 368 | final int size = mListeners.size(); |
| 369 | for (int i = 0; i < size; ++i) { |
| 370 | if (mListeners.get(i).hasSpellCheckerListener(scListener)) { |
| 371 | // do not add the lister if the group already contains this. |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 372 | return null; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 373 | } |
| 374 | } |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 375 | recipient = new InternalDeathRecipient( |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame^] | 376 | this, tsListener, locale, scListener, uid); |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 377 | scListener.asBinder().linkToDeath(recipient, 0); |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame^] | 378 | mListeners.add(recipient); |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 379 | } catch(RemoteException e) { |
| 380 | // do nothing |
| 381 | } |
| 382 | cleanLocked(); |
| 383 | } |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 384 | return recipient; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | public void removeListener(ISpellCheckerSessionListener listener) { |
satok | da317ef | 2011-07-26 08:02:45 +0900 | [diff] [blame] | 388 | if (DBG) { |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame^] | 389 | Slog.w(TAG, "remove listener: " + listener.hashCode()); |
satok | da317ef | 2011-07-26 08:02:45 +0900 | [diff] [blame] | 390 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 391 | synchronized(mSpellCheckerMap) { |
| 392 | final int size = mListeners.size(); |
| 393 | final ArrayList<InternalDeathRecipient> removeList = |
| 394 | new ArrayList<InternalDeathRecipient>(); |
| 395 | for (int i = 0; i < size; ++i) { |
| 396 | final InternalDeathRecipient tempRecipient = mListeners.get(i); |
| 397 | if(tempRecipient.hasSpellCheckerListener(listener)) { |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame^] | 398 | if (DBG) { |
| 399 | Slog.w(TAG, "found existing listener."); |
| 400 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 401 | removeList.add(tempRecipient); |
| 402 | } |
| 403 | } |
| 404 | final int removeSize = removeList.size(); |
| 405 | for (int i = 0; i < removeSize; ++i) { |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame^] | 406 | if (DBG) { |
| 407 | Slog.w(TAG, "Remove " + removeList.get(i)); |
| 408 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 409 | mListeners.remove(removeList.get(i)); |
| 410 | } |
| 411 | cleanLocked(); |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | private void cleanLocked() { |
satok | da317ef | 2011-07-26 08:02:45 +0900 | [diff] [blame] | 416 | if (DBG) { |
| 417 | Slog.d(TAG, "cleanLocked"); |
| 418 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 419 | if (mListeners.isEmpty()) { |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 420 | if (mSpellCheckerBindGroups.containsKey(this)) { |
| 421 | mSpellCheckerBindGroups.remove(this); |
| 422 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 423 | // Unbind service when there is no active clients. |
| 424 | mContext.unbindService(mInternalConnection); |
| 425 | } |
| 426 | } |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 427 | |
| 428 | public void removeAll() { |
| 429 | Slog.e(TAG, "Remove the spell checker bind unexpectedly."); |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame^] | 430 | synchronized(mSpellCheckerMap) { |
| 431 | mListeners.clear(); |
| 432 | cleanLocked(); |
| 433 | } |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 434 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | private class InternalServiceConnection implements ServiceConnection { |
| 438 | private final ISpellCheckerSessionListener mListener; |
| 439 | private final String mSciId; |
| 440 | private final String mLocale; |
| 441 | public InternalServiceConnection( |
| 442 | String id, String locale, ISpellCheckerSessionListener listener) { |
| 443 | mSciId = id; |
| 444 | mLocale = locale; |
| 445 | mListener = listener; |
| 446 | } |
| 447 | |
| 448 | @Override |
| 449 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 450 | synchronized(mSpellCheckerMap) { |
satok | 6be6d75 | 2011-07-28 20:40:38 +0900 | [diff] [blame] | 451 | if (DBG) { |
| 452 | Slog.w(TAG, "onServiceConnected: " + name); |
| 453 | } |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 454 | ISpellCheckerService spellChecker = ISpellCheckerService.Stub.asInterface(service); |
| 455 | final SpellCheckerBindGroup group = mSpellCheckerBindGroups.get(mSciId); |
| 456 | if (group != null) { |
| 457 | group.onServiceConnected(spellChecker); |
| 458 | } |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | @Override |
| 463 | public void onServiceDisconnected(ComponentName name) { |
| 464 | mSpellCheckerBindGroups.remove(mSciId); |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | private class InternalDeathRecipient implements IBinder.DeathRecipient { |
| 469 | public final ITextServicesSessionListener mTsListener; |
| 470 | public final ISpellCheckerSessionListener mScListener; |
| 471 | public final String mScLocale; |
| 472 | private final SpellCheckerBindGroup mGroup; |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame^] | 473 | public final int mUid; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 474 | public InternalDeathRecipient(SpellCheckerBindGroup group, |
| 475 | ITextServicesSessionListener tsListener, String scLocale, |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame^] | 476 | ISpellCheckerSessionListener scListener, int uid) { |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 477 | mTsListener = tsListener; |
| 478 | mScListener = scListener; |
| 479 | mScLocale = scLocale; |
| 480 | mGroup = group; |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame^] | 481 | mUid = uid; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | public boolean hasSpellCheckerListener(ISpellCheckerSessionListener listener) { |
satok | df5659d | 2011-07-29 18:38:21 +0900 | [diff] [blame^] | 485 | return listener.asBinder().equals(mScListener.asBinder()); |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | @Override |
| 489 | public void binderDied() { |
| 490 | mGroup.removeListener(mScListener); |
| 491 | } |
| 492 | } |
| 493 | } |